OLD | NEW |
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/profiles/profile_shortcut_manager_win.h" | 5 #include "chrome/browser/profiles/profile_shortcut_manager_win.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
13 #include "base/string16.h" | 13 #include "base/string16.h" |
| 14 #include "base/string_util.h" |
14 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
15 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
16 #include "base/win/shortcut.h" | 17 #include "base/win/shortcut.h" |
17 #include "chrome/browser/app_icon_win.h" | 18 #include "chrome/browser/app_icon_win.h" |
18 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
19 #include "chrome/browser/profiles/profile_info_cache_observer.h" | 20 #include "chrome/browser/profiles/profile_info_cache_observer.h" |
20 #include "chrome/browser/profiles/profile_info_util.h" | 21 #include "chrome/browser/profiles/profile_info_util.h" |
21 #include "chrome/browser/profiles/profile_manager.h" | 22 #include "chrome/browser/profiles/profile_manager.h" |
22 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
23 #include "chrome/installer/util/browser_distribution.h" | 24 #include "chrome/installer/util/browser_distribution.h" |
24 #include "chrome/installer/util/product.h" | 25 #include "chrome/installer/util/product.h" |
25 #include "chrome/installer/util/shell_util.h" | 26 #include "chrome/installer/util/shell_util.h" |
26 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
27 #include "skia/ext/image_operations.h" | 28 #include "skia/ext/image_operations.h" |
28 #include "skia/ext/platform_canvas.h" | 29 #include "skia/ext/platform_canvas.h" |
29 #include "ui/base/resource/resource_bundle.h" | 30 #include "ui/base/resource/resource_bundle.h" |
30 #include "ui/gfx/icon_util.h" | 31 #include "ui/gfx/icon_util.h" |
31 #include "ui/gfx/image/image.h" | 32 #include "ui/gfx/image/image.h" |
32 | 33 |
33 using content::BrowserThread; | 34 using content::BrowserThread; |
34 | 35 |
35 namespace { | 36 namespace { |
36 | 37 |
| 38 // Characters that are not allowed in Windows filenames. Taken from |
| 39 // http://msdn.microsoft.com/en-us/library/aa365247.aspx |
| 40 const char16 kReservedCharacters[] = L"<>:\"/\\|?*\x01\x02\x03\x04\x05\x06\x07" |
| 41 L"\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19" |
| 42 L"\x1A\x1B\x1C\x1D\x1E\x1F"; |
| 43 |
| 44 // The maximum number of characters allowed in profile shortcuts' file names. |
| 45 // Warning: migration code will be needed if this is changed later, since |
| 46 // existing shortcuts might no longer be found if the name is generated |
| 47 // differently than it was when a shortcut was originally created. |
| 48 const int kMaxProfileShortcutFileNameLength = 64; |
| 49 |
37 const int kProfileAvatarShortcutBadgeWidth = 28; | 50 const int kProfileAvatarShortcutBadgeWidth = 28; |
38 const int kProfileAvatarShortcutBadgeHeight = 28; | 51 const int kProfileAvatarShortcutBadgeHeight = 28; |
39 const int kShortcutIconSize = 48; | 52 const int kShortcutIconSize = 48; |
40 | 53 |
41 // Creates a desktop shortcut icon file (.ico) on the disk for a given profile, | 54 // Creates a desktop shortcut icon file (.ico) on the disk for a given profile, |
42 // badging the browser distribution icon with the profile avatar. | 55 // badging the browser distribution icon with the profile avatar. |
43 // Returns a path to the shortcut icon file on disk, which is empty if this | 56 // Returns a path to the shortcut icon file on disk, which is empty if this |
44 // fails. Use index 0 when assigning the resulting file as the icon. | 57 // fails. Use index 0 when assigning the resulting file as the icon. |
45 FilePath CreateChromeDesktopShortcutIconForProfile( | 58 FilePath CreateChromeDesktopShortcutIconForProfile( |
46 const FilePath& profile_path, | 59 const FilePath& profile_path, |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 const string16 shortcut_name = | 284 const string16 shortcut_name = |
272 shortcuts[i].BaseName().RemoveExtension().value(); | 285 shortcuts[i].BaseName().RemoveExtension().value(); |
273 ShellUtil::RemoveShortcut(ShellUtil::SHORTCUT_LOCATION_DESKTOP, | 286 ShellUtil::RemoveShortcut(ShellUtil::SHORTCUT_LOCATION_DESKTOP, |
274 distribution, chrome_exe, ShellUtil::CURRENT_USER, | 287 distribution, chrome_exe, ShellUtil::CURRENT_USER, |
275 &shortcut_name); | 288 &shortcut_name); |
276 } | 289 } |
277 | 290 |
278 file_util::Delete(icon_path, false); | 291 file_util::Delete(icon_path, false); |
279 } | 292 } |
280 | 293 |
| 294 // Replaces any reserved characters with spaces, and trims the resulting string |
| 295 // to prevent any leading and trailing spaces. Also makes sure that the |
| 296 // resulting filename doesn't exceed |kMaxProfileShortcutFileNameLength|. |
| 297 // TODO(macourteau): find a way to limit the total path's length to MAX_PATH |
| 298 // instead of limiting the profile's name to |kMaxProfileShortcutFileNameLength| |
| 299 // characters. |
| 300 string16 SanitizeShortcutProfileNameString(const string16& profile_name) { |
| 301 string16 sanitized = profile_name; |
| 302 size_t pos = sanitized.find_first_of(kReservedCharacters); |
| 303 while (pos != string16::npos) { |
| 304 sanitized[pos] = L' '; |
| 305 pos = sanitized.find_first_of(kReservedCharacters, pos + 1); |
| 306 } |
| 307 |
| 308 TrimWhitespace(sanitized, TRIM_LEADING, &sanitized); |
| 309 if (sanitized.size() > kMaxProfileShortcutFileNameLength) |
| 310 sanitized.erase(kMaxProfileShortcutFileNameLength); |
| 311 TrimWhitespace(sanitized, TRIM_TRAILING, &sanitized); |
| 312 |
| 313 return sanitized; |
| 314 } |
| 315 |
281 } // namespace | 316 } // namespace |
282 | 317 |
283 namespace profiles { | 318 namespace profiles { |
284 namespace internal { | 319 namespace internal { |
285 | 320 |
286 const char kProfileIconFileName[] = "Google Profile.ico"; | 321 const char kProfileIconFileName[] = "Google Profile.ico"; |
287 | 322 |
288 string16 GetShortcutFilenameForProfile(const string16& profile_name, | 323 string16 GetShortcutFilenameForProfile(const string16& profile_name, |
289 BrowserDistribution* distribution) { | 324 BrowserDistribution* distribution) { |
290 string16 shortcut_name; | 325 string16 shortcut_name; |
291 if (!profile_name.empty()) { | 326 if (!profile_name.empty()) { |
292 shortcut_name.append(profile_name); | 327 shortcut_name.append(SanitizeShortcutProfileNameString(profile_name)); |
293 shortcut_name.append(L" - "); | 328 shortcut_name.append(L" - "); |
294 } | 329 } |
295 shortcut_name.append(distribution->GetAppShortCutName()); | 330 shortcut_name.append(distribution->GetAppShortCutName()); |
296 return shortcut_name + installer::kLnkExt; | 331 return shortcut_name + installer::kLnkExt; |
297 } | 332 } |
298 | 333 |
299 string16 CreateProfileShortcutFlags(const FilePath& profile_path) { | 334 string16 CreateProfileShortcutFlags(const FilePath& profile_path) { |
300 return base::StringPrintf(L"--%ls=\"%ls\"", | 335 return base::StringPrintf(L"--%ls=\"%ls\"", |
301 ASCIIToUTF16(switches::kProfileDirectory).c_str(), | 336 ASCIIToUTF16(switches::kProfileDirectory).c_str(), |
302 profile_path.BaseName().value().c_str()); | 337 profile_path.BaseName().value().c_str()); |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
459 } | 494 } |
460 BrowserThread::PostTask( | 495 BrowserThread::PostTask( |
461 BrowserThread::FILE, FROM_HERE, | 496 BrowserThread::FILE, FROM_HERE, |
462 base::Bind(&CreateOrUpdateDesktopShortcutsForProfile, | 497 base::Bind(&CreateOrUpdateDesktopShortcutsForProfile, |
463 profile_path, new_shortcut_appended_name, | 498 profile_path, new_shortcut_appended_name, |
464 profile_avatar_bitmap_copy, create_mode, action)); | 499 profile_avatar_bitmap_copy, create_mode, action)); |
465 | 500 |
466 cache->SetShortcutNameOfProfileAtIndex(profile_index, | 501 cache->SetShortcutNameOfProfileAtIndex(profile_index, |
467 new_shortcut_appended_name); | 502 new_shortcut_appended_name); |
468 } | 503 } |
OLD | NEW |