15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/profiles/profile_shortcut_manager_win.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <shlobj.h>  // For SHChangeNotify().
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string>
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <vector>
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/files/file_enumerator.h"
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/path_service.h"
17ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "base/prefs/pref_service.h"
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string16.h"
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_util.h"
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/stringprintf.h"
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/win/shortcut.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/app_icon_win.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/browser_process.h"
25ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "chrome/browser/chrome_notification_types.h"
26c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "chrome/browser/profiles/profile_avatar_icon_util.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile_info_cache_observer.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile_manager.h"
2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/browser/shell_integration.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
31ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "chrome/common/pref_names.h"
3203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "chrome/grit/chromium_strings.h"
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/util/browser_distribution.h"
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/util/product.h"
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/util/shell_util.h"
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_thread.h"
37ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "content/public/browser/notification_service.h"
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "grit/chrome_unscaled_resources.h"
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "skia/ext/image_operations.h"
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "skia/ext/platform_canvas.h"
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/resource/resource_bundle.h"
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/icon_util.h"
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/image/image.h"
45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/gfx/image/image_family.h"
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::BrowserThread;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
52ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch// Name of the badged icon file generated for a given profile.
53ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochconst char kProfileIconFileName[] = "Google Profile.ico";
54ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Characters that are not allowed in Windows filenames. Taken from
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// http://msdn.microsoft.com/en-us/library/aa365247.aspx
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const base::char16 kReservedCharacters[] = L"<>:\"/\\|?*\x01\x02\x03\x04\x05"
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    L"\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17"
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    L"\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F";
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// The maximum number of characters allowed in profile shortcuts' file names.
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Warning: migration code will be needed if this is changed later, since
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// existing shortcuts might no longer be found if the name is generated
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// differently than it was when a shortcut was originally created.
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const int kMaxProfileShortcutFileNameLength = 64;
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
674e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// The avatar badge size needs to be half of the shortcut icon size because
684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// the Windows taskbar icon is 32x32 and the avatar icon overlay is 16x16. So to
694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// get the shortcut avatar badge and the avatar icon overlay to match up, we
704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// need to preserve those ratios when creating the shortcut icon.
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int kShortcutIconSize = 48;
724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)const int kProfileAvatarBadgeSize = kShortcutIconSize / 2;
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
746e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)const int kCurrentProfileIconVersion = 3;
75ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 2x sized profile avatar icons. Mirrors |kDefaultAvatarIconResources| in
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// profile_info_cache.cc.
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const int kProfileAvatarIconResources2x[] = {
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_0,
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_1,
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_2,
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_3,
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_4,
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_5,
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_6,
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_7,
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_8,
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_9,
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_10,
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_11,
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_12,
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_13,
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_14,
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_15,
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_16,
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_17,
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_18,
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_19,
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_20,
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_21,
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_22,
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_23,
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_24,
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IDR_PROFILE_AVATAR_2X_25,
1050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  IDR_PROFILE_AVATAR_2X_26,
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Badges |app_icon_bitmap| with |avatar_bitmap| at the bottom right corner and
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// returns the resulting SkBitmap.
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)SkBitmap BadgeIcon(const SkBitmap& app_icon_bitmap,
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   const SkBitmap& avatar_bitmap,
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   int scale_factor) {
113cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SkBitmap source_bitmap =
114cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      profiles::GetAvatarIconAsSquare(avatar_bitmap, scale_factor);
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int avatar_badge_size = kProfileAvatarBadgeSize;
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (app_icon_bitmap.width() != kShortcutIconSize) {
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    avatar_badge_size =
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        app_icon_bitmap.width() * kProfileAvatarBadgeSize / kShortcutIconSize;
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SkBitmap sk_icon = skia::ImageOperations::Resize(
121cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      source_bitmap, skia::ImageOperations::RESIZE_LANCZOS3, avatar_badge_size,
122cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      source_bitmap.height() * avatar_badge_size / source_bitmap.width());
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overlay the avatar on the icon, anchoring it to the bottom-right of the
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // icon.
126a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SkBitmap badged_bitmap;
127a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  badged_bitmap.allocN32Pixels(app_icon_bitmap.width(),
128a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                               app_icon_bitmap.height());
129a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SkCanvas offscreen_canvas(badged_bitmap);
130a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  offscreen_canvas.clear(SK_ColorTRANSPARENT);
131a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
132a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  offscreen_canvas.drawBitmap(app_icon_bitmap, 0, 0);
133a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  offscreen_canvas.drawBitmap(sk_icon,
134a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                              app_icon_bitmap.width() - sk_icon.width(),
135a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                              app_icon_bitmap.height() - sk_icon.height());
136a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return badged_bitmap;
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Updates the preferences with the current icon version on icon creation
1400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// success.
1410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void OnProfileIconCreateSuccess(base::FilePath profile_path) {
1420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (!g_browser_process->profile_manager())
1440f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return;
1450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  Profile* profile =
1460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      g_browser_process->profile_manager()->GetProfileByPath(profile_path);
1470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (profile) {
1480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    profile->GetPrefs()->SetInteger(prefs::kProfileIconVersion,
1490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                                    kCurrentProfileIconVersion);
1500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
1510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
1520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Creates a desktop shortcut icon file (.ico) on the disk for a given profile,
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// badging the browser distribution icon with the profile avatar.
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Returns a path to the shortcut icon file on disk, which is empty if this
156ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch// fails. Use index 0 when assigning the resulting file as the icon. If both
157ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch// given bitmaps are empty, an unbadged icon is created.
158ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch// Returns the path to the created icon on success and an empty base::FilePath
159ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch// on failure.
160ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch// TODO(calamity): Ideally we'd just copy the app icon verbatim from the exe's
161ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch// resources in the case of an unbadged icon.
162ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochbase::FilePath CreateOrUpdateShortcutIconForProfile(
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& profile_path,
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const SkBitmap& avatar_bitmap_1x,
1650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    const SkBitmap& avatar_bitmap_2x) {
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
167ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
168ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  if (!base::PathExists(profile_path)) {
169ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    LOG(ERROR) << "Profile directory " << profile_path.value()
170ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch               << " did not exist when trying to create profile icon";
171ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    return base::FilePath();
172ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  }
173ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<SkBitmap> app_icon_bitmap(GetAppIconForSize(kShortcutIconSize));
175b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  if (!app_icon_bitmap)
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return base::FilePath();
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
178c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  gfx::ImageFamily badged_bitmaps;
179ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  if (!avatar_bitmap_1x.empty()) {
180ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    badged_bitmaps.Add(gfx::Image::CreateFrom1xBitmap(
181ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch        BadgeIcon(*app_icon_bitmap, avatar_bitmap_1x, 1)));
182ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  }
1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
184ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  scoped_ptr<SkBitmap> large_app_icon_bitmap(
185ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      GetAppIconForSize(IconUtil::kLargeIconSize));
186ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  if (large_app_icon_bitmap && !avatar_bitmap_2x.empty()) {
187c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    badged_bitmaps.Add(gfx::Image::CreateFrom1xBitmap(
188ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch        BadgeIcon(*large_app_icon_bitmap, avatar_bitmap_2x, 2)));
189c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
191ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  // If we have no badged bitmaps, we should just use the default chrome icon.
192ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  if (badged_bitmaps.empty()) {
193ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    badged_bitmaps.Add(gfx::Image::CreateFrom1xBitmap(*app_icon_bitmap));
194ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    if (large_app_icon_bitmap) {
195ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      badged_bitmaps.Add(
196ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch          gfx::Image::CreateFrom1xBitmap(*large_app_icon_bitmap));
197ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    }
198ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  }
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Finally, write the .ico file containing this new bitmap.
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const base::FilePath icon_path =
201ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      profiles::internal::GetProfileIconPath(profile_path);
202ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  const bool had_icon = base::PathExists(icon_path);
203ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
204ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  if (!IconUtil::CreateIconFileFromImageFamily(badged_bitmaps, icon_path)) {
205ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // This can happen in theory if the profile directory is deleted between the
206ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // beginning of this function and here; however this is extremely unlikely
207ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // and this check will help catch any regression where this call would start
208ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // failing constantly.
209ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    NOTREACHED();
2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return base::FilePath();
211ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  }
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
213ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  if (had_icon) {
214ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // This invalidates the Windows icon cache and causes the icon changes to
215ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // register with the taskbar and desktop. SHCNE_ASSOCCHANGED will cause a
216ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // desktop flash and we would like to avoid that if possible.
217ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);
218ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  } else {
219ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    SHChangeNotify(SHCNE_CREATE, SHCNF_PATH, icon_path.value().c_str(), NULL);
220ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  }
2210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  BrowserThread::PostTask(
2220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      BrowserThread::UI, FROM_HERE,
2230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      base::Bind(&OnProfileIconCreateSuccess, profile_path));
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return icon_path;
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Gets the user and system directories for desktop shortcuts. Parameters may
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// be NULL if a directory type is not needed. Returns true on success.
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool GetDesktopShortcutsDirectories(
2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::FilePath* user_shortcuts_directory,
2312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::FilePath* system_shortcuts_directory) {
2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (user_shortcuts_directory &&
2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      !ShellUtil::GetShortcutPath(ShellUtil::SHORTCUT_LOCATION_DESKTOP,
2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                  distribution, ShellUtil::CURRENT_USER,
2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                  user_shortcuts_directory)) {
2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    NOTREACHED();
2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
2392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (system_shortcuts_directory &&
2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      !ShellUtil::GetShortcutPath(ShellUtil::SHORTCUT_LOCATION_DESKTOP,
2422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                  distribution, ShellUtil::SYSTEM_LEVEL,
2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                  system_shortcuts_directory)) {
2442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    NOTREACHED();
2452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
2462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return true;
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Returns the long form of |path|, which will expand any shortened components
2512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// like "foo~2" to their full names.
2522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)base::FilePath ConvertToLongPath(const base::FilePath& path) {
2532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const size_t length = GetLongPathName(path.value().c_str(), NULL, 0);
2542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (length != 0 && length != path.value().length()) {
2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::vector<wchar_t> long_path(length);
2562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (GetLongPathName(path.value().c_str(), &long_path[0], length) != 0)
2572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return base::FilePath(&long_path[0]);
2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return path;
2602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Returns true if the file at |path| is a Chrome shortcut and returns its
2632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// command line in output parameter |command_line|.
2642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool IsChromeShortcut(const base::FilePath& path,
2652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                      const base::FilePath& chrome_exe,
266a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                      base::string16* command_line) {
2672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
2682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (path.Extension() != installer::kLnkExt)
2702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
2712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath target_path;
2732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!base::win::ResolveShortcut(path, &target_path, command_line))
2742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // One of the paths may be in short (elided) form. Compare long paths to
2762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // ensure these are still properly matched.
2772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return ConvertToLongPath(target_path) == ConvertToLongPath(chrome_exe);
2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Populates |paths| with the file paths of Chrome desktop shortcuts that have
2812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// the specified |command_line|. If |include_empty_command_lines| is true,
2822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Chrome desktop shortcuts with empty command lines will also be included.
2832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ListDesktopShortcutsWithCommandLine(const base::FilePath& chrome_exe,
284a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                         const base::string16& command_line,
2852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                         bool include_empty_command_lines,
2862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                         std::vector<base::FilePath>* paths) {
2872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath user_shortcuts_directory;
2882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!GetDesktopShortcutsDirectories(&user_shortcuts_directory, NULL))
2892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
291868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::FileEnumerator enumerator(user_shortcuts_directory, false,
292868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                  base::FileEnumerator::FILES);
2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (base::FilePath path = enumerator.Next(); !path.empty();
2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       path = enumerator.Next()) {
295a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    base::string16 shortcut_command_line;
2962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!IsChromeShortcut(path, chrome_exe, &shortcut_command_line))
2972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      continue;
2982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // TODO(asvitkine): Change this to build a CommandLine object and ensure all
3002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // args from |command_line| are present in the shortcut's CommandLine. This
3012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // will be more robust when |command_line| contains multiple args.
3022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if ((shortcut_command_line.empty() && include_empty_command_lines) ||
303a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        (shortcut_command_line.find(command_line) != base::string16::npos)) {
3042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      paths->push_back(path);
3052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
3062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
309c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Renames the given desktop shortcut and informs the shell of this change.
310c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool RenameDesktopShortcut(const base::FilePath& old_shortcut_path,
311c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                           const base::FilePath& new_shortcut_path) {
312eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!base::Move(old_shortcut_path, new_shortcut_path))
313c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return false;
314c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
315c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Notify the shell of the rename, which allows the icon to keep its position
316c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // on the desktop when renamed. Note: This only works if either SHCNF_FLUSH or
317c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // SHCNF_FLUSHNOWAIT is specified as a flag.
318c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_PATH | SHCNF_FLUSHNOWAIT,
319c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                 old_shortcut_path.value().c_str(),
320c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                 new_shortcut_path.value().c_str());
321c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return true;
322c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
323c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Renames an existing Chrome desktop profile shortcut. Must be called on the
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// FILE thread.
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void RenameChromeDesktopShortcutForProfile(
327a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& old_shortcut_filename,
328a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& new_shortcut_filename) {
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
3302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath user_shortcuts_directory;
3322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath system_shortcuts_directory;
3332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!GetDesktopShortcutsDirectories(&user_shortcuts_directory,
3342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                      &system_shortcuts_directory)) {
3352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
3362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const base::FilePath old_shortcut_path =
3392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      user_shortcuts_directory.Append(old_shortcut_filename);
3402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const base::FilePath new_shortcut_path =
3412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      user_shortcuts_directory.Append(new_shortcut_filename);
3422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3437dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (base::PathExists(old_shortcut_path)) {
3442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Rename the old shortcut unless a system-level shortcut exists at the
3452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // destination, in which case the old shortcut is simply deleted.
3462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath possible_new_system_shortcut =
3472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        system_shortcuts_directory.Append(new_shortcut_filename);
3487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    if (base::PathExists(possible_new_system_shortcut))
3497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      base::DeleteFile(old_shortcut_path, false);
350c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    else if (!RenameDesktopShortcut(old_shortcut_path, new_shortcut_path))
3512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      DLOG(ERROR) << "Could not rename Windows profile desktop shortcut.";
3522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // If the shortcut does not exist, it may have been renamed by the user. In
3542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // that case, its name should not be changed.
3552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // It's also possible that a system-level shortcut exists instead - this
3562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // should only be the case for the original Chrome shortcut from an
3572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // installation. If that's the case, copy that one over - it will get its
358ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // properties updated by
359ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // |CreateOrUpdateDesktopShortcutsAndIconForProfile()|.
3602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath possible_old_system_shortcut =
3612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        system_shortcuts_directory.Append(old_shortcut_filename);
3627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    if (base::PathExists(possible_old_system_shortcut))
3637dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      base::CopyFile(possible_old_system_shortcut, new_shortcut_path);
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
367ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochstruct CreateOrUpdateShortcutsParams {
368ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  CreateOrUpdateShortcutsParams(
369ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      base::FilePath profile_path,
370ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      ProfileShortcutManagerWin::CreateOrUpdateMode create_mode,
371ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      ProfileShortcutManagerWin::NonProfileShortcutAction action)
372ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      : profile_path(profile_path), create_mode(create_mode), action(action) {}
373ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  ~CreateOrUpdateShortcutsParams() {}
374ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
375ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  ProfileShortcutManagerWin::CreateOrUpdateMode create_mode;
376ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  ProfileShortcutManagerWin::NonProfileShortcutAction action;
377ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
378ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  // The path for this profile.
379ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  base::FilePath profile_path;
380ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  // The profile name before this update. Empty on create.
381a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 old_profile_name;
382ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  // The new profile name.
383a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 profile_name;
384ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  // Avatar images for this profile.
385ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  SkBitmap avatar_image_1x;
386ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  SkBitmap avatar_image_2x;
387ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch};
388ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
3892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Updates all desktop shortcuts for the given profile to have the specified
390ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch// parameters. If |params.create_mode| is CREATE_WHEN_NONE_FOUND, a new shortcut
391ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch// is created if no existing ones were found. Whether non-profile shortcuts
392ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch// should be updated is specified by |params.action|. Must be called on the FILE
3930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// thread.
394ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochvoid CreateOrUpdateDesktopShortcutsAndIconForProfile(
3950f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    const CreateOrUpdateShortcutsParams& params) {
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
398ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  const base::FilePath shortcut_icon =
399ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      CreateOrUpdateShortcutIconForProfile(params.profile_path,
400ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                                           params.avatar_image_1x,
4010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                                           params.avatar_image_2x);
402ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  if (shortcut_icon.empty() ||
403ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      params.create_mode ==
404ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch          ProfileShortcutManagerWin::CREATE_OR_UPDATE_ICON_ONLY) {
405ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    return;
406ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  }
407ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
4082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath chrome_exe;
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NOTREACHED();
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
4152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Ensure that the distribution supports creating shortcuts. If it doesn't,
4162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // the following code may result in NOTREACHED() being hit.
4172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(distribution->CanCreateDesktopShortcuts());
4182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
419ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  if (params.old_profile_name != params.profile_name) {
420a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16 old_shortcut_filename =
421ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch        profiles::internal::GetShortcutFilenameForProfile(
422ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch            params.old_profile_name,
423ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch            distribution);
424a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16 new_shortcut_filename =
425ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch        profiles::internal::GetShortcutFilenameForProfile(params.profile_name,
4262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                          distribution);
4272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    RenameChromeDesktopShortcutForProfile(old_shortcut_filename,
4282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                          new_shortcut_filename);
4292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellUtil::ShortcutProperties properties(ShellUtil::CURRENT_USER);
4322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  installer::Product product(distribution);
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  product.AddDefaultShortcutProperties(chrome_exe, &properties);
4342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
435a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  const base::string16 command_line =
436ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      profiles::internal::CreateProfileShortcutFlags(params.profile_path);
4372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Only set the profile-specific properties when |profile_name| is non empty.
4392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If it is empty, it means the shortcut being created should be a regular,
4402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // non-profile Chrome shortcut.
441ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  if (!params.profile_name.empty()) {
4422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    properties.set_arguments(command_line);
443ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    properties.set_icon(shortcut_icon, 0);
4442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
4452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Set the arguments explicitly to the empty string to ensure that
4462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // |ShellUtil::CreateOrUpdateShortcut| updates that part of the shortcut.
4475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    properties.set_arguments(base::string16());
4482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
45090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  properties.set_app_id(
451ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      ShellIntegration::GetChromiumModelIdForProfile(params.profile_path));
45290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellUtil::ShortcutOperation operation =
4542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ShellUtil::SHELL_SHORTCUT_REPLACE_EXISTING;
4552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<base::FilePath> shortcuts;
4572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ListDesktopShortcutsWithCommandLine(chrome_exe, command_line,
458ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      params.action == ProfileShortcutManagerWin::UPDATE_NON_PROFILE_SHORTCUTS,
4592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      &shortcuts);
460ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  if (params.create_mode == ProfileShortcutManagerWin::CREATE_WHEN_NONE_FOUND &&
4612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shortcuts.empty()) {
462a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16 shortcut_name =
463ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch        profiles::internal::GetShortcutFilenameForProfile(params.profile_name,
4642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                          distribution);
4652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    shortcuts.push_back(base::FilePath(shortcut_name));
4662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    operation = ShellUtil::SHELL_SHORTCUT_CREATE_IF_NO_SYSTEM_LEVEL;
4672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < shortcuts.size(); ++i) {
4702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath shortcut_name =
4712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        shortcuts[i].BaseName().RemoveExtension();
4722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    properties.set_shortcut_name(shortcut_name.value());
4732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ShellUtil::CreateOrUpdateShortcut(ShellUtil::SHORTCUT_LOCATION_DESKTOP,
4742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        distribution, properties, operation);
4752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Returns true if any desktop shortcuts exist with target |chrome_exe|,
4792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// regardless of their command line arguments.
4802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool ChromeDesktopShortcutsExist(const base::FilePath& chrome_exe) {
4812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath user_shortcuts_directory;
4822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!GetDesktopShortcutsDirectories(&user_shortcuts_directory, NULL))
4832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
4842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
485868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::FileEnumerator enumerator(user_shortcuts_directory, false,
486868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                  base::FileEnumerator::FILES);
4872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (base::FilePath path = enumerator.Next(); !path.empty();
4882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       path = enumerator.Next()) {
4892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (IsChromeShortcut(path, chrome_exe, NULL))
4902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return true;
4912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
4942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
496ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch// Deletes all desktop shortcuts for the specified profile. If
497ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch// |ensure_shortcuts_remain| is true, then a regular non-profile shortcut will
498ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch// be created if this function would otherwise delete the last Chrome desktop
499ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch// shortcut(s). Must be called on the FILE thread.
500ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochvoid DeleteDesktopShortcuts(const base::FilePath& profile_path,
501ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                            bool ensure_shortcuts_remain) {
5022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
5032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath chrome_exe;
5052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
5062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    NOTREACHED();
5072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
5082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
5092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
510a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  const base::string16 command_line =
5112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      profiles::internal::CreateProfileShortcutFlags(profile_path);
5122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<base::FilePath> shortcuts;
5132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, false,
5142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                      &shortcuts);
5152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < shortcuts.size(); ++i) {
5170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    // Use base::DeleteFile() instead of ShellUtil::RemoveShortcuts(), as the
5180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    // latter causes non-profile taskbar shortcuts to be removed since it
5190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    // doesn't consider the command-line of the shortcuts it deletes.
5200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    // TODO(huangs): Refactor with ShellUtil::RemoveShortcuts().
5210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    base::win::TaskbarUnpinShortcutLink(shortcuts[i].value().c_str());
5227dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    base::DeleteFile(shortcuts[i], false);
5232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Notify the shell that the shortcut was deleted to ensure desktop refresh.
5242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SHChangeNotify(SHCNE_DELETE, SHCNF_PATH, shortcuts[i].value().c_str(),
5252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   NULL);
5262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
5272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If |ensure_shortcuts_remain| is true and deleting this profile caused the
5292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // last shortcuts to be removed, re-create a regular non-profile shortcut.
5302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const bool had_shortcuts = !shortcuts.empty();
5312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (ensure_shortcuts_remain && had_shortcuts &&
5322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      !ChromeDesktopShortcutsExist(chrome_exe)) {
5332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
5342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Ensure that the distribution supports creating shortcuts. If it doesn't,
5352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // the following code may result in NOTREACHED() being hit.
5362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DCHECK(distribution->CanCreateDesktopShortcuts());
5372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    installer::Product product(distribution);
5382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ShellUtil::ShortcutProperties properties(ShellUtil::CURRENT_USER);
5402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    product.AddDefaultShortcutProperties(chrome_exe, &properties);
5412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    properties.set_shortcut_name(
5425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        profiles::internal::GetShortcutFilenameForProfile(base::string16(),
5432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                          distribution));
5442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ShellUtil::CreateOrUpdateShortcut(
5452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        ShellUtil::SHORTCUT_LOCATION_DESKTOP, distribution, properties,
5462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        ShellUtil::SHELL_SHORTCUT_CREATE_IF_NO_SYSTEM_LEVEL);
5472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
5482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Returns true if profile at |profile_path| has any shortcuts. Does not
5512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// consider non-profile shortcuts. Must be called on the FILE thread.
5522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool HasAnyProfileShortcuts(const base::FilePath& profile_path) {
5532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
5542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath chrome_exe;
5562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
5572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    NOTREACHED();
5582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
5592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
5602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
561a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  const base::string16 command_line =
5622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      profiles::internal::CreateProfileShortcutFlags(profile_path);
5632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<base::FilePath> shortcuts;
5642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, false,
5652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                      &shortcuts);
5662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return !shortcuts.empty();
5672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Replaces any reserved characters with spaces, and trims the resulting string
5702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// to prevent any leading and trailing spaces. Also makes sure that the
5712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// resulting filename doesn't exceed |kMaxProfileShortcutFileNameLength|.
5722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// TODO(macourteau): find a way to limit the total path's length to MAX_PATH
5732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// instead of limiting the profile's name to |kMaxProfileShortcutFileNameLength|
5742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// characters.
5755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::string16 SanitizeShortcutProfileNameString(
5765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::string16& profile_name) {
577a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 sanitized = profile_name;
5782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  size_t pos = sanitized.find_first_of(kReservedCharacters);
579a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  while (pos != base::string16::npos) {
5802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    sanitized[pos] = L' ';
5812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    pos = sanitized.find_first_of(kReservedCharacters, pos + 1);
5822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
5832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
584a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::TrimWhitespace(sanitized, base::TRIM_LEADING, &sanitized);
5852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (sanitized.size() > kMaxProfileShortcutFileNameLength)
5862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    sanitized.erase(kMaxProfileShortcutFileNameLength);
587a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::TrimWhitespace(sanitized, base::TRIM_TRAILING, &sanitized);
5882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return sanitized;
5902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5926e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// Returns a copied SkBitmap for the given image that can be safely passed to
5936e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// another thread.
5946e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)SkBitmap GetSkBitmapCopy(const gfx::Image& image) {
5952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(!image.IsEmpty());
5962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const SkBitmap* image_bitmap = image.ToSkBitmap();
5972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SkBitmap bitmap_copy;
598a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  image_bitmap->deepCopyTo(&bitmap_copy);
5992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return bitmap_copy;
6005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6026e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// Returns a copied SkBitmap for the given resource id that can be safely passed
6036e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// to another thread.
6046e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)SkBitmap GetImageResourceSkBitmapCopy(int resource_id) {
6056e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  const gfx::Image image =
6066e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      ResourceBundle::GetSharedInstance().GetNativeImageNamed(resource_id);
6076e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return GetSkBitmapCopy(image);
6086e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
6096e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
6105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
6115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace profiles {
6132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace internal {
6142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
615ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochbase::FilePath GetProfileIconPath(const base::FilePath& profile_path) {
616ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  return profile_path.AppendASCII(kProfileIconFileName);
617ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch}
6182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::string16 GetShortcutFilenameForProfile(
6205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::string16& profile_name,
6215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    BrowserDistribution* distribution) {
622a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 shortcut_name;
6232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!profile_name.empty()) {
6242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    shortcut_name.append(SanitizeShortcutProfileNameString(profile_name));
6252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    shortcut_name.append(L" - ");
6262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    shortcut_name.append(l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME));
6272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
6283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    shortcut_name.append(
6293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        distribution->GetShortcutName(BrowserDistribution::SHORTCUT_CHROME));
6302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
6312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return shortcut_name + installer::kLnkExt;
6322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::string16 CreateProfileShortcutFlags(const base::FilePath& profile_path) {
6352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return base::StringPrintf(L"--%ls=\"%ls\"",
6365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                            base::ASCIIToUTF16(
6375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                switches::kProfileDirectory).c_str(),
6382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                            profile_path.BaseName().value().c_str());
6392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace internal
6422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace profiles
6435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
6455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ProfileShortcutManager::IsFeatureEnabled() {
6460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  CommandLine* command_line = CommandLine::ForCurrentProcess();
6470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  return command_line->HasSwitch(switches::kEnableProfileShortcutManager) ||
6480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)         (BrowserDistribution::GetDistribution()->CanCreateDesktopShortcuts() &&
6490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)          !command_line->HasSwitch(switches::kUserDataDir));
6505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
6535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileShortcutManager* ProfileShortcutManager::Create(
6545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ProfileManager* manager) {
6555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return new ProfileShortcutManagerWin(manager);
6565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileShortcutManagerWin::ProfileShortcutManagerWin(ProfileManager* manager)
6595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : profile_manager_(manager) {
6602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_EQ(
6612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      arraysize(kProfileAvatarIconResources2x),
662c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      profiles::GetDefaultAvatarIconCount());
6632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
664ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED,
665ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                 content::NotificationService::AllSources());
666ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
6672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  profile_manager_->GetProfileInfoCache().AddObserver(this);
6685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileShortcutManagerWin::~ProfileShortcutManagerWin() {
6712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  profile_manager_->GetProfileInfoCache().RemoveObserver(this);
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
674ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochvoid ProfileShortcutManagerWin::CreateOrUpdateProfileIcon(
6750f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    const base::FilePath& profile_path) {
676ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  CreateOrUpdateShortcutsForProfileAtPath(profile_path,
677ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                                          CREATE_OR_UPDATE_ICON_ONLY,
6780f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                                          IGNORE_NON_PROFILE_SHORTCUTS);
679ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch}
680ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
6815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileShortcutManagerWin::CreateProfileShortcut(
6822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& profile_path) {
6832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  CreateOrUpdateShortcutsForProfileAtPath(profile_path, CREATE_WHEN_NONE_FOUND,
6840f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                                          IGNORE_NON_PROFILE_SHORTCUTS);
6852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ProfileShortcutManagerWin::RemoveProfileShortcuts(
6882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& profile_path) {
6892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BrowserThread::PostTask(
6902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      BrowserThread::FILE, FROM_HERE,
691ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      base::Bind(&DeleteDesktopShortcuts, profile_path, false));
6922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ProfileShortcutManagerWin::HasProfileShortcuts(
6952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& profile_path,
6962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::Callback<void(bool)>& callback) {
6972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BrowserThread::PostTaskAndReplyWithResult(
6982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      BrowserThread::FILE, FROM_HERE,
6992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&HasAnyProfileShortcuts, profile_path), callback);
7005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void ProfileShortcutManagerWin::GetShortcutProperties(
7030f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    const base::FilePath& profile_path,
7040f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    CommandLine* command_line,
705a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    base::string16* name,
7060f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    base::FilePath* icon_path) {
7070f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  base::FilePath chrome_exe;
7080f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
7090f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    NOTREACHED();
7100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return;
7110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
7120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
7130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
7140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  size_t profile_index = cache.GetIndexOfProfileWithPath(profile_path);
7150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  DCHECK_LT(profile_index, cache.GetNumberOfProfiles());
7160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
7170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // The used profile name should be empty if there is only 1 profile.
7180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  base::string16 shortcut_profile_name;
7190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (cache.GetNumberOfProfiles() > 1)
7200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    shortcut_profile_name = cache.GetNameOfProfileAtIndex(profile_index);
7210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
7220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  *name = base::FilePath(profiles::internal::GetShortcutFilenameForProfile(
7230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      shortcut_profile_name,
7240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      BrowserDistribution::GetDistribution())).RemoveExtension().value();
7250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
7260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  command_line->ParseFromString(L"\"" + chrome_exe.value() + L"\" " +
7270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      profiles::internal::CreateProfileShortcutFlags(profile_path));
7280f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
7290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  *icon_path = profiles::internal::GetProfileIconPath(profile_path);
7300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
7310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
7322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ProfileShortcutManagerWin::OnProfileAdded(
7332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& profile_path) {
7340f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  CreateOrUpdateProfileIcon(profile_path);
73590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (profile_manager_->GetProfileInfoCache().GetNumberOfProfiles() == 2) {
73690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // When the second profile is added, make existing non-profile shortcuts
73790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // point to the first profile and be badged/named appropriately.
7382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    CreateOrUpdateShortcutsForProfileAtPath(GetOtherProfilePath(profile_path),
7392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                            UPDATE_EXISTING_ONLY,
7400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                                            UPDATE_NON_PROFILE_SHORTCUTS);
7415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileShortcutManagerWin::OnProfileWasRemoved(
7452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& profile_path,
746a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& profile_name) {
7472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
7485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If there is only one profile remaining, remove the badging information
7495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // from an existing shortcut.
7502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const bool deleting_down_to_last_profile = (cache.GetNumberOfProfiles() == 1);
7512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (deleting_down_to_last_profile) {
752ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // This is needed to unbadge the icon.
7532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    CreateOrUpdateShortcutsForProfileAtPath(cache.GetPathOfProfileAtIndex(0),
7542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                            UPDATE_EXISTING_ONLY,
7550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                                            IGNORE_NON_PROFILE_SHORTCUTS);
7565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
759ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                          base::Bind(&DeleteDesktopShortcuts,
7602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                     profile_path,
7612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                     deleting_down_to_last_profile));
7625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileShortcutManagerWin::OnProfileNameChanged(
7652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& profile_path,
766a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& old_profile_name) {
7672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  CreateOrUpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY,
7680f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                                          IGNORE_NON_PROFILE_SHORTCUTS);
7695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileShortcutManagerWin::OnProfileAvatarChanged(
7722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& profile_path) {
7730f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  CreateOrUpdateProfileIcon(profile_path);
7745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)base::FilePath ProfileShortcutManagerWin::GetOtherProfilePath(
7772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& profile_path) {
7782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
7792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_EQ(2U, cache.GetNumberOfProfiles());
7805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the index of the current profile, in order to find the index of the
7815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // other profile.
7822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  size_t current_profile_index = cache.GetIndexOfProfileWithPath(profile_path);
7835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t other_profile_index = (current_profile_index == 0) ? 1 : 0;
7842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return cache.GetPathOfProfileAtIndex(other_profile_index);
7855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ProfileShortcutManagerWin::CreateOrUpdateShortcutsForProfileAtPath(
7882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& profile_path,
7892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    CreateOrUpdateMode create_mode,
7900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    NonProfileShortcutAction action) {
791ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
792ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch         BrowserThread::CurrentlyOn(BrowserThread::UI));
793ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  CreateOrUpdateShortcutsParams params(profile_path, create_mode, action);
794ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
7952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ProfileInfoCache* cache = &profile_manager_->GetProfileInfoCache();
7962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  size_t profile_index = cache->GetIndexOfProfileWithPath(profile_path);
7975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (profile_index == std::string::npos)
7982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
7992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool remove_badging = cache->GetNumberOfProfiles() == 1;
8005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
801ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  params.old_profile_name =
8022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      cache->GetShortcutNameOfProfileAtIndex(profile_index);
8032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Exit early if the mode is to update existing profile shortcuts only and
8052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // none were ever created for this profile, per the shortcut name not being
8062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // set in the profile info cache.
807ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  if (params.old_profile_name.empty() &&
8082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      create_mode == UPDATE_EXISTING_ONLY &&
8092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      action == IGNORE_NON_PROFILE_SHORTCUTS) {
8102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
8115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!remove_badging) {
814ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    params.profile_name = cache->GetNameOfProfileAtIndex(profile_index);
815ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
8166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    // The profile might be using the Gaia avatar, which is not in the
8176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    // resources array.
8186e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    bool has_gaia_image = false;
8196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    if (cache->IsUsingGAIAPictureOfProfileAtIndex(profile_index)) {
8206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      const gfx::Image* image =
8216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          cache->GetGAIAPictureOfProfileAtIndex(profile_index);
8226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      if (image) {
8236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        params.avatar_image_1x = GetSkBitmapCopy(*image);
8246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        // Gaia images are 256px, which makes them big enough to use in the
8256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        // large icon case as well.
8266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        DCHECK_GE(image->Width(), IconUtil::kLargeIconSize);
8276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        params.avatar_image_2x = params.avatar_image_1x;
8286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        has_gaia_image = true;
8296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      }
8306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    }
8316e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
8326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    // If the profile isn't using a Gaia image, or if the Gaia image did not
8336e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    // exist, revert to the previously used avatar icon.
8346e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    if (!has_gaia_image) {
8356e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      const size_t icon_index =
8366e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          cache->GetAvatarIconIndexOfProfileAtIndex(profile_index);
8376e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      const int resource_id_1x =
8386e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          profiles::GetDefaultAvatarIconResourceIDAtIndex(icon_index);
8396e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      const int resource_id_2x = kProfileAvatarIconResources2x[icon_index];
8406e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      // Make a copy of the SkBitmaps to ensure that we can safely use the image
8416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      // data on the FILE thread.
8426e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      params.avatar_image_1x = GetImageResourceSkBitmapCopy(resource_id_1x);
8436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      params.avatar_image_2x = GetImageResourceSkBitmapCopy(resource_id_2x);
8446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    }
8455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  BrowserThread::PostTask(
8475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      BrowserThread::FILE, FROM_HERE,
8480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      base::Bind(&CreateOrUpdateDesktopShortcutsAndIconForProfile, params));
8495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  cache->SetShortcutNameOfProfileAtIndex(profile_index,
851ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                                         params.profile_name);
852ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch}
853ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
854ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochvoid ProfileShortcutManagerWin::Observe(
855ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    int type,
856ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    const content::NotificationSource& source,
857ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    const content::NotificationDetails& details) {
858ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  switch (type) {
859ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    // This notification is triggered when a profile is loaded.
860ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    case chrome::NOTIFICATION_PROFILE_CREATED: {
861ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      Profile* profile =
862ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch          content::Source<Profile>(source).ptr()->GetOriginalProfile();
863ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      if (profile->GetPrefs()->GetInteger(prefs::kProfileIconVersion) <
864ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch          kCurrentProfileIconVersion) {
865ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch        // Ensure the profile's icon file has been created.
8660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        CreateOrUpdateProfileIcon(profile->GetPath());
867ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      }
868ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      break;
869ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    }
870ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    default:
871ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      NOTREACHED();
872ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      break;
873ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  }
8745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
875