devtools: fix filesyatem api usage and use prefs to track filesystem paths
authorRobo <hop2deep@gmail.com>
Tue, 15 Mar 2016 02:21:36 +0000 (07:51 +0530)
committerRobo <hop2deep@gmail.com>
Tue, 15 Mar 2016 02:21:36 +0000 (07:51 +0530)
atom/browser/atom_browser_context.cc
atom/browser/common_web_contents_delegate.cc
atom/browser/common_web_contents_delegate.h
chromium_src/chrome/common/pref_names.cc
chromium_src/chrome/common/pref_names.h

index 2ff9a51..d6724ff 100644 (file)
@@ -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) {
index 07cc20c..62e854a 100644 (file)
@@ -4,17 +4,22 @@
 
 #include "atom/browser/common_web_contents_delegate.h"
 
+#include <set>
 #include <string>
 #include <vector>
 
+#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[] = "<root>";
+
 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<atom::AtomBrowserContext*>(context)->prefs();
+}
+
+std::set<std::string> 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<std::string> 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<FileSystem> 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,
-                                                  &registered_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());
-
-  scoped_ptr<base::StringValue> error_string_value(
-      new base::StringValue(std::string()));
-  scoped_ptr<base::DictionaryValue> 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);
+                                                 file_system_id,
+                                                 path.AsUTF8Unsafe());
+  scoped_ptr<base::DictionaryValue> file_system_value(
+      CreateFileSystemValue(file_system));
+
+  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(
index 83ef285..61ff637 100644 (file)
@@ -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<std::string, base::FilePath> PathsMap;
   PathsMap saved_files_;
 
-  // Maps file system id to file path, used by the file system requests
-  // sent from devtools.
-  typedef std::map<std::string, base::FilePath> WorkspaceMap;
-  WorkspaceMap saved_paths_;
-
   DISALLOW_COPY_AND_ASSIGN(CommonWebContentsDelegate);
 };
 
index 3e3a73b..23235cd 100644 (file)
@@ -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
index 542a2d2..5101c72 100644 (file)
@@ -8,5 +8,6 @@ namespace prefs {
 
 extern const char kSelectFileLastDirectory[];
 extern const char kDownloadDefaultDirectory[];
+extern const char kDevToolsFileSystemPaths[];
 
 }  // namespace prefs