Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(284)

Side by Side Diff: chrome/browser/devtools/devtools_file_helper.cc

Issue 14081036: DevTools: Replace .allow-devtools-edit file check with confirmation infobar. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed code style Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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
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
311 ValidateFoldersCallback validate_folders_callback = Bind( 295 string16 message = l10n_util::GetStringFUTF16(
312 &DevToolsFileHelper::AddValidatedFileSystem, 296 IDS_DEV_TOOLS_CONFIRM_ADD_FILE_SYSTEM_MESSAGE,
313 weak_factory_.GetWeakPtr(), 297 UTF8ToUTF16(path.AsUTF8Unsafe() + "/"));
314 callback); 298 show_info_bar_callback.Run(
315 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 299 message,
316 Bind(&ValidateFoldersOnFileThread, 300 Bind(&DevToolsFileHelper::AddUserConfirmedFileSystem,
317 file_paths, 301 weak_factory_.GetWeakPtr(),
318 validate_folders_callback)); 302 callback,
303 path),
304 Bind(callback, FileSystem()));
319 } 305 }
320 306
321 void DevToolsFileHelper::AddValidatedFileSystem( 307 void DevToolsFileHelper::AddUserConfirmedFileSystem(
322 const AddFileSystemCallback& callback, 308 const AddFileSystemCallback& callback,
323 const std::vector<base::FilePath>& permitted_paths) { 309 const base::FilePath& path) {
324 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
325 if (permitted_paths.empty()) {
326 std::string magic_file_name = base::FilePath(kMagicFileName).AsUTF8Unsafe();
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;
332 }
333 base::FilePath path = permitted_paths.at(0);
334 std::string registered_name; 310 std::string registered_name;
335 std::string file_system_id = RegisterFileSystem(web_contents_, 311 std::string file_system_id = RegisterFileSystem(web_contents_,
336 path, 312 path,
337 &registered_name); 313 &registered_name);
338 std::string file_system_path = path.AsUTF8Unsafe(); 314 std::string file_system_path = path.AsUTF8Unsafe();
339 315
340 DictionaryPrefUpdate update(profile_->GetPrefs(), 316 DictionaryPrefUpdate update(profile_->GetPrefs(),
341 prefs::kDevToolsFileSystemPaths); 317 prefs::kDevToolsFileSystemPaths);
342 DictionaryValue* file_systems_paths_value = update.Get(); 318 DictionaryValue* file_systems_paths_value = update.Get();
343 file_systems_paths_value->Set(file_system_path, Value::CreateNullValue()); 319 file_systems_paths_value->Set(file_system_path, Value::CreateNullValue());
344 320
345 FileSystem filesystem = CreateFileSystemStruct(web_contents_, 321 FileSystem filesystem = CreateFileSystemStruct(web_contents_,
346 file_system_id, 322 file_system_id,
347 registered_name, 323 registered_name,
348 file_system_path); 324 file_system_path);
349 callback.Run(std::string(), filesystem); 325 callback.Run(filesystem);
350 } 326 }
351 327
352 void DevToolsFileHelper::RequestFileSystems( 328 void DevToolsFileHelper::RequestFileSystems(
353 const RequestFileSystemsCallback& callback) { 329 const RequestFileSystemsCallback& callback) {
354 const DictionaryValue* file_systems_paths_value = 330 const DictionaryValue* file_systems_paths_value =
355 profile_->GetPrefs()->GetDictionary(prefs::kDevToolsFileSystemPaths); 331 profile_->GetPrefs()->GetDictionary(prefs::kDevToolsFileSystemPaths);
356 std::vector<base::FilePath> saved_paths; 332 std::vector<FileSystem> file_systems;
357 for (DictionaryValue::Iterator it(*file_systems_paths_value); !it.IsAtEnd(); 333 for (DictionaryValue::Iterator it(*file_systems_paths_value); !it.IsAtEnd();
358 it.Advance()) { 334 it.Advance()) {
359 std::string file_system_path = it.key(); 335 std::string file_system_path = it.key();
360 base::FilePath path = base::FilePath::FromUTF8Unsafe(file_system_path); 336 base::FilePath path = base::FilePath::FromUTF8Unsafe(file_system_path);
361 saved_paths.push_back(path);
362 }
363 337
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; 338 std::string registered_name;
382 std::string file_system_id = RegisterFileSystem(web_contents_, 339 std::string file_system_id = RegisterFileSystem(web_contents_,
383 *it, 340 path,
384 &registered_name); 341 &registered_name);
385 std::string file_system_path = it->AsUTF8Unsafe();
386 FileSystem filesystem = CreateFileSystemStruct(web_contents_, 342 FileSystem filesystem = CreateFileSystemStruct(web_contents_,
387 file_system_id, 343 file_system_id,
388 registered_name, 344 registered_name,
389 file_system_path); 345 file_system_path);
390 file_systems.push_back(filesystem); 346 file_systems.push_back(filesystem);
391 } 347 }
392 callback.Run(file_systems); 348 callback.Run(file_systems);
393 } 349 }
394 350
395 void DevToolsFileHelper::RemoveFileSystem(const std::string& file_system_path) { 351 void DevToolsFileHelper::RemoveFileSystem(const std::string& file_system_path) {
396 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 352 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
397 base::FilePath path = base::FilePath::FromUTF8Unsafe(file_system_path); 353 base::FilePath path = base::FilePath::FromUTF8Unsafe(file_system_path);
398 isolated_context()->RevokeFileSystemByPath(path); 354 isolated_context()->RevokeFileSystemByPath(path);
399 355
400 DictionaryPrefUpdate update(profile_->GetPrefs(), 356 DictionaryPrefUpdate update(profile_->GetPrefs(),
401 prefs::kDevToolsFileSystemPaths); 357 prefs::kDevToolsFileSystemPaths);
402 DictionaryValue* file_systems_paths_value = update.Get(); 358 DictionaryValue* file_systems_paths_value = update.Get();
403 file_systems_paths_value->RemoveWithoutPathExpansion(file_system_path, NULL); 359 file_systems_paths_value->RemoveWithoutPathExpansion(file_system_path, NULL);
404 } 360 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698