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

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: Rewrite getDisplayPath to focus on home directories, adds better Mac support 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 #if defined(OS_MACOSX)
benwells 2012/09/04 04:15:01 Move OS specific includes to after the rest (http:
thorogood 2012/09/04 05:57:33 Done, thanks.
8 #include "base/mac/foundation_util.h"
9 #endif
10
7 #include "base/bind.h" 11 #include "base/bind.h"
8 #include "base/file_path.h" 12 #include "base/file_path.h"
9 #include "base/file_util.h" 13 #include "base/file_util.h"
10 #include "base/logging.h" 14 #include "base/logging.h"
11 #include "base/path_service.h" 15 #include "base/path_service.h"
12 #include "base/string_util.h" 16 #include "base/string_util.h"
13 #include "base/utf_string_conversions.h" 17 #include "base/utf_string_conversions.h"
14 #include "chrome/browser/extensions/shell_window_registry.h" 18 #include "chrome/browser/extensions/shell_window_registry.h"
15 #include "chrome/browser/platform_util.h" 19 #include "chrome/browser/platform_util.h"
16 #include "chrome/browser/ui/chrome_select_file_policy.h" 20 #include "chrome/browser/ui/chrome_select_file_policy.h"
(...skipping 25 matching lines...) Expand all
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.
benwells 2012/09/04 04:15:01 How are you planning on doing MyDocuments etc. now
thorogood 2012/09/04 05:57:33 Yup, I'm happy to do that too, but I realised that
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 std::string display_name = base::mac::GetDisplayNameForPath(actual_path);
89 display_path = display_path.Append(display_name);
benwells 2012/09/04 04:15:01 Having two appends here feels strange. If you get
thorogood 2012/09/04 05:57:33 GetDisplayNameForPath only returns a prettified ba
benwells 2012/09/04 06:38:11 Makes sense now, this is only confusing because of
thorogood 2012/09/04 06:45:43 Updated this bit for the new method call.
90
91 // If the display path is absolute, we haven't replaced the home directory
92 // subcomponents with "~" yet.
93 if (display_path.IsAbsolute() && home_path == actual_path)
benwells 2012/09/04 04:15:01 Why is the check to IsAbsolute needed here? Can ho
thorogood 2012/09/04 05:57:33 It was mostly a short-circuit; once you find $HOME
94 display_path = FilePath("~");
95 }
96 DCHECK(actual_path == source_path);
97 return display_path;
98 #else
99 // If found, replace the leading home directory with "~".
100 FilePath display_path("~");
101 if (!home_path.empty() && home_path.AppendRelativePath(source_path, &output))
102 return display_path;
103 return source_path;
62 #endif 104 #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 } 105 }
85 106
86 bool g_skip_picker_for_test = false; 107 bool g_skip_picker_for_test = false;
87 FilePath* g_path_to_be_picked_for_test; 108 FilePath* g_path_to_be_picked_for_test;
88 109
89 bool GetFilePathOfFileEntry(const std::string& filesystem_name, 110 bool GetFilePathOfFileEntry(const std::string& filesystem_name,
90 const std::string& filesystem_path, 111 const std::string& filesystem_path,
91 const content::RenderViewHost* render_view_host, 112 const content::RenderViewHost* render_view_host,
92 FilePath* file_path, 113 FilePath* file_path,
93 std::string* error) { 114 std::string* error) {
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 574
554 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) { 575 if (entry_type == WRITABLE && !HasFileSystemWritePermission()) {
555 error_ = kRequiresFileSystemWriteError; 576 error_ = kRequiresFileSystemWriteError;
556 return false; 577 return false;
557 } 578 }
558 579
559 return ShowPicker(suggested_name, file_type_info, picker_type, entry_type); 580 return ShowPicker(suggested_name, file_type_info, picker_type, entry_type);
560 } 581 }
561 582
562 } // namespace extensions 583 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698