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 RewritePair g_rewrite_pairs[] = { | |
49 #if defined(OS_WIN) | |
50 {base::DIR_PROFILE, "~"}, | |
51 #endif | |
52 {-1, NULL} | |
53 }; | |
54 | |
55 void PrettifyPath(FilePath& file_path) { | |
not at google - send to devlin
2012/07/09 02:07:38
Except in special circumstances, only pass const-r
thorogood
2012/07/09 05:12:45
I've made it return the new FilePath (by value). I
not at google - send to devlin
2012/07/09 05:44:58
The reason for these const-reference vs non-const-
thorogood
2012/07/09 05:58:35
Great, thanks for the write-up.
| |
56 for (int i = 0; g_rewrite_pairs[i].output; ++i) { | |
not at google - send to devlin
2012/07/09 02:07:38
Use arraysize(g_rewrite_pairs) rather than a senti
thorogood
2012/07/09 05:12:45
Hilariously, I can't use arraysize() on an empty a
| |
57 FilePath candidate_path; | |
58 if (!PathService::Get(g_rewrite_pairs[i].path_key, &candidate_path)) { | |
59 continue; | |
60 } | |
not at google - send to devlin
2012/07/09 02:07:38
nit: no {}
thorogood
2012/07/09 05:12:45
Done.
| |
61 | |
62 FilePath output = FilePath::FromUTF8Unsafe(g_rewrite_pairs[i].output); | |
63 if (candidate_path.AppendRelativePath(file_path, &output)) { | |
64 DCHECK(!output.IsAbsolute()); | |
not at google - send to devlin
2012/07/09 02:07:38
This check could be done at construction time of P
thorogood
2012/07/09 05:12:45
I've added a comment. Basically I'm trying to say
| |
65 file_path = output; | |
66 return; // Only prettify the first hit. | |
not at google - send to devlin
2012/07/09 02:07:38
nit: 2 spaces between ; and //
thorogood
2012/07/09 05:12:45
Done.
| |
67 } | |
68 } | |
69 } | |
not at google - send to devlin
2012/07/09 02:07:38
TBH I think this would be more nicely factored as
benwells
2012/07/09 03:42:45
FWIW I have no strong preference but prefer it lik
thorogood
2012/07/09 05:12:45
I've taken benwells@' comment and left this as-is,
| |
70 | |
41 bool g_skip_picker_for_test = false; | 71 bool g_skip_picker_for_test = false; |
42 FilePath* g_path_to_be_picked_for_test; | 72 FilePath* g_path_to_be_picked_for_test; |
43 | 73 |
44 bool GetFilePathOfFileEntry(const std::string& filesystem_name, | 74 bool GetFilePathOfFileEntry(const std::string& filesystem_name, |
45 const std::string& filesystem_path, | 75 const std::string& filesystem_path, |
46 const content::RenderViewHost* render_view_host, | 76 const content::RenderViewHost* render_view_host, |
47 FilePath* file_path, | 77 FilePath* file_path, |
48 std::string* error) { | 78 std::string* error) { |
49 std::string filesystem_id; | 79 std::string filesystem_id; |
50 if (!fileapi::CrackIsolatedFileSystemName(filesystem_name, &filesystem_id)) { | 80 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; | 130 std::string filesystem_name; |
101 std::string filesystem_path; | 131 std::string filesystem_path; |
102 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); | 132 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); |
103 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); | 133 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); |
104 | 134 |
105 FilePath file_path; | 135 FilePath file_path; |
106 if (!GetFilePathOfFileEntry(filesystem_name, filesystem_path, | 136 if (!GetFilePathOfFileEntry(filesystem_name, filesystem_path, |
107 render_view_host_, &file_path, &error_)) | 137 render_view_host_, &file_path, &error_)) |
108 return false; | 138 return false; |
109 | 139 |
140 PrettifyPath(file_path); | |
110 result_.reset(base::Value::CreateStringValue(file_path.value())); | 141 result_.reset(base::Value::CreateStringValue(file_path.value())); |
111 return true; | 142 return true; |
112 } | 143 } |
113 | 144 |
114 bool FileSystemEntryFunction::HasFileSystemWritePermission() { | 145 bool FileSystemEntryFunction::HasFileSystemWritePermission() { |
115 const extensions::Extension* extension = GetExtension(); | 146 const extensions::Extension* extension = GetExtension(); |
116 if (!extension) | 147 if (!extension) |
117 return false; | 148 return false; |
118 | 149 |
119 return extension->HasAPIPermission(APIPermission::kFileSystemWrite); | 150 return extension->HasAPIPermission(APIPermission::kFileSystemWrite); |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
348 | 379 |
349 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) { | 380 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) { |
350 error_ = kRequiresFileSystemWriteError; | 381 error_ = kRequiresFileSystemWriteError; |
351 return false; | 382 return false; |
352 } | 383 } |
353 | 384 |
354 return ShowPicker(FilePath(), picker_type, entry_type); | 385 return ShowPicker(FilePath(), picker_type, entry_type); |
355 } | 386 } |
356 | 387 |
357 } // namespace extensions | 388 } // namespace extensions |
OLD | NEW |