Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/devtools/devtools_file_helper.h" | 5 #include "chrome/browser/devtools/devtools_file_helper.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/md5.h" | 13 #include "base/md5.h" |
| 14 #include "base/utf_string_conversions.h" | |
| 14 #include "base/value_conversions.h" | 15 #include "base/value_conversions.h" |
| 15 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/download/download_prefs.h" | 17 #include "chrome/browser/download/download_prefs.h" |
| 17 #include "chrome/browser/prefs/pref_service.h" | 18 #include "chrome/browser/prefs/pref_service.h" |
| 18 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 19 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 19 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 20 #include "chrome/browser/ui/chrome_select_file_policy.h" | 21 #include "chrome/browser/ui/chrome_select_file_policy.h" |
| 21 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
| 22 #include "content/public/browser/browser_context.h" | 23 #include "content/public/browser/browser_context.h" |
| 24 #include "content/public/browser/browser_thread.h" | |
| 25 #include "content/public/browser/child_process_security_policy.h" | |
| 23 #include "content/public/browser/download_manager.h" | 26 #include "content/public/browser/download_manager.h" |
| 27 #include "content/public/browser/render_process_host.h" | |
| 28 #include "content/public/browser/render_view_host.h" | |
| 29 #include "content/public/browser/web_contents.h" | |
| 30 #include "grit/generated_resources.h" | |
| 24 #include "ui/base/dialogs/select_file_dialog.h" | 31 #include "ui/base/dialogs/select_file_dialog.h" |
| 32 #include "ui/base/l10n/l10n_util.h" | |
| 33 #include "webkit/fileapi/isolated_context.h" | |
| 25 | 34 |
| 26 using base::Bind; | 35 using base::Bind; |
| 27 using base::Callback; | 36 using base::Callback; |
| 28 using content::BrowserContext; | 37 using content::BrowserContext; |
| 29 using content::BrowserThread; | 38 using content::BrowserThread; |
| 30 using content::DownloadManager; | 39 using content::DownloadManager; |
| 40 using content::RenderViewHost; | |
| 41 using content::WebContents; | |
| 31 | 42 |
| 32 namespace { | 43 namespace { |
| 33 | 44 |
| 34 base::LazyInstance<FilePath>::Leaky | 45 base::LazyInstance<FilePath>::Leaky |
| 35 g_last_save_path = LAZY_INSTANCE_INITIALIZER; | 46 g_last_save_path = LAZY_INSTANCE_INITIALIZER; |
| 36 | 47 |
| 37 } // namespace | 48 } // namespace |
| 38 | 49 |
| 39 namespace { | 50 namespace { |
| 40 | 51 |
| 41 typedef Callback<void(const FilePath&)> SelectedCallback; | 52 typedef Callback<void(const FilePath&)> SelectedCallback; |
| 42 typedef Callback<void(void)> CanceledCallback; | 53 typedef Callback<void(void)> CanceledCallback; |
| 43 | 54 |
| 55 const char kMagicFileName[] = ".allow-devtools-edit"; | |
| 56 | |
| 44 class SelectFileDialog : public ui::SelectFileDialog::Listener, | 57 class SelectFileDialog : public ui::SelectFileDialog::Listener, |
| 45 public base::RefCounted<SelectFileDialog> { | 58 public base::RefCounted<SelectFileDialog> { |
| 46 public: | 59 public: |
| 47 SelectFileDialog(const SelectedCallback& selected_callback, | 60 SelectFileDialog(const SelectedCallback& selected_callback, |
| 48 const CanceledCallback& canceled_callback) | 61 const CanceledCallback& canceled_callback) |
| 49 : selected_callback_(selected_callback), | 62 : selected_callback_(selected_callback), |
| 50 canceled_callback_(canceled_callback) { | 63 canceled_callback_(canceled_callback) { |
| 51 select_file_dialog_ = ui::SelectFileDialog::Create( | 64 select_file_dialog_ = ui::SelectFileDialog::Create( |
| 52 this, new ChromeSelectFilePolicy(NULL)); | 65 this, new ChromeSelectFilePolicy(NULL)); |
| 53 } | 66 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 file_util::WriteFile(path, content.c_str(), content.length()); | 111 file_util::WriteFile(path, content.c_str(), content.length()); |
| 99 } | 112 } |
| 100 | 113 |
| 101 void AppendToFile(const FilePath& path, const std::string& content) { | 114 void AppendToFile(const FilePath& path, const std::string& content) { |
| 102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 103 DCHECK(!path.empty()); | 116 DCHECK(!path.empty()); |
| 104 | 117 |
| 105 file_util::AppendToFile(path, content.c_str(), content.length()); | 118 file_util::AppendToFile(path, content.c_str(), content.length()); |
| 106 } | 119 } |
| 107 | 120 |
| 121 fileapi::IsolatedContext* isolated_context() { | |
| 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 123 fileapi::IsolatedContext* isolated_context = | |
| 124 fileapi::IsolatedContext::GetInstance(); | |
| 125 DCHECK(isolated_context); | |
| 126 return isolated_context; | |
| 127 } | |
| 128 | |
| 129 std::string RegisterFilesystem(WebContents* web_contents, | |
| 130 const FilePath& path, | |
| 131 std::string* registered_name) { | |
| 132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
|
pfeldman
2012/12/29 10:23:02
CHECK(content::GetContentClient()->HasWebUIScheme(
| |
| 133 std::string filesystem_id = isolated_context()->RegisterFileSystemForPath( | |
| 134 fileapi::kFileSystemTypeNativeLocal, path, registered_name); | |
| 135 | |
| 136 content::ChildProcessSecurityPolicy* policy = | |
| 137 content::ChildProcessSecurityPolicy::GetInstance(); | |
| 138 RenderViewHost* render_view_host = web_contents->GetRenderViewHost(); | |
| 139 int renderer_id = render_view_host->GetProcess()->GetID(); | |
| 140 policy->GrantReadWriteFileSystem(renderer_id, filesystem_id); | |
| 141 | |
| 142 // We only need file level access for reading FileEntries. Saving FileEntries | |
| 143 // just needs the file system to have read/write access, which is granted | |
| 144 // above if required. | |
| 145 if (!policy->CanReadFile(renderer_id, path)) | |
| 146 policy->GrantReadFile(renderer_id, path); | |
| 147 | |
| 148 return filesystem_id; | |
| 149 } | |
| 150 | |
| 151 typedef Callback<void(const std::vector<FilePath>&)> CheckFoldersCallback; | |
|
pfeldman
2012/12/29 10:23:02
Check -> Validate
vsevik
2012/12/29 14:23:34
Done.
| |
| 152 | |
| 153 void FoldersCheckedOnFileThread(const CheckFoldersCallback& callback, | |
| 154 const std::vector<FilePath>& permitted_paths) { | |
| 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 156 callback.Run(permitted_paths); | |
| 157 } | |
| 158 | |
| 159 void CheckFoldersOnFileThread(const std::vector<FilePath>& file_paths, | |
| 160 const CheckFoldersCallback& callback) { | |
| 161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 162 std::vector<FilePath> permitted_paths; | |
| 163 std::vector<FilePath>::const_iterator it; | |
| 164 for (it = file_paths.begin(); it != file_paths.end(); ++it) { | |
| 165 FilePath security_file_path = it->Append(FILE_PATH_LITERAL(kMagicFileName)); | |
| 166 if (file_util::PathExists(security_file_path)) | |
| 167 permitted_paths.push_back(*it); | |
| 168 } | |
| 169 BrowserThread::PostTask( | |
| 170 BrowserThread::UI, FROM_HERE, | |
| 171 Bind(&FoldersCheckedOnFileThread, callback, permitted_paths)); | |
| 172 } | |
| 173 | |
| 108 } // namespace | 174 } // namespace |
| 109 | 175 |
| 110 DevToolsFileHelper::DevToolsFileHelper(Profile* profile) : profile_(profile), | 176 DevToolsFileHelper::Filesystem::Filesystem() { |
| 111 weak_factory_(this) { | 177 } |
| 178 | |
| 179 DevToolsFileHelper::Filesystem::Filesystem(const std::string& filesystem_id, | |
| 180 const std::string& registered_name, | |
| 181 const std::string& filesystem_path) | |
| 182 : filesystem_id(filesystem_id), | |
| 183 registered_name(registered_name), | |
| 184 filesystem_path(filesystem_path) { | |
| 185 } | |
| 186 | |
| 187 DevToolsFileHelper::DevToolsFileHelper(WebContents* web_contents, | |
| 188 Profile* profile) | |
| 189 : web_contents_(web_contents), profile_(profile), weak_factory_(this) { | |
|
pfeldman
2012/12/29 10:23:02
stack there.
vsevik
2012/12/29 14:23:34
Done.
| |
| 112 } | 190 } |
| 113 | 191 |
| 114 DevToolsFileHelper::~DevToolsFileHelper() { | 192 DevToolsFileHelper::~DevToolsFileHelper() { |
| 115 } | 193 } |
| 116 | 194 |
| 117 void DevToolsFileHelper::Save(const std::string& url, | 195 void DevToolsFileHelper::Save(const std::string& url, |
| 118 const std::string& content, | 196 const std::string& content, |
| 119 bool save_as, | 197 bool save_as, |
| 120 const SaveCallback& callback) { | 198 const SaveCallback& callback) { |
| 121 PathsMap::iterator it = saved_files_.find(url); | 199 PathsMap::iterator it = saved_files_.find(url); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 163 } | 241 } |
| 164 | 242 |
| 165 void DevToolsFileHelper::Append(const std::string& url, | 243 void DevToolsFileHelper::Append(const std::string& url, |
| 166 const std::string& content, | 244 const std::string& content, |
| 167 const AppendCallback& callback) { | 245 const AppendCallback& callback) { |
| 168 PathsMap::iterator it = saved_files_.find(url); | 246 PathsMap::iterator it = saved_files_.find(url); |
| 169 if (it == saved_files_.end()) | 247 if (it == saved_files_.end()) |
| 170 return; | 248 return; |
| 171 callback.Run(); | 249 callback.Run(); |
| 172 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 250 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 173 base::Bind(&AppendToFile, it->second, content)); | 251 Bind(&AppendToFile, it->second, content)); |
| 174 } | 252 } |
| 175 | 253 |
| 176 void DevToolsFileHelper::SaveAsFileSelected(const std::string& url, | 254 void DevToolsFileHelper::SaveAsFileSelected(const std::string& url, |
| 177 const std::string& content, | 255 const std::string& content, |
| 178 const SaveCallback& callback, | 256 const SaveCallback& callback, |
| 179 const FilePath& path) { | 257 const FilePath& path) { |
| 180 *g_last_save_path.Pointer() = path; | 258 *g_last_save_path.Pointer() = path; |
| 181 saved_files_[url] = path; | 259 saved_files_[url] = path; |
| 182 | 260 |
| 183 DictionaryPrefUpdate update(profile_->GetPrefs(), | 261 DictionaryPrefUpdate update(profile_->GetPrefs(), |
| 184 prefs::kDevToolsEditedFiles); | 262 prefs::kDevToolsEditedFiles); |
| 185 DictionaryValue* files_map = update.Get(); | 263 DictionaryValue* files_map = update.Get(); |
| 186 files_map->SetWithoutPathExpansion(base::MD5String(url), | 264 files_map->SetWithoutPathExpansion(base::MD5String(url), |
| 187 base::CreateFilePathValue(path)); | 265 base::CreateFilePathValue(path)); |
| 188 callback.Run(); | 266 callback.Run(); |
| 189 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 267 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 190 base::Bind(&WriteToFile, path, content)); | 268 Bind(&WriteToFile, path, content)); |
| 191 } | 269 } |
| 192 | 270 |
| 193 void DevToolsFileHelper::SaveAsFileSelectionCanceled() { | 271 void DevToolsFileHelper::SaveAsFileSelectionCanceled() { |
| 194 } | 272 } |
| 273 | |
| 274 void DevToolsFileHelper::AddFilesystem(const AddFilesystemCallback& callback) { | |
| 275 scoped_refptr<SelectFileDialog> select_file_dialog = new SelectFileDialog( | |
| 276 Bind(&DevToolsFileHelper::FolderSelected, | |
| 277 weak_factory_.GetWeakPtr(), | |
| 278 callback), | |
| 279 Bind(&DevToolsFileHelper::FolderSelectionCanceled, | |
| 280 weak_factory_.GetWeakPtr(), | |
| 281 callback)); | |
| 282 select_file_dialog->Show(ui::SelectFileDialog::SELECT_FOLDER, FilePath()); | |
| 283 } | |
| 284 | |
| 285 void DevToolsFileHelper::FolderSelected(const AddFilesystemCallback& callback, | |
|
pfeldman
2012/12/29 10:23:02
InnerAddFilesystem
vsevik
2012/12/29 14:23:34
Done.
| |
| 286 const FilePath& path) { | |
| 287 std::vector<FilePath> file_paths(1, path); | |
| 288 CheckFoldersCallback check_folders_callback = Bind( | |
| 289 &DevToolsFileHelper::SelectedFilesystemChecked, | |
|
pfeldman
2012/12/29 10:23:02
AddValidatedFilesystem
vsevik
2012/12/29 14:23:34
Done.
| |
| 290 weak_factory_.GetWeakPtr(), | |
| 291 callback); | |
| 292 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 293 Bind(&CheckFoldersOnFileThread, | |
| 294 file_paths, | |
| 295 check_folders_callback)); | |
| 296 } | |
| 297 | |
| 298 void DevToolsFileHelper::SelectedFilesystemChecked( | |
| 299 const AddFilesystemCallback& callback, | |
| 300 const std::vector<FilePath>& permitted_paths) { | |
| 301 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 302 if (permitted_paths.empty()) { | |
| 303 std::string error_string = l10n_util::GetStringFUTF8( | |
| 304 IDS_DEV_TOOLS_MAGIC_FILE_NOT_EXISTS_MESSAGE, | |
| 305 UTF8ToUTF16(kMagicFileName)); | |
| 306 callback.Run(error_string, Filesystem()); | |
| 307 return; | |
| 308 } | |
| 309 FilePath path = permitted_paths.at(0); | |
| 310 std::string registered_name; | |
| 311 std::string filesystem_id = RegisterFilesystem(web_contents_, | |
| 312 path, | |
| 313 ®istered_name); | |
| 314 std::string filesystem_path = path.AsUTF8Unsafe(); | |
| 315 | |
| 316 DictionaryPrefUpdate update(profile_->GetPrefs(), | |
| 317 prefs::kDevToolsFilesystemPaths); | |
| 318 DictionaryValue* filesystems_paths_value = update.Get(); | |
| 319 filesystems_paths_value->Set(filesystem_path, Value::CreateNullValue()); | |
| 320 | |
| 321 Filesystem filesystem(filesystem_id, registered_name, filesystem_path); | |
| 322 callback.Run("", filesystem); | |
| 323 } | |
| 324 | |
| 325 void DevToolsFileHelper::FolderSelectionCanceled( | |
| 326 const AddFilesystemCallback& callback) { | |
| 327 callback.Run("", Filesystem()); | |
| 328 } | |
| 329 | |
| 330 void DevToolsFileHelper::RequestFilesystems( | |
| 331 const RequestFilesystemsCallback& callback) { | |
| 332 const DictionaryValue* filesystems_paths_value = | |
| 333 profile_->GetPrefs()->GetDictionary(prefs::kDevToolsFilesystemPaths); | |
| 334 std::vector<FilePath> saved_paths; | |
| 335 DictionaryValue::key_iterator it = filesystems_paths_value->begin_keys(); | |
| 336 for (; it != filesystems_paths_value->end_keys(); ++it) { | |
| 337 std::string filesystem_path = *it; | |
| 338 FilePath path = FilePath::FromUTF8Unsafe(filesystem_path); | |
| 339 saved_paths.push_back(path); | |
| 340 } | |
| 341 | |
| 342 CheckFoldersCallback check_folders_callback = Bind( | |
| 343 &DevToolsFileHelper::RegisterPermittedFilesystems, | |
|
pfeldman
2012/12/29 10:23:02
RestoreFilesystems
vsevik
2012/12/29 14:23:34
Done.
| |
| 344 weak_factory_.GetWeakPtr(), | |
| 345 callback); | |
| 346 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 347 Bind(&CheckFoldersOnFileThread, | |
| 348 saved_paths, | |
| 349 check_folders_callback)); | |
| 350 } | |
| 351 | |
| 352 void DevToolsFileHelper::RegisterPermittedFilesystems( | |
|
pfeldman
2012/12/29 10:23:02
RestoreValidatedFilesystems
vsevik
2012/12/29 14:23:34
Done.
vsevik
2012/12/29 14:23:34
Done.
| |
| 353 const RequestFilesystemsCallback& callback, | |
| 354 const std::vector<FilePath>& permitted_paths) { | |
| 355 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 356 std::vector<Filesystem> filesystems; | |
| 357 std::vector<FilePath>::const_iterator it; | |
| 358 for (it = permitted_paths.begin(); it != permitted_paths.end(); ++it) { | |
| 359 std::string registered_name; | |
| 360 std::string filesystem_id = RegisterFilesystem(web_contents_, | |
| 361 *it, | |
| 362 ®istered_name); | |
| 363 std::string filesystem_path = it->AsUTF8Unsafe(); | |
| 364 Filesystem filesystem(filesystem_id, registered_name, filesystem_path); | |
| 365 filesystems.push_back(filesystem); | |
| 366 } | |
| 367 callback.Run(filesystems); | |
| 368 } | |
| 369 | |
| 370 void DevToolsFileHelper::RemoveFilesystem(const std::string& filesystem_path) { | |
| 371 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 372 FilePath path = FilePath::FromUTF8Unsafe(filesystem_path); | |
| 373 isolated_context()->RevokeFileSystemByPath(path); | |
| 374 | |
| 375 DictionaryPrefUpdate update(profile_->GetPrefs(), | |
| 376 prefs::kDevToolsFilesystemPaths); | |
| 377 DictionaryValue* filesystems_paths_value = update.Get(); | |
| 378 filesystems_paths_value->RemoveWithoutPathExpansion(filesystem_path, NULL); | |
| 379 } | |
| OLD | NEW |