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

Side by Side Diff: chrome/browser/web_applications/web_app.cc

Issue 9346013: Publish app shortcuts on Mac (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address review comments Created 8 years, 10 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/web_applications/web_app.h" 5 #include "chrome/browser/web_applications/web_app.h"
6 6
7 #if defined(OS_WIN)
8 #include <shlobj.h>
9 #endif // defined(OS_WIN)
10
11 #include "base/bind.h" 7 #include "base/bind.h"
12 #include "base/command_line.h"
13 #include "base/file_util.h" 8 #include "base/file_util.h"
14 #include "base/i18n/file_util_icu.h" 9 #include "base/i18n/file_util_icu.h"
15 #include "base/md5.h"
16 #include "base/path_service.h"
17 #include "base/stringprintf.h"
18 #include "base/string_util.h" 10 #include "base/string_util.h"
19 #include "base/threading/thread.h" 11 #include "base/threading/thread.h"
20 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
21 #include "base/win/windows_version.h"
22 #include "chrome/common/chrome_constants.h" 13 #include "chrome/common/chrome_constants.h"
23 #include "chrome/common/chrome_paths.h"
24 #include "chrome/common/url_constants.h" 14 #include "chrome/common/url_constants.h"
25 #include "content/public/browser/browser_thread.h" 15 #include "content/public/browser/browser_thread.h"
26 16
27 #if defined(OS_POSIX) && !defined(OS_MACOSX)
28 #include "base/environment.h"
29 #endif
30
31 #if defined(OS_WIN)
32 #include "ui/gfx/icon_util.h"
33 #endif // defined(OS_WIN)
34
35 using content::BrowserThread; 17 using content::BrowserThread;
36 18
37 namespace { 19 namespace {
38 20
39 // Allow tests to disable shortcut creation, to prevent developers desktops 21 // Allow tests to disable shortcut creation, to prevent developers desktops
40 // becoming overrun with shortcuts. 22 // becoming overrun with shortcuts.
41 bool disable_shortcut_creation_for_tests = false; 23 bool disable_shortcut_creation_for_tests = false;
42 24
43 #if defined(OS_WIN)
44 const FilePath::CharType kIconChecksumFileExt[] = FILE_PATH_LITERAL(".ico.md5");
45
46 // Returns true if |ch| is in visible ASCII range and not one of
47 // "/ \ : * ? " < > | ; ,".
48 bool IsValidFilePathChar(char16 c) {
49 if (c < 32)
50 return false;
51
52 switch (c) {
53 case '/':
54 case '\\':
55 case ':':
56 case '*':
57 case '?':
58 case '"':
59 case '<':
60 case '>':
61 case '|':
62 case ';':
63 case ',':
64 return false;
65 };
66
67 return true;
68 }
69
70 #endif // defined(OS_WIN)
71
72 // Returns relative directory of given web app url. 25 // Returns relative directory of given web app url.
73 FilePath GetWebAppDir(const ShellIntegration::ShortcutInfo& info) { 26 FilePath GetWebAppDir(const ShellIntegration::ShortcutInfo& info) {
74 if (!info.extension_id.empty()) { 27 if (!info.extension_id.empty()) {
75 std::string app_name = 28 std::string app_name =
76 web_app::GenerateApplicationNameFromExtensionId(info.extension_id); 29 web_app::GenerateApplicationNameFromExtensionId(info.extension_id);
77 #if defined(OS_WIN) 30 #if defined(OS_WIN)
78 return FilePath(UTF8ToWide(app_name)); 31 return FilePath(UTF8ToWide(app_name));
79 #elif defined(OS_POSIX) 32 #elif defined(OS_POSIX)
80 return FilePath(app_name); 33 return FilePath(app_name);
81 #endif 34 #endif
(...skipping 16 matching lines...) Expand all
98 } 51 }
99 52
100 #if defined(TOOLKIT_VIEWS) 53 #if defined(TOOLKIT_VIEWS)
101 // Predicator for sorting images from largest to smallest. 54 // Predicator for sorting images from largest to smallest.
102 bool IconPrecedes(const WebApplicationInfo::IconInfo& left, 55 bool IconPrecedes(const WebApplicationInfo::IconInfo& left,
103 const WebApplicationInfo::IconInfo& right) { 56 const WebApplicationInfo::IconInfo& right) {
104 return left.width < right.width; 57 return left.width < right.width;
105 } 58 }
106 #endif 59 #endif
107 60
108 #if defined(OS_WIN)
109 // Calculates image checksum using MD5.
110 void GetImageCheckSum(const SkBitmap& image, base::MD5Digest* digest) {
111 DCHECK(digest);
112
113 SkAutoLockPixels image_lock(image);
114 MD5Sum(image.getPixels(), image.getSize(), digest);
115 }
116
117 // Saves |image| as an |icon_file| with the checksum.
118 bool SaveIconWithCheckSum(const FilePath& icon_file, const SkBitmap& image) {
119 if (!IconUtil::CreateIconFileFromSkBitmap(image, icon_file))
120 return false;
121
122 base::MD5Digest digest;
123 GetImageCheckSum(image, &digest);
124
125 FilePath cheksum_file(icon_file.ReplaceExtension(kIconChecksumFileExt));
126 return file_util::WriteFile(cheksum_file,
127 reinterpret_cast<const char*>(&digest),
128 sizeof(digest)) == sizeof(digest);
129 }
130
131 // Returns true if |icon_file| is missing or different from |image|.
132 bool ShouldUpdateIcon(const FilePath& icon_file, const SkBitmap& image) {
133 FilePath checksum_file(icon_file.ReplaceExtension(kIconChecksumFileExt));
134
135 // Returns true if icon_file or checksum file is missing.
136 if (!file_util::PathExists(icon_file) ||
137 !file_util::PathExists(checksum_file))
138 return true;
139
140 base::MD5Digest persisted_image_checksum;
141 if (sizeof(persisted_image_checksum) != file_util::ReadFile(checksum_file,
142 reinterpret_cast<char*>(&persisted_image_checksum),
143 sizeof(persisted_image_checksum)))
144 return true;
145
146 base::MD5Digest downloaded_image_checksum;
147 GetImageCheckSum(image, &downloaded_image_checksum);
148
149 // Update icon if checksums are not equal.
150 return memcmp(&persisted_image_checksum, &downloaded_image_checksum,
151 sizeof(base::MD5Digest)) != 0;
152 }
153
154 #endif // defined(OS_WIN)
155
156 void CreateShortcutTask(const FilePath& web_app_path,
157 const FilePath& profile_path,
158 const ShellIntegration::ShortcutInfo& shortcut_info) {
159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
160
161 if (disable_shortcut_creation_for_tests)
162 return;
163
164 #if defined(OS_POSIX) && !defined(OS_MACOSX)
165 scoped_ptr<base::Environment> env(base::Environment::Create());
166
167 std::string shortcut_template;
168 if (!ShellIntegration::GetDesktopShortcutTemplate(env.get(),
169 &shortcut_template)) {
170 return;
171 }
172 ShellIntegration::CreateDesktopShortcut(shortcut_info, shortcut_template);
173 return; // assuming always success.
174 #elif defined(OS_WIN)
175 // Shortcut paths under which to create shortcuts.
176 std::vector<FilePath> shortcut_paths;
177
178 // Locations to add to shortcut_paths.
179 struct {
180 const bool& use_this_location;
181 int location_id;
182 const wchar_t* sub_dir;
183 } locations[] = {
184 {
185 shortcut_info.create_on_desktop,
186 chrome::DIR_USER_DESKTOP,
187 NULL
188 }, {
189 shortcut_info.create_in_applications_menu,
190 base::DIR_START_MENU,
191 NULL
192 }, {
193 shortcut_info.create_in_quick_launch_bar,
194 // For Win7, create_in_quick_launch_bar means pinning to taskbar. Use
195 // base::PATH_START as a flag for this case.
196 (base::win::GetVersion() >= base::win::VERSION_WIN7) ?
197 base::PATH_START : base::DIR_APP_DATA,
198 (base::win::GetVersion() >= base::win::VERSION_WIN7) ?
199 NULL : L"Microsoft\\Internet Explorer\\Quick Launch"
200 }
201 };
202
203 // Populate shortcut_paths.
204 for (int i = 0; i < arraysize(locations); ++i) {
205 if (locations[i].use_this_location) {
206 FilePath path;
207
208 // Skip the Win7 case.
209 if (locations[i].location_id == base::PATH_START)
210 continue;
211
212 if (!PathService::Get(locations[i].location_id, &path)) {
213 return;
214 }
215
216 if (locations[i].sub_dir != NULL)
217 path = path.Append(locations[i].sub_dir);
218
219 shortcut_paths.push_back(path);
220 }
221 }
222
223 bool pin_to_taskbar =
224 shortcut_info.create_in_quick_launch_bar &&
225 (base::win::GetVersion() >= base::win::VERSION_WIN7);
226
227 // For Win7's pinning support, any shortcut could be used. So we only create
228 // the shortcut file when there is no shortcut file will be created. That is,
229 // user only selects "Pin to taskbar".
230 if (pin_to_taskbar && shortcut_paths.empty()) {
231 // Creates the shortcut in web_app_path in this case.
232 shortcut_paths.push_back(web_app_path);
233 }
234
235 if (shortcut_paths.empty()) {
236 return;
237 }
238
239 // Ensure web_app_path exists.
240 if (!file_util::PathExists(web_app_path) &&
241 !file_util::CreateDirectory(web_app_path)) {
242 return;
243 }
244
245 // Generates file name to use with persisted ico and shortcut file.
246 FilePath file_name =
247 web_app::internals::GetSanitizedFileName(shortcut_info.title);
248
249 // Creates an ico file to use with shortcut.
250 FilePath icon_file = web_app_path.Append(file_name).ReplaceExtension(
251 FILE_PATH_LITERAL(".ico"));
252 if (!web_app::internals::CheckAndSaveIcon(icon_file,
253 shortcut_info.favicon)) {
254 return;
255 }
256
257 FilePath chrome_exe;
258 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
259 return;
260 }
261
262 // Working directory.
263 FilePath chrome_folder = chrome_exe.DirName();
264
265 CommandLine cmd_line =
266 ShellIntegration::CommandLineArgsForLauncher(shortcut_info.url,
267 shortcut_info.extension_id);
268 // TODO(evan): we rely on the fact that command_line_string() is
269 // properly quoted for a Windows command line. The method on
270 // CommandLine should probably be renamed to better reflect that
271 // fact.
272 string16 wide_switches(cmd_line.GetCommandLineString());
273
274 // Sanitize description
275 string16 description = shortcut_info.description;
276 if (description.length() >= MAX_PATH)
277 description.resize(MAX_PATH - 1);
278
279 // Generates app id from web app url and profile path.
280 std::string app_name =
281 web_app::GenerateApplicationNameFromInfo(shortcut_info);
282 std::wstring app_id = ShellIntegration::GetAppId(
283 UTF8ToWide(app_name), profile_path);
284
285 FilePath shortcut_to_pin;
286
287 bool success = true;
288 for (size_t i = 0; i < shortcut_paths.size(); ++i) {
289 FilePath shortcut_file = shortcut_paths[i].Append(file_name).
290 ReplaceExtension(FILE_PATH_LITERAL(".lnk"));
291
292 int unique_number =
293 file_util::GetUniquePathNumber(shortcut_file, FILE_PATH_LITERAL(""));
294 if (unique_number == -1) {
295 success = false;
296 continue;
297 } else if (unique_number > 0) {
298 shortcut_file = shortcut_file.InsertBeforeExtensionASCII(
299 StringPrintf(" (%d)", unique_number));
300 }
301
302 success &= file_util::CreateShortcutLink(chrome_exe.value().c_str(),
303 shortcut_file.value().c_str(),
304 chrome_folder.value().c_str(),
305 wide_switches.c_str(),
306 description.c_str(),
307 icon_file.value().c_str(),
308 0,
309 app_id.c_str());
310
311 // Any shortcut would work for the pinning. We use the first one.
312 if (success && pin_to_taskbar && shortcut_to_pin.empty())
313 shortcut_to_pin = shortcut_file;
314 }
315
316 if (success && pin_to_taskbar) {
317 if (!shortcut_to_pin.empty()) {
318 success &= file_util::TaskbarPinShortcutLink(
319 shortcut_to_pin.value().c_str());
320 } else {
321 success = false;
322 }
323 }
324 #else
325 NOTIMPLEMENTED();
326 #endif
327 }
328
329 } // namespace 61 } // namespace
330 62
331 namespace web_app { 63 namespace web_app {
332 64
333 // The following string is used to build the directory name for 65 // The following string is used to build the directory name for
334 // shortcuts to chrome applications (the kind which are installed 66 // shortcuts to chrome applications (the kind which are installed
335 // from a CRX). Application shortcuts to URLs use the {host}_{path} 67 // from a CRX). Application shortcuts to URLs use the {host}_{path}
336 // for the name of this directory. Hosts can't include an underscore. 68 // for the name of this directory. Hosts can't include an underscore.
337 // By starting this string with an underscore, we ensure that there 69 // By starting this string with an underscore, we ensure that there
338 // are no naming conflicts. 70 // are no naming conflicts.
339 static const char* kCrxAppPrefix = "_crx_"; 71 static const char* kCrxAppPrefix = "_crx_";
340 72
341 namespace internals { 73 namespace internals {
342 74
343 #if defined(OS_WIN)
344 // Returns sanitized name that could be used as a file name
345 FilePath GetSanitizedFileName(const string16& name) {
346 string16 file_name;
347
348 for (size_t i = 0; i < name.length(); ++i) {
349 char16 c = name[i];
350 if (!IsValidFilePathChar(c))
351 c = '_';
352
353 file_name += c;
354 }
355
356 return FilePath(file_name);
357 }
358
359 // Saves |image| to |icon_file| if the file is outdated and refresh shell's
360 // icon cache to ensure correct icon is displayed. Returns true if icon_file
361 // is up to date or successfully updated.
362 bool CheckAndSaveIcon(const FilePath& icon_file, const SkBitmap& image) {
363 if (ShouldUpdateIcon(icon_file, image)) {
364 if (SaveIconWithCheckSum(icon_file, image)) {
365 // Refresh shell's icon cache. This call is quite disruptive as user would
366 // see explorer rebuilding the icon cache. It would be great that we find
367 // a better way to achieve this.
368 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST | SHCNF_FLUSHNOWAIT,
369 NULL, NULL);
370 } else {
371 return false;
372 }
373 }
374
375 return true;
376 }
377 #endif // OS_WIN
378
379 // Returns data directory for given web app url 75 // Returns data directory for given web app url
380 FilePath GetWebAppDataDirectory(const FilePath& root_dir, 76 FilePath GetWebAppDataDirectory(const FilePath& root_dir,
381 const ShellIntegration::ShortcutInfo& info) { 77 const ShellIntegration::ShortcutInfo& info) {
382 return root_dir.Append(GetWebAppDir(info)); 78 return root_dir.Append(GetWebAppDir(info));
383 } 79 }
384 80
81 FilePath GetSanitizedFileName(const string16& name) {
82 #if defined(OS_WIN)
83 string16 file_name = name;
84 #else
85 std::string file_name = UTF16ToUTF8(name);
86 #endif
87 file_util::ReplaceIllegalCharactersInPath(&file_name, '_');
88 return FilePath(file_name);
89 }
90
385 } // namespace internals 91 } // namespace internals
386 92
387 std::string GenerateApplicationNameFromInfo( 93 std::string GenerateApplicationNameFromInfo(
388 const ShellIntegration::ShortcutInfo& shortcut_info) { 94 const ShellIntegration::ShortcutInfo& shortcut_info) {
389 if (!shortcut_info.extension_id.empty()) { 95 if (!shortcut_info.extension_id.empty()) {
390 return web_app::GenerateApplicationNameFromExtensionId( 96 return web_app::GenerateApplicationNameFromExtensionId(
391 shortcut_info.extension_id); 97 shortcut_info.extension_id);
392 } else { 98 } else {
393 return web_app::GenerateApplicationNameFromURL( 99 return web_app::GenerateApplicationNameFromURL(
394 shortcut_info.url); 100 shortcut_info.url);
(...skipping 17 matching lines...) Expand all
412 std::string GetExtensionIdFromApplicationName(const std::string& app_name) { 118 std::string GetExtensionIdFromApplicationName(const std::string& app_name) {
413 std::string prefix(kCrxAppPrefix); 119 std::string prefix(kCrxAppPrefix);
414 if (app_name.substr(0, prefix.length()) != prefix) 120 if (app_name.substr(0, prefix.length()) != prefix)
415 return std::string(); 121 return std::string();
416 return app_name.substr(prefix.length()); 122 return app_name.substr(prefix.length());
417 } 123 }
418 124
419 void CreateShortcut( 125 void CreateShortcut(
420 const FilePath& data_dir, 126 const FilePath& data_dir,
421 const ShellIntegration::ShortcutInfo& shortcut_info) { 127 const ShellIntegration::ShortcutInfo& shortcut_info) {
128 if (disable_shortcut_creation_for_tests)
129 return;
130
422 BrowserThread::PostTask( 131 BrowserThread::PostTask(
423 BrowserThread::FILE, 132 BrowserThread::FILE,
424 FROM_HERE, 133 FROM_HERE,
425 base::Bind(&CreateShortcutTask, 134 base::Bind(&internals::CreateShortcutTask,
426 web_app::internals::GetWebAppDataDirectory( 135 web_app::internals::GetWebAppDataDirectory(
427 web_app::GetDataDir(data_dir), 136 web_app::GetDataDir(data_dir),
---DO-NOT-USE---rsesek1 2012/02/08 12:24:33 nit: this should be indented 2 more
sail 2012/02/08 19:09:15 Done.
428 shortcut_info), 137 shortcut_info),
429 data_dir, 138 data_dir,
430 shortcut_info)); 139 shortcut_info));
431 } 140 }
432 141
433 void SetDisableShortcutCreationForTests(bool disable) { 142 void SetDisableShortcutCreationForTests(bool disable) {
434 disable_shortcut_creation_for_tests = disable; 143 disable_shortcut_creation_for_tests = disable;
435 } 144 }
436 145
437 bool IsValidUrl(const GURL& url) { 146 bool IsValidUrl(const GURL& url) {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 183
475 #if defined(TOOLKIT_USES_GTK) 184 #if defined(TOOLKIT_USES_GTK)
476 std::string GetWMClassFromAppName(std::string app_name) { 185 std::string GetWMClassFromAppName(std::string app_name) {
477 file_util::ReplaceIllegalCharactersInPath(&app_name, '_'); 186 file_util::ReplaceIllegalCharactersInPath(&app_name, '_');
478 TrimString(app_name, "_", &app_name); 187 TrimString(app_name, "_", &app_name);
479 return app_name; 188 return app_name;
480 } 189 }
481 #endif 190 #endif
482 191
483 } // namespace web_app 192 } // namespace web_app
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698