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" |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 #include "content/public/browser/child_process_security_policy.h" | 22 #include "content/public/browser/child_process_security_policy.h" |
| 23 #include "content/public/browser/render_view_host.h" | 23 #include "content/public/browser/render_view_host.h" |
| 24 #include "content/public/browser/render_process_host.h" | 24 #include "content/public/browser/render_process_host.h" |
| 25 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
| 26 #include "webkit/fileapi/file_system_types.h" | 26 #include "webkit/fileapi/file_system_types.h" |
| 27 #include "webkit/fileapi/file_system_util.h" | 27 #include "webkit/fileapi/file_system_util.h" |
| 28 #include "webkit/fileapi/isolated_context.h" | 28 #include "webkit/fileapi/isolated_context.h" |
| 29 #include "ui/base/l10n/l10n_util.h" | 29 #include "ui/base/l10n/l10n_util.h" |
| 30 #include "ui/base/dialogs/select_file_dialog.h" | 30 #include "ui/base/dialogs/select_file_dialog.h" |
| 31 | 31 |
| 32 #if defined(OS_MACOSX) | |
| 33 #include "base/mac/foundation_util.h" | |
| 34 #endif | |
| 35 | |
| 32 using fileapi::IsolatedContext; | 36 using fileapi::IsolatedContext; |
| 33 | 37 |
| 34 const char kInvalidParameters[] = "Invalid parameters"; | 38 const char kInvalidParameters[] = "Invalid parameters"; |
| 35 const char kSecurityError[] = "Security error"; | 39 const char kSecurityError[] = "Security error"; |
| 36 const char kInvalidCallingPage[] = "Invalid calling page"; | 40 const char kInvalidCallingPage[] = "Invalid calling page"; |
| 37 const char kUserCancelled[] = "User cancelled"; | 41 const char kUserCancelled[] = "User cancelled"; |
| 38 const char kWritableFileError[] = "Invalid file for writing"; | 42 const char kWritableFileError[] = "Invalid file for writing"; |
| 39 const char kRequiresFileSystemWriteError[] = | 43 const char kRequiresFileSystemWriteError[] = |
| 40 "Operation requires fileSystemWrite permission"; | 44 "Operation requires fileSystemWrite permission"; |
| 41 const char kUnknownChooseFileType[] = "Unknown type"; | 45 const char kUnknownChooseFileType[] = "Unknown type"; |
| 42 | 46 |
| 43 const char kOpenFileOption[] = "openFile"; | 47 const char kOpenFileOption[] = "openFile"; |
| 44 const char kOpenWritableFileOption[] ="openWritableFile"; | 48 const char kOpenWritableFileOption[] ="openWritableFile"; |
| 45 const char kSaveFileOption[] = "saveFile"; | 49 const char kSaveFileOption[] = "saveFile"; |
| 46 | 50 |
| 47 namespace file_system = extensions::api::file_system; | 51 namespace file_system = extensions::api::file_system; |
| 48 namespace ChooseFile = file_system::ChooseFile; | 52 namespace ChooseFile = file_system::ChooseFile; |
| 49 | 53 |
| 50 namespace { | 54 namespace { |
| 51 | 55 |
| 52 struct RewritePair { | 56 // Get the user's (optional) home directory via PathService. |
| 53 int path_key; | 57 FilePath GetHome() { |
| 54 const char* output; | 58 int home_key = 0; |
| 55 }; | 59 #if defined(OS_WIN) |
| 60 home_key = base::DIR_PROFILE; | |
| 61 #elif defined(OS_POSIX) | |
| 62 home_key = base::DIR_HOME; | |
| 63 #endif | |
| 64 FilePath home_path; | |
| 65 if (home_key && PathService::Get(home_key, &home_path)) { | |
| 66 return home_path; | |
| 67 } | |
| 68 return FilePath(); | |
| 69 } | |
| 56 | 70 |
| 57 const RewritePair g_rewrite_pairs[] = { | 71 // Prettifies |source_path|. If the path is inside the user's home directory, |
| 58 #if defined(OS_WIN) | 72 // then replace the home directory component with "~". |
| 59 {base::DIR_PROFILE, "~"}, | 73 FilePath PrettifyPath(const FilePath& source_path) { |
| 60 #elif defined(OS_POSIX) | 74 DCHECK(source_path.IsAbsolute()); |
| 61 {base::DIR_HOME, "~"}, | 75 FilePath home_path = GetHome(); |
| 76 | |
| 77 #if defined(OS_MACOSX) | |
| 78 // Break down the incoming path into components, and grab the display name | |
| 79 // for every component. This will match app bundles, ".localized" folders, | |
| 80 // and localized subfolders of the user's home directory. | |
| 81 // Don't grab the display name of the first component, i.e., "/", as it'll | |
| 82 // show up as the HDD name. | |
| 83 std::vector<FilePath::StringType> components; | |
| 84 source_path.GetComponents(&components); | |
| 85 FilePath display_path = FilePath(components[0]); | |
| 86 FilePath actual_path = display_path; | |
| 87 for (std::vector<FilePath::StringType>::iterator i = components.begin() + 1; | |
| 88 i != components.end(); ++i) { | |
| 89 actual_path = actual_path.Append(*i); | |
| 90 if (actual_path == home_path) { | |
| 91 display_path = FilePath("~"); | |
| 92 home_path = FilePath(); | |
| 93 continue; | |
| 94 } | |
| 95 std::string display = base::mac::GetDisplayBaseName(actual_path); | |
| 96 display_path = display_path.Append(display); | |
| 97 } | |
| 98 DCHECK(actual_path == source_path); | |
|
Nico
2012/09/04 14:09:37
DCHECK_EQ
thorogood
2012/09/06 00:16:53
This seems to try to convert one of the arguments
| |
| 99 return display_path; | |
| 100 #else | |
| 101 // If found, replace the leading home directory with "~". | |
| 102 FilePath display_path("~"); | |
| 103 if (!home_path.empty() && home_path.AppendRelativePath(source_path, &output)) | |
| 104 return display_path; | |
| 105 return source_path; | |
| 62 #endif | 106 #endif |
| 63 }; | |
| 64 | |
| 65 FilePath PrettifyPath(const FilePath& file_path) { | |
| 66 #if defined(OS_WIN) || defined(OS_POSIX) | |
| 67 for (size_t i = 0; i < arraysize(g_rewrite_pairs); ++i) { | |
| 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 } | |
| 81 #endif | |
| 82 | |
| 83 return file_path; | |
| 84 } | 107 } |
| 85 | 108 |
| 86 bool g_skip_picker_for_test = false; | 109 bool g_skip_picker_for_test = false; |
| 87 FilePath* g_path_to_be_picked_for_test; | 110 FilePath* g_path_to_be_picked_for_test; |
| 88 | 111 |
| 89 bool GetFilePathOfFileEntry(const std::string& filesystem_name, | 112 bool GetFilePathOfFileEntry(const std::string& filesystem_name, |
| 90 const std::string& filesystem_path, | 113 const std::string& filesystem_path, |
| 91 const content::RenderViewHost* render_view_host, | 114 const content::RenderViewHost* render_view_host, |
| 92 FilePath* file_path, | 115 FilePath* file_path, |
| 93 std::string* error) { | 116 std::string* error) { |
| (...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 553 | 576 |
| 554 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) { | 577 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) { |
| 555 error_ = kRequiresFileSystemWriteError; | 578 error_ = kRequiresFileSystemWriteError; |
| 556 return false; | 579 return false; |
| 557 } | 580 } |
| 558 | 581 |
| 559 return ShowPicker(suggested_name, file_type_info, picker_type, entry_type); | 582 return ShowPicker(suggested_name, file_type_info, picker_type, entry_type); |
| 560 } | 583 } |
| 561 | 584 |
| 562 } // namespace extensions | 585 } // namespace extensions |
| OLD | NEW |