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/logging.h" |
| 11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "base/sys_string_conversions.h" | |
| 13 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/browser/extensions/shell_window_registry.h" | 15 #include "chrome/browser/extensions/shell_window_registry.h" |
| 15 #include "chrome/browser/platform_util.h" | 16 #include "chrome/browser/platform_util.h" |
| 16 #include "chrome/browser/ui/chrome_select_file_policy.h" | 17 #include "chrome/browser/ui/chrome_select_file_policy.h" |
| 17 #include "chrome/browser/ui/extensions/shell_window.h" | 18 #include "chrome/browser/ui/extensions/shell_window.h" |
| 18 #include "chrome/common/extensions/api/file_system.h" | 19 #include "chrome/common/extensions/api/file_system.h" |
| 19 #include "chrome/common/extensions/permissions/api_permission.h" | 20 #include "chrome/common/extensions/permissions/api_permission.h" |
| 20 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
| 21 #include "net/base/mime_util.h" | 22 #include "net/base/mime_util.h" |
| 22 #include "content/public/browser/child_process_security_policy.h" | 23 #include "content/public/browser/child_process_security_policy.h" |
| 23 #include "content/public/browser/render_view_host.h" | 24 #include "content/public/browser/render_view_host.h" |
| 24 #include "content/public/browser/render_process_host.h" | 25 #include "content/public/browser/render_process_host.h" |
| 25 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 26 #include "webkit/fileapi/file_system_types.h" | 27 #include "webkit/fileapi/file_system_types.h" |
| 27 #include "webkit/fileapi/file_system_util.h" | 28 #include "webkit/fileapi/file_system_util.h" |
| 28 #include "webkit/fileapi/isolated_context.h" | 29 #include "webkit/fileapi/isolated_context.h" |
| 29 #include "ui/base/l10n/l10n_util.h" | 30 #include "ui/base/l10n/l10n_util.h" |
| 30 #include "ui/base/dialogs/select_file_dialog.h" | 31 #include "ui/base/dialogs/select_file_dialog.h" |
| 31 | 32 |
| 33 #if defined(OS_MACOSX) | |
| 34 #include "base/mac/foundation_util.h" | |
| 35 #include <CoreFoundation/CoreFoundation.h> | |
| 36 #endif | |
| 37 | |
| 32 using fileapi::IsolatedContext; | 38 using fileapi::IsolatedContext; |
| 33 | 39 |
| 34 const char kInvalidParameters[] = "Invalid parameters"; | 40 const char kInvalidParameters[] = "Invalid parameters"; |
| 35 const char kSecurityError[] = "Security error"; | 41 const char kSecurityError[] = "Security error"; |
| 36 const char kInvalidCallingPage[] = "Invalid calling page"; | 42 const char kInvalidCallingPage[] = "Invalid calling page"; |
| 37 const char kUserCancelled[] = "User cancelled"; | 43 const char kUserCancelled[] = "User cancelled"; |
| 38 const char kWritableFileError[] = "Invalid file for writing"; | 44 const char kWritableFileError[] = "Invalid file for writing"; |
| 39 const char kRequiresFileSystemWriteError[] = | 45 const char kRequiresFileSystemWriteError[] = |
| 40 "Operation requires fileSystemWrite permission"; | 46 "Operation requires fileSystemWrite permission"; |
| 41 const char kUnknownChooseFileType[] = "Unknown type"; | 47 const char kUnknownChooseFileType[] = "Unknown type"; |
| 42 | 48 |
| 43 const char kOpenFileOption[] = "openFile"; | 49 const char kOpenFileOption[] = "openFile"; |
| 44 const char kOpenWritableFileOption[] ="openWritableFile"; | 50 const char kOpenWritableFileOption[] ="openWritableFile"; |
| 45 const char kSaveFileOption[] = "saveFile"; | 51 const char kSaveFileOption[] = "saveFile"; |
| 46 | 52 |
| 47 namespace file_system = extensions::api::file_system; | 53 namespace file_system = extensions::api::file_system; |
| 48 namespace ChooseFile = file_system::ChooseFile; | 54 namespace ChooseFile = file_system::ChooseFile; |
| 49 | 55 |
| 50 namespace { | 56 namespace { |
| 51 | 57 |
| 52 struct RewritePair { | 58 #if defined(OS_MACOSX) |
| 53 int path_key; | 59 // Retrieves the localized display name for the base name of the given path. |
| 54 const char* output; | 60 // If the path is not localized, this will just return the base name. |
| 55 }; | 61 std::string GetDisplayBaseName(const FilePath& path) { |
| 62 base::mac::ScopedCFTypeRef<CFURLRef> url( | |
| 63 CFURLCreateFromFileSystemRepresentation( | |
| 64 NULL, | |
| 65 (const UInt8*)path.value().c_str(), | |
| 66 path.value().length(), | |
| 67 true)); | |
| 68 if (!url) | |
| 69 return path.BaseName().value(); | |
| 56 | 70 |
| 57 const RewritePair g_rewrite_pairs[] = { | 71 CFStringRef str; |
| 72 if (LSCopyDisplayNameForURL(url, &str) != noErr) | |
| 73 return path.BaseName().value(); | |
| 74 | |
| 75 std::string result(base::SysCFStringRefToUTF8(str)); | |
| 76 CFRelease(str); | |
| 77 return result; | |
| 78 } | |
| 79 | |
| 80 // Prettifies |source_path| for OS X, by localizing every component of the | |
| 81 // path. Additionally, if the path is inside the user's home directory, then | |
| 82 // replace the home directory component with "~". | |
| 83 FilePath PrettifyPath(const FilePath& source_path) { | |
| 84 FilePath home_path; | |
| 85 PathService::Get(base::DIR_HOME, &home_path); | |
| 86 DCHECK(source_path.IsAbsolute()); | |
| 87 | |
| 88 // Break down the incoming path into components, and grab the display name | |
| 89 // for every component. This will match app bundles, ".localized" folders, | |
| 90 // and localized subfolders of the user's home directory. | |
| 91 // Don't grab the display name of the first component, i.e., "/", as it'll | |
| 92 // show up as the HDD name. | |
| 93 std::vector<FilePath::StringType> components; | |
| 94 source_path.GetComponents(&components); | |
| 95 FilePath display_path = FilePath(components[0]); | |
| 96 FilePath actual_path = display_path; | |
| 97 for (std::vector<FilePath::StringType>::iterator i = components.begin() + 1; | |
| 98 i != components.end(); ++i) { | |
| 99 actual_path = actual_path.Append(*i); | |
| 100 if (actual_path == home_path) { | |
| 101 display_path = FilePath("~"); | |
| 102 home_path = FilePath(); | |
| 103 continue; | |
| 104 } | |
| 105 std::string display = GetDisplayBaseName(actual_path); | |
| 106 display_path = display_path.Append(display); | |
| 107 } | |
| 108 DCHECK_EQ(actual_path.value(), source_path.value()); | |
| 109 return display_path; | |
| 110 } | |
| 111 #else // defined(OS_MACOSX) | |
| 112 // Prettifies |source_path|, by replacing the user's home directory with "~" | |
| 113 // (if applicable). | |
| 114 FilePath PrettifyPath(const FilePath& source_path) { | |
| 115 #if defined(OS_WIN) || defined(OS_POSIX) | |
| 58 #if defined(OS_WIN) | 116 #if defined(OS_WIN) |
| 59 {base::DIR_PROFILE, "~"}, | 117 int home_key = base::DIR_PROFILE; |
| 60 #elif defined(OS_POSIX) | 118 #elif defined(OS_POSIX) |
| 61 {base::DIR_HOME, "~"}, | 119 int home_key = base::DIR_HOME; |
| 62 #endif | 120 #endif |
| 63 }; | 121 FilePath home_path; |
| 64 | 122 FilePath display_path; |
| 65 FilePath PrettifyPath(const FilePath& file_path) { | 123 if (PathService::Get(home_key, &home_path) |
| 66 #if defined(OS_WIN) || defined(OS_POSIX) | 124 && home_path.AppendRelativePath(source_path, &display_path)) |
| 67 for (size_t i = 0; i < arraysize(g_rewrite_pairs); ++i) { | 125 return display_path; |
| 68 FilePath candidate_path; | |
| 69 if (!PathService::Get(g_rewrite_pairs[i].path_key, &candidate_path)) | |
| 70 continue; // We don't DCHECK this value, as Get will return false even | |
| 71 // if the path_key gives a blank string as a result. | |
| 72 | |
| 73 FilePath output = FilePath::FromUTF8Unsafe(g_rewrite_pairs[i].output); | |
| 74 if (candidate_path.AppendRelativePath(file_path, &output)) { | |
| 75 // The output path must not be absolute, as it might collide with the | |
| 76 // real filesystem. | |
| 77 DCHECK(!output.IsAbsolute()); | |
| 78 return output; | |
| 79 } | |
| 80 } | 126 } |
| 81 #endif | 127 #endif |
| 82 | 128 return source_path; |
| 83 return file_path; | |
| 84 } | 129 } |
| 130 #endif // defined(OS_MACOSX) | |
|
benwells
2012/09/12 02:32:24
Nit: two spaces before // (and above)
thorogood
2012/09/13 02:49:39
Done.
| |
| 85 | 131 |
| 86 bool g_skip_picker_for_test = false; | 132 bool g_skip_picker_for_test = false; |
| 87 FilePath* g_path_to_be_picked_for_test; | 133 FilePath* g_path_to_be_picked_for_test; |
| 88 | 134 |
| 89 bool GetFilePathOfFileEntry(const std::string& filesystem_name, | 135 bool GetFilePathOfFileEntry(const std::string& filesystem_name, |
| 90 const std::string& filesystem_path, | 136 const std::string& filesystem_path, |
| 91 const content::RenderViewHost* render_view_host, | 137 const content::RenderViewHost* render_view_host, |
| 92 FilePath* file_path, | 138 FilePath* file_path, |
| 93 std::string* error) { | 139 std::string* error) { |
| 94 std::string filesystem_id; | 140 std::string filesystem_id; |
| (...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 553 | 599 |
| 554 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) { | 600 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) { |
| 555 error_ = kRequiresFileSystemWriteError; | 601 error_ = kRequiresFileSystemWriteError; |
| 556 return false; | 602 return false; |
| 557 } | 603 } |
| 558 | 604 |
| 559 return ShowPicker(suggested_name, file_type_info, picker_type, entry_type); | 605 return ShowPicker(suggested_name, file_type_info, picker_type, entry_type); |
| 560 } | 606 } |
| 561 | 607 |
| 562 } // namespace extensions | 608 } // namespace extensions |
| OLD | NEW |