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