Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(505)

Side by Side Diff: chrome/browser/extensions/api/file_system/file_system_api.cc

Issue 10823157: Prettify Mac names, rewrite getDisplayPath API call to focus on $HOME (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: update for benwells Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 std::vector<FilePath::StringType> components;
82 source_path.GetComponents(&components);
83 FilePath display_path = FilePath(components[0]);
84 FilePath actual_path = display_path;
85 for (std::vector<FilePath::StringType>::iterator i = components.begin() + 1;
86 i != components.end(); ++i) {
87 actual_path = actual_path.Append(*i);
88 if (actual_path == home_path) {
89 display_path = FilePath("~");
90 home_path = FilePath();
91 continue;
92 }
93 std::string display_basename =
94 base::mac::GetDisplayNameForPath(actual_path);
95 display_path = display_path.Append(display_basename);
96 }
97 DCHECK(actual_path == source_path);
98 return display_path;
99 #else
100 // If found, replace the leading home directory with "~".
101 FilePath display_path("~");
102 if (!home_path.empty() && home_path.AppendRelativePath(source_path, &output))
103 return display_path;
104 return source_path;
62 #endif 105 #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 } 106 }
85 107
86 bool g_skip_picker_for_test = false; 108 bool g_skip_picker_for_test = false;
87 FilePath* g_path_to_be_picked_for_test; 109 FilePath* g_path_to_be_picked_for_test;
88 110
89 bool GetFilePathOfFileEntry(const std::string& filesystem_name, 111 bool GetFilePathOfFileEntry(const std::string& filesystem_name,
90 const std::string& filesystem_path, 112 const std::string& filesystem_path,
91 const content::RenderViewHost* render_view_host, 113 const content::RenderViewHost* render_view_host,
92 FilePath* file_path, 114 FilePath* file_path,
93 std::string* error) { 115 std::string* error) {
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 575
554 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) { 576 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) {
555 error_ = kRequiresFileSystemWriteError; 577 error_ = kRequiresFileSystemWriteError;
556 return false; 578 return false;
557 } 579 }
558 580
559 return ShowPicker(suggested_name, file_type_info, picker_type, entry_type); 581 return ShowPicker(suggested_name, file_type_info, picker_type, entry_type);
560 } 582 }
561 583
562 } // namespace extensions 584 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698