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

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

Powered by Google App Engine
This is Rietveld 408576698