From cdc7b8d15e881f12c1ff3cba7dd92fb2fd759730 Mon Sep 17 00:00:00 2001 From: Robo Date: Tue, 15 Mar 2016 07:51:36 +0530 Subject: [PATCH] devtools: fix filesyatem api usage and use prefs to track filesystem paths --- atom/browser/atom_browser_context.cc | 1 + atom/browser/common_web_contents_delegate.cc | 119 +++++++++++++------ atom/browser/common_web_contents_delegate.h | 6 +- chromium_src/chrome/common/pref_names.cc | 1 + chromium_src/chrome/common/pref_names.h | 1 + 5 files changed, 86 insertions(+), 42 deletions(-) diff --git a/atom/browser/atom_browser_context.cc b/atom/browser/atom_browser_context.cc index 2ff9a510db47..d6724ff53384 100644 --- a/atom/browser/atom_browser_context.cc +++ b/atom/browser/atom_browser_context.cc @@ -194,6 +194,7 @@ void AtomBrowserContext::RegisterPrefs(PrefRegistrySimple* pref_registry) { PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &download_dir); pref_registry->RegisterFilePathPref(prefs::kDownloadDefaultDirectory, download_dir); + pref_registry->RegisterDictionaryPref(prefs::kDevToolsFileSystemPaths); } bool AtomBrowserContext::AllowNTLMCredentialsForDomain(const GURL& origin) { diff --git a/atom/browser/common_web_contents_delegate.cc b/atom/browser/common_web_contents_delegate.cc index 07cc20c7d108..62e854ac5655 100644 --- a/atom/browser/common_web_contents_delegate.cc +++ b/atom/browser/common_web_contents_delegate.cc @@ -4,17 +4,22 @@ #include "atom/browser/common_web_contents_delegate.h" +#include #include #include +#include "atom/browser/atom_browser_context.h" #include "atom/browser/atom_javascript_dialog_manager.h" #include "atom/browser/native_window.h" #include "atom/browser/ui/file_dialog.h" #include "atom/browser/web_dialog_helper.h" #include "base/files/file_util.h" +#include "base/prefs/pref_service.h" +#include "base/prefs/scoped_user_pref_update.h" #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 "chrome/common/pref_names.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" @@ -36,6 +41,8 @@ namespace atom { namespace { +const char kRootName[] = ""; + struct FileSystem { FileSystem() { } @@ -53,14 +60,14 @@ struct FileSystem { }; std::string RegisterFileSystem(content::WebContents* web_contents, - const base::FilePath& path, - std::string* registered_name) { + const base::FilePath& path) { auto isolated_context = storage::IsolatedContext::GetInstance(); + std::string root_name(kRootName); std::string file_system_id = isolated_context->RegisterFileSystemForPath( storage::kFileSystemTypeNativeLocal, std::string(), path, - registered_name); + &root_name); content::ChildProcessSecurityPolicy* policy = content::ChildProcessSecurityPolicy::GetInstance(); @@ -80,13 +87,12 @@ std::string RegisterFileSystem(content::WebContents* web_contents, FileSystem CreateFileSystemStruct( content::WebContents* web_contents, const std::string& file_system_id, - const std::string& registered_name, const std::string& file_system_path) { const GURL origin = web_contents->GetURL().GetOrigin(); std::string file_system_name = storage::GetIsolatedFileSystemName(origin, file_system_id); std::string root_url = storage::GetIsolatedFileSystemRootURIString( - origin, file_system_id, registered_name); + origin, file_system_id, kRootName); return FileSystem(file_system_name, root_url, file_system_path); } @@ -114,6 +120,26 @@ void AppendToFile(const base::FilePath& path, base::AppendToFile(path, content.data(), content.size()); } +PrefService* GetPrefService(content::WebContents* web_contents) { + auto context = web_contents->GetBrowserContext(); + return static_cast(context)->prefs(); +} + +std::set GetAddedFileSystemPaths( + content::WebContents* web_contents) { + auto pref_service = GetPrefService(web_contents); + const base::DictionaryValue* file_system_paths_value = + pref_service->GetDictionary(prefs::kDevToolsFileSystemPaths); + std::set result; + if (file_system_paths_value) { + base::DictionaryValue::Iterator it(*file_system_paths_value); + for (; !it.IsAtEnd(); it.Advance()) { + result.insert(it.key()); + } + } + return result; +} + } // namespace CommonWebContentsDelegate::CommonWebContentsDelegate() @@ -278,6 +304,34 @@ void CommonWebContentsDelegate::DevToolsAppendToFile( base::Unretained(this), url)); } +void CommonWebContentsDelegate::DevToolsRequestFileSystems() { + auto file_system_paths = GetAddedFileSystemPaths(GetDevToolsWebContents()); + if (file_system_paths.empty()) { + base::ListValue empty_file_system_value; + web_contents_->CallClientFunction("DevToolsAPI.fileSystemsLoaded", + &empty_file_system_value, + nullptr, nullptr); + return; + } + + std::vector file_systems; + for (auto file_system_path : file_system_paths) { + base::FilePath path = base::FilePath::FromUTF8Unsafe(file_system_path); + std::string file_system_id = RegisterFileSystem(GetDevToolsWebContents(), + path); + FileSystem file_system = CreateFileSystemStruct(GetDevToolsWebContents(), + file_system_id, + file_system_path); + file_systems.push_back(file_system); + } + + base::ListValue file_system_value; + for (size_t i = 0; i < file_systems.size(); ++i) + file_system_value.Append(CreateFileSystemValue(file_systems[i])); + web_contents_->CallClientFunction("DevToolsAPI.fileSystemsLoaded", + &file_system_value, nullptr, nullptr); +} + void CommonWebContentsDelegate::DevToolsAddFileSystem( const base::FilePath& file_system_path) { base::FilePath path = file_system_path; @@ -293,32 +347,26 @@ void CommonWebContentsDelegate::DevToolsAddFileSystem( path = paths[0]; } - std::string registered_name; std::string file_system_id = RegisterFileSystem(GetDevToolsWebContents(), - path, - ®istered_name); - - WorkspaceMap::iterator it = saved_paths_.find(file_system_id); - if (it != saved_paths_.end()) + path); + auto file_system_paths = GetAddedFileSystemPaths(GetDevToolsWebContents()); + if (file_system_paths.find(path.AsUTF8Unsafe()) != file_system_paths.end()) return; - saved_paths_[file_system_id] = path; - FileSystem file_system = CreateFileSystemStruct(GetDevToolsWebContents(), - file_system_id, - registered_name, - path.AsUTF8Unsafe()); + file_system_id, + path.AsUTF8Unsafe()); + scoped_ptr file_system_value( + CreateFileSystemValue(file_system)); - scoped_ptr error_string_value( - new base::StringValue(std::string())); - scoped_ptr file_system_value; - if (!file_system.file_system_path.empty()) - file_system_value.reset(CreateFileSystemValue(file_system)); - web_contents_->CallClientFunction( - "DevToolsAPI.fileSystemAdded", - error_string_value.get(), - file_system_value.get(), - nullptr); + auto pref_service = GetPrefService(GetDevToolsWebContents()); + DictionaryPrefUpdate update(pref_service, prefs::kDevToolsFileSystemPaths); + update.Get()->SetWithoutPathExpansion( + path.AsUTF8Unsafe(), base::Value::CreateNullValue()); + + web_contents_->CallClientFunction("DevToolsAPI.fileSystemAdded", + file_system_value.get(), + nullptr, nullptr); } void CommonWebContentsDelegate::DevToolsRemoveFileSystem( @@ -326,21 +374,18 @@ void CommonWebContentsDelegate::DevToolsRemoveFileSystem( if (!web_contents_) return; + std::string path = file_system_path.AsUTF8Unsafe(); storage::IsolatedContext::GetInstance()-> RevokeFileSystemByPath(file_system_path); - for (auto it = saved_paths_.begin(); it != saved_paths_.end(); ++it) - if (it->second == file_system_path) { - saved_paths_.erase(it); - break; - } + auto pref_service = GetPrefService(GetDevToolsWebContents()); + DictionaryPrefUpdate update(pref_service, prefs::kDevToolsFileSystemPaths); + update.Get()->RemoveWithoutPathExpansion(path, nullptr); - base::StringValue file_system_path_value(file_system_path.AsUTF8Unsafe()); - web_contents_->CallClientFunction( - "DevToolsAPI.fileSystemRemoved", - &file_system_path_value, - nullptr, - nullptr); + base::StringValue file_system_path_value(path); + web_contents_->CallClientFunction("DevToolsAPI.fileSystemRemoved", + &file_system_path_value, + nullptr, nullptr); } void CommonWebContentsDelegate::OnDevToolsSaveToFile( diff --git a/atom/browser/common_web_contents_delegate.h b/atom/browser/common_web_contents_delegate.h index 83ef2850bd96..61ff63793df5 100644 --- a/atom/browser/common_web_contents_delegate.h +++ b/atom/browser/common_web_contents_delegate.h @@ -83,6 +83,7 @@ class CommonWebContentsDelegate bool save_as) override; void DevToolsAppendToFile(const std::string& url, const std::string& content) override; + void DevToolsRequestFileSystems() override; void DevToolsAddFileSystem(const base::FilePath& path) override; void DevToolsRemoveFileSystem( const base::FilePath& file_system_path) override; @@ -128,11 +129,6 @@ class CommonWebContentsDelegate typedef std::map PathsMap; PathsMap saved_files_; - // Maps file system id to file path, used by the file system requests - // sent from devtools. - typedef std::map WorkspaceMap; - WorkspaceMap saved_paths_; - DISALLOW_COPY_AND_ASSIGN(CommonWebContentsDelegate); }; diff --git a/chromium_src/chrome/common/pref_names.cc b/chromium_src/chrome/common/pref_names.cc index 3e3a73b99837..23235cd1f4b0 100644 --- a/chromium_src/chrome/common/pref_names.cc +++ b/chromium_src/chrome/common/pref_names.cc @@ -8,5 +8,6 @@ namespace prefs { const char kSelectFileLastDirectory[] = "selectfile.last_directory"; const char kDownloadDefaultDirectory[] = "download.default_directory"; +const char kDevToolsFileSystemPaths[] = "devtools.file_system_paths"; } // namespace prefs diff --git a/chromium_src/chrome/common/pref_names.h b/chromium_src/chrome/common/pref_names.h index 542a2d2c733f..5101c720133d 100644 --- a/chromium_src/chrome/common/pref_names.h +++ b/chromium_src/chrome/common/pref_names.h @@ -8,5 +8,6 @@ namespace prefs { extern const char kSelectFileLastDirectory[]; extern const char kDownloadDefaultDirectory[]; +extern const char kDevToolsFileSystemPaths[]; } // namespace prefs