[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 7001915 | 2012-12-19 11:44:19 | [diff] [blame] | 5 | #include "chrome/browser/devtools/devtools_file_helper.h" |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 6 | |
[email protected] | 200bd33 | 2013-08-05 16:19:11 | [diff] [blame] | 7 | #include <set> |
[email protected] | d5b2197 | 2012-07-20 02:06:01 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 10 | #include "base/bind.h" |
[email protected] | 6e1fcd1 | 2012-07-02 17:14:20 | [diff] [blame] | 11 | #include "base/callback.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 12 | #include "base/files/file_util.h" |
Daniel Cheng | d07ef71 | 2019-03-28 23:13:41 | [diff] [blame] | 13 | #include "base/hash/md5.h" |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 14 | #include "base/lazy_instance.h" |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 15 | #include "base/macros.h" |
[email protected] | 135cb80 | 2013-06-09 16:44:20 | [diff] [blame] | 16 | #include "base/strings/utf_string_conversions.h" |
Gabriel Charette | 44db142 | 2018-08-06 11:19:33 | [diff] [blame] | 17 | #include "base/task/post_task.h" |
Andrey Kosyakov | 7dabf71 | 2017-06-20 23:50:30 | [diff] [blame] | 18 | #include "base/threading/sequenced_task_runner_handle.h" |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 19 | #include "base/value_conversions.h" |
| 20 | #include "chrome/browser/browser_process.h" |
pfeldman | 7d01b87 | 2015-11-05 08:24:35 | [diff] [blame] | 21 | #include "chrome/browser/devtools/devtools_file_watcher.h" |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 22 | #include "chrome/browser/download/download_prefs.h" |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 23 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 6e1fcd1 | 2012-07-02 17:14:20 | [diff] [blame] | 24 | #include "chrome/browser/ui/chrome_select_file_policy.h" |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 25 | #include "chrome/common/pref_names.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 26 | #include "chrome/grit/generated_resources.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 27 | #include "components/prefs/pref_service.h" |
| 28 | #include "components/prefs/scoped_user_pref_update.h" |
[email protected] | d5b2197 | 2012-07-20 02:06:01 | [diff] [blame] | 29 | #include "content/public/browser/browser_context.h" |
[email protected] | b7b6387 | 2013-01-03 02:41:19 | [diff] [blame] | 30 | #include "content/public/browser/browser_thread.h" |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 31 | #include "content/public/browser/child_process_security_policy.h" |
[email protected] | d5b2197 | 2012-07-20 02:06:01 | [diff] [blame] | 32 | #include "content/public/browser/download_manager.h" |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 33 | #include "content/public/browser/render_process_host.h" |
| 34 | #include "content/public/browser/render_view_host.h" |
| 35 | #include "content/public/browser/web_contents.h" |
| 36 | #include "content/public/common/content_client.h" |
[email protected] | 86a0a6e | 2013-01-28 06:33:03 | [diff] [blame] | 37 | #include "content/public/common/url_constants.h" |
pilgrim | e92c5fcd | 2014-09-10 23:31:23 | [diff] [blame] | 38 | #include "storage/browser/fileapi/file_system_url.h" |
| 39 | #include "storage/browser/fileapi/isolated_context.h" |
pilgrim | 1633055 | 2014-09-10 01:32:22 | [diff] [blame] | 40 | #include "storage/common/fileapi/file_system_util.h" |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 41 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 4344a3c | 2013-01-17 23:49:20 | [diff] [blame] | 42 | #include "ui/shell_dialogs/select_file_dialog.h" |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 43 | |
[email protected] | d879ed3 | 2012-12-27 15:35:36 | [diff] [blame] | 44 | using base::Bind; |
| 45 | using base::Callback; |
[email protected] | d5b2197 | 2012-07-20 02:06:01 | [diff] [blame] | 46 | using content::BrowserContext; |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 47 | using content::BrowserThread; |
[email protected] | d5b2197 | 2012-07-20 02:06:01 | [diff] [blame] | 48 | using content::DownloadManager; |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 49 | using content::RenderViewHost; |
| 50 | using content::WebContents; |
[email protected] | 200bd33 | 2013-08-05 16:19:11 | [diff] [blame] | 51 | using std::set; |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 52 | |
| 53 | namespace { |
| 54 | |
pfeldman | 31834e2 | 2015-10-10 00:47:05 | [diff] [blame] | 55 | static const char kRootName[] = "<root>"; |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 56 | static const char kPermissionDenied[] = "<permission denied>"; |
pfeldman | 31834e2 | 2015-10-10 00:47:05 | [diff] [blame] | 57 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 58 | base::LazyInstance<base::FilePath>::Leaky |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 59 | g_last_save_path = LAZY_INSTANCE_INITIALIZER; |
| 60 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 61 | typedef Callback<void(const base::FilePath&)> SelectedCallback; |
[email protected] | d879ed3 | 2012-12-27 15:35:36 | [diff] [blame] | 62 | typedef Callback<void(void)> CanceledCallback; |
| 63 | |
| 64 | class SelectFileDialog : public ui::SelectFileDialog::Listener, |
| 65 | public base::RefCounted<SelectFileDialog> { |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 66 | public: |
[email protected] | d879ed3 | 2012-12-27 15:35:36 | [diff] [blame] | 67 | SelectFileDialog(const SelectedCallback& selected_callback, |
[email protected] | 7ad5be6 | 2013-07-11 00:02:59 | [diff] [blame] | 68 | const CanceledCallback& canceled_callback, |
| 69 | WebContents* web_contents) |
[email protected] | d879ed3 | 2012-12-27 15:35:36 | [diff] [blame] | 70 | : selected_callback_(selected_callback), |
[email protected] | 7ad5be6 | 2013-07-11 00:02:59 | [diff] [blame] | 71 | canceled_callback_(canceled_callback), |
| 72 | web_contents_(web_contents) { |
[email protected] | 92f5408 | 2012-07-31 01:43:14 | [diff] [blame] | 73 | select_file_dialog_ = ui::SelectFileDialog::Create( |
Brett Wilson | 804e83c | 2017-08-18 22:57:33 | [diff] [blame] | 74 | this, std::make_unique<ChromeSelectFilePolicy>(web_contents)); |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 75 | } |
| 76 | |
[email protected] | d879ed3 | 2012-12-27 15:35:36 | [diff] [blame] | 77 | void Show(ui::SelectFileDialog::Type type, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 78 | const base::FilePath& default_path) { |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 79 | AddRef(); // Balanced in the three listener outcomes. |
[email protected] | 7ad5be6 | 2013-07-11 00:02:59 | [diff] [blame] | 80 | select_file_dialog_->SelectFile( |
| 81 | type, |
[email protected] | 4317aee | 2013-11-28 07:20:11 | [diff] [blame] | 82 | base::string16(), |
[email protected] | 7ad5be6 | 2013-07-11 00:02:59 | [diff] [blame] | 83 | default_path, |
| 84 | NULL, |
| 85 | 0, |
| 86 | base::FilePath::StringType(), |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 87 | platform_util::GetTopLevel(web_contents_->GetNativeView()), |
[email protected] | 7ad5be6 | 2013-07-11 00:02:59 | [diff] [blame] | 88 | NULL); |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 89 | } |
| 90 | |
[email protected] | 92f5408 | 2012-07-31 01:43:14 | [diff] [blame] | 91 | // ui::SelectFileDialog::Listener implementation. |
dcheng | 03748a4 | 2014-10-21 10:19:13 | [diff] [blame] | 92 | void FileSelected(const base::FilePath& path, |
| 93 | int index, |
| 94 | void* params) override { |
[email protected] | d879ed3 | 2012-12-27 15:35:36 | [diff] [blame] | 95 | selected_callback_.Run(path); |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 96 | Release(); // Balanced in ::Show. |
| 97 | } |
| 98 | |
dcheng | 03748a4 | 2014-10-21 10:19:13 | [diff] [blame] | 99 | void MultiFilesSelected(const std::vector<base::FilePath>& files, |
| 100 | void* params) override { |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 101 | Release(); // Balanced in ::Show. |
| 102 | NOTREACHED() << "Should not be able to select multiple files"; |
| 103 | } |
| 104 | |
dcheng | 03748a4 | 2014-10-21 10:19:13 | [diff] [blame] | 105 | void FileSelectionCanceled(void* params) override { |
pfeldman | 7b96f89c | 2015-10-13 00:49:47 | [diff] [blame] | 106 | if (!canceled_callback_.is_null()) |
| 107 | canceled_callback_.Run(); |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 108 | Release(); // Balanced in ::Show. |
| 109 | } |
| 110 | |
| 111 | private: |
[email protected] | d879ed3 | 2012-12-27 15:35:36 | [diff] [blame] | 112 | friend class base::RefCounted<SelectFileDialog>; |
dcheng | 03748a4 | 2014-10-21 10:19:13 | [diff] [blame] | 113 | ~SelectFileDialog() override {} |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 114 | |
[email protected] | 92f5408 | 2012-07-31 01:43:14 | [diff] [blame] | 115 | scoped_refptr<ui::SelectFileDialog> select_file_dialog_; |
[email protected] | d879ed3 | 2012-12-27 15:35:36 | [diff] [blame] | 116 | SelectedCallback selected_callback_; |
| 117 | CanceledCallback canceled_callback_; |
[email protected] | 7ad5be6 | 2013-07-11 00:02:59 | [diff] [blame] | 118 | WebContents* web_contents_; |
[email protected] | 4317aee | 2013-11-28 07:20:11 | [diff] [blame] | 119 | |
| 120 | DISALLOW_COPY_AND_ASSIGN(SelectFileDialog); |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 121 | }; |
| 122 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 123 | void WriteToFile(const base::FilePath& path, const std::string& content) { |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 124 | DCHECK(!path.empty()); |
| 125 | |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 126 | base::WriteFile(path, content.c_str(), content.length()); |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 127 | } |
| 128 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 129 | void AppendToFile(const base::FilePath& path, const std::string& content) { |
[email protected] | e2641d8 | 2012-05-02 07:48:42 | [diff] [blame] | 130 | DCHECK(!path.empty()); |
| 131 | |
chirantan | 75ea2fd | 2014-10-07 23:15:30 | [diff] [blame] | 132 | base::AppendToFile(path, content.c_str(), content.size()); |
[email protected] | e2641d8 | 2012-05-02 07:48:42 | [diff] [blame] | 133 | } |
| 134 | |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 135 | storage::IsolatedContext* isolated_context() { |
mostynb | 13260d5 | 2015-03-26 09:12:09 | [diff] [blame] | 136 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 137 | storage::IsolatedContext* isolated_context = |
| 138 | storage::IsolatedContext::GetInstance(); |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 139 | DCHECK(isolated_context); |
| 140 | return isolated_context; |
| 141 | } |
| 142 | |
| 143 | std::string RegisterFileSystem(WebContents* web_contents, |
pfeldman | 31834e2 | 2015-10-10 00:47:05 | [diff] [blame] | 144 | const base::FilePath& path) { |
mostynb | 13260d5 | 2015-03-26 09:12:09 | [diff] [blame] | 145 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | b3690f7 | 2014-02-17 00:32:48 | [diff] [blame] | 146 | CHECK(web_contents->GetURL().SchemeIs(content::kChromeDevToolsScheme)); |
pfeldman | 31834e2 | 2015-10-10 00:47:05 | [diff] [blame] | 147 | std::string root_name(kRootName); |
Marijn Kruisselbrink | d0134ef | 2019-05-21 01:24:52 | [diff] [blame] | 148 | storage::IsolatedContext::ScopedFSHandle file_system = |
| 149 | isolated_context()->RegisterFileSystemForPath( |
| 150 | storage::kFileSystemTypeNativeLocal, std::string(), path, &root_name); |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 151 | |
| 152 | content::ChildProcessSecurityPolicy* policy = |
| 153 | content::ChildProcessSecurityPolicy::GetInstance(); |
| 154 | RenderViewHost* render_view_host = web_contents->GetRenderViewHost(); |
| 155 | int renderer_id = render_view_host->GetProcess()->GetID(); |
Marijn Kruisselbrink | d0134ef | 2019-05-21 01:24:52 | [diff] [blame] | 156 | policy->GrantReadFileSystem(renderer_id, file_system.id()); |
| 157 | policy->GrantWriteFileSystem(renderer_id, file_system.id()); |
| 158 | policy->GrantCreateFileForFileSystem(renderer_id, file_system.id()); |
| 159 | policy->GrantDeleteFromFileSystem(renderer_id, file_system.id()); |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 160 | |
| 161 | // We only need file level access for reading FileEntries. Saving FileEntries |
| 162 | // just needs the file system to have read/write access, which is granted |
| 163 | // above if required. |
| 164 | if (!policy->CanReadFile(renderer_id, path)) |
| 165 | policy->GrantReadFile(renderer_id, path); |
Marijn Kruisselbrink | d0134ef | 2019-05-21 01:24:52 | [diff] [blame] | 166 | return file_system.id(); |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 167 | } |
| 168 | |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 169 | DevToolsFileHelper::FileSystem CreateFileSystemStruct( |
| 170 | WebContents* web_contents, |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 171 | const std::string& type, |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 172 | const std::string& file_system_id, |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 173 | const std::string& file_system_path) { |
| 174 | const GURL origin = web_contents->GetURL().GetOrigin(); |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 175 | std::string file_system_name = |
| 176 | storage::GetIsolatedFileSystemName(origin, file_system_id); |
| 177 | std::string root_url = storage::GetIsolatedFileSystemRootURIString( |
pfeldman | 31834e2 | 2015-10-10 00:47:05 | [diff] [blame] | 178 | origin, file_system_id, kRootName); |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 179 | return DevToolsFileHelper::FileSystem(type, file_system_name, root_url, |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 180 | file_system_path); |
| 181 | } |
| 182 | |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 183 | using PathToType = std::map<std::string, std::string>; |
| 184 | PathToType GetAddedFileSystemPaths(Profile* profile) { |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 185 | const base::DictionaryValue* file_systems_paths_value = |
[email protected] | 200bd33 | 2013-08-05 16:19:11 | [diff] [blame] | 186 | profile->GetPrefs()->GetDictionary(prefs::kDevToolsFileSystemPaths); |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 187 | PathToType result; |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 188 | for (base::DictionaryValue::Iterator it(*file_systems_paths_value); |
| 189 | !it.IsAtEnd(); it.Advance()) { |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 190 | std::string type = |
| 191 | it.value().is_string() ? it.value().GetString() : std::string(); |
| 192 | result[it.key()] = type; |
[email protected] | 200bd33 | 2013-08-05 16:19:11 | [diff] [blame] | 193 | } |
| 194 | return result; |
| 195 | } |
| 196 | |
[email protected] | d879ed3 | 2012-12-27 15:35:36 | [diff] [blame] | 197 | } // namespace |
| 198 | |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 199 | DevToolsFileHelper::FileSystem::FileSystem() = default; |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 200 | |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 201 | DevToolsFileHelper::FileSystem::~FileSystem() = default; |
| 202 | |
| 203 | DevToolsFileHelper::FileSystem::FileSystem(const FileSystem& other) = default; |
| 204 | |
| 205 | DevToolsFileHelper::FileSystem::FileSystem(const std::string& type, |
| 206 | const std::string& file_system_name, |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 207 | const std::string& root_url, |
| 208 | const std::string& file_system_path) |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 209 | : type(type), |
| 210 | file_system_name(file_system_name), |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 211 | root_url(root_url), |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 212 | file_system_path(file_system_path) {} |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 213 | |
| 214 | DevToolsFileHelper::DevToolsFileHelper(WebContents* web_contents, |
pfeldman | 31834e2 | 2015-10-10 00:47:05 | [diff] [blame] | 215 | Profile* profile, |
| 216 | Delegate* delegate) |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 217 | : web_contents_(web_contents), |
| 218 | profile_(profile), |
pfeldman | 31834e2 | 2015-10-10 00:47:05 | [diff] [blame] | 219 | delegate_(delegate), |
Sami Kyostila | 4ba007d | 2019-08-14 12:03:14 | [diff] [blame] | 220 | file_task_runner_(base::CreateSequencedTaskRunner( |
| 221 | {base::ThreadPool(), base::MayBlock()})) { |
Sam McNally | 71714a7ab | 2017-06-09 02:44:23 | [diff] [blame] | 222 | pref_change_registrar_.Init(profile_->GetPrefs()); |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 223 | } |
| 224 | |
Andrey Kosyakov | 7dabf71 | 2017-06-20 23:50:30 | [diff] [blame] | 225 | DevToolsFileHelper::~DevToolsFileHelper() = default; |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 226 | |
| 227 | void DevToolsFileHelper::Save(const std::string& url, |
| 228 | const std::string& content, |
[email protected] | d879ed3 | 2012-12-27 15:35:36 | [diff] [blame] | 229 | bool save_as, |
[email protected] | 9a3b90ad | 2013-11-28 12:59:49 | [diff] [blame] | 230 | const SaveCallback& saveCallback, |
Nathan Bruer | 4dfb9b15 | 2017-10-02 22:42:41 | [diff] [blame] | 231 | const CancelCallback& cancelCallback) { |
jdoerrie | c6fe63e | 2018-10-03 20:53:40 | [diff] [blame] | 232 | auto it = saved_files_.find(url); |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 233 | if (it != saved_files_.end() && !save_as) { |
[email protected] | 9a3b90ad | 2013-11-28 12:59:49 | [diff] [blame] | 234 | SaveAsFileSelected(url, content, saveCallback, it->second); |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 235 | return; |
| 236 | } |
| 237 | |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 238 | const base::DictionaryValue* file_map = |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 239 | profile_->GetPrefs()->GetDictionary(prefs::kDevToolsEditedFiles); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 240 | base::FilePath initial_path; |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 241 | |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 242 | const base::Value* path_value; |
Xiaohan Wang | ec1c552a | 2019-02-07 18:29:08 | [diff] [blame] | 243 | if (file_map->Get(base::MD5String(url), &path_value)) { |
| 244 | // Ignore base::GetValueAsFilePath() failure since we handle empty |
| 245 | // |initial_path| below. |
| 246 | ignore_result(base::GetValueAsFilePath(*path_value, &initial_path)); |
| 247 | } |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 248 | |
| 249 | if (initial_path.empty()) { |
| 250 | GURL gurl(url); |
| 251 | std::string suggested_file_name = gurl.is_valid() ? |
| 252 | gurl.ExtractFileName() : url; |
| 253 | |
[email protected] | 0cfb430 | 2012-12-12 13:45:06 | [diff] [blame] | 254 | if (suggested_file_name.length() > 64) |
| 255 | suggested_file_name = suggested_file_name.substr(0, 64); |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 256 | |
| 257 | if (!g_last_save_path.Pointer()->empty()) { |
| 258 | initial_path = g_last_save_path.Pointer()->DirName().AppendASCII( |
| 259 | suggested_file_name); |
| 260 | } else { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 261 | base::FilePath download_path = DownloadPrefs::FromDownloadManager( |
[email protected] | d5b2197 | 2012-07-20 02:06:01 | [diff] [blame] | 262 | BrowserContext::GetDownloadManager(profile_))->DownloadPath(); |
| 263 | initial_path = download_path.AppendASCII(suggested_file_name); |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
[email protected] | d879ed3 | 2012-12-27 15:35:36 | [diff] [blame] | 267 | scoped_refptr<SelectFileDialog> select_file_dialog = new SelectFileDialog( |
| 268 | Bind(&DevToolsFileHelper::SaveAsFileSelected, |
| 269 | weak_factory_.GetWeakPtr(), |
| 270 | url, |
| 271 | content, |
[email protected] | 9a3b90ad | 2013-11-28 12:59:49 | [diff] [blame] | 272 | saveCallback), |
pfeldman | 86b26df | 2015-10-05 20:12:23 | [diff] [blame] | 273 | cancelCallback, |
[email protected] | 7ad5be6 | 2013-07-11 00:02:59 | [diff] [blame] | 274 | web_contents_); |
[email protected] | d879ed3 | 2012-12-27 15:35:36 | [diff] [blame] | 275 | select_file_dialog->Show(ui::SelectFileDialog::SELECT_SAVEAS_FILE, |
| 276 | initial_path); |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 277 | } |
| 278 | |
[email protected] | e2641d8 | 2012-05-02 07:48:42 | [diff] [blame] | 279 | void DevToolsFileHelper::Append(const std::string& url, |
[email protected] | d879ed3 | 2012-12-27 15:35:36 | [diff] [blame] | 280 | const std::string& content, |
| 281 | const AppendCallback& callback) { |
jdoerrie | c6fe63e | 2018-10-03 20:53:40 | [diff] [blame] | 282 | auto it = saved_files_.find(url); |
[email protected] | e2641d8 | 2012-05-02 07:48:42 | [diff] [blame] | 283 | if (it == saved_files_.end()) |
| 284 | return; |
[email protected] | d879ed3 | 2012-12-27 15:35:36 | [diff] [blame] | 285 | callback.Run(); |
Andrey Kosyakov | 7dabf71 | 2017-06-20 23:50:30 | [diff] [blame] | 286 | file_task_runner_->PostTask(FROM_HERE, |
| 287 | BindOnce(&AppendToFile, it->second, content)); |
[email protected] | e2641d8 | 2012-05-02 07:48:42 | [diff] [blame] | 288 | } |
| 289 | |
[email protected] | d879ed3 | 2012-12-27 15:35:36 | [diff] [blame] | 290 | void DevToolsFileHelper::SaveAsFileSelected(const std::string& url, |
| 291 | const std::string& content, |
| 292 | const SaveCallback& callback, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 293 | const base::FilePath& path) { |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 294 | *g_last_save_path.Pointer() = path; |
| 295 | saved_files_[url] = path; |
| 296 | |
| 297 | DictionaryPrefUpdate update(profile_->GetPrefs(), |
| 298 | prefs::kDevToolsEditedFiles); |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 299 | base::DictionaryValue* files_map = update.Get(); |
Christian Dullweber | 1374008 | 2018-07-26 13:09:09 | [diff] [blame] | 300 | files_map->SetKey(base::MD5String(url), base::CreateFilePathValue(path)); |
Nathan Bruer | 4dfb9b15 | 2017-10-02 22:42:41 | [diff] [blame] | 301 | std::string file_system_path = path.AsUTF8Unsafe(); |
| 302 | callback.Run(file_system_path); |
Andrey Kosyakov | 7dabf71 | 2017-06-20 23:50:30 | [diff] [blame] | 303 | file_task_runner_->PostTask(FROM_HERE, BindOnce(&WriteToFile, path, content)); |
[email protected] | d879ed3 | 2012-12-27 15:35:36 | [diff] [blame] | 304 | } |
[email protected] | 2997502 | 2012-04-04 15:23:50 | [diff] [blame] | 305 | |
[email protected] | 94252185 | 2013-04-26 12:29:23 | [diff] [blame] | 306 | void DevToolsFileHelper::AddFileSystem( |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 307 | const std::string& type, |
[email protected] | 94252185 | 2013-04-26 12:29:23 | [diff] [blame] | 308 | const ShowInfoBarCallback& show_info_bar_callback) { |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 309 | scoped_refptr<SelectFileDialog> select_file_dialog = new SelectFileDialog( |
| 310 | Bind(&DevToolsFileHelper::InnerAddFileSystem, weak_factory_.GetWeakPtr(), |
| 311 | show_info_bar_callback, type), |
| 312 | Bind(&DevToolsFileHelper::FailedToAddFileSystem, |
| 313 | weak_factory_.GetWeakPtr(), kPermissionDenied), |
| 314 | web_contents_); |
| 315 | select_file_dialog->Show(ui::SelectFileDialog::SELECT_FOLDER, |
| 316 | base::FilePath()); |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 317 | } |
| 318 | |
[email protected] | d3d9b45b | 2013-11-05 20:21:44 | [diff] [blame] | 319 | void DevToolsFileHelper::UpgradeDraggedFileSystemPermissions( |
| 320 | const std::string& file_system_url, |
[email protected] | d3d9b45b | 2013-11-05 20:21:44 | [diff] [blame] | 321 | const ShowInfoBarCallback& show_info_bar_callback) { |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 322 | storage::FileSystemURL root_url = |
[email protected] | d3d9b45b | 2013-11-05 20:21:44 | [diff] [blame] | 323 | isolated_context()->CrackURL(GURL(file_system_url)); |
pfeldman | 31834e2 | 2015-10-10 00:47:05 | [diff] [blame] | 324 | if (!root_url.is_valid() || !root_url.path().empty()) |
[email protected] | d3d9b45b | 2013-11-05 20:21:44 | [diff] [blame] | 325 | return; |
[email protected] | d3d9b45b | 2013-11-05 20:21:44 | [diff] [blame] | 326 | |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 327 | std::vector<storage::MountPoints::MountPointInfo> mount_points; |
[email protected] | d3d9b45b | 2013-11-05 20:21:44 | [diff] [blame] | 328 | isolated_context()->GetDraggedFileInfo(root_url.filesystem_id(), |
| 329 | &mount_points); |
| 330 | |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 331 | std::vector<storage::MountPoints::MountPointInfo>::const_iterator it = |
[email protected] | d3d9b45b | 2013-11-05 20:21:44 | [diff] [blame] | 332 | mount_points.begin(); |
| 333 | for (; it != mount_points.end(); ++it) |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 334 | InnerAddFileSystem(show_info_bar_callback, std::string(), it->path); |
[email protected] | d3d9b45b | 2013-11-05 20:21:44 | [diff] [blame] | 335 | } |
| 336 | |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 337 | void DevToolsFileHelper::InnerAddFileSystem( |
[email protected] | 94252185 | 2013-04-26 12:29:23 | [diff] [blame] | 338 | const ShowInfoBarCallback& show_info_bar_callback, |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 339 | const std::string& type, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 340 | const base::FilePath& path) { |
[email protected] | 2c7136e | 2013-07-19 19:42:40 | [diff] [blame] | 341 | std::string file_system_path = path.AsUTF8Unsafe(); |
| 342 | |
Joel Einbinder | 92f494e | 2017-12-12 01:45:25 | [diff] [blame] | 343 | if (IsFileSystemAdded(file_system_path)) |
| 344 | RemoveFileSystem(file_system_path); |
[email protected] | 2c7136e | 2013-07-19 19:42:40 | [diff] [blame] | 345 | |
[email protected] | ad1eec6 | 2013-08-12 15:21:05 | [diff] [blame] | 346 | std::string path_display_name = path.AsEndingWithSeparator().AsUTF8Unsafe(); |
[email protected] | 4317aee | 2013-11-28 07:20:11 | [diff] [blame] | 347 | base::string16 message = l10n_util::GetStringFUTF16( |
[email protected] | 94252185 | 2013-04-26 12:29:23 | [diff] [blame] | 348 | IDS_DEV_TOOLS_CONFIRM_ADD_FILE_SYSTEM_MESSAGE, |
[email protected] | 0433872 | 2013-12-24 23:18:05 | [diff] [blame] | 349 | base::UTF8ToUTF16(path_display_name)); |
[email protected] | 94252185 | 2013-04-26 12:29:23 | [diff] [blame] | 350 | show_info_bar_callback.Run( |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 351 | message, Bind(&DevToolsFileHelper::AddUserConfirmedFileSystem, |
| 352 | weak_factory_.GetWeakPtr(), type, path)); |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 353 | } |
| 354 | |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 355 | void DevToolsFileHelper::AddUserConfirmedFileSystem(const std::string& type, |
| 356 | const base::FilePath& path, |
| 357 | bool allowed) { |
Dmitry Gozman | ffa4fa2 | 2017-10-20 04:00:49 | [diff] [blame] | 358 | if (!allowed) { |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 359 | FailedToAddFileSystem(kPermissionDenied); |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 360 | return; |
Dmitry Gozman | ffa4fa2 | 2017-10-20 04:00:49 | [diff] [blame] | 361 | } |
pfeldman | 31834e2 | 2015-10-10 00:47:05 | [diff] [blame] | 362 | |
| 363 | std::string file_system_id = RegisterFileSystem(web_contents_, path); |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 364 | std::string file_system_path = path.AsUTF8Unsafe(); |
| 365 | |
| 366 | DictionaryPrefUpdate update(profile_->GetPrefs(), |
| 367 | prefs::kDevToolsFileSystemPaths); |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 368 | base::DictionaryValue* file_systems_paths_value = update.Get(); |
| 369 | file_systems_paths_value->SetWithoutPathExpansion( |
Jeremy Roman | ec48d7a | 2018-03-01 17:35:09 | [diff] [blame] | 370 | file_system_path, std::make_unique<base::Value>(type)); |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 371 | } |
| 372 | |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 373 | void DevToolsFileHelper::FailedToAddFileSystem(const std::string& error) { |
| 374 | delegate_->FileSystemAdded(error, nullptr); |
Dmitry Gozman | ffa4fa2 | 2017-10-20 04:00:49 | [diff] [blame] | 375 | } |
| 376 | |
pfeldman | 31834e2 | 2015-10-10 00:47:05 | [diff] [blame] | 377 | std::vector<DevToolsFileHelper::FileSystem> |
| 378 | DevToolsFileHelper::GetFileSystems() { |
| 379 | file_system_paths_ = GetAddedFileSystemPaths(profile_); |
[email protected] | 94252185 | 2013-04-26 12:29:23 | [diff] [blame] | 380 | std::vector<FileSystem> file_systems; |
Pavel Feldman | b353680e | 2017-06-09 19:29:39 | [diff] [blame] | 381 | if (!file_watcher_) { |
Andrey Kosyakov | 7dabf71 | 2017-06-20 23:50:30 | [diff] [blame] | 382 | file_watcher_.reset(new DevToolsFileWatcher( |
| 383 | base::Bind(&DevToolsFileHelper::FilePathsChanged, |
| 384 | weak_factory_.GetWeakPtr()), |
| 385 | base::SequencedTaskRunnerHandle::Get())); |
Pavel Feldman | b353680e | 2017-06-09 19:29:39 | [diff] [blame] | 386 | pref_change_registrar_.Add( |
| 387 | prefs::kDevToolsFileSystemPaths, |
| 388 | base::Bind(&DevToolsFileHelper::FileSystemPathsSettingChanged, |
| 389 | base::Unretained(this))); |
| 390 | } |
pfeldman | 31834e2 | 2015-10-10 00:47:05 | [diff] [blame] | 391 | for (auto file_system_path : file_system_paths_) { |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 392 | base::FilePath path = |
| 393 | base::FilePath::FromUTF8Unsafe(file_system_path.first); |
pfeldman | 31834e2 | 2015-10-10 00:47:05 | [diff] [blame] | 394 | std::string file_system_id = RegisterFileSystem(web_contents_, path); |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 395 | FileSystem filesystem = |
| 396 | CreateFileSystemStruct(web_contents_, file_system_path.second, |
| 397 | file_system_id, file_system_path.first); |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 398 | file_systems.push_back(filesystem); |
Andrey Kosyakov | 7dabf71 | 2017-06-20 23:50:30 | [diff] [blame] | 399 | file_watcher_->AddWatch(std::move(path)); |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 400 | } |
pfeldman | 31834e2 | 2015-10-10 00:47:05 | [diff] [blame] | 401 | return file_systems; |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | void DevToolsFileHelper::RemoveFileSystem(const std::string& file_system_path) { |
mostynb | 13260d5 | 2015-03-26 09:12:09 | [diff] [blame] | 405 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 406 | base::FilePath path = base::FilePath::FromUTF8Unsafe(file_system_path); |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 407 | isolated_context()->RevokeFileSystemByPath(path); |
| 408 | |
| 409 | DictionaryPrefUpdate update(profile_->GetPrefs(), |
| 410 | prefs::kDevToolsFileSystemPaths); |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 411 | base::DictionaryValue* file_systems_paths_value = update.Get(); |
[email protected] | e492a80 | 2013-01-16 11:12:11 | [diff] [blame] | 412 | file_systems_paths_value->RemoveWithoutPathExpansion(file_system_path, NULL); |
| 413 | } |
[email protected] | 200bd33 | 2013-08-05 16:19:11 | [diff] [blame] | 414 | |
| 415 | bool DevToolsFileHelper::IsFileSystemAdded( |
| 416 | const std::string& file_system_path) { |
mostynb | 13260d5 | 2015-03-26 09:12:09 | [diff] [blame] | 417 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 418 | |
| 419 | const base::DictionaryValue* file_systems_paths_value = |
| 420 | profile_->GetPrefs()->GetDictionary(prefs::kDevToolsFileSystemPaths); |
| 421 | return file_systems_paths_value->HasKey(file_system_path); |
[email protected] | 200bd33 | 2013-08-05 16:19:11 | [diff] [blame] | 422 | } |
pfeldman | 31834e2 | 2015-10-10 00:47:05 | [diff] [blame] | 423 | |
Joel Einbinder | 488905f | 2018-01-09 20:53:10 | [diff] [blame] | 424 | void DevToolsFileHelper::OnOpenItemComplete( |
| 425 | const base::FilePath& path, |
| 426 | platform_util::OpenOperationResult result) { |
| 427 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 428 | if (result == platform_util::OPEN_FAILED_INVALID_TYPE) |
| 429 | platform_util::ShowItemInFolder(profile_, path); |
| 430 | } |
| 431 | |
| 432 | void DevToolsFileHelper::ShowItemInFolder(const std::string& file_system_path) { |
| 433 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 434 | if (file_system_path.empty()) |
| 435 | return; |
| 436 | base::FilePath path = base::FilePath::FromUTF8Unsafe(file_system_path); |
| 437 | platform_util::OpenItem(profile_, path, platform_util::OPEN_FOLDER, |
| 438 | base::Bind(&DevToolsFileHelper::OnOpenItemComplete, |
| 439 | weak_factory_.GetWeakPtr(), path)); |
| 440 | } |
| 441 | |
pfeldman | 31834e2 | 2015-10-10 00:47:05 | [diff] [blame] | 442 | void DevToolsFileHelper::FileSystemPathsSettingChanged() { |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 443 | PathToType remaining; |
pfeldman | 31834e2 | 2015-10-10 00:47:05 | [diff] [blame] | 444 | remaining.swap(file_system_paths_); |
Pavel Feldman | b353680e | 2017-06-09 19:29:39 | [diff] [blame] | 445 | DCHECK(file_watcher_.get()); |
pfeldman | 31834e2 | 2015-10-10 00:47:05 | [diff] [blame] | 446 | |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 447 | for (auto file_system : GetAddedFileSystemPaths(profile_)) { |
| 448 | if (remaining.find(file_system.first) == remaining.end()) { |
| 449 | base::FilePath path = base::FilePath::FromUTF8Unsafe(file_system.first); |
pfeldman | 31834e2 | 2015-10-10 00:47:05 | [diff] [blame] | 450 | std::string file_system_id = RegisterFileSystem(web_contents_, path); |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 451 | FileSystem filesystem = CreateFileSystemStruct( |
| 452 | web_contents_, file_system.second, file_system_id, file_system.first); |
| 453 | delegate_->FileSystemAdded(std::string(), &filesystem); |
Andrey Kosyakov | 7dabf71 | 2017-06-20 23:50:30 | [diff] [blame] | 454 | file_watcher_->AddWatch(std::move(path)); |
pfeldman | 31834e2 | 2015-10-10 00:47:05 | [diff] [blame] | 455 | } else { |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 456 | remaining.erase(file_system.first); |
pfeldman | 31834e2 | 2015-10-10 00:47:05 | [diff] [blame] | 457 | } |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 458 | file_system_paths_[file_system.first] = file_system.second; |
pfeldman | 31834e2 | 2015-10-10 00:47:05 | [diff] [blame] | 459 | } |
| 460 | |
Pavel Feldman | 44bd9917 | 2017-10-20 16:37:19 | [diff] [blame] | 461 | for (auto file_system : remaining) { |
| 462 | delegate_->FileSystemRemoved(file_system.first); |
| 463 | base::FilePath path = base::FilePath::FromUTF8Unsafe(file_system.first); |
Andrey Kosyakov | 7dabf71 | 2017-06-20 23:50:30 | [diff] [blame] | 464 | file_watcher_->RemoveWatch(std::move(path)); |
pfeldman | 7d01b87 | 2015-11-05 08:24:35 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
| 468 | void DevToolsFileHelper::FilePathsChanged( |
lushnikov | 2eaab0b3 | 2016-10-04 17:17:17 | [diff] [blame] | 469 | const std::vector<std::string>& changed_paths, |
| 470 | const std::vector<std::string>& added_paths, |
| 471 | const std::vector<std::string>& removed_paths) { |
| 472 | delegate_->FilePathsChanged(changed_paths, added_paths, removed_paths); |
pfeldman | 31834e2 | 2015-10-10 00:47:05 | [diff] [blame] | 473 | } |