]> khamidou — GitHub mirrors - electron.git/commitdiff
Enable writing files in devtools.
authorCheng Zhao <zcbenz@gmail.com>
Fri, 4 Apr 2014 16:10:57 +0000 (00:10 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Sat, 5 Apr 2014 01:22:25 +0000 (09:22 +0800)
atom/browser/native_window.cc
atom/browser/native_window.h
vendor/brightray

index 6f6f163a1eac258588dad52fc2acdaa675f97a7c..f19bdf08237ed9b73de0d7ba5084ab73403af9f0 100644 (file)
 #include "atom/browser/browser.h"
 #include "atom/browser/devtools_delegate.h"
 #include "atom/browser/devtools_web_contents_observer.h"
+#include "atom/browser/ui/file_dialog.h"
 #include "atom/browser/window_list.h"
 #include "atom/common/api/api_messages.h"
 #include "atom/common/atom_version.h"
 #include "atom/common/options_switches.h"
 #include "base/command_line.h"
 #include "base/file_util.h"
+#include "base/json/json_writer.h"
 #include "base/prefs/pref_service.h"
 #include "base/message_loop/message_loop.h"
 #include "base/strings/stringprintf.h"
@@ -519,6 +521,41 @@ bool NativeWindow::DevToolsShow(std::string* dock_side) {
   return false;
 }
 
+void NativeWindow::DevToolsSaveToFile(const std::string& url,
+                                      const std::string& content,
+                                      bool save_as) {
+  base::FilePath path;
+  PathsMap::iterator it = saved_files_.find(url);
+  if (it != saved_files_.end() && !save_as) {
+    path = it->second;
+  } else {
+    if (!file_dialog::ShowSaveDialog(this, url, base::FilePath(url), &path))
+      return;
+  }
+
+  saved_files_[url] = path;
+  file_util::WriteFile(path, content.data(), content.size());
+
+  // Notify devtools.
+  base::StringValue url_value(url);
+  CallDevToolsFunction("InspectorFrontendAPI.savedURL", &url_value);
+
+  // TODO(zcbenz): In later Chrome we need to call canceledSaveURL when the save
+  // failed.
+}
+
+void NativeWindow::DevToolsAppendToFile(const std::string& url,
+                                        const std::string& content) {
+  PathsMap::iterator it = saved_files_.find(url);
+  if (it == saved_files_.end())
+    return;
+  file_util::AppendToFile(it->second, content.data(), content.size());
+
+  // Notify devtools.
+  base::StringValue url_value(url);
+  CallDevToolsFunction("InspectorFrontendAPI.appendedToURL", &url_value);
+}
+
 void NativeWindow::ScheduleUnresponsiveEvent(int ms) {
   window_unresposive_closure_.Reset(
       base::Bind(&NativeWindow::NotifyWindowUnresponsive,
@@ -547,6 +584,30 @@ void NativeWindow::OnCapturePageDone(const CapturePageCallback& callback,
   callback.Run(data);
 }
 
+void NativeWindow::CallDevToolsFunction(const std::string& function_name,
+                                        const base::Value* arg1,
+                                        const base::Value* arg2,
+                                        const base::Value* arg3) {
+  std::string params;
+  if (arg1) {
+    std::string json;
+    base::JSONWriter::Write(arg1, &json);
+    params.append(json);
+    if (arg2) {
+      base::JSONWriter::Write(arg2, &json);
+      params.append(", " + json);
+      if (arg3) {
+        base::JSONWriter::Write(arg3, &json);
+        params.append(", " + json);
+      }
+    }
+  }
+  base::string16 javascript =
+      base::ASCIIToUTF16(function_name + "(" + params + ");");
+  GetDevToolsWebContents()->GetRenderViewHost()->ExecuteJavascriptInWebFrame(
+      string16(), javascript);
+}
+
 void NativeWindow::OnRendererMessage(const string16& channel,
                                      const base::ListValue& args) {
   AtomBrowserMainParts::Get()->atom_bindings()->OnRendererMessage(
index 3028da12d534208d501c3291c946f88e405f70a0..acfd3ca546ab1c608f83605d9c37fc8639cc6665 100644 (file)
@@ -237,6 +237,11 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate,
   virtual bool DevToolsSetDockSide(const std::string& dock_side,
                                    bool* succeed) OVERRIDE;
   virtual bool DevToolsShow(std::string* dock_side) OVERRIDE;
+  virtual void DevToolsSaveToFile(const std::string& url,
+                                  const std::string& content,
+                                  bool save_as) OVERRIDE;
+  virtual void DevToolsAppendToFile(const std::string& url,
+                                    const std::string& content) OVERRIDE;
 
   // Whether window has standard frame.
   bool has_frame_;
@@ -251,6 +256,12 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate,
   // Dispatch unresponsive event to observers.
   void NotifyWindowUnresponsive();
 
+  // Call a function in devtools.
+  void CallDevToolsFunction(const std::string& function_name,
+                            const base::Value* arg1 = NULL,
+                            const base::Value* arg2 = NULL,
+                            const base::Value* arg3 = NULL);
+
   // Called when CapturePage has done.
   void OnCapturePageDone(const CapturePageCallback& callback,
                          bool succeed,
@@ -293,6 +304,10 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate,
   scoped_ptr<AtomJavaScriptDialogManager> dialog_manager_;
   scoped_ptr<brightray::InspectableWebContents> inspectable_web_contents_;
 
+  // Maps url to file path, used by the file requests sent from devtools.
+  typedef std::map<std::string, base::FilePath> PathsMap;
+  PathsMap saved_files_;
+
   DISALLOW_COPY_AND_ASSIGN(NativeWindow);
 };
 
index 794cc6b6a6d269eacce0bbaf930d10f642e7a907..dab731f2329285a63c0c2cff30793a34fde91c39 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 794cc6b6a6d269eacce0bbaf930d10f642e7a907
+Subproject commit dab731f2329285a63c0c2cff30793a34fde91c39