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/extensions/api/file_system/file_system_api.h" | 5 #include "chrome/browser/extensions/api/file_system/file_system_api.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | |
| 7 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | |
| 8 #include "chrome/browser/extensions/shell_window_registry.h" | 10 #include "chrome/browser/extensions/shell_window_registry.h" |
| 9 #include "chrome/browser/platform_util.h" | 11 #include "chrome/browser/platform_util.h" |
| 10 #include "chrome/browser/ui/extensions/shell_window.h" | 12 #include "chrome/browser/ui/extensions/shell_window.h" |
| 11 #include "chrome/browser/ui/select_file_dialog.h" | 13 #include "chrome/browser/ui/select_file_dialog.h" |
| 14 #include "chrome/common/extensions/api/file_system.h" | |
| 12 #include "content/public/browser/child_process_security_policy.h" | 15 #include "content/public/browser/child_process_security_policy.h" |
| 13 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
| 14 #include "content/public/browser/render_process_host.h" | 17 #include "content/public/browser/render_process_host.h" |
| 15 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| 16 #include "webkit/fileapi/file_system_util.h" | 19 #include "webkit/fileapi/file_system_util.h" |
| 17 #include "webkit/fileapi/isolated_context.h" | 20 #include "webkit/fileapi/isolated_context.h" |
| 18 | 21 |
| 19 const char kInvalidParameters[] = "Invalid parameters"; | 22 const char kInvalidParameters[] = "Invalid parameters"; |
| 20 const char kSecurityError[] = "Security error"; | 23 const char kSecurityError[] = "Security error"; |
| 21 const char kInvalidCallingPage[] = "Invalid calling page"; | 24 const char kInvalidCallingPage[] = "Invalid calling page"; |
| 22 const char kUserCancelled[] = "User cancelled"; | 25 const char kUserCancelled[] = "User cancelled"; |
| 26 const char kWritableFileError[] = "Invalid file for writing"; | |
| 27 | |
| 28 const char kSaveFileOption[] = "saveFile"; | |
| 29 | |
| 30 namespace file_system = extensions::api::file_system; | |
| 31 namespace ChooseFile = file_system::ChooseFile; | |
| 23 | 32 |
| 24 namespace { | 33 namespace { |
| 25 | 34 |
| 26 bool GetFilePathOfFileEntry(const std::string& filesystem_name, | 35 bool GetFilePathOfFileEntry(const std::string& filesystem_name, |
| 27 const std::string& filesystem_path, | 36 const std::string& filesystem_path, |
| 28 const content::RenderViewHost* render_view_host, | 37 const content::RenderViewHost* render_view_host, |
| 29 FilePath* file_path, | 38 FilePath* file_path, |
| 30 std::string* error) { | 39 std::string* error) { |
| 31 std::string filesystem_id; | 40 std::string filesystem_id; |
| 32 if (!fileapi::CrackIsolatedFileSystemName(filesystem_name, &filesystem_id)) { | 41 if (!fileapi::CrackIsolatedFileSystemName(filesystem_name, &filesystem_id)) { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 52 &filesystem_id, | 61 &filesystem_id, |
| 53 NULL, | 62 NULL, |
| 54 file_path)) { | 63 file_path)) { |
| 55 *error = kInvalidParameters; | 64 *error = kInvalidParameters; |
| 56 return false; | 65 return false; |
| 57 } | 66 } |
| 58 | 67 |
| 59 return true; | 68 return true; |
| 60 } | 69 } |
| 61 | 70 |
| 71 bool DoCheckWritableFile(const FilePath& path) { | |
| 72 // Don't allow links. | |
| 73 if (file_util::PathExists(path) && file_util::IsLink(path)) | |
| 74 return false; | |
| 75 | |
| 76 // Create the file if it doesn't already exist. | |
| 77 base::PlatformFileError error = base::PLATFORM_FILE_OK; | |
| 78 int creation_flags = base::PLATFORM_FILE_CREATE | | |
| 79 base::PLATFORM_FILE_READ | | |
| 80 base::PLATFORM_FILE_WRITE; | |
| 81 base::CreatePlatformFile(path, creation_flags, NULL, &error); | |
| 82 return error == base::PLATFORM_FILE_OK; | |
| 83 } | |
| 84 | |
| 62 } // namespace | 85 } // namespace |
| 63 | 86 |
| 64 namespace extensions { | 87 namespace extensions { |
| 65 | 88 |
| 66 bool FileSystemGetDisplayPathFunction::RunImpl() { | 89 bool FileSystemGetDisplayPathFunction::RunImpl() { |
| 67 std::string filesystem_name; | 90 std::string filesystem_name; |
| 68 std::string filesystem_path; | 91 std::string filesystem_path; |
| 69 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); | 92 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); |
| 70 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); | 93 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); |
| 71 | 94 |
| 72 FilePath file_path; | 95 FilePath file_path; |
| 73 if (!GetFilePathOfFileEntry(filesystem_name, filesystem_path, | 96 if (!GetFilePathOfFileEntry(filesystem_name, filesystem_path, |
| 74 render_view_host_, &file_path, &error_)) { | 97 render_view_host_, &file_path, &error_)) { |
| 75 return false; | 98 return false; |
| 76 } | 99 } |
| 77 | 100 |
| 78 result_.reset(base::Value::CreateStringValue(file_path.value())); | 101 result_.reset(base::Value::CreateStringValue(file_path.value())); |
| 79 return true; | 102 return true; |
| 80 } | 103 } |
| 81 | 104 |
| 82 // Handles showing a dialog to the user to ask for the filename for a file to | 105 // Handles showing a dialog to the user to ask for the filename for a file to |
| 83 // save. | 106 // save. |
| 84 class FileSystemGetWritableFileEntryFunction::FilePicker | 107 class FileSystemPickerFunction::FilePicker : public SelectFileDialog::Listener { |
| 85 : public SelectFileDialog::Listener { | |
| 86 public: | 108 public: |
| 87 FilePicker(FileSystemGetWritableFileEntryFunction* function, | 109 FilePicker(FileSystemPickerFunction* function, |
| 88 content::WebContents* web_contents, | 110 content::WebContents* web_contents, |
| 89 const FilePath& suggested_path) | 111 const FilePath& suggested_path, |
| 112 bool for_save) | |
| 90 : suggested_path_(suggested_path), | 113 : suggested_path_(suggested_path), |
| 114 for_save_(for_save), | |
| 91 function_(function) { | 115 function_(function) { |
| 92 select_file_dialog_ = SelectFileDialog::Create(this); | 116 select_file_dialog_ = SelectFileDialog::Create(this); |
| 93 SelectFileDialog::FileTypeInfo file_type_info; | 117 SelectFileDialog::FileTypeInfo file_type_info; |
| 94 FilePath::StringType extension = suggested_path.Extension(); | 118 FilePath::StringType extension = suggested_path.Extension(); |
| 95 if (!extension.empty()) { | 119 if (!extension.empty()) { |
| 96 extension.erase(extension.begin()); // drop the . | 120 extension.erase(extension.begin()); // drop the . |
| 97 file_type_info.extensions.resize(1); | 121 file_type_info.extensions.resize(1); |
| 98 file_type_info.extensions[0].push_back(extension); | 122 file_type_info.extensions[0].push_back(extension); |
| 99 } | 123 } |
| 100 file_type_info.include_all_files = true; | 124 file_type_info.include_all_files = true; |
| 101 gfx::NativeWindow owning_window = web_contents ? | 125 gfx::NativeWindow owning_window = web_contents ? |
| 102 platform_util::GetTopLevel(web_contents->GetNativeView()) : NULL; | 126 platform_util::GetTopLevel(web_contents->GetNativeView()) : NULL; |
| 103 | 127 |
| 104 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE, | 128 select_file_dialog_->SelectFile(for_save ? |
| 129 SelectFileDialog::SELECT_SAVEAS_FILE : | |
| 130 SelectFileDialog::SELECT_OPEN_FILE, | |
| 105 string16(), | 131 string16(), |
| 106 suggested_path, | 132 suggested_path, |
| 107 &file_type_info, 0, FILE_PATH_LITERAL(""), | 133 &file_type_info, 0, FILE_PATH_LITERAL(""), |
| 108 web_contents, owning_window, NULL); | 134 web_contents, owning_window, NULL); |
| 109 } | 135 } |
| 110 | 136 |
| 111 virtual ~FilePicker() {} | 137 virtual ~FilePicker() {} |
| 112 | 138 |
| 113 private: | 139 private: |
| 114 // SelectFileDialog::Listener implementation. | 140 // SelectFileDialog::Listener implementation. |
| 115 virtual void FileSelected(const FilePath& path, | 141 virtual void FileSelected(const FilePath& path, |
| 116 int index, | 142 int index, |
| 117 void* params) OVERRIDE { | 143 void* params) OVERRIDE { |
| 118 function_->FileSelected(path); | 144 function_->FileSelected(path, for_save_); |
| 119 delete this; | 145 delete this; |
| 120 } | 146 } |
| 121 | 147 |
| 122 virtual void FileSelectionCanceled(void* params) OVERRIDE { | 148 virtual void FileSelectionCanceled(void* params) OVERRIDE { |
| 123 function_->FileSelectionCanceled(); | 149 function_->FileSelectionCanceled(); |
| 124 delete this; | 150 delete this; |
| 125 } | 151 } |
| 126 | 152 |
| 127 FilePath suggested_path_; | 153 FilePath suggested_path_; |
| 128 | 154 |
| 129 // For managing select file dialogs. | 155 // for_save_ is false when using the picker to open a file, and true |
| 156 // when saving. It affects the style of the dialog and also what happens | |
| 157 // after a file is selected by the user. | |
| 158 bool for_save_; | |
| 159 | |
| 130 scoped_refptr<SelectFileDialog> select_file_dialog_; | 160 scoped_refptr<SelectFileDialog> select_file_dialog_; |
| 131 scoped_refptr<FileSystemGetWritableFileEntryFunction> function_; | 161 scoped_refptr<FileSystemPickerFunction> function_; |
| 132 | 162 |
| 133 DISALLOW_COPY_AND_ASSIGN(FilePicker); | 163 DISALLOW_COPY_AND_ASSIGN(FilePicker); |
| 134 }; | 164 }; |
| 135 | 165 |
| 136 bool FileSystemGetWritableFileEntryFunction::RunImpl() { | 166 bool FileSystemPickerFunction::ShowPicker(const FilePath& suggested_path, |
| 137 std::string filesystem_name; | 167 bool for_save) { |
| 138 std::string filesystem_path; | |
| 139 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); | |
| 140 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); | |
| 141 | |
| 142 FilePath suggested_path; | |
| 143 if (!GetFilePathOfFileEntry(filesystem_name, filesystem_path, | |
| 144 render_view_host_, &suggested_path, &error_)) { | |
| 145 return false; | |
| 146 } | |
| 147 | |
| 148 ShellWindowRegistry* registry = ShellWindowRegistry::Get(profile()); | 168 ShellWindowRegistry* registry = ShellWindowRegistry::Get(profile()); |
| 149 DCHECK(registry); | 169 DCHECK(registry); |
| 150 ShellWindow* shell_window = registry->GetShellWindowForRenderViewHost( | 170 ShellWindow* shell_window = registry->GetShellWindowForRenderViewHost( |
| 151 render_view_host()); | 171 render_view_host()); |
| 152 if (!shell_window) { | 172 if (!shell_window) { |
| 153 error_ = kInvalidCallingPage; | 173 error_ = kInvalidCallingPage; |
| 154 return false; | 174 return false; |
| 155 } | 175 } |
| 156 | 176 |
| 157 // The file picker will hold a reference to this function instance, preventing | 177 // The file picker will hold a reference to this function instance, preventing |
| 158 // its destruction (and subsequent sending of the function response) until the | 178 // its destruction (and subsequent sending of the function response) until the |
| 159 // user has selected a file or cancelled the picker. At that point, the picker | 179 // user has selected a file or cancelled the picker. At that point, the picker |
| 160 // will delete itself, which will also free the function instance. | 180 // will delete itself, which will also free the function instance. |
| 161 new FilePicker(this, shell_window->web_contents(), suggested_path); | 181 new FilePicker(this, shell_window->web_contents(), suggested_path, for_save); |
| 162 return true; | 182 return true; |
| 163 } | 183 } |
| 164 | 184 |
| 165 void FileSystemGetWritableFileEntryFunction::FileSelected( | 185 void FileSystemPickerFunction::FileSelected(const FilePath& path, |
| 166 const FilePath& path) { | 186 bool for_save) { |
| 187 if (for_save) { | |
| 188 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE, | |
| 189 base::Bind(&FileSystemPickerFunction::CheckWritableFile, this, path)); | |
| 190 return; | |
| 191 } | |
| 192 | |
| 193 // Don't need to check the file, it's for reading. | |
| 194 RegisterFileSystemAndSendResponse(path, false); | |
| 195 } | |
| 196 | |
| 197 void FileSystemPickerFunction::FileSelectionCanceled() { | |
| 198 error_ = kUserCancelled; | |
| 199 SendResponse(false); | |
| 200 } | |
| 201 | |
| 202 void FileSystemPickerFunction::CheckWritableFile(const FilePath& path) { | |
| 203 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | |
| 204 if (DoCheckWritableFile(path)) { | |
| 205 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
| 206 base::Bind(&FileSystemPickerFunction::RegisterFileSystemAndSendResponse, | |
| 207 this, path, true)); | |
| 208 return; | |
| 209 } | |
| 210 | |
| 211 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
| 212 base::Bind(&FileSystemPickerFunction::HandleWritableFileError, this)); | |
| 213 } | |
| 214 | |
| 215 void FileSystemPickerFunction::RegisterFileSystemAndSendResponse( | |
| 216 const FilePath& path, bool for_save) { | |
| 217 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 167 std::set<FilePath> filesets; | 218 std::set<FilePath> filesets; |
| 168 filesets.insert(path); | 219 filesets.insert(path); |
| 169 | 220 |
| 170 fileapi::IsolatedContext* isolated_context = | 221 fileapi::IsolatedContext* isolated_context = |
| 171 fileapi::IsolatedContext::GetInstance(); | 222 fileapi::IsolatedContext::GetInstance(); |
| 172 DCHECK(isolated_context); | 223 DCHECK(isolated_context); |
| 173 std::string filesystem_id = isolated_context->RegisterIsolatedFileSystem( | 224 std::string filesystem_id = isolated_context->RegisterIsolatedFileSystem( |
| 174 filesets); | 225 filesets); |
| 175 | 226 |
| 176 content::ChildProcessSecurityPolicy* policy = | 227 content::ChildProcessSecurityPolicy* policy = |
| 177 content::ChildProcessSecurityPolicy::GetInstance(); | 228 content::ChildProcessSecurityPolicy::GetInstance(); |
| 178 int renderer_id = render_view_host_->GetProcess()->GetID(); | 229 int renderer_id = render_view_host_->GetProcess()->GetID(); |
| 179 policy->GrantReadWriteFileSystem(renderer_id, filesystem_id); | 230 if (for_save) |
| 231 policy->GrantReadWriteFileSystem(renderer_id, filesystem_id); | |
| 232 else | |
| 233 policy->GrantReadFileSystem(renderer_id, filesystem_id); | |
| 234 | |
| 235 // We only need file level access for reading FileEntries. Saving FileEntries | |
| 236 // just needs the file system to have read/write access, which is granted | |
| 237 // above if required. | |
| 238 if (!policy->CanReadFile(renderer_id, path)) | |
| 239 policy->GrantReadFile(renderer_id, path); | |
| 180 | 240 |
| 181 DictionaryValue* dict = new DictionaryValue(); | 241 DictionaryValue* dict = new DictionaryValue(); |
| 182 result_.reset(dict); | 242 result_.reset(dict); |
| 183 dict->SetString("fileSystemId", filesystem_id); | 243 dict->SetString("fileSystemId", filesystem_id); |
| 184 dict->SetString("baseName", path.BaseName().AsUTF8Unsafe()); | 244 dict->SetString("baseName", path.BaseName().AsUTF8Unsafe()); |
| 185 SendResponse(true); | 245 SendResponse(true); |
| 186 } | 246 } |
| 187 | 247 |
| 188 void FileSystemGetWritableFileEntryFunction::FileSelectionCanceled() { | 248 void FileSystemPickerFunction::HandleWritableFileError() { |
| 189 error_ = kUserCancelled; | 249 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 250 error_ = kWritableFileError; | |
| 190 SendResponse(false); | 251 SendResponse(false); |
| 191 } | 252 } |
| 192 | 253 |
| 254 bool FileSystemGetWritableFileEntryFunction::RunImpl() { | |
| 255 std::string filesystem_name; | |
| 256 std::string filesystem_path; | |
| 257 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); | |
| 258 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); | |
| 259 | |
| 260 FilePath suggested_path; | |
| 261 if (!GetFilePathOfFileEntry(filesystem_name, filesystem_path, | |
| 262 render_view_host_, &suggested_path, &error_)) { | |
| 263 return false; | |
| 264 } | |
| 265 | |
| 266 return ShowPicker(suggested_path, true); | |
| 267 } | |
| 268 | |
| 269 bool FileSystemChooseFileFunction::RunImpl() { | |
| 270 scoped_ptr<ChooseFile::Params> params(ChooseFile::Params::Create(*args_)); | |
| 271 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 272 | |
| 273 bool for_save = false; | |
| 274 file_system::ChooseFileOptions* options = params->options.get(); | |
| 275 if (options) { | |
| 276 if (options->type.get() && *options->type == kSaveFileOption) | |
|
Mihai Parparita -not on Chrome
2012/06/15 04:19:15
Nit: extra space before the *
| |
| 277 for_save = true; | |
| 278 } | |
| 279 | |
| 280 return ShowPicker(FilePath(), for_save); | |
| 281 } | |
| 282 | |
| 193 } // namespace extensions | 283 } // namespace extensions |
| OLD | NEW |