]> khamidou — GitHub mirrors - electron.git/commitdiff
devtools: write to file in FILE thread
authordeepak1556 <hop2deep@gmail.com>
Mon, 22 Jun 2015 02:00:33 +0000 (07:30 +0530)
committerdeepak1556 <hop2deep@gmail.com>
Tue, 23 Jun 2015 15:28:56 +0000 (20:58 +0530)
atom/browser/common_web_contents_delegate.cc
atom/browser/common_web_contents_delegate.h

index 285b93014e449e3592b26e35437ee7f91b8d5fb6..97fee2f755d1462d0f6fbc3c3f30760299366a1b 100644 (file)
 #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()) {
index 84564e0ce7433dee2fa01967af662471bd1bb351..a50f4288613a5b79977192d08dd4892ec4df0e19 100644 (file)
@@ -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);