diff --git a/atom/browser/common_web_contents_delegate.cc b/atom/browser/common_web_contents_delegate.cc index 285b93014e44..97fee2f755d1 100644 --- a/atom/browser/common_web_contents_delegate.cc +++ b/atom/browser/common_web_contents_delegate.cc @@ -15,11 +15,14 @@ #include "chrome/browser/printing/print_preview_message_handler.h" #include "chrome/browser/printing/print_view_manager_basic.h" #include "chrome/browser/ui/browser_dialogs.h" +#include "content/public/browser/browser_thread.h" #include "content/public/browser/child_process_security_policy.h" #include "content/public/browser/render_process_host.h" #include "content/public/browser/render_view_host.h" #include "storage/browser/fileapi/isolated_context.h" +using content::BrowserThread; + namespace atom { namespace { @@ -86,6 +89,22 @@ base::DictionaryValue* CreateFileSystemValue(const FileSystem& file_system) { return file_system_value; } +void WriteToFile(const base::FilePath& path, + const std::string& content) { + DCHECK_CURRENTLY_ON(BrowserThread::FILE); + DCHECK(!path.empty()); + + base::WriteFile(path, content.data(), content.size()); +} + +void AppendToFile(const base::FilePath& path, + const std::string& content) { + DCHECK_CURRENTLY_ON(BrowserThread::FILE); + DCHECK(!path.empty()); + + base::AppendToFile(path, content.data(), content.size()); +} + } // namespace CommonWebContentsDelegate::CommonWebContentsDelegate(bool is_guest) @@ -237,12 +256,11 @@ void CommonWebContentsDelegate::DevToolsSaveToFile( } saved_files_[url] = path; - base::WriteFile(path, content.data(), content.size()); - - // Notify devtools. - base::StringValue url_value(url); - web_contents_->CallClientFunction( - "DevToolsAPI.savedURL", &url_value, nullptr, nullptr); + BrowserThread::PostTaskAndReply( + BrowserThread::FILE, FROM_HERE, + base::Bind(&WriteToFile, path, content), + base::Bind(&CommonWebContentsDelegate::OnDevToolsSaveToFile, + base::Unretained(this), url)); } void CommonWebContentsDelegate::DevToolsAppendToFile( @@ -250,12 +268,12 @@ void CommonWebContentsDelegate::DevToolsAppendToFile( PathsMap::iterator it = saved_files_.find(url); if (it == saved_files_.end()) return; - base::AppendToFile(it->second, content.data(), content.size()); - // Notify devtools. - base::StringValue url_value(url); - web_contents_->CallClientFunction( - "DevToolsAPI.appendedToURL", &url_value, nullptr, nullptr); + BrowserThread::PostTaskAndReply( + BrowserThread::FILE, FROM_HERE, + base::Bind(&AppendToFile, it->second, content), + base::Bind(&CommonWebContentsDelegate::OnDevToolsAppendToFile, + base::Unretained(this), url)); } void CommonWebContentsDelegate::DevToolsAddFileSystem() { @@ -318,6 +336,22 @@ void CommonWebContentsDelegate::DevToolsRemoveFileSystem( nullptr); } +void CommonWebContentsDelegate::OnDevToolsSaveToFile( + const std::string& url) { + // Notify DevTools. + base::StringValue url_value(url); + web_contents_->CallClientFunction( + "DevToolsAPI.savedURL", &url_value, nullptr, nullptr); +} + +void CommonWebContentsDelegate::OnDevToolsAppendToFile( + const std::string& url) { + // Notify DevTools. + base::StringValue url_value(url); + web_contents_->CallClientFunction( + "DevToolsAPI.appendedToURL", &url_value, nullptr, nullptr); +} + void CommonWebContentsDelegate::SetHtmlApiFullscreen(bool enter_fullscreen) { // Window is already in fullscreen mode, save the state. if (enter_fullscreen && owner_window_->IsFullscreen()) { diff --git a/atom/browser/common_web_contents_delegate.h b/atom/browser/common_web_contents_delegate.h index 84564e0ce743..a50f4288613a 100644 --- a/atom/browser/common_web_contents_delegate.h +++ b/atom/browser/common_web_contents_delegate.h @@ -83,6 +83,12 @@ class CommonWebContentsDelegate void DevToolsRemoveFileSystem(const std::string& file_system_path) override; private: + // Callback for when DevToolsSaveToFile has completed. + void OnDevToolsSaveToFile(const std::string& url); + + // Callback for when DevToolsAppendToFile has completed. + void OnDevToolsAppendToFile(const std::string& url); + // Set fullscreen mode triggered by html api. void SetHtmlApiFullscreen(bool enter_fullscreen);