From 66553eea1ab39fd454b4ee3ca0c4cbdf24fa9c15 Mon Sep 17 00:00:00 2001 From: Robo Date: Fri, 24 Jul 2015 15:09:11 +0530 Subject: [PATCH] webContents: api to add/remove path from devtools workspace --- atom/browser/api/atom_api_web_contents.cc | 19 +++++++++++ atom/browser/api/atom_api_web_contents.h | 8 +++-- atom/browser/common_web_contents_delegate.cc | 33 +++++++++++--------- atom/browser/common_web_contents_delegate.h | 5 +-- docs/api/browser-window.md | 12 +++++++ 5 files changed, 59 insertions(+), 18 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index b79338548063..7070b7d6ebab 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -14,6 +14,7 @@ #include "atom/browser/web_view_guest_delegate.h" #include "atom/common/api/api_messages.h" #include "atom/common/event_emitter_caller.h" +#include "atom/common/native_mate_converters/file_path_converter.h" #include "atom/common/native_mate_converters/gfx_converter.h" #include "atom/common/native_mate_converters/gurl_converter.h" #include "atom/common/native_mate_converters/image_converter.h" @@ -689,6 +690,22 @@ void WebContents::PrintToPDF(const base::DictionaryValue& setting, PrintToPDF(setting, callback); } +void WebContents::AddWorkSpace(const base::FilePath& path) { + if (path.empty()) { + node::ThrowError(isolate(), "path cannot be empty"); + return; + } + DevToolsAddFileSystem(path); +} + +void WebContents::RemoveWorkSpace(const base::FilePath& path) { + if (path.empty()) { + node::ThrowError(isolate(), "path cannot be empty"); + return; + } + DevToolsRemoveFileSystem(path); +} + void WebContents::Undo() { web_contents()->Undo(); } @@ -812,6 +829,8 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder( .SetMethod("inspectServiceWorker", &WebContents::InspectServiceWorker) .SetMethod("print", &WebContents::Print) .SetMethod("_printToPDF", &WebContents::PrintToPDF) + .SetMethod("addWorkSpace", &WebContents::AddWorkSpace) + .SetMethod("removeWorkSpace", &WebContents::RemoveWorkSpace) .SetProperty("session", &WebContents::Session) .Build()); diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 1dcf5f4f6d29..68e0ad310529 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -37,8 +37,8 @@ class WebContents : public mate::TrackableObject, public content::WebContentsObserver { public: // For node.js callback function type: function(error, buffer) - typedef base::Callback, v8::Local)> - PrintToPDFCallback; + using PrintToPDFCallback = + base::Callback, v8::Local)>; // Create from an existing WebContents. static mate::Handle CreateFrom( @@ -83,6 +83,10 @@ class WebContents : public mate::TrackableObject, void PrintToPDF(const base::DictionaryValue& setting, const PrintToPDFCallback& callback); + // DevTools workspace api. + void AddWorkSpace(const base::FilePath& path); + void RemoveWorkSpace(const base::FilePath& path); + // Editing commands. void Undo(); void Redo(); diff --git a/atom/browser/common_web_contents_delegate.cc b/atom/browser/common_web_contents_delegate.cc index af429929812a..3cef7c6d68cc 100644 --- a/atom/browser/common_web_contents_delegate.cc +++ b/atom/browser/common_web_contents_delegate.cc @@ -274,16 +274,21 @@ void CommonWebContentsDelegate::DevToolsAppendToFile( base::Unretained(this), url)); } -void CommonWebContentsDelegate::DevToolsAddFileSystem() { - file_dialog::Filters filters; - base::FilePath default_path; - std::vector paths; - int flag = file_dialog::FILE_DIALOG_OPEN_DIRECTORY; - if (!file_dialog::ShowOpenDialog(owner_window(), "", default_path, - filters, flag, &paths)) - return; +void CommonWebContentsDelegate::DevToolsAddFileSystem( + const base::FilePath& file_system_path) { + base::FilePath path = file_system_path; + if (path.empty()) { + file_dialog::Filters filters; + base::FilePath default_path; + std::vector paths; + int flag = file_dialog::FILE_DIALOG_OPEN_DIRECTORY; + if (!file_dialog::ShowOpenDialog(owner_window(), "", default_path, + filters, flag, &paths)) + return; + + path = paths[0]; + } - base::FilePath path = paths[0]; std::string registered_name; std::string file_system_id = RegisterFileSystem(GetDevToolsWebContents(), path, @@ -313,20 +318,20 @@ void CommonWebContentsDelegate::DevToolsAddFileSystem() { } void CommonWebContentsDelegate::DevToolsRemoveFileSystem( - const std::string& file_system_path) { + const base::FilePath& file_system_path) { if (!web_contents_) return; - base::FilePath path = base::FilePath::FromUTF8Unsafe(file_system_path); - storage::IsolatedContext::GetInstance()->RevokeFileSystemByPath(path); + storage::IsolatedContext::GetInstance()-> + RevokeFileSystemByPath(file_system_path); for (auto it = saved_paths_.begin(); it != saved_paths_.end(); ++it) - if (it->second == path) { + if (it->second == file_system_path) { saved_paths_.erase(it); break; } - base::StringValue file_system_path_value(file_system_path); + base::StringValue file_system_path_value(file_system_path.AsUTF8Unsafe()); web_contents_->CallClientFunction( "DevToolsAPI.fileSystemRemoved", &file_system_path_value, diff --git a/atom/browser/common_web_contents_delegate.h b/atom/browser/common_web_contents_delegate.h index 8c87548951b2..e21cd24e9107 100644 --- a/atom/browser/common_web_contents_delegate.h +++ b/atom/browser/common_web_contents_delegate.h @@ -80,8 +80,9 @@ class CommonWebContentsDelegate bool save_as) override; void DevToolsAppendToFile(const std::string& url, const std::string& content) override; - void DevToolsAddFileSystem() override; - void DevToolsRemoveFileSystem(const std::string& file_system_path) override; + void DevToolsAddFileSystem(const base::FilePath& path) override; + void DevToolsRemoveFileSystem( + const base::FilePath& file_system_path) override; private: // Callback for when DevToolsSaveToFile has completed. diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index cee68183f783..cb4941daaa7f 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -1042,6 +1042,18 @@ win.webContents.on("did-finish-load", function() { }); ``` +### WebContents.addWorkSpace(path) + +* `path` String + +Adds the specified path to devtools workspace. + +### WebContents.removeWorkSpace(path) + +* `path` String + +Removes the specified path from devtools workspace. + ### WebContents.send(channel[, args...]) * `channel` String