web_app_win.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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)
5eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/browser/web_applications/web_app_win.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <shlobj.h>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
10eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/files/file_enumerator.h"
111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/md5.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/path_service.h"
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/strings/string_piece.h"
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/stringprintf.h"
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/win/shortcut.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/win/windows_version.h"
20c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "chrome/browser/web_applications/update_shortcut_worker_win.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
22a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "chrome/installer/util/browser_distribution.h"
23a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "chrome/installer/util/shell_util.h"
24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/installer/util/util_constants.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_thread.h"
26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "ui/base/win/shell.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/icon_util.h"
28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/gfx/image/image.h"
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/gfx/image/image_family.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const base::FilePath::CharType kIconChecksumFileExt[] =
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FILE_PATH_LITERAL(".ico.md5");
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
36c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Calculates checksum of an icon family using MD5.
37c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// The checksum is derived from all of the icons in the family.
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void GetImageCheckSum(const gfx::ImageFamily& image, base::MD5Digest* digest) {
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(digest);
40c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::MD5Context md5_context;
41c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::MD5Init(&md5_context);
42c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
43c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (gfx::ImageFamily::const_iterator it = image.begin(); it != image.end();
44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)       ++it) {
45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SkBitmap bitmap = it->AsBitmap();
46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SkAutoLockPixels image_lock(bitmap);
48c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    base::StringPiece image_data(
49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        reinterpret_cast<const char*>(bitmap.getPixels()), bitmap.getSize());
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    base::MD5Update(&md5_context, image_data);
51c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::MD5Final(digest, &md5_context);
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Saves |image| as an |icon_file| with the checksum.
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool SaveIconWithCheckSum(const base::FilePath& icon_file,
58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                          const gfx::ImageFamily& image) {
59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IconUtil::CreateIconFileFromImageFamily(image, icon_file))
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::MD5Digest digest;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GetImageCheckSum(image, &digest);
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath cheksum_file(icon_file.ReplaceExtension(kIconChecksumFileExt));
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return base::WriteFile(cheksum_file,
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                         reinterpret_cast<const char*>(&digest),
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                         sizeof(digest)) == sizeof(digest);
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if |icon_file| is missing or different from |image|.
72c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool ShouldUpdateIcon(const base::FilePath& icon_file,
73c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                      const gfx::ImageFamily& image) {
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath checksum_file(
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      icon_file.ReplaceExtension(kIconChecksumFileExt));
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if icon_file or checksum file is missing.
787dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!base::PathExists(icon_file) ||
797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      !base::PathExists(checksum_file))
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::MD5Digest persisted_image_checksum;
83a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (sizeof(persisted_image_checksum) != base::ReadFile(checksum_file,
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      reinterpret_cast<char*>(&persisted_image_checksum),
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      sizeof(persisted_image_checksum)))
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::MD5Digest downloaded_image_checksum;
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GetImageCheckSum(image, &downloaded_image_checksum);
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Update icon if checksums are not equal.
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return memcmp(&persisted_image_checksum, &downloaded_image_checksum,
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                sizeof(base::MD5Digest)) != 0;
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
96eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Returns true if |shortcut_file_name| matches profile |profile_path|, and has
97eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// an --app-id flag.
98eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool IsAppShortcutForProfile(const base::FilePath& shortcut_file_name,
99eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                             const base::FilePath& profile_path) {
100a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 cmd_line_string;
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (base::win::ResolveShortcut(shortcut_file_name, NULL, &cmd_line_string)) {
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cmd_line_string = L"program " + cmd_line_string;
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CommandLine shortcut_cmd_line = CommandLine::FromString(cmd_line_string);
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return shortcut_cmd_line.HasSwitch(switches::kProfileDirectory) &&
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           shortcut_cmd_line.GetSwitchValuePath(switches::kProfileDirectory) ==
106eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch               profile_path.BaseName() &&
107eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch           shortcut_cmd_line.HasSwitch(switches::kAppId);
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
113eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Finds shortcuts in |shortcut_path| that match profile for |profile_path| and
114eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// extension with title |shortcut_name|.
115eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// If |shortcut_name| is empty, finds all shortcuts matching |profile_path|.
116eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochstd::vector<base::FilePath> FindAppShortcutsByProfileAndTitle(
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& shortcut_path,
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& profile_path,
119a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& shortcut_name) {
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<base::FilePath> shortcut_paths;
121eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
122eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (shortcut_name.empty()) {
123eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // Find all shortcuts for this profile.
124eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    base::FileEnumerator files(shortcut_path, false,
125eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                               base::FileEnumerator::FILES,
126eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                               FILE_PATH_LITERAL("*.lnk"));
127eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    base::FilePath shortcut_file = files.Next();
128eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    while (!shortcut_file.empty()) {
129eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (IsAppShortcutForProfile(shortcut_file, profile_path))
130eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        shortcut_paths.push_back(shortcut_file);
131eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      shortcut_file = files.Next();
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
133eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  } else {
134eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // Find all shortcuts matching |shortcut_name|.
135eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    base::FilePath base_path = shortcut_path.
136eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        Append(web_app::internals::GetSanitizedFileName(shortcut_name)).
137eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        AddExtension(FILE_PATH_LITERAL(".lnk"));
138eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
139eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const int fileNamesToCheck = 10;
140eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    for (int i = 0; i < fileNamesToCheck; ++i) {
141eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      base::FilePath shortcut_file = base_path;
142eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (i > 0) {
143eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        shortcut_file = shortcut_file.InsertBeforeExtensionASCII(
144eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            base::StringPrintf(" (%d)", i));
145eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
1467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if (base::PathExists(shortcut_file) &&
147eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          IsAppShortcutForProfile(shortcut_file, profile_path)) {
148eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        shortcut_paths.push_back(shortcut_file);
149eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
152eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return shortcut_paths;
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Creates application shortcuts in a given set of paths.
15790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// |shortcut_paths| is a list of directories in which shortcuts should be
158558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch// created. If |creation_reason| is SHORTCUT_CREATION_AUTOMATED and there is an
159558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch// existing shortcut to this app for this profile, does nothing (succeeding).
16090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Returns true on success, false on failure.
16190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Must be called on the FILE thread.
16290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool CreateShortcutsInPaths(
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& web_app_path,
164a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    const web_app::ShortcutInfo& shortcut_info,
165eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const std::vector<base::FilePath>& shortcut_paths,
166558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch    web_app::ShortcutCreationReason creation_reason,
167eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    std::vector<base::FilePath>* out_filenames) {
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Ensure web_app_path exists.
1697dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!base::PathExists(web_app_path) &&
170a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      !base::CreateDirectory(web_app_path)) {
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Generates file name to use with persisted ico and shortcut file.
175effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  base::FilePath icon_file =
176effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      web_app::internals::GetIconFilePath(web_app_path, shortcut_info.title);
177c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!web_app::internals::CheckAndSaveIcon(icon_file, shortcut_info.favicon)) {
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
181c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::FilePath chrome_exe;
182c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
183c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    NOTREACHED();
184c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return false;
185c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Working directory.
188c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::FilePath working_dir(chrome_exe.DirName());
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CommandLine cmd_line(CommandLine::NO_PROGRAM);
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  cmd_line = ShellIntegration::CommandLineArgsForLauncher(shortcut_info.url,
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shortcut_info.extension_id, shortcut_info.profile_path);
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(evan): we rely on the fact that command_line_string() is
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // properly quoted for a Windows command line.  The method on
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // CommandLine should probably be renamed to better reflect that
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // fact.
198a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 wide_switches(cmd_line.GetCommandLineString());
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sanitize description
201a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 description = shortcut_info.description;
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (description.length() >= MAX_PATH)
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    description.resize(MAX_PATH - 1);
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Generates app id from web app url and profile path.
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string app_name(web_app::GenerateApplicationNameFromInfo(shortcut_info));
207a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 app_id(ShellIntegration::GetAppModelIdForProfile(
2085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::UTF8ToUTF16(app_name), shortcut_info.profile_path));
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool success = true;
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < shortcut_paths.size(); ++i) {
212effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    base::FilePath shortcut_file =
213effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        shortcut_paths[i]
214effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch            .Append(
215effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                 web_app::internals::GetSanitizedFileName(shortcut_info.title))
216effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch            .AddExtension(installer::kLnkExt);
217558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch    if (creation_reason == web_app::SHORTCUT_CREATION_AUTOMATED) {
218eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      // Check whether there is an existing shortcut to this app.
219eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      std::vector<base::FilePath> shortcut_files =
220eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          FindAppShortcutsByProfileAndTitle(shortcut_paths[i],
221eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                            shortcut_info.profile_path,
222eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                            shortcut_info.title);
223eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (!shortcut_files.empty())
224eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        continue;
225eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (shortcut_paths[i] != web_app_path) {
2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      int unique_number =
228a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          base::GetUniquePathNumber(shortcut_file,
229a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                    base::FilePath::StringType());
2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (unique_number == -1) {
2312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        success = false;
2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        continue;
2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      } else if (unique_number > 0) {
2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        shortcut_file = shortcut_file.InsertBeforeExtensionASCII(
2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            base::StringPrintf(" (%d)", unique_number));
2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::win::ShortcutProperties shortcut_properties;
239c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    shortcut_properties.set_target(chrome_exe);
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shortcut_properties.set_working_dir(working_dir);
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shortcut_properties.set_arguments(wide_switches);
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shortcut_properties.set_description(description);
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shortcut_properties.set_icon(icon_file, 0);
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shortcut_properties.set_app_id(app_id);
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shortcut_properties.set_dual_mode(false);
2467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    if (!base::PathExists(shortcut_file.DirName()) &&
247a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        !base::CreateDirectory(shortcut_file.DirName())) {
248c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      NOTREACHED();
249c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return false;
250c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    success = base::win::CreateOrUpdateShortcutLink(
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        shortcut_file, shortcut_properties,
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        base::win::SHORTCUT_CREATE_ALWAYS) && success;
254eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (out_filenames)
255eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      out_filenames->push_back(shortcut_file);
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
25890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return success;
25990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
26090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
26190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Gets the directories with shortcuts for an app, and deletes the shortcuts.
26290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// This will search the standard locations for shortcuts named |title| that open
26390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// in the profile with |profile_path|.
26490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// |was_pinned_to_taskbar| will be set to true if there was previously a
26590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// shortcut pinned to the taskbar for this app; false otherwise.
266eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// If |web_app_path| is empty, this will not delete shortcuts from the web app
267eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// directory. If |title| is empty, all shortcuts for this profile will be
268eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// deleted.
26990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// |shortcut_paths| will be populated with a list of directories where shortcuts
27090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// for this app were found (and deleted). This will delete duplicate shortcuts,
27190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// but only return each path once, even if it contained multiple deleted
27290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// shortcuts. Both of these may be NULL.
27390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void GetShortcutLocationsAndDeleteShortcuts(
27490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const base::FilePath& web_app_path,
27590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const base::FilePath& profile_path,
276a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& title,
27790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    bool* was_pinned_to_taskbar,
27890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    std::vector<base::FilePath>* shortcut_paths) {
27990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
28090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
28190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Get all possible locations for shortcuts.
282a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  web_app::ShortcutLocations all_shortcut_locations;
28390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  all_shortcut_locations.in_quick_launch_bar = true;
28490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  all_shortcut_locations.on_desktop = true;
28590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Delete shortcuts from the Chrome Apps subdirectory.
28690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // This matches the subdir name set by CreateApplicationShortcutView::Accept
28790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // for Chrome apps (not URL apps, but this function does not apply for them).
288a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  all_shortcut_locations.applications_menu_location =
289a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS;
29090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::vector<base::FilePath> all_paths = web_app::internals::GetShortcutPaths(
29190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      all_shortcut_locations);
292eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (base::win::GetVersion() >= base::win::VERSION_WIN7 &&
293eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      !web_app_path.empty()) {
29490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    all_paths.push_back(web_app_path);
295eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
29690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
29790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (was_pinned_to_taskbar) {
29890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // Determine if there is a link to this app in the TaskBar pin directory.
29990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    base::FilePath taskbar_pin_path;
30090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (PathService::Get(base::DIR_TASKBAR_PINS, &taskbar_pin_path)) {
30190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::vector<base::FilePath> taskbar_pin_files =
302eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          FindAppShortcutsByProfileAndTitle(taskbar_pin_path, profile_path,
303eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                            title);
30490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      *was_pinned_to_taskbar = !taskbar_pin_files.empty();
30590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    } else {
30690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      *was_pinned_to_taskbar = false;
30790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
30890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
30990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
31090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (std::vector<base::FilePath>::const_iterator i = all_paths.begin();
31190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)       i != all_paths.end(); ++i) {
31290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    std::vector<base::FilePath> shortcut_files =
313eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        FindAppShortcutsByProfileAndTitle(*i, profile_path, title);
31490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (shortcut_paths && !shortcut_files.empty()) {
31590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      shortcut_paths->push_back(*i);
31690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
31790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    for (std::vector<base::FilePath>::const_iterator j = shortcut_files.begin();
31890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)         j != shortcut_files.end(); ++j) {
31990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Any shortcut could have been pinned, either by chrome or the user, so
32090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // they are all unpinned.
32190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      base::win::TaskbarUnpinShortcutLink(j->value().c_str());
3227dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      base::DeleteFile(*j, false);
32390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
32490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
32590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
32690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
327cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void CreateIconAndSetRelaunchDetails(const base::FilePath& web_app_path,
328cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                     const base::FilePath& icon_file,
329cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                     const web_app::ShortcutInfo& shortcut_info,
330cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                     HWND hwnd) {
331cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
332cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
333cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  CommandLine command_line =
334cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      ShellIntegration::CommandLineArgsForLauncher(shortcut_info.url,
335cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                                   shortcut_info.extension_id,
336cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                                   shortcut_info.profile_path);
337cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
338cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::FilePath chrome_exe;
339cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
340cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    NOTREACHED();
341cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return;
342cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
343cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  command_line.SetProgram(chrome_exe);
344cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ui::win::SetRelaunchDetailsForWindow(
345cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      command_line.GetCommandLineString(), shortcut_info.title, hwnd);
346cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
347cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (!base::PathExists(web_app_path) && !base::CreateDirectory(web_app_path))
348cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return;
349cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
350cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ui::win::SetAppIconForWindow(icon_file.value(), hwnd);
351cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  web_app::internals::CheckAndSaveIcon(icon_file, shortcut_info.favicon);
352cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
353cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
354cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void OnShortcutInfoLoadedForSetRelaunchDetails(
355cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    HWND hwnd,
356cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const web_app::ShortcutInfo& shortcut_info) {
357cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
358cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
359cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Set window's icon to the one we're about to create/update in the web app
360cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // path. The icon cache will refresh on icon creation.
361cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::FilePath web_app_path =
362cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      web_app::GetWebAppDataDirectory(shortcut_info.profile_path,
363cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                      shortcut_info.extension_id,
364cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                      shortcut_info.url);
365cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::FilePath icon_file =
366cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      web_app_path.Append(web_app::internals::GetSanitizedFileName(
367cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                              shortcut_info.title))
368cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          .ReplaceExtension(FILE_PATH_LITERAL(".ico"));
369cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
370cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  content::BrowserThread::PostBlockingPoolTask(
371cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      FROM_HERE,
372cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      base::Bind(&CreateIconAndSetRelaunchDetails,
373cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                 web_app_path,
374cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                 icon_file,
375cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                 shortcut_info,
376cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                 hwnd));
377cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
378cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
37990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}  // namespace
38090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
38190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace web_app {
38290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
383cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)base::FilePath CreateShortcutInWebAppDir(const base::FilePath& web_app_dir,
384cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                         const ShortcutInfo& shortcut_info) {
385eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  std::vector<base::FilePath> paths;
386eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  paths.push_back(web_app_dir);
387eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  std::vector<base::FilePath> out_filenames;
388effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  base::FilePath web_app_dir_shortcut =
389effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      web_app_dir.Append(internals::GetSanitizedFileName(shortcut_info.title))
390effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch          .AddExtension(installer::kLnkExt);
391effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!PathExists(web_app_dir_shortcut)) {
392effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    CreateShortcutsInPaths(web_app_dir,
393effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                           shortcut_info,
394effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                           paths,
395effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                           SHORTCUT_CREATION_BY_USER,
396effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                           &out_filenames);
397effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    DCHECK_EQ(out_filenames.size(), 1u);
398effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    DCHECK_EQ(out_filenames[0].value(), web_app_dir_shortcut.value());
399effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  } else {
400effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    internals::CheckAndSaveIcon(
401effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        internals::GetIconFilePath(web_app_dir, shortcut_info.title),
402effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        shortcut_info.favicon);
403effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
404effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  return web_app_dir_shortcut;
405eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
406eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
407cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void UpdateRelaunchDetailsForApp(Profile* profile,
408cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                 const extensions::Extension* extension,
409cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                 HWND hwnd) {
410116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  web_app::GetShortcutInfoForApp(
411cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      extension,
412cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      profile,
413cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      base::Bind(&OnShortcutInfoLoadedForSetRelaunchDetails, hwnd));
414cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
415cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
41646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)void UpdateShortcutsForAllApps(Profile* profile,
41746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                               const base::Closure& callback) {
41846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  callback.Run();
41946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
42046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
42190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace internals {
42290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
42390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Saves |image| to |icon_file| if the file is outdated and refresh shell's
42490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// icon cache to ensure correct icon is displayed. Returns true if icon_file
42590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// is up to date or successfully updated.
42690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool CheckAndSaveIcon(const base::FilePath& icon_file,
42790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                      const gfx::ImageFamily& image) {
42890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (ShouldUpdateIcon(icon_file, image)) {
42990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (SaveIconWithCheckSum(icon_file, image)) {
43090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Refresh shell's icon cache. This call is quite disruptive as user would
43190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // see explorer rebuilding the icon cache. It would be great that we find
43290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // a better way to achieve this.
43390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST | SHCNF_FLUSHNOWAIT,
43490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                     NULL, NULL);
43590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    } else {
43690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return false;
43790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
43890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
43990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
44090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return true;
44190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
44290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
44390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool CreatePlatformShortcuts(
44490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const base::FilePath& web_app_path,
445cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const ShortcutInfo& shortcut_info,
446a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    const extensions::FileHandlersInfo& file_handlers_info,
447cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const ShortcutLocations& creation_locations,
448558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch    ShortcutCreationReason creation_reason) {
44990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
45090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
451f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Nothing to do on Windows for hidden apps.
452f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (creation_locations.applications_menu_location == APP_MENU_LOCATION_HIDDEN)
453f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return true;
454f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
45590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Shortcut paths under which to create shortcuts.
45690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::vector<base::FilePath> shortcut_paths =
45790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetShortcutPaths(creation_locations);
45890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
45990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool pin_to_taskbar = creation_locations.in_quick_launch_bar &&
46090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                        (base::win::GetVersion() >= base::win::VERSION_WIN7);
46190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
46290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Create/update the shortcut in the web app path for the "Pin To Taskbar"
46390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // option in Win7. We use the web app path shortcut because we will overwrite
46490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // it rather than appending unique numbers if the shortcut already exists.
46590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // This prevents pinned apps from having unique numbers in their names.
46690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (pin_to_taskbar)
46790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    shortcut_paths.push_back(web_app_path);
46890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
46990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (shortcut_paths.empty())
47090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
47190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
472eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!CreateShortcutsInPaths(web_app_path, shortcut_info, shortcut_paths,
473558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch                              creation_reason, NULL))
47490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
47590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
47690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (pin_to_taskbar) {
477effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    base::FilePath file_name = GetSanitizedFileName(shortcut_info.title);
4782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Use the web app path shortcut for pinning to avoid having unique numbers
4792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // in the application name.
4802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::FilePath shortcut_to_pin = web_app_path.Append(file_name).
481c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        AddExtension(installer::kLnkExt);
48290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (!base::win::TaskbarPinShortcutLink(shortcut_to_pin.value().c_str()))
48390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return false;
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
48690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return true;
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void UpdatePlatformShortcuts(
4902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& web_app_path,
491a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& old_app_title,
492cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const ShortcutInfo& shortcut_info,
493a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    const extensions::FileHandlersInfo& file_handlers_info) {
494c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
495c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Generates file name to use with persisted ico and shortcut file.
4972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath file_name =
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      web_app::internals::GetSanitizedFileName(shortcut_info.title);
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
50090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (old_app_title != shortcut_info.title) {
50190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // The app's title has changed. Delete all existing app shortcuts and
50290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // recreate them in any locations they already existed (but do not add them
50390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // to locations where they do not currently exist).
50490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    bool was_pinned_to_taskbar;
50590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    std::vector<base::FilePath> shortcut_paths;
50690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    GetShortcutLocationsAndDeleteShortcuts(
50790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        web_app_path, shortcut_info.profile_path, old_app_title,
50890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        &was_pinned_to_taskbar, &shortcut_paths);
509eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    CreateShortcutsInPaths(web_app_path, shortcut_info, shortcut_paths,
510558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch                           SHORTCUT_CREATION_BY_USER, NULL);
51190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // If the shortcut was pinned to the taskbar,
51290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // GetShortcutLocationsAndDeleteShortcuts will have deleted it. In that
51390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // case, re-pin it.
51490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (was_pinned_to_taskbar) {
515effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      base::FilePath file_name = GetSanitizedFileName(shortcut_info.title);
51690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Use the web app path shortcut for pinning to avoid having unique
51790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // numbers in the application name.
51890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      base::FilePath shortcut_to_pin = web_app_path.Append(file_name).
51990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          AddExtension(installer::kLnkExt);
52090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      base::win::TaskbarPinShortcutLink(shortcut_to_pin.value().c_str());
52190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
52290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
52390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
52423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // Update the icon if necessary.
525effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  base::FilePath icon_file = GetIconFilePath(web_app_path, shortcut_info.title);
526effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  CheckAndSaveIcon(icon_file, shortcut_info.favicon);
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
529cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void DeletePlatformShortcuts(const base::FilePath& web_app_path,
530cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                             const ShortcutInfo& shortcut_info) {
53190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GetShortcutLocationsAndDeleteShortcuts(
53290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      web_app_path, shortcut_info.profile_path, shortcut_info.title, NULL,
53390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      NULL);
534c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
535c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If there are no more shortcuts in the Chrome Apps subdirectory, remove it.
536c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::FilePath chrome_apps_dir;
537a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (ShellUtil::GetShortcutPath(
538a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_APPS_DIR,
539a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          BrowserDistribution::GetDistribution(),
540a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          ShellUtil::CURRENT_USER,
541a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          &chrome_apps_dir)) {
542a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (base::IsDirectoryEmpty(chrome_apps_dir))
5437dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      base::DeleteFile(chrome_apps_dir, false);
544eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
545eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
546eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
547eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid DeleteAllShortcutsForProfile(const base::FilePath& profile_path) {
548eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  GetShortcutLocationsAndDeleteShortcuts(base::FilePath(), profile_path, L"",
549eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                         NULL, NULL);
550eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
551eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // If there are no more shortcuts in the Chrome Apps subdirectory, remove it.
552eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  base::FilePath chrome_apps_dir;
553a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (ShellUtil::GetShortcutPath(
554a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_APPS_DIR,
555a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          BrowserDistribution::GetDistribution(),
556a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          ShellUtil::CURRENT_USER,
557a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          &chrome_apps_dir)) {
558a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (base::IsDirectoryEmpty(chrome_apps_dir))
5597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      base::DeleteFile(chrome_apps_dir, false);
560c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
561c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
562c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
563c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)std::vector<base::FilePath> GetShortcutPaths(
564cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const ShortcutLocations& creation_locations) {
565c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Shortcut paths under which to create shortcuts.
566c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::vector<base::FilePath> shortcut_paths;
567c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Locations to add to shortcut_paths.
568c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  struct {
569c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    bool use_this_location;
570a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    ShellUtil::ShortcutLocation location_id;
571c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } locations[] = {
572c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
573c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      creation_locations.on_desktop,
574a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      ShellUtil::SHORTCUT_LOCATION_DESKTOP
575c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }, {
576a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      creation_locations.applications_menu_location ==
577cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          APP_MENU_LOCATION_ROOT,
578a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      ShellUtil::SHORTCUT_LOCATION_START_MENU_ROOT
579c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }, {
580a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      creation_locations.applications_menu_location ==
581cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          APP_MENU_LOCATION_SUBDIR_CHROME,
582a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_DIR
583a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    }, {
584a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      creation_locations.applications_menu_location ==
585cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          APP_MENU_LOCATION_SUBDIR_CHROMEAPPS,
586a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_APPS_DIR
587a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    }, {
588a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      // For Win7+, |in_quick_launch_bar| indicates that we are pinning to
589a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      // taskbar. This needs to be handled by callers.
590a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      creation_locations.in_quick_launch_bar &&
591a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          base::win::GetVersion() < base::win::VERSION_WIN7,
592a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      ShellUtil::SHORTCUT_LOCATION_QUICK_LAUNCH
593c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
594c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
595a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
596a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  BrowserDistribution* dist = BrowserDistribution::GetDistribution();
597c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Populate shortcut_paths.
598c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (int i = 0; i < arraysize(locations); ++i) {
599c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (locations[i].use_this_location) {
600c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::FilePath path;
601a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      if (!ShellUtil::GetShortcutPath(locations[i].location_id,
602a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                      dist,
603a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                      ShellUtil::CURRENT_USER,
604a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                      &path)) {
605a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        NOTREACHED();
606c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        continue;
607c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
608c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      shortcut_paths.push_back(path);
609c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
610c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
611c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return shortcut_paths;
6125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
614effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochbase::FilePath GetIconFilePath(const base::FilePath& web_app_path,
615effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                               const base::string16& title) {
616effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  return web_app_path.Append(GetSanitizedFileName(title))
617effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      .AddExtension(FILE_PATH_LITERAL(".ico"));
618effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
619effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace internals
6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
622c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochvoid UpdateShortcutForTabContents(content::WebContents* web_contents) {
623c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // UpdateShortcutWorker will delete itself when it's done.
624c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  UpdateShortcutWorker* worker = new UpdateShortcutWorker(web_contents);
625c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  worker->Run();
626c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
627c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
6285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace web_app
629