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" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 base::LazyInstance<base::FilePath>::Leaky | 48 base::LazyInstance<base::FilePath>::Leaky |
49 g_last_save_path = LAZY_INSTANCE_INITIALIZER; | 49 g_last_save_path = LAZY_INSTANCE_INITIALIZER; |
50 | 50 |
51 } // namespace | 51 } // namespace |
52 | 52 |
53 namespace { | 53 namespace { |
54 | 54 |
55 typedef Callback<void(const base::FilePath&)> SelectedCallback; | 55 typedef Callback<void(const base::FilePath&)> SelectedCallback; |
56 typedef Callback<void(void)> CanceledCallback; | 56 typedef Callback<void(void)> CanceledCallback; |
57 | 57 |
58 const base::FilePath::CharType kMagicFileName[] = | |
59 FILE_PATH_LITERAL(".allow-devtools-edit"); | |
60 | |
61 class SelectFileDialog : public ui::SelectFileDialog::Listener, | 58 class SelectFileDialog : public ui::SelectFileDialog::Listener, |
62 public base::RefCounted<SelectFileDialog> { | 59 public base::RefCounted<SelectFileDialog> { |
63 public: | 60 public: |
64 SelectFileDialog(const SelectedCallback& selected_callback, | 61 SelectFileDialog(const SelectedCallback& selected_callback, |
65 const CanceledCallback& canceled_callback) | 62 const CanceledCallback& canceled_callback) |
66 : selected_callback_(selected_callback), | 63 : selected_callback_(selected_callback), |
67 canceled_callback_(canceled_callback) { | 64 canceled_callback_(canceled_callback) { |
68 select_file_dialog_ = ui::SelectFileDialog::Create( | 65 select_file_dialog_ = ui::SelectFileDialog::Create( |
69 this, new ChromeSelectFilePolicy(NULL)); | 66 this, new ChromeSelectFilePolicy(NULL)); |
70 } | 67 } |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 | 147 |
151 // We only need file level access for reading FileEntries. Saving FileEntries | 148 // We only need file level access for reading FileEntries. Saving FileEntries |
152 // just needs the file system to have read/write access, which is granted | 149 // just needs the file system to have read/write access, which is granted |
153 // above if required. | 150 // above if required. |
154 if (!policy->CanReadFile(renderer_id, path)) | 151 if (!policy->CanReadFile(renderer_id, path)) |
155 policy->GrantReadFile(renderer_id, path); | 152 policy->GrantReadFile(renderer_id, path); |
156 | 153 |
157 return file_system_id; | 154 return file_system_id; |
158 } | 155 } |
159 | 156 |
160 typedef Callback<void(const std::vector<base::FilePath>&)> | |
161 ValidateFoldersCallback; | |
162 | |
163 void ValidateFoldersOnFileThread(const std::vector<base::FilePath>& file_paths, | |
164 const ValidateFoldersCallback& callback) { | |
165 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
166 std::vector<base::FilePath> permitted_paths; | |
167 std::vector<base::FilePath>::const_iterator it; | |
168 for (it = file_paths.begin(); it != file_paths.end(); ++it) { | |
169 base::FilePath security_file_path = it->Append(kMagicFileName); | |
170 if (file_util::PathExists(security_file_path)) | |
171 permitted_paths.push_back(*it); | |
172 } | |
173 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
174 Bind(callback, permitted_paths)); | |
175 } | |
176 | |
177 DevToolsFileHelper::FileSystem CreateFileSystemStruct( | 157 DevToolsFileHelper::FileSystem CreateFileSystemStruct( |
178 WebContents* web_contents, | 158 WebContents* web_contents, |
179 const std::string& file_system_id, | 159 const std::string& file_system_id, |
180 const std::string& registered_name, | 160 const std::string& registered_name, |
181 const std::string& file_system_path) { | 161 const std::string& file_system_path) { |
182 const GURL origin = web_contents->GetURL().GetOrigin(); | 162 const GURL origin = web_contents->GetURL().GetOrigin(); |
183 std::string file_system_name = fileapi::GetIsolatedFileSystemName( | 163 std::string file_system_name = fileapi::GetIsolatedFileSystemName( |
184 origin, | 164 origin, |
185 file_system_id); | 165 file_system_id); |
186 std::string root_url = fileapi::GetIsolatedFileSystemRootURIString( | 166 std::string root_url = fileapi::GetIsolatedFileSystemRootURIString( |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 files_map->SetWithoutPathExpansion(base::MD5String(url), | 267 files_map->SetWithoutPathExpansion(base::MD5String(url), |
288 base::CreateFilePathValue(path)); | 268 base::CreateFilePathValue(path)); |
289 callback.Run(); | 269 callback.Run(); |
290 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 270 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
291 Bind(&WriteToFile, path, content)); | 271 Bind(&WriteToFile, path, content)); |
292 } | 272 } |
293 | 273 |
294 void DevToolsFileHelper::SaveAsFileSelectionCanceled() { | 274 void DevToolsFileHelper::SaveAsFileSelectionCanceled() { |
295 } | 275 } |
296 | 276 |
297 void DevToolsFileHelper::AddFileSystem(const AddFileSystemCallback& callback) { | 277 void DevToolsFileHelper::AddFileSystem( |
| 278 const AddFileSystemCallback& callback, |
| 279 const ShowInfoBarCallback& show_info_bar_callback) { |
298 scoped_refptr<SelectFileDialog> select_file_dialog = new SelectFileDialog( | 280 scoped_refptr<SelectFileDialog> select_file_dialog = new SelectFileDialog( |
299 Bind(&DevToolsFileHelper::InnerAddFileSystem, | 281 Bind(&DevToolsFileHelper::InnerAddFileSystem, |
300 weak_factory_.GetWeakPtr(), | 282 weak_factory_.GetWeakPtr(), |
301 callback), | 283 callback, |
302 Bind(callback, "", FileSystem())); | 284 show_info_bar_callback), |
| 285 Bind(callback, FileSystem())); |
303 select_file_dialog->Show(ui::SelectFileDialog::SELECT_FOLDER, | 286 select_file_dialog->Show(ui::SelectFileDialog::SELECT_FOLDER, |
304 base::FilePath()); | 287 base::FilePath()); |
305 } | 288 } |
306 | 289 |
307 void DevToolsFileHelper::InnerAddFileSystem( | 290 void DevToolsFileHelper::InnerAddFileSystem( |
308 const AddFileSystemCallback& callback, | 291 const AddFileSystemCallback& callback, |
| 292 const ShowInfoBarCallback& show_info_bar_callback, |
309 const base::FilePath& path) { | 293 const base::FilePath& path) { |
310 std::vector<base::FilePath> file_paths(1, path); | 294 string16 message = l10n_util::GetStringFUTF16( |
311 ValidateFoldersCallback validate_folders_callback = Bind( | 295 IDS_DEV_TOOLS_CONFIRM_ADD_FILE_SYSTEM_MESSAGE, |
312 &DevToolsFileHelper::AddValidatedFileSystem, | 296 UTF8ToUTF16(path.AsUTF8Unsafe() + "/")); |
313 weak_factory_.GetWeakPtr(), | 297 show_info_bar_callback.Run( |
314 callback); | 298 message, |
315 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 299 Bind(&DevToolsFileHelper::AddUserConfirmedFileSystem, |
316 Bind(&ValidateFoldersOnFileThread, | 300 weak_factory_.GetWeakPtr(), |
317 file_paths, | 301 callback, path)); |
318 validate_folders_callback)); | |
319 } | 302 } |
320 | 303 |
321 void DevToolsFileHelper::AddValidatedFileSystem( | 304 void DevToolsFileHelper::AddUserConfirmedFileSystem( |
322 const AddFileSystemCallback& callback, | 305 const AddFileSystemCallback& callback, |
323 const std::vector<base::FilePath>& permitted_paths) { | 306 const base::FilePath& path, |
324 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 307 bool allowed) { |
325 if (permitted_paths.empty()) { | 308 if (!allowed) { |
326 std::string magic_file_name = base::FilePath(kMagicFileName).AsUTF8Unsafe(); | 309 callback.Run(FileSystem()); |
327 std::string error_string = l10n_util::GetStringFUTF8( | |
328 IDS_DEV_TOOLS_MAGIC_FILE_NOT_EXISTS_MESSAGE, | |
329 UTF8ToUTF16(magic_file_name)); | |
330 callback.Run(error_string, FileSystem()); | |
331 return; | 310 return; |
332 } | 311 } |
333 base::FilePath path = permitted_paths.at(0); | |
334 std::string registered_name; | 312 std::string registered_name; |
335 std::string file_system_id = RegisterFileSystem(web_contents_, | 313 std::string file_system_id = RegisterFileSystem(web_contents_, |
336 path, | 314 path, |
337 ®istered_name); | 315 ®istered_name); |
338 std::string file_system_path = path.AsUTF8Unsafe(); | 316 std::string file_system_path = path.AsUTF8Unsafe(); |
339 | 317 |
340 DictionaryPrefUpdate update(profile_->GetPrefs(), | 318 DictionaryPrefUpdate update(profile_->GetPrefs(), |
341 prefs::kDevToolsFileSystemPaths); | 319 prefs::kDevToolsFileSystemPaths); |
342 DictionaryValue* file_systems_paths_value = update.Get(); | 320 DictionaryValue* file_systems_paths_value = update.Get(); |
343 file_systems_paths_value->Set(file_system_path, Value::CreateNullValue()); | 321 file_systems_paths_value->Set(file_system_path, Value::CreateNullValue()); |
344 | 322 |
345 FileSystem filesystem = CreateFileSystemStruct(web_contents_, | 323 FileSystem filesystem = CreateFileSystemStruct(web_contents_, |
346 file_system_id, | 324 file_system_id, |
347 registered_name, | 325 registered_name, |
348 file_system_path); | 326 file_system_path); |
349 callback.Run(std::string(), filesystem); | 327 callback.Run(filesystem); |
350 } | 328 } |
351 | 329 |
352 void DevToolsFileHelper::RequestFileSystems( | 330 void DevToolsFileHelper::RequestFileSystems( |
353 const RequestFileSystemsCallback& callback) { | 331 const RequestFileSystemsCallback& callback) { |
354 const DictionaryValue* file_systems_paths_value = | 332 const DictionaryValue* file_systems_paths_value = |
355 profile_->GetPrefs()->GetDictionary(prefs::kDevToolsFileSystemPaths); | 333 profile_->GetPrefs()->GetDictionary(prefs::kDevToolsFileSystemPaths); |
356 std::vector<base::FilePath> saved_paths; | 334 std::vector<FileSystem> file_systems; |
357 for (DictionaryValue::Iterator it(*file_systems_paths_value); !it.IsAtEnd(); | 335 for (DictionaryValue::Iterator it(*file_systems_paths_value); !it.IsAtEnd(); |
358 it.Advance()) { | 336 it.Advance()) { |
359 std::string file_system_path = it.key(); | 337 std::string file_system_path = it.key(); |
360 base::FilePath path = base::FilePath::FromUTF8Unsafe(file_system_path); | 338 base::FilePath path = base::FilePath::FromUTF8Unsafe(file_system_path); |
361 saved_paths.push_back(path); | |
362 } | |
363 | 339 |
364 ValidateFoldersCallback validate_folders_callback = Bind( | |
365 &DevToolsFileHelper::RestoreValidatedFileSystems, | |
366 weak_factory_.GetWeakPtr(), | |
367 callback); | |
368 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
369 Bind(&ValidateFoldersOnFileThread, | |
370 saved_paths, | |
371 validate_folders_callback)); | |
372 } | |
373 | |
374 void DevToolsFileHelper::RestoreValidatedFileSystems( | |
375 const RequestFileSystemsCallback& callback, | |
376 const std::vector<base::FilePath>& permitted_paths) { | |
377 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
378 std::vector<FileSystem> file_systems; | |
379 std::vector<base::FilePath>::const_iterator it; | |
380 for (it = permitted_paths.begin(); it != permitted_paths.end(); ++it) { | |
381 std::string registered_name; | 340 std::string registered_name; |
382 std::string file_system_id = RegisterFileSystem(web_contents_, | 341 std::string file_system_id = RegisterFileSystem(web_contents_, |
383 *it, | 342 path, |
384 ®istered_name); | 343 ®istered_name); |
385 std::string file_system_path = it->AsUTF8Unsafe(); | |
386 FileSystem filesystem = CreateFileSystemStruct(web_contents_, | 344 FileSystem filesystem = CreateFileSystemStruct(web_contents_, |
387 file_system_id, | 345 file_system_id, |
388 registered_name, | 346 registered_name, |
389 file_system_path); | 347 file_system_path); |
390 file_systems.push_back(filesystem); | 348 file_systems.push_back(filesystem); |
391 } | 349 } |
392 callback.Run(file_systems); | 350 callback.Run(file_systems); |
393 } | 351 } |
394 | 352 |
395 void DevToolsFileHelper::RemoveFileSystem(const std::string& file_system_path) { | 353 void DevToolsFileHelper::RemoveFileSystem(const std::string& file_system_path) { |
396 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 354 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
397 base::FilePath path = base::FilePath::FromUTF8Unsafe(file_system_path); | 355 base::FilePath path = base::FilePath::FromUTF8Unsafe(file_system_path); |
398 isolated_context()->RevokeFileSystemByPath(path); | 356 isolated_context()->RevokeFileSystemByPath(path); |
399 | 357 |
400 DictionaryPrefUpdate update(profile_->GetPrefs(), | 358 DictionaryPrefUpdate update(profile_->GetPrefs(), |
401 prefs::kDevToolsFileSystemPaths); | 359 prefs::kDevToolsFileSystemPaths); |
402 DictionaryValue* file_systems_paths_value = update.Get(); | 360 DictionaryValue* file_systems_paths_value = update.Get(); |
403 file_systems_paths_value->RemoveWithoutPathExpansion(file_system_path, NULL); | 361 file_systems_paths_value->RemoveWithoutPathExpansion(file_system_path, NULL); |
404 } | 362 } |
OLD | NEW |