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

Side by Side Diff: chrome/browser/web_applications/web_app_mac.mm

Issue 9426030: Mac: Enable WebAppShortcutCreatorTest.CreateShortcut unit test (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Patch for landing Created 8 years, 9 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 | Annotate | Revision Log
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 #import "chrome/browser/web_applications/web_app_mac.h" 5 #import "chrome/browser/web_applications/web_app_mac.h"
6 6
7 #import <Cocoa/Cocoa.h> 7 #import <Cocoa/Cocoa.h>
8 8
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/mac/bundle_locations.h" 10 #include "base/mac/bundle_locations.h"
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 73
74 if (!UpdateIcon(staging_path)) 74 if (!UpdateIcon(staging_path))
75 return false; 75 return false;
76 76
77 FilePath dst_path = GetDestinationPath(app_file_name); 77 FilePath dst_path = GetDestinationPath(app_file_name);
78 if (!file_util::CopyDirectory(staging_path, dst_path, true)) { 78 if (!file_util::CopyDirectory(staging_path, dst_path, true)) {
79 LOG(ERROR) << "Copying app to dst path: " << dst_path.value() << " failed"; 79 LOG(ERROR) << "Copying app to dst path: " << dst_path.value() << " failed";
80 return false; 80 return false;
81 } 81 }
82 82
83 [[NSWorkspace sharedWorkspace] 83 RevealGeneratedBundleInFinder(dst_path);
84 selectFile:base::mac::FilePathToNSString(dst_path) 84
85 inFileViewerRootedAtPath:nil];
86 return true; 85 return true;
87 } 86 }
88 87
89 FilePath WebAppShortcutCreator::GetAppLoaderPath() const { 88 FilePath WebAppShortcutCreator::GetAppLoaderPath() const {
90 return base::mac::PathForFrameworkBundleResource( 89 return base::mac::PathForFrameworkBundleResource(
91 base::mac::NSToCFCast(@"app_mode_loader.app")); 90 base::mac::NSToCFCast(@"app_mode_loader.app"));
92 } 91 }
93 92
94 FilePath WebAppShortcutCreator::GetDestinationPath( 93 FilePath WebAppShortcutCreator::GetDestinationPath(
95 const FilePath& app_file_name) const { 94 const FilePath& app_file_name) const {
96 FilePath path; 95 FilePath path;
97 if (base::mac::GetLocalDirectory(NSApplicationDirectory, &path) && 96 if (base::mac::GetLocalDirectory(NSApplicationDirectory, &path) &&
98 file_util::PathIsWritable(path)) { 97 file_util::PathIsWritable(path)) {
99 return path; 98 return path;
100 } 99 }
101 100
102 if (base::mac::GetUserDirectory(NSApplicationDirectory, &path)) 101 if (base::mac::GetUserDirectory(NSApplicationDirectory, &path))
103 return path; 102 return path;
104 103
105 return FilePath(); 104 return FilePath();
106 } 105 }
107 106
108 bool WebAppShortcutCreator::UpdatePlist(const FilePath& app_path) const { 107 bool WebAppShortcutCreator::UpdatePlist(const FilePath& app_path) const {
109 NSString* plist_path = base::mac::FilePathToNSString( 108 NSString* plist_path = base::mac::FilePathToNSString(
110 app_path.Append("Contents").Append("Info.plist")); 109 app_path.Append("Contents").Append("Info.plist"));
111 NSMutableDictionary* dict = 110
111 NSString* extension_id = base::SysUTF8ToNSString(info_.extension_id);
112 NSString* extension_title = base::SysUTF16ToNSString(info_.title);
113 NSString* extension_url = base::SysUTF8ToNSString(info_.url.spec());
114 NSString* chrome_bundle_id = base::SysUTF16ToNSString(chrome_bundle_id_);
115 NSDictionary* replacement_dict =
116 [NSDictionary dictionaryWithObjectsAndKeys:
117 extension_id, app_mode::kShortcutIdPlaceholder,
118 extension_title, app_mode::kShortcutNamePlaceholder,
119 extension_url, app_mode::kShortcutURLPlaceholder,
120 chrome_bundle_id, app_mode::kShortcutBrowserBundleIDPlaceholder,
121 nil];
122
123 NSMutableDictionary* plist =
112 [NSMutableDictionary dictionaryWithContentsOfFile:plist_path]; 124 [NSMutableDictionary dictionaryWithContentsOfFile:plist_path];
125 NSArray* keys = [plist allKeys];
113 126
114 [dict setObject:GetBundleIdentifier(dict) 127 // 1. Fill in variables.
115 forKey:base::mac::CFToNSCast(kCFBundleIdentifierKey)]; 128 for (id key in keys) {
116 [dict setObject:base::SysUTF8ToNSString(info_.extension_id) 129 NSString* value = [plist valueForKey:key];
117 forKey:app_mode::kCrAppModeShortcutIDKey]; 130 if (![value isKindOfClass:[NSString class]] || [value length] < 2)
118 [dict setObject:base::SysUTF16ToNSString(info_.title) 131 continue;
119 forKey:app_mode::kCrAppModeShortcutNameKey]; 132
120 [dict setObject:base::SysUTF8ToNSString(info_.url.spec()) 133 // Remove leading and trailing '@'s.
121 forKey:app_mode::kCrAppModeShortcutURLKey]; 134 NSString* variable =
122 [dict setObject:base::mac::FilePathToNSString(user_data_dir_) 135 [value substringWithRange:NSMakeRange(1, [value length] - 2)];
123 forKey:app_mode::kCrAppModeUserDataDirKey]; 136
124 [dict setObject:base::mac::FilePathToNSString(info_.extension_path) 137 NSString* substitution = [replacement_dict valueForKey:variable];
125 forKey:app_mode::kCrAppModeExtensionPathKey]; 138 if (substitution)
126 [dict setObject:base::SysUTF16ToNSString(chrome_bundle_id_) 139 [plist setObject:substitution forKey:key];
127 forKey:app_mode::kBrowserBundleIDKey]; 140 }
128 return [dict writeToFile:plist_path atomically:YES]; 141
142 // 2. Fill in other values.
143 [plist setObject:GetBundleIdentifier(plist)
144 forKey:base::mac::CFToNSCast(kCFBundleIdentifierKey)];
145 [plist setObject:base::mac::FilePathToNSString(user_data_dir_)
146 forKey:app_mode::kCrAppModeUserDataDirKey];
147 [plist setObject:base::mac::FilePathToNSString(info_.extension_path)
148 forKey:app_mode::kCrAppModeExtensionPathKey];
149 return [plist writeToFile:plist_path atomically:YES];
129 } 150 }
130 151
131 bool WebAppShortcutCreator::UpdateIcon(const FilePath& app_path) const { 152 bool WebAppShortcutCreator::UpdateIcon(const FilePath& app_path) const {
132 // TODO(sail): Add support for multiple icon sizes. 153 // TODO(sail): Add support for multiple icon sizes.
133 if (info_.favicon.empty() || info_.favicon.width() != 32 || 154 if (info_.favicon.empty() || info_.favicon.width() != 32 ||
134 info_.favicon.height() != 32) { 155 info_.favicon.height() != 32) {
135 return true; 156 return true;
136 } 157 }
137 158
138 NSBitmapImageRep* image_rep = SkBitmapToImageRep(info_.favicon); 159 NSBitmapImageRep* image_rep = SkBitmapToImageRep(info_.favicon);
(...skipping 27 matching lines...) Expand all
166 NSString* extension_id = base::SysUTF8ToNSString(info_.extension_id); 187 NSString* extension_id = base::SysUTF8ToNSString(info_.extension_id);
167 NSString* placeholder = 188 NSString* placeholder =
168 [NSString stringWithFormat:@"@%@@", app_mode::kShortcutIdPlaceholder]; 189 [NSString stringWithFormat:@"@%@@", app_mode::kShortcutIdPlaceholder];
169 NSString* bundle_id = 190 NSString* bundle_id =
170 [bundle_id_template 191 [bundle_id_template
171 stringByReplacingOccurrencesOfString:placeholder 192 stringByReplacingOccurrencesOfString:placeholder
172 withString:extension_id]; 193 withString:extension_id];
173 return bundle_id; 194 return bundle_id;
174 } 195 }
175 196
197 void WebAppShortcutCreator::RevealGeneratedBundleInFinder(
198 const FilePath& generated_bundle) const {
199 [[NSWorkspace sharedWorkspace]
200 selectFile:base::mac::FilePathToNSString(generated_bundle)
201 inFileViewerRootedAtPath:nil];
202 }
203
176 } // namespace 204 } // namespace
177 205
178 namespace web_app { 206 namespace web_app {
179 namespace internals { 207 namespace internals {
180 208
181 void CreateShortcutTask(const FilePath& web_app_path, 209 void CreateShortcutTask(const FilePath& web_app_path,
182 const FilePath& profile_path, 210 const FilePath& profile_path,
183 const ShellIntegration::ShortcutInfo& shortcut_info) { 211 const ShellIntegration::ShortcutInfo& shortcut_info) {
184 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); 212 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
185 string16 bundle_id = UTF8ToUTF16(base::mac::BaseBundleID()); 213 string16 bundle_id = UTF8ToUTF16(base::mac::BaseBundleID());
186 WebAppShortcutCreator shortcut_creator(web_app_path, shortcut_info, 214 WebAppShortcutCreator shortcut_creator(web_app_path, shortcut_info,
187 bundle_id); 215 bundle_id);
188 shortcut_creator.CreateShortcut(); 216 shortcut_creator.CreateShortcut();
189 } 217 }
190 218
191 } // namespace internals 219 } // namespace internals
192 } // namespace web_app 220 } // namespace web_app
OLDNEW
« no previous file with comments | « chrome/browser/web_applications/web_app_mac.h ('k') | chrome/browser/web_applications/web_app_mac_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698