Chromium Code Reviews| Index: chrome/browser/devtools/devtools_file_helper.cc |
| diff --git a/chrome/browser/devtools/devtools_file_helper.cc b/chrome/browser/devtools/devtools_file_helper.cc |
| index 945de5f72935184ffbba30a2f2229a6a759b00f6..cf354836f49913e8f205bcbd62ca9813be232786 100644 |
| --- a/chrome/browser/devtools/devtools_file_helper.cc |
| +++ b/chrome/browser/devtools/devtools_file_helper.cc |
| @@ -11,6 +11,7 @@ |
| #include "base/file_util.h" |
| #include "base/lazy_instance.h" |
| #include "base/md5.h" |
| +#include "base/utf_string_conversions.h" |
| #include "base/value_conversions.h" |
| #include "chrome/browser/browser_process.h" |
| #include "chrome/browser/download/download_prefs.h" |
| @@ -21,14 +22,24 @@ |
| #include "chrome/common/pref_names.h" |
| #include "content/public/browser/browser_context.h" |
| #include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/child_process_security_policy.h" |
| #include "content/public/browser/download_manager.h" |
| +#include "content/public/browser/render_process_host.h" |
| +#include "content/public/browser/render_view_host.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/common/content_client.h" |
| +#include "grit/generated_resources.h" |
| #include "ui/base/dialogs/select_file_dialog.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "webkit/fileapi/isolated_context.h" |
| using base::Bind; |
| using base::Callback; |
| using content::BrowserContext; |
| using content::BrowserThread; |
| using content::DownloadManager; |
| +using content::RenderViewHost; |
| +using content::WebContents; |
| namespace { |
| @@ -42,6 +53,9 @@ namespace { |
| typedef Callback<void(const FilePath&)> SelectedCallback; |
| typedef Callback<void(void)> CanceledCallback; |
| +const FilePath::CharType kMagicFileName[] = |
| + FILE_PATH_LITERAL(".allow-devtools-edit"); |
| + |
| class SelectFileDialog : public ui::SelectFileDialog::Listener, |
| public base::RefCounted<SelectFileDialog> { |
| public: |
| @@ -106,10 +120,71 @@ void AppendToFile(const FilePath& path, const std::string& content) { |
| file_util::AppendToFile(path, content.c_str(), content.length()); |
| } |
| +fileapi::IsolatedContext* isolated_context() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + fileapi::IsolatedContext* isolated_context = |
| + fileapi::IsolatedContext::GetInstance(); |
| + DCHECK(isolated_context); |
| + return isolated_context; |
| +} |
| + |
| +std::string RegisterFileSystem(WebContents* web_contents, |
| + const FilePath& path, |
| + std::string* registered_name) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + CHECK(content::GetContentClient()->HasWebUIScheme(web_contents->GetURL())); |
| + std::string file_system_id = isolated_context()->RegisterFileSystemForPath( |
| + fileapi::kFileSystemTypeNativeLocal, path, registered_name); |
| + |
| + content::ChildProcessSecurityPolicy* policy = |
| + content::ChildProcessSecurityPolicy::GetInstance(); |
| + RenderViewHost* render_view_host = web_contents->GetRenderViewHost(); |
| + int renderer_id = render_view_host->GetProcess()->GetID(); |
| + policy->GrantReadWriteFileSystem(renderer_id, file_system_id); |
| + |
| + // We only need file level access for reading FileEntries. Saving FileEntries |
| + // just needs the file system to have read/write access, which is granted |
| + // above if required. |
| + if (!policy->CanReadFile(renderer_id, path)) |
| + policy->GrantReadFile(renderer_id, path); |
| + |
| + return file_system_id; |
| +} |
| + |
| +typedef Callback<void(const std::vector<FilePath>&)> ValidateFoldersCallback; |
| + |
| +void ValidateFoldersOnFileThread(const std::vector<FilePath>& file_paths, |
| + const ValidateFoldersCallback& callback) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + std::vector<FilePath> permitted_paths; |
| + std::vector<FilePath>::const_iterator it; |
| + for (it = file_paths.begin(); it != file_paths.end(); ++it) { |
| + FilePath security_file_path = it->Append(kMagicFileName); |
| + if (file_util::PathExists(security_file_path)) |
| + permitted_paths.push_back(*it); |
|
kinuko
2013/01/09 15:17:50
I'm afraid I don't fully understand this 'kMagicFi
vsevik
2013/01/09 17:13:17
The directory will become writable only if the cor
kinuko
2013/01/10 07:33:58
Who creates the magic file? Sorry if I'm asking so
vsevik
2013/01/10 08:14:21
User should create a magic file in order to add a
|
| + } |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| + Bind(callback, permitted_paths)); |
| +} |
| + |
| } // namespace |
| -DevToolsFileHelper::DevToolsFileHelper(Profile* profile) : profile_(profile), |
| - weak_factory_(this) { |
| +DevToolsFileHelper::FileSystem::FileSystem() { |
| +} |
| + |
| +DevToolsFileHelper::FileSystem::FileSystem(const std::string& file_system_id, |
| + const std::string& registered_name, |
| + const std::string& file_system_path) |
| + : file_system_id(file_system_id), |
| + registered_name(registered_name), |
| + file_system_path(file_system_path) { |
| +} |
| + |
| +DevToolsFileHelper::DevToolsFileHelper(WebContents* web_contents, |
| + Profile* profile) |
| + : web_contents_(web_contents), |
| + profile_(profile), |
| + weak_factory_(this) { |
|
kinuko
2013/01/09 15:17:50
I think you need to use ALLOW_THIS_IN_INITIALIZER_
vsevik
2013/01/10 07:01:16
Done.
|
| } |
| DevToolsFileHelper::~DevToolsFileHelper() { |
| @@ -171,7 +246,7 @@ void DevToolsFileHelper::Append(const std::string& url, |
| return; |
| callback.Run(); |
| BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| - base::Bind(&AppendToFile, it->second, content)); |
| + Bind(&AppendToFile, it->second, content)); |
| } |
| void DevToolsFileHelper::SaveAsFileSelected(const std::string& url, |
| @@ -188,8 +263,110 @@ void DevToolsFileHelper::SaveAsFileSelected(const std::string& url, |
| base::CreateFilePathValue(path)); |
| callback.Run(); |
| BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| - base::Bind(&WriteToFile, path, content)); |
| + Bind(&WriteToFile, path, content)); |
| } |
| void DevToolsFileHelper::SaveAsFileSelectionCanceled() { |
| } |
| + |
| +void DevToolsFileHelper::AddFileSystem(const AddFileSystemCallback& callback) { |
| + scoped_refptr<SelectFileDialog> select_file_dialog = new SelectFileDialog( |
| + Bind(&DevToolsFileHelper::InnerAddFileSystem, |
| + weak_factory_.GetWeakPtr(), |
| + callback), |
| + Bind(callback, "", FileSystem())); |
| + select_file_dialog->Show(ui::SelectFileDialog::SELECT_FOLDER, FilePath()); |
| +} |
| + |
| +void DevToolsFileHelper::InnerAddFileSystem( |
| + const AddFileSystemCallback& callback, |
| + const FilePath& path) { |
| + std::vector<FilePath> file_paths(1, path); |
| + ValidateFoldersCallback validate_folders_callback = Bind( |
| + &DevToolsFileHelper::AddValidatedFileSystem, |
| + weak_factory_.GetWeakPtr(), |
| + callback); |
| + BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| + Bind(&ValidateFoldersOnFileThread, |
| + file_paths, |
| + validate_folders_callback)); |
| +} |
| + |
| +void DevToolsFileHelper::AddValidatedFileSystem( |
| + const AddFileSystemCallback& callback, |
| + const std::vector<FilePath>& permitted_paths) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + if (permitted_paths.empty()) { |
| + std::string magic_file_name = FilePath(kMagicFileName).AsUTF8Unsafe(); |
| + std::string error_string = l10n_util::GetStringFUTF8( |
| + IDS_DEV_TOOLS_MAGIC_FILE_NOT_EXISTS_MESSAGE, |
| + UTF8ToUTF16(magic_file_name)); |
| + callback.Run(error_string, FileSystem()); |
| + return; |
| + } |
| + FilePath path = permitted_paths.at(0); |
| + std::string registered_name; |
| + std::string file_system_id = RegisterFileSystem(web_contents_, |
| + path, |
| + ®istered_name); |
|
kinuko
2013/01/09 15:17:50
nit: indent is off by 1 char? (line 310-311)
vsevik
2013/01/10 07:01:16
Done.
|
| + std::string file_system_path = path.AsUTF8Unsafe(); |
| + |
| + DictionaryPrefUpdate update(profile_->GetPrefs(), |
| + prefs::kDevToolsFileSystemPaths); |
| + DictionaryValue* file_systems_paths_value = update.Get(); |
| + file_systems_paths_value->Set(file_system_path, Value::CreateNullValue()); |
| + |
| + FileSystem filesystem(file_system_id, registered_name, file_system_path); |
| + callback.Run("", filesystem); |
| +} |
| + |
| +void DevToolsFileHelper::RequestFileSystems( |
| + const RequestFileSystemsCallback& callback) { |
| + const DictionaryValue* file_systems_paths_value = |
| + profile_->GetPrefs()->GetDictionary(prefs::kDevToolsFileSystemPaths); |
| + std::vector<FilePath> saved_paths; |
| + DictionaryValue::key_iterator it = file_systems_paths_value->begin_keys(); |
| + for (; it != file_systems_paths_value->end_keys(); ++it) { |
| + std::string file_system_path = *it; |
| + FilePath path = FilePath::FromUTF8Unsafe(file_system_path); |
| + saved_paths.push_back(path); |
| + } |
| + |
| + ValidateFoldersCallback validate_folders_callback = Bind( |
| + &DevToolsFileHelper::RestoreValidatedFileSystems, |
| + weak_factory_.GetWeakPtr(), |
| + callback); |
| + BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| + Bind(&ValidateFoldersOnFileThread, |
| + saved_paths, |
| + validate_folders_callback)); |
| +} |
| + |
| +void DevToolsFileHelper::RestoreValidatedFileSystems( |
| + const RequestFileSystemsCallback& callback, |
| + const std::vector<FilePath>& permitted_paths) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + std::vector<FileSystem> file_systems; |
| + std::vector<FilePath>::const_iterator it; |
| + for (it = permitted_paths.begin(); it != permitted_paths.end(); ++it) { |
| + std::string registered_name; |
| + std::string file_system_id = RegisterFileSystem(web_contents_, |
| + *it, |
| + ®istered_name); |
|
kinuko
2013/01/09 15:17:50
nit: indent? (line 354-355)
vsevik
2013/01/10 07:01:16
Done.
|
| + std::string file_system_path = it->AsUTF8Unsafe(); |
| + FileSystem filesystem(file_system_id, registered_name, file_system_path); |
| + file_systems.push_back(filesystem); |
| + } |
| + callback.Run(file_systems); |
| +} |
| + |
| +void DevToolsFileHelper::RemoveFileSystem(const std::string& file_system_path) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + FilePath path = FilePath::FromUTF8Unsafe(file_system_path); |
| + isolated_context()->RevokeFileSystemByPath(path); |
| + |
| + DictionaryPrefUpdate update(profile_->GetPrefs(), |
| + prefs::kDevToolsFileSystemPaths); |
| + DictionaryValue* file_systems_paths_value = update.Get(); |
| + file_systems_paths_value->RemoveWithoutPathExpansion(file_system_path, NULL); |
| +} |