From: Cheng Zhao Date: Fri, 4 Apr 2014 16:10:57 +0000 (+0800) Subject: Enable writing files in devtools. X-Git-Tag: v0.11.6~7^2~3 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=877277d837a3927bacf4cc32b6df5ca1c0bdd66f;p=electron.git Enable writing files in devtools. --- diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index 6f6f163a..f19bdf08 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -15,12 +15,14 @@ #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( diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index 3028da12..acfd3ca5 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -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 dialog_manager_; scoped_ptr inspectable_web_contents_; + // Maps url to file path, used by the file requests sent from devtools. + typedef std::map PathsMap; + PathsMap saved_files_; + DISALLOW_COPY_AND_ASSIGN(NativeWindow); }; diff --git a/vendor/brightray b/vendor/brightray index 794cc6b6..dab731f2 160000 --- a/vendor/brightray +++ b/vendor/brightray @@ -1 +1 @@ -Subproject commit 794cc6b6a6d269eacce0bbaf930d10f642e7a907 +Subproject commit dab731f2329285a63c0c2cff30793a34fde91c39