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/bind.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/logging.h" | |
| 10 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 11 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/extensions/shell_window_registry.h" | 13 #include "chrome/browser/extensions/shell_window_registry.h" |
| 13 #include "chrome/browser/platform_util.h" | 14 #include "chrome/browser/platform_util.h" |
| 14 #include "chrome/browser/ui/chrome_select_file_policy.h" | 15 #include "chrome/browser/ui/chrome_select_file_policy.h" |
| 15 #include "chrome/browser/ui/extensions/shell_window.h" | 16 #include "chrome/browser/ui/extensions/shell_window.h" |
| 16 #include "chrome/common/extensions/api/file_system.h" | 17 #include "chrome/common/extensions/api/file_system.h" |
| 17 #include "chrome/common/extensions/permissions/api_permission.h" | 18 #include "chrome/common/extensions/permissions/api_permission.h" |
| 18 #include "content/public/browser/child_process_security_policy.h" | 19 #include "content/public/browser/child_process_security_policy.h" |
| 19 #include "content/public/browser/render_view_host.h" | 20 #include "content/public/browser/render_view_host.h" |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 // Create the file if it doesn't already exist. | 126 // Create the file if it doesn't already exist. |
| 126 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 127 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
| 127 int creation_flags = base::PLATFORM_FILE_CREATE | | 128 int creation_flags = base::PLATFORM_FILE_CREATE | |
| 128 base::PLATFORM_FILE_READ | | 129 base::PLATFORM_FILE_READ | |
| 129 base::PLATFORM_FILE_WRITE; | 130 base::PLATFORM_FILE_WRITE; |
| 130 base::CreatePlatformFile(path, creation_flags, NULL, &error); | 131 base::CreatePlatformFile(path, creation_flags, NULL, &error); |
| 131 return error == base::PLATFORM_FILE_OK || | 132 return error == base::PLATFORM_FILE_OK || |
| 132 error == base::PLATFORM_FILE_ERROR_EXISTS; | 133 error == base::PLATFORM_FILE_ERROR_EXISTS; |
| 133 } | 134 } |
| 134 | 135 |
| 136 bool HasFileSystemWritePermission(const extensions::Extension* extension) { | |
| 137 if (!extension) | |
| 138 return false; | |
|
benwells
2012/07/10 06:40:23
Nit: add blank line after early return.
thorogood
2012/07/10 06:49:22
Done.
| |
| 139 return extension->HasAPIPermission( | |
| 140 extensions::APIPermission::kFileSystemWrite); | |
| 141 } | |
| 142 | |
| 135 } // namespace | 143 } // namespace |
| 136 | 144 |
| 137 namespace extensions { | 145 namespace extensions { |
| 138 | 146 |
| 139 bool FileSystemGetDisplayPathFunction::RunImpl() { | 147 bool FileSystemGetDisplayPathFunction::RunImpl() { |
| 140 std::string filesystem_name; | 148 std::string filesystem_name; |
| 141 std::string filesystem_path; | 149 std::string filesystem_path; |
| 142 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); | 150 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); |
| 143 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); | 151 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); |
| 144 | 152 |
| 145 FilePath file_path; | 153 FilePath file_path; |
| 146 if (!GetFilePathOfFileEntry(filesystem_name, filesystem_path, | 154 if (!GetFilePathOfFileEntry(filesystem_name, filesystem_path, |
| 147 render_view_host_, &file_path, &error_)) | 155 render_view_host_, &file_path, &error_)) |
| 148 return false; | 156 return false; |
| 149 | 157 |
| 150 file_path = PrettifyPath(file_path); | 158 file_path = PrettifyPath(file_path); |
| 151 result_.reset(base::Value::CreateStringValue(file_path.value())); | 159 result_.reset(base::Value::CreateStringValue(file_path.value())); |
| 152 return true; | 160 return true; |
| 153 } | 161 } |
| 154 | 162 |
| 155 bool FileSystemEntryFunction::HasFileSystemWritePermission() { | |
| 156 const extensions::Extension* extension = GetExtension(); | |
| 157 if (!extension) | |
| 158 return false; | |
| 159 | |
| 160 return extension->HasAPIPermission(APIPermission::kFileSystemWrite); | |
| 161 } | |
| 162 | |
| 163 void FileSystemEntryFunction::CheckWritableFile(const FilePath& path) { | 163 void FileSystemEntryFunction::CheckWritableFile(const FilePath& path) { |
| 164 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 164 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 165 if (DoCheckWritableFile(path)) { | 165 if (DoCheckWritableFile(path)) { |
| 166 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 166 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 167 base::Bind(&FileSystemEntryFunction::RegisterFileSystemAndSendResponse, | 167 base::Bind(&FileSystemEntryFunction::RegisterFileSystemAndSendResponse, |
| 168 this, path, WRITABLE)); | 168 this, path, WRITABLE)); |
| 169 return; | 169 return; |
| 170 } | 170 } |
| 171 | 171 |
| 172 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 172 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 211 error_ = kWritableFileError; | 211 error_ = kWritableFileError; |
| 212 SendResponse(false); | 212 SendResponse(false); |
| 213 } | 213 } |
| 214 | 214 |
| 215 bool FileSystemGetWritableFileEntryFunction::RunImpl() { | 215 bool FileSystemGetWritableFileEntryFunction::RunImpl() { |
| 216 std::string filesystem_name; | 216 std::string filesystem_name; |
| 217 std::string filesystem_path; | 217 std::string filesystem_path; |
| 218 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); | 218 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); |
| 219 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); | 219 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); |
| 220 | 220 |
| 221 if (!HasFileSystemWritePermission()) { | 221 if (!HasFileSystemWritePermission(GetExtension())) { |
| 222 error_ = kRequiresFileSystemWriteError; | 222 error_ = kRequiresFileSystemWriteError; |
| 223 return false; | 223 return false; |
| 224 } | 224 } |
| 225 | 225 |
| 226 FilePath path; | 226 FilePath path; |
| 227 if (!GetFilePathOfFileEntry(filesystem_name, filesystem_path, | 227 if (!GetFilePathOfFileEntry(filesystem_name, filesystem_path, |
| 228 render_view_host_, &path, &error_)) | 228 render_view_host_, &path, &error_)) |
| 229 return false; | 229 return false; |
| 230 | 230 |
| 231 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE, | 231 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE, |
| 232 base::Bind(&FileSystemGetWritableFileEntryFunction::CheckWritableFile, | 232 base::Bind(&FileSystemGetWritableFileEntryFunction::CheckWritableFile, |
| 233 this, path)); | 233 this, path)); |
| 234 return true; | 234 return true; |
| 235 } | 235 } |
| 236 | 236 |
| 237 bool FileSystemIsWritableFileEntryFunction::RunImpl() { | |
| 238 std::string filesystem_name; | |
| 239 std::string filesystem_path; | |
| 240 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); | |
| 241 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); | |
| 242 | |
| 243 if (!HasFileSystemWritePermission(GetExtension())) { | |
|
benwells
2012/07/10 06:40:23
I'm not sure about this. Calls to IsWritableFileEn
thorogood
2012/07/10 06:49:22
True. We would never grant the policy if they do n
thorogood
2012/07/10 08:06:16
I've added a call to isWritableFileEntry to checkE
| |
| 244 error_ = kRequiresFileSystemWriteError; | |
| 245 return false; | |
| 246 } | |
| 247 | |
| 248 std::string filesystem_id; | |
| 249 if (!fileapi::CrackIsolatedFileSystemName(filesystem_name, &filesystem_id)) { | |
| 250 error_ = kInvalidParameters; | |
| 251 return false; | |
| 252 } | |
| 253 | |
| 254 content::ChildProcessSecurityPolicy* policy = | |
| 255 content::ChildProcessSecurityPolicy::GetInstance(); | |
| 256 int renderer_id = render_view_host_->GetProcess()->GetID(); | |
| 257 bool is_writable = policy->CanReadWriteFileSystem(renderer_id, | |
| 258 filesystem_id); | |
| 259 | |
| 260 result_.reset(base::Value::CreateBooleanValue(is_writable)); | |
| 261 return true; | |
| 262 } | |
| 263 | |
| 237 // Handles showing a dialog to the user to ask for the filename for a file to | 264 // Handles showing a dialog to the user to ask for the filename for a file to |
| 238 // save or open. | 265 // save or open. |
| 239 class FileSystemChooseFileFunction::FilePicker | 266 class FileSystemChooseFileFunction::FilePicker |
| 240 : public SelectFileDialog::Listener { | 267 : public SelectFileDialog::Listener { |
| 241 public: | 268 public: |
| 242 FilePicker(FileSystemChooseFileFunction* function, | 269 FilePicker(FileSystemChooseFileFunction* function, |
| 243 content::WebContents* web_contents, | 270 content::WebContents* web_contents, |
| 244 const FilePath& suggested_path, | 271 const FilePath& suggested_path, |
| 245 SelectFileDialog::Type picker_type, | 272 SelectFileDialog::Type picker_type, |
| 246 EntryType entry_type) | 273 EntryType entry_type) |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 380 entry_type = WRITABLE; | 407 entry_type = WRITABLE; |
| 381 } else if (*options->type == kSaveFileOption) { | 408 } else if (*options->type == kSaveFileOption) { |
| 382 entry_type = WRITABLE; | 409 entry_type = WRITABLE; |
| 383 picker_type = SelectFileDialog::SELECT_SAVEAS_FILE; | 410 picker_type = SelectFileDialog::SELECT_SAVEAS_FILE; |
| 384 } else if (*options->type != kOpenFileOption) { | 411 } else if (*options->type != kOpenFileOption) { |
| 385 error_ = kUnknownChooseFileType; | 412 error_ = kUnknownChooseFileType; |
| 386 return false; | 413 return false; |
| 387 } | 414 } |
| 388 } | 415 } |
| 389 | 416 |
| 390 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) { | 417 if (entry_type == WRITABLE && |
| 418 !HasFileSystemWritePermission(GetExtension())) { | |
| 391 error_ = kRequiresFileSystemWriteError; | 419 error_ = kRequiresFileSystemWriteError; |
| 392 return false; | 420 return false; |
| 393 } | 421 } |
| 394 | 422 |
| 395 return ShowPicker(FilePath(), picker_type, entry_type); | 423 return ShowPicker(FilePath(), picker_type, entry_type); |
| 396 } | 424 } |
| 397 | 425 |
| 398 } // namespace extensions | 426 } // namespace extensions |
| OLD | NEW |