From 1d9524118516721144986d15e134d34dfbb7c0ee Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Sun, 10 Dec 2017 22:58:06 +0530 Subject: [PATCH] FIXME: refactor and remove usage of ScopedAllowIO where possible --- atom/common/api/atom_api_native_image.cc | 8 +++-- atom/common/asar/archive.cc | 33 ++++++++++++++----- .../crash_reporter/crash_reporter_mac.mm | 8 +++-- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/atom/common/api/atom_api_native_image.cc b/atom/common/api/atom_api_native_image.cc index 3939a0afe56..05e6e4501ff 100644 --- a/atom/common/api/atom_api_native_image.cc +++ b/atom/common/api/atom_api_native_image.cc @@ -15,6 +15,7 @@ #include "base/files/file_util.h" #include "base/strings/pattern.h" #include "base/strings/string_util.h" +#include "base/threading/thread_restrictions.h" #include "native_mate/object_template_builder.h" #include "net/base/data_url.h" #include "third_party/skia/include/core/SkBitmap.h" @@ -130,8 +131,11 @@ bool AddImageSkiaRep(gfx::ImageSkia* image, const base::FilePath& path, double scale_factor) { std::string file_contents; - if (!asar::ReadFileToString(path, &file_contents)) - return false; + { + base::ThreadRestrictions::ScopedAllowIO allow_io; + if (!asar::ReadFileToString(path, &file_contents)) + return false; + } const unsigned char* data = reinterpret_cast(file_contents.data()); diff --git a/atom/common/asar/archive.cc b/atom/common/asar/archive.cc index c8d85f904d9..0533365892e 100644 --- a/atom/common/asar/archive.cc +++ b/atom/common/asar/archive.cc @@ -14,6 +14,8 @@ #include "base/logging.h" #include "base/pickle.h" #include "base/strings/string_number_conversions.h" +#include "base/task_scheduler/post_task.h" +#include "base/threading/thread_restrictions.h" #include "base/values.h" #if defined(OS_WIN) @@ -115,17 +117,19 @@ bool FillFileInfoWithNode(Archive::FileInfo* info, } // namespace Archive::Archive(const base::FilePath& path) - : path_(path), - file_(path_, base::File::FLAG_OPEN | base::File::FLAG_READ), + : path_(path), file_(base::File::FILE_OK), header_size_(0) { + { + base::ThreadRestrictions::ScopedAllowIO allow_io; + file_.Initialize(path_, base::File::FLAG_OPEN | base::File::FLAG_READ); #if defined(OS_WIN) - fd_(_open_osfhandle( - reinterpret_cast(file_.GetPlatformFile()), 0)), + fd_ = + _open_osfhandle(reinterpret_cast(file_.GetPlatformFile()), 0); #elif defined(OS_POSIX) - fd_(file_.GetPlatformFile()), + fd_ = file_.GetPlatformFile(); #else - fd_(-1), + fd_ = -1; #endif - header_size_(0) { + } } Archive::~Archive() { @@ -136,6 +140,11 @@ Archive::~Archive() { file_.TakePlatformFile(); } #endif + base::PostTaskWithTraits( + FROM_HERE, + {base::MayBlock(), base::TaskPriority::BACKGROUND, + base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, + base::Bind([](base::File file) { file.Close(); }, Passed(&file_))); } bool Archive::Init() { @@ -151,7 +160,10 @@ bool Archive::Init() { int len; buf.resize(8); - len = file_.ReadAtCurrentPos(buf.data(), buf.size()); + { + base::ThreadRestrictions::ScopedAllowIO allow_io; + len = file_.ReadAtCurrentPos(buf.data(), buf.size()); + } if (len != static_cast(buf.size())) { PLOG(ERROR) << "Failed to read header size from " << path_.value(); return false; @@ -165,7 +177,10 @@ bool Archive::Init() { } buf.resize(size); - len = file_.ReadAtCurrentPos(buf.data(), buf.size()); + { + base::ThreadRestrictions::ScopedAllowIO allow_io; + len = file_.ReadAtCurrentPos(buf.data(), buf.size()); + } if (len != static_cast(buf.size())) { PLOG(ERROR) << "Failed to read header from " << path_.value(); return false; diff --git a/atom/common/crash_reporter/crash_reporter_mac.mm b/atom/common/crash_reporter/crash_reporter_mac.mm index 3f82d2466fb..55f387756a0 100644 --- a/atom/common/crash_reporter/crash_reporter_mac.mm +++ b/atom/common/crash_reporter/crash_reporter_mac.mm @@ -13,6 +13,7 @@ #include "base/strings/string_piece.h" #include "base/strings/stringprintf.h" #include "base/strings/sys_string_conversions.h" +#include "base/threading/thread_restrictions.h" #include "vendor/crashpad/client/crashpad_client.h" #include "vendor/crashpad/client/crashpad_info.h" #include "vendor/crashpad/client/settings.h" @@ -139,8 +140,11 @@ std::vector CrashReporterMac::GetUploadedReports(const base::FilePath& crashes_dir) { std::vector uploaded_reports; - if (!base::PathExists(crashes_dir)) { - return uploaded_reports; + { + base::ThreadRestrictions::ScopedAllowIO allow_io; + if (!base::PathExists(crashes_dir)) { + return uploaded_reports; + } } // Load crashpad database. std::unique_ptr database =