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/path_service.h" |
| 11 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/extensions/shell_window_registry.h" | 12 #include "chrome/browser/extensions/shell_window_registry.h" |
11 #include "chrome/browser/platform_util.h" | 13 #include "chrome/browser/platform_util.h" |
12 #include "chrome/browser/ui/chrome_select_file_policy.h" | 14 #include "chrome/browser/ui/chrome_select_file_policy.h" |
13 #include "chrome/browser/ui/extensions/shell_window.h" | 15 #include "chrome/browser/ui/extensions/shell_window.h" |
14 #include "chrome/common/extensions/api/file_system.h" | 16 #include "chrome/common/extensions/api/file_system.h" |
15 #include "chrome/common/extensions/permissions/api_permission.h" | 17 #include "chrome/common/extensions/permissions/api_permission.h" |
16 #include "content/public/browser/child_process_security_policy.h" | 18 #include "content/public/browser/child_process_security_policy.h" |
17 #include "content/public/browser/render_view_host.h" | 19 #include "content/public/browser/render_view_host.h" |
18 #include "content/public/browser/render_process_host.h" | 20 #include "content/public/browser/render_process_host.h" |
19 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
(...skipping 11 matching lines...) Expand all Loading... |
31 | 33 |
32 const char kOpenFileOption[] = "openFile"; | 34 const char kOpenFileOption[] = "openFile"; |
33 const char kOpenWritableFileOption[] ="openWritableFile"; | 35 const char kOpenWritableFileOption[] ="openWritableFile"; |
34 const char kSaveFileOption[] = "saveFile"; | 36 const char kSaveFileOption[] = "saveFile"; |
35 | 37 |
36 namespace file_system = extensions::api::file_system; | 38 namespace file_system = extensions::api::file_system; |
37 namespace ChooseFile = file_system::ChooseFile; | 39 namespace ChooseFile = file_system::ChooseFile; |
38 | 40 |
39 namespace { | 41 namespace { |
40 | 42 |
| 43 struct RewritePair { |
| 44 int path_key; |
| 45 const char* output; |
| 46 }; |
| 47 |
| 48 const RewritePair g_rewrite_pairs[] = { |
| 49 #if defined(OS_WIN) |
| 50 {base::DIR_PROFILE, "~"}, |
| 51 #endif |
| 52 }; |
| 53 |
| 54 FilePath PrettifyPath(const FilePath& file_path) { |
| 55 // Note that when g_rewrite_pairs includes at least one value on all |
| 56 // platforms, this code can be cleaned up. |
| 57 #if defined(OS_WIN) |
| 58 size_t size = arraysize(g_rewrite_pairs); |
| 59 #else |
| 60 size_t size = 0; |
| 61 #endif |
| 62 |
| 63 for (size_t i = 0; i < size; ++i) { |
| 64 FilePath candidate_path; |
| 65 if (!PathService::Get(g_rewrite_pairs[i].path_key, &candidate_path)) |
| 66 continue; // We don't DCHECK this value, as Get will return false even |
| 67 // if the path_key gives a blank string as a result. |
| 68 |
| 69 FilePath output = FilePath::FromUTF8Unsafe(g_rewrite_pairs[i].output); |
| 70 if (candidate_path.AppendRelativePath(file_path, &output)) { |
| 71 // The output path must not be absolute, as it might collide with the |
| 72 // real filesystem. |
| 73 DCHECK(!output.IsAbsolute()); |
| 74 return output; |
| 75 } |
| 76 } |
| 77 |
| 78 return file_path; |
| 79 } |
| 80 |
41 bool g_skip_picker_for_test = false; | 81 bool g_skip_picker_for_test = false; |
42 FilePath* g_path_to_be_picked_for_test; | 82 FilePath* g_path_to_be_picked_for_test; |
43 | 83 |
44 bool GetFilePathOfFileEntry(const std::string& filesystem_name, | 84 bool GetFilePathOfFileEntry(const std::string& filesystem_name, |
45 const std::string& filesystem_path, | 85 const std::string& filesystem_path, |
46 const content::RenderViewHost* render_view_host, | 86 const content::RenderViewHost* render_view_host, |
47 FilePath* file_path, | 87 FilePath* file_path, |
48 std::string* error) { | 88 std::string* error) { |
49 std::string filesystem_id; | 89 std::string filesystem_id; |
50 if (!fileapi::CrackIsolatedFileSystemName(filesystem_name, &filesystem_id)) { | 90 if (!fileapi::CrackIsolatedFileSystemName(filesystem_name, &filesystem_id)) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 std::string filesystem_name; | 140 std::string filesystem_name; |
101 std::string filesystem_path; | 141 std::string filesystem_path; |
102 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); | 142 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); |
103 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); | 143 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); |
104 | 144 |
105 FilePath file_path; | 145 FilePath file_path; |
106 if (!GetFilePathOfFileEntry(filesystem_name, filesystem_path, | 146 if (!GetFilePathOfFileEntry(filesystem_name, filesystem_path, |
107 render_view_host_, &file_path, &error_)) | 147 render_view_host_, &file_path, &error_)) |
108 return false; | 148 return false; |
109 | 149 |
| 150 file_path = PrettifyPath(file_path); |
110 result_.reset(base::Value::CreateStringValue(file_path.value())); | 151 result_.reset(base::Value::CreateStringValue(file_path.value())); |
111 return true; | 152 return true; |
112 } | 153 } |
113 | 154 |
114 bool FileSystemEntryFunction::HasFileSystemWritePermission() { | 155 bool FileSystemEntryFunction::HasFileSystemWritePermission() { |
115 const extensions::Extension* extension = GetExtension(); | 156 const extensions::Extension* extension = GetExtension(); |
116 if (!extension) | 157 if (!extension) |
117 return false; | 158 return false; |
118 | 159 |
119 return extension->HasAPIPermission(APIPermission::kFileSystemWrite); | 160 return extension->HasAPIPermission(APIPermission::kFileSystemWrite); |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 | 389 |
349 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) { | 390 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) { |
350 error_ = kRequiresFileSystemWriteError; | 391 error_ = kRequiresFileSystemWriteError; |
351 return false; | 392 return false; |
352 } | 393 } |
353 | 394 |
354 return ShowPicker(FilePath(), picker_type, entry_type); | 395 return ShowPicker(FilePath(), picker_type, entry_type); |
355 } | 396 } |
356 | 397 |
357 } // namespace extensions | 398 } // namespace extensions |
OLD | NEW |