| 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 "chrome/browser/extensions/shell_window_registry.h" | 10 #include "chrome/browser/extensions/shell_window_registry.h" |
| 11 #include "chrome/browser/platform_util.h" | 11 #include "chrome/browser/platform_util.h" |
| 12 #include "chrome/browser/ui/extensions/shell_window.h" | 12 #include "chrome/browser/ui/extensions/shell_window.h" |
| 13 #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" | 14 #include "chrome/common/extensions/api/file_system.h" |
| 15 #include "content/public/browser/child_process_security_policy.h" | 15 #include "content/public/browser/child_process_security_policy.h" |
| 16 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
| 17 #include "content/public/browser/render_process_host.h" | 17 #include "content/public/browser/render_process_host.h" |
| 18 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| 19 #include "webkit/fileapi/file_system_util.h" | 19 #include "webkit/fileapi/file_system_util.h" |
| 20 #include "webkit/fileapi/isolated_context.h" | 20 #include "webkit/fileapi/isolated_context.h" |
| 21 | 21 |
| 22 using fileapi::IsolatedContext; |
| 23 |
| 22 const char kInvalidParameters[] = "Invalid parameters"; | 24 const char kInvalidParameters[] = "Invalid parameters"; |
| 23 const char kSecurityError[] = "Security error"; | 25 const char kSecurityError[] = "Security error"; |
| 24 const char kInvalidCallingPage[] = "Invalid calling page"; | 26 const char kInvalidCallingPage[] = "Invalid calling page"; |
| 25 const char kUserCancelled[] = "User cancelled"; | 27 const char kUserCancelled[] = "User cancelled"; |
| 26 const char kWritableFileError[] = "Invalid file for writing"; | 28 const char kWritableFileError[] = "Invalid file for writing"; |
| 27 | 29 |
| 28 const char kSaveFileOption[] = "saveFile"; | 30 const char kSaveFileOption[] = "saveFile"; |
| 29 | 31 |
| 30 namespace file_system = extensions::api::file_system; | 32 namespace file_system = extensions::api::file_system; |
| 31 namespace ChooseFile = file_system::ChooseFile; | 33 namespace ChooseFile = file_system::ChooseFile; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 49 // Only return the display path if the process has read access to the | 51 // Only return the display path if the process has read access to the |
| 50 // filesystem. | 52 // filesystem. |
| 51 content::ChildProcessSecurityPolicy* policy = | 53 content::ChildProcessSecurityPolicy* policy = |
| 52 content::ChildProcessSecurityPolicy::GetInstance(); | 54 content::ChildProcessSecurityPolicy::GetInstance(); |
| 53 if (!policy->CanReadFileSystem(render_view_host->GetProcess()->GetID(), | 55 if (!policy->CanReadFileSystem(render_view_host->GetProcess()->GetID(), |
| 54 filesystem_id)) { | 56 filesystem_id)) { |
| 55 *error = kSecurityError; | 57 *error = kSecurityError; |
| 56 return false; | 58 return false; |
| 57 } | 59 } |
| 58 | 60 |
| 59 fileapi::IsolatedContext* context = fileapi::IsolatedContext::GetInstance(); | 61 IsolatedContext* context = IsolatedContext::GetInstance(); |
| 60 FilePath relative_path = FilePath::FromUTF8Unsafe(filesystem_path); | 62 FilePath relative_path = FilePath::FromUTF8Unsafe(filesystem_path); |
| 61 FilePath virtual_path = context->CreateVirtualPath(filesystem_id, | 63 FilePath virtual_path = context->CreateVirtualRootPath(filesystem_id) |
| 62 relative_path); | 64 .Append(relative_path); |
| 63 if (!context->CrackIsolatedPath(virtual_path, | 65 if (!context->CrackIsolatedPath(virtual_path, |
| 64 &filesystem_id, | 66 &filesystem_id, |
| 65 NULL, | 67 NULL, |
| 66 file_path)) { | 68 file_path)) { |
| 67 *error = kInvalidParameters; | 69 *error = kInvalidParameters; |
| 68 return false; | 70 return false; |
| 69 } | 71 } |
| 70 | 72 |
| 71 return true; | 73 return true; |
| 72 } | 74 } |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 return; | 247 return; |
| 246 } | 248 } |
| 247 | 249 |
| 248 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 250 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 249 base::Bind(&FileSystemPickerFunction::HandleWritableFileError, this)); | 251 base::Bind(&FileSystemPickerFunction::HandleWritableFileError, this)); |
| 250 } | 252 } |
| 251 | 253 |
| 252 void FileSystemPickerFunction::RegisterFileSystemAndSendResponse( | 254 void FileSystemPickerFunction::RegisterFileSystemAndSendResponse( |
| 253 const FilePath& path, bool for_save) { | 255 const FilePath& path, bool for_save) { |
| 254 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 256 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 255 std::set<FilePath> filesets; | |
| 256 filesets.insert(path); | |
| 257 | 257 |
| 258 fileapi::IsolatedContext* isolated_context = | 258 IsolatedContext* isolated_context = IsolatedContext::GetInstance(); |
| 259 fileapi::IsolatedContext::GetInstance(); | 259 std::string file_name = IsolatedContext::GetNameForPath(path); |
| 260 |
| 260 DCHECK(isolated_context); | 261 DCHECK(isolated_context); |
| 261 std::string filesystem_id = isolated_context->RegisterIsolatedFileSystem( | 262 std::string filesystem_id = isolated_context->RegisterFileSystemForFile( |
| 262 filesets); | 263 file_name, path); |
| 263 | 264 |
| 264 content::ChildProcessSecurityPolicy* policy = | 265 content::ChildProcessSecurityPolicy* policy = |
| 265 content::ChildProcessSecurityPolicy::GetInstance(); | 266 content::ChildProcessSecurityPolicy::GetInstance(); |
| 266 int renderer_id = render_view_host_->GetProcess()->GetID(); | 267 int renderer_id = render_view_host_->GetProcess()->GetID(); |
| 267 if (for_save) | 268 if (for_save) |
| 268 policy->GrantReadWriteFileSystem(renderer_id, filesystem_id); | 269 policy->GrantReadWriteFileSystem(renderer_id, filesystem_id); |
| 269 else | 270 else |
| 270 policy->GrantReadFileSystem(renderer_id, filesystem_id); | 271 policy->GrantReadFileSystem(renderer_id, filesystem_id); |
| 271 | 272 |
| 272 // We only need file level access for reading FileEntries. Saving FileEntries | 273 // We only need file level access for reading FileEntries. Saving FileEntries |
| 273 // just needs the file system to have read/write access, which is granted | 274 // just needs the file system to have read/write access, which is granted |
| 274 // above if required. | 275 // above if required. |
| 275 if (!policy->CanReadFile(renderer_id, path)) | 276 if (!policy->CanReadFile(renderer_id, path)) |
| 276 policy->GrantReadFile(renderer_id, path); | 277 policy->GrantReadFile(renderer_id, path); |
| 277 | 278 |
| 278 DictionaryValue* dict = new DictionaryValue(); | 279 DictionaryValue* dict = new DictionaryValue(); |
| 279 result_.reset(dict); | 280 result_.reset(dict); |
| 280 dict->SetString("fileSystemId", filesystem_id); | 281 dict->SetString("fileSystemId", filesystem_id); |
| 281 dict->SetString("baseName", path.BaseName().AsUTF8Unsafe()); | 282 dict->SetString("baseName", file_name); |
| 282 SendResponse(true); | 283 SendResponse(true); |
| 283 } | 284 } |
| 284 | 285 |
| 285 void FileSystemPickerFunction::HandleWritableFileError() { | 286 void FileSystemPickerFunction::HandleWritableFileError() { |
| 286 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 287 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 287 error_ = kWritableFileError; | 288 error_ = kWritableFileError; |
| 288 SendResponse(false); | 289 SendResponse(false); |
| 289 } | 290 } |
| 290 | 291 |
| 291 bool FileSystemGetWritableFileEntryFunction::RunImpl() { | 292 bool FileSystemGetWritableFileEntryFunction::RunImpl() { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 311 file_system::ChooseFileOptions* options = params->options.get(); | 312 file_system::ChooseFileOptions* options = params->options.get(); |
| 312 if (options) { | 313 if (options) { |
| 313 if (options->type.get() && *options->type == kSaveFileOption) | 314 if (options->type.get() && *options->type == kSaveFileOption) |
| 314 for_save = true; | 315 for_save = true; |
| 315 } | 316 } |
| 316 | 317 |
| 317 return ShowPicker(FilePath(), for_save); | 318 return ShowPicker(FilePath(), for_save); |
| 318 } | 319 } |
| 319 | 320 |
| 320 } // namespace extensions | 321 } // namespace extensions |
| OLD | NEW |