web_app_win.cc revision effb81e5f8246d0db0270817048dc992db66e9fb
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"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/file_util.h"
11eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/files/file_enumerator.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"
20eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/browser/web_applications/web_app.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"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/icon_util.h"
27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/gfx/image/image.h"
28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/gfx/image/image_family.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const base::FilePath::CharType kIconChecksumFileExt[] =
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FILE_PATH_LITERAL(".ico.md5");
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
35c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Calculates checksum of an icon family using MD5.
36c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// The checksum is derived from all of the icons in the family.
37c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void GetImageCheckSum(const gfx::ImageFamily& image, base::MD5Digest* digest) {
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(digest);
39c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::MD5Context md5_context;
40c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::MD5Init(&md5_context);
41c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
42c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (gfx::ImageFamily::const_iterator it = image.begin(); it != image.end();
43c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)       ++it) {
44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SkBitmap bitmap = it->AsBitmap();
45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SkAutoLockPixels image_lock(bitmap);
47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    base::StringPiece image_data(
48c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        reinterpret_cast<const char*>(bitmap.getPixels()), bitmap.getSize());
49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    base::MD5Update(&md5_context, image_data);
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::MD5Final(digest, &md5_context);
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Saves |image| as an |icon_file| with the checksum.
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool SaveIconWithCheckSum(const base::FilePath& icon_file,
57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                          const gfx::ImageFamily& image) {
58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!IconUtil::CreateIconFileFromImageFamily(image, icon_file))
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::MD5Digest digest;
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GetImageCheckSum(image, &digest);
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath cheksum_file(icon_file.ReplaceExtension(kIconChecksumFileExt));
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return base::WriteFile(cheksum_file,
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                         reinterpret_cast<const char*>(&digest),
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                         sizeof(digest)) == sizeof(digest);
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if |icon_file| is missing or different from |image|.
71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool ShouldUpdateIcon(const base::FilePath& icon_file,
72c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                      const gfx::ImageFamily& image) {
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath checksum_file(
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      icon_file.ReplaceExtension(kIconChecksumFileExt));
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if icon_file or checksum file is missing.
777dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!base::PathExists(icon_file) ||
787dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      !base::PathExists(checksum_file))
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::MD5Digest persisted_image_checksum;
82a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (sizeof(persisted_image_checksum) != base::ReadFile(checksum_file,
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      reinterpret_cast<char*>(&persisted_image_checksum),
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      sizeof(persisted_image_checksum)))
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::MD5Digest downloaded_image_checksum;
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GetImageCheckSum(image, &downloaded_image_checksum);
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Update icon if checksums are not equal.
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return memcmp(&persisted_image_checksum, &downloaded_image_checksum,
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                sizeof(base::MD5Digest)) != 0;
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Returns true if |shortcut_file_name| matches profile |profile_path|, and has
96eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// an --app-id flag.
97eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool IsAppShortcutForProfile(const base::FilePath& shortcut_file_name,
98eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                             const base::FilePath& profile_path) {
99a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 cmd_line_string;
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (base::win::ResolveShortcut(shortcut_file_name, NULL, &cmd_line_string)) {
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cmd_line_string = L"program " + cmd_line_string;
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CommandLine shortcut_cmd_line = CommandLine::FromString(cmd_line_string);
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return shortcut_cmd_line.HasSwitch(switches::kProfileDirectory) &&
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           shortcut_cmd_line.GetSwitchValuePath(switches::kProfileDirectory) ==
105eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch               profile_path.BaseName() &&
106eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch           shortcut_cmd_line.HasSwitch(switches::kAppId);
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
112eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Finds shortcuts in |shortcut_path| that match profile for |profile_path| and
113eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// extension with title |shortcut_name|.
114eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// If |shortcut_name| is empty, finds all shortcuts matching |profile_path|.
115eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochstd::vector<base::FilePath> FindAppShortcutsByProfileAndTitle(
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& shortcut_path,
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& profile_path,
118a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& shortcut_name) {
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<base::FilePath> shortcut_paths;
120eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
121eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (shortcut_name.empty()) {
122eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // Find all shortcuts for this profile.
123eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    base::FileEnumerator files(shortcut_path, false,
124eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                               base::FileEnumerator::FILES,
125eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                               FILE_PATH_LITERAL("*.lnk"));
126eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    base::FilePath shortcut_file = files.Next();
127eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    while (!shortcut_file.empty()) {
128eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (IsAppShortcutForProfile(shortcut_file, profile_path))
129eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        shortcut_paths.push_back(shortcut_file);
130eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      shortcut_file = files.Next();
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
132eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  } else {
133eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // Find all shortcuts matching |shortcut_name|.
134eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    base::FilePath base_path = shortcut_path.
135eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        Append(web_app::internals::GetSanitizedFileName(shortcut_name)).
136eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        AddExtension(FILE_PATH_LITERAL(".lnk"));
137eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
138eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const int fileNamesToCheck = 10;
139eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    for (int i = 0; i < fileNamesToCheck; ++i) {
140eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      base::FilePath shortcut_file = base_path;
141eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (i > 0) {
142eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        shortcut_file = shortcut_file.InsertBeforeExtensionASCII(
143eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            base::StringPrintf(" (%d)", i));
144eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
1457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if (base::PathExists(shortcut_file) &&
146eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          IsAppShortcutForProfile(shortcut_file, profile_path)) {
147eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        shortcut_paths.push_back(shortcut_file);
148eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
151eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return shortcut_paths;
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Creates application shortcuts in a given set of paths.
15690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// |shortcut_paths| is a list of directories in which shortcuts should be
157558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch// created. If |creation_reason| is SHORTCUT_CREATION_AUTOMATED and there is an
158558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch// existing shortcut to this app for this profile, does nothing (succeeding).
15990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Returns true on success, false on failure.
16090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Must be called on the FILE thread.
16190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool CreateShortcutsInPaths(
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& web_app_path,
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const ShellIntegration::ShortcutInfo& shortcut_info,
164eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const std::vector<base::FilePath>& shortcut_paths,
165558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch    web_app::ShortcutCreationReason creation_reason,
166eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    std::vector<base::FilePath>* out_filenames) {
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Ensure web_app_path exists.
1687dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!base::PathExists(web_app_path) &&
169a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      !base::CreateDirectory(web_app_path)) {
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Generates file name to use with persisted ico and shortcut file.
174effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  base::FilePath icon_file =
175effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      web_app::internals::GetIconFilePath(web_app_path, shortcut_info.title);
176c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!web_app::internals::CheckAndSaveIcon(icon_file, shortcut_info.favicon)) {
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
180c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::FilePath chrome_exe;
181c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
182c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    NOTREACHED();
183c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return false;
184c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Working directory.
187c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::FilePath working_dir(chrome_exe.DirName());
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CommandLine cmd_line(CommandLine::NO_PROGRAM);
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  cmd_line = ShellIntegration::CommandLineArgsForLauncher(shortcut_info.url,
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shortcut_info.extension_id, shortcut_info.profile_path);
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(evan): we rely on the fact that command_line_string() is
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // properly quoted for a Windows command line.  The method on
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // CommandLine should probably be renamed to better reflect that
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // fact.
197a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 wide_switches(cmd_line.GetCommandLineString());
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sanitize description
200a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 description = shortcut_info.description;
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (description.length() >= MAX_PATH)
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    description.resize(MAX_PATH - 1);
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Generates app id from web app url and profile path.
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string app_name(web_app::GenerateApplicationNameFromInfo(shortcut_info));
206a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 app_id(ShellIntegration::GetAppModelIdForProfile(
2075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::UTF8ToUTF16(app_name), shortcut_info.profile_path));
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool success = true;
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < shortcut_paths.size(); ++i) {
211effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    base::FilePath shortcut_file =
212effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        shortcut_paths[i]
213effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch            .Append(
214effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                 web_app::internals::GetSanitizedFileName(shortcut_info.title))
215effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch            .AddExtension(installer::kLnkExt);
216558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch    if (creation_reason == web_app::SHORTCUT_CREATION_AUTOMATED) {
217eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      // Check whether there is an existing shortcut to this app.
218eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      std::vector<base::FilePath> shortcut_files =
219eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          FindAppShortcutsByProfileAndTitle(shortcut_paths[i],
220eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                            shortcut_info.profile_path,
221eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                            shortcut_info.title);
222eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (!shortcut_files.empty())
223eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        continue;
224eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (shortcut_paths[i] != web_app_path) {
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      int unique_number =
227a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          base::GetUniquePathNumber(shortcut_file,
228a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                    base::FilePath::StringType());
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (unique_number == -1) {
2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        success = false;
2312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        continue;
2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      } else if (unique_number > 0) {
2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        shortcut_file = shortcut_file.InsertBeforeExtensionASCII(
2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            base::StringPrintf(" (%d)", unique_number));
2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::win::ShortcutProperties shortcut_properties;
238c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    shortcut_properties.set_target(chrome_exe);
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shortcut_properties.set_working_dir(working_dir);
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shortcut_properties.set_arguments(wide_switches);
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shortcut_properties.set_description(description);
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shortcut_properties.set_icon(icon_file, 0);
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shortcut_properties.set_app_id(app_id);
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shortcut_properties.set_dual_mode(false);
2457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    if (!base::PathExists(shortcut_file.DirName()) &&
246a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        !base::CreateDirectory(shortcut_file.DirName())) {
247c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      NOTREACHED();
248c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return false;
249c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    success = base::win::CreateOrUpdateShortcutLink(
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        shortcut_file, shortcut_properties,
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        base::win::SHORTCUT_CREATE_ALWAYS) && success;
253eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (out_filenames)
254eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      out_filenames->push_back(shortcut_file);
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
25790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return success;
25890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
25990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
26090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Gets the directories with shortcuts for an app, and deletes the shortcuts.
26190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// This will search the standard locations for shortcuts named |title| that open
26290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// in the profile with |profile_path|.
26390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// |was_pinned_to_taskbar| will be set to true if there was previously a
26490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// shortcut pinned to the taskbar for this app; false otherwise.
265eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// If |web_app_path| is empty, this will not delete shortcuts from the web app
266eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// directory. If |title| is empty, all shortcuts for this profile will be
267eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// deleted.
26890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// |shortcut_paths| will be populated with a list of directories where shortcuts
26990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// for this app were found (and deleted). This will delete duplicate shortcuts,
27090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// but only return each path once, even if it contained multiple deleted
27190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// shortcuts. Both of these may be NULL.
27290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void GetShortcutLocationsAndDeleteShortcuts(
27390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const base::FilePath& web_app_path,
27490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const base::FilePath& profile_path,
275a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& title,
27690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    bool* was_pinned_to_taskbar,
27790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    std::vector<base::FilePath>* shortcut_paths) {
27890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
27990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
28090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Get all possible locations for shortcuts.
28190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ShellIntegration::ShortcutLocations all_shortcut_locations;
28290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  all_shortcut_locations.in_quick_launch_bar = true;
28390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  all_shortcut_locations.on_desktop = true;
28490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Delete shortcuts from the Chrome Apps subdirectory.
28590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // This matches the subdir name set by CreateApplicationShortcutView::Accept
28690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // for Chrome apps (not URL apps, but this function does not apply for them).
287a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  all_shortcut_locations.applications_menu_location =
288a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      ShellIntegration::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS;
28990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::vector<base::FilePath> all_paths = web_app::internals::GetShortcutPaths(
29090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      all_shortcut_locations);
291eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (base::win::GetVersion() >= base::win::VERSION_WIN7 &&
292eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      !web_app_path.empty()) {
29390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    all_paths.push_back(web_app_path);
294eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
29590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
29690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (was_pinned_to_taskbar) {
29790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // Determine if there is a link to this app in the TaskBar pin directory.
29890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    base::FilePath taskbar_pin_path;
29990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (PathService::Get(base::DIR_TASKBAR_PINS, &taskbar_pin_path)) {
30090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      std::vector<base::FilePath> taskbar_pin_files =
301eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          FindAppShortcutsByProfileAndTitle(taskbar_pin_path, profile_path,
302eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                            title);
30390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      *was_pinned_to_taskbar = !taskbar_pin_files.empty();
30490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    } else {
30590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      *was_pinned_to_taskbar = false;
30690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
30790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
30890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
30990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (std::vector<base::FilePath>::const_iterator i = all_paths.begin();
31090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)       i != all_paths.end(); ++i) {
31190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    std::vector<base::FilePath> shortcut_files =
312eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        FindAppShortcutsByProfileAndTitle(*i, profile_path, title);
31390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (shortcut_paths && !shortcut_files.empty()) {
31490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      shortcut_paths->push_back(*i);
31590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
31690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    for (std::vector<base::FilePath>::const_iterator j = shortcut_files.begin();
31790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)         j != shortcut_files.end(); ++j) {
31890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Any shortcut could have been pinned, either by chrome or the user, so
31990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // they are all unpinned.
32090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      base::win::TaskbarUnpinShortcutLink(j->value().c_str());
3217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      base::DeleteFile(*j, false);
32290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
32390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
32490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
32590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
32690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}  // namespace
32790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
32890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace web_app {
32990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
330eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbase::FilePath CreateShortcutInWebAppDir(
331eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const base::FilePath& web_app_dir,
332eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const ShellIntegration::ShortcutInfo& shortcut_info) {
333eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  std::vector<base::FilePath> paths;
334eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  paths.push_back(web_app_dir);
335eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  std::vector<base::FilePath> out_filenames;
336effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  base::FilePath web_app_dir_shortcut =
337effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      web_app_dir.Append(internals::GetSanitizedFileName(shortcut_info.title))
338effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch          .AddExtension(installer::kLnkExt);
339effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!PathExists(web_app_dir_shortcut)) {
340effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    CreateShortcutsInPaths(web_app_dir,
341effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                           shortcut_info,
342effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                           paths,
343effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                           SHORTCUT_CREATION_BY_USER,
344effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                           &out_filenames);
345effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    DCHECK_EQ(out_filenames.size(), 1u);
346effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    DCHECK_EQ(out_filenames[0].value(), web_app_dir_shortcut.value());
347effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  } else {
348effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    internals::CheckAndSaveIcon(
349effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        internals::GetIconFilePath(web_app_dir, shortcut_info.title),
350effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        shortcut_info.favicon);
351effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
352effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  return web_app_dir_shortcut;
353eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
354eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
35590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace internals {
35690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
35790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Saves |image| to |icon_file| if the file is outdated and refresh shell's
35890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// icon cache to ensure correct icon is displayed. Returns true if icon_file
35990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// is up to date or successfully updated.
36090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool CheckAndSaveIcon(const base::FilePath& icon_file,
36190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                      const gfx::ImageFamily& image) {
36290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (ShouldUpdateIcon(icon_file, image)) {
36390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (SaveIconWithCheckSum(icon_file, image)) {
36490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Refresh shell's icon cache. This call is quite disruptive as user would
36590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // see explorer rebuilding the icon cache. It would be great that we find
36690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // a better way to achieve this.
36790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST | SHCNF_FLUSHNOWAIT,
36890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                     NULL, NULL);
36990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    } else {
37090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return false;
37190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
37290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
37390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
37490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return true;
37590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
37690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
37790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool CreatePlatformShortcuts(
37890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const base::FilePath& web_app_path,
37990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const ShellIntegration::ShortcutInfo& shortcut_info,
380eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const ShellIntegration::ShortcutLocations& creation_locations,
381558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch    ShortcutCreationReason creation_reason) {
38290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
38390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
38490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Shortcut paths under which to create shortcuts.
38590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::vector<base::FilePath> shortcut_paths =
38690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetShortcutPaths(creation_locations);
38790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
38890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool pin_to_taskbar = creation_locations.in_quick_launch_bar &&
38990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                        (base::win::GetVersion() >= base::win::VERSION_WIN7);
39090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
39190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Create/update the shortcut in the web app path for the "Pin To Taskbar"
39290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // option in Win7. We use the web app path shortcut because we will overwrite
39390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // it rather than appending unique numbers if the shortcut already exists.
39490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // This prevents pinned apps from having unique numbers in their names.
39590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (pin_to_taskbar)
39690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    shortcut_paths.push_back(web_app_path);
39790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
39890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (shortcut_paths.empty())
39990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
40090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
401eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!CreateShortcutsInPaths(web_app_path, shortcut_info, shortcut_paths,
402558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch                              creation_reason, NULL))
40390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
40490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
40590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (pin_to_taskbar) {
406effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    base::FilePath file_name = GetSanitizedFileName(shortcut_info.title);
4072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Use the web app path shortcut for pinning to avoid having unique numbers
4082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // in the application name.
4092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::FilePath shortcut_to_pin = web_app_path.Append(file_name).
410c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        AddExtension(installer::kLnkExt);
41190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (!base::win::TaskbarPinShortcutLink(shortcut_to_pin.value().c_str()))
41290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return false;
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
41590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return true;
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void UpdatePlatformShortcuts(
4192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& web_app_path,
420a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& old_app_title,
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const ShellIntegration::ShortcutInfo& shortcut_info) {
422c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
423c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Generates file name to use with persisted ico and shortcut file.
4252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath file_name =
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      web_app::internals::GetSanitizedFileName(shortcut_info.title);
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
42890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (old_app_title != shortcut_info.title) {
42990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // The app's title has changed. Delete all existing app shortcuts and
43090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // recreate them in any locations they already existed (but do not add them
43190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // to locations where they do not currently exist).
43290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    bool was_pinned_to_taskbar;
43390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    std::vector<base::FilePath> shortcut_paths;
43490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    GetShortcutLocationsAndDeleteShortcuts(
43590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        web_app_path, shortcut_info.profile_path, old_app_title,
43690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        &was_pinned_to_taskbar, &shortcut_paths);
437eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    CreateShortcutsInPaths(web_app_path, shortcut_info, shortcut_paths,
438558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch                           SHORTCUT_CREATION_BY_USER, NULL);
43990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // If the shortcut was pinned to the taskbar,
44090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // GetShortcutLocationsAndDeleteShortcuts will have deleted it. In that
44190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // case, re-pin it.
44290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (was_pinned_to_taskbar) {
443effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      base::FilePath file_name = GetSanitizedFileName(shortcut_info.title);
44490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Use the web app path shortcut for pinning to avoid having unique
44590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // numbers in the application name.
44690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      base::FilePath shortcut_to_pin = web_app_path.Append(file_name).
44790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          AddExtension(installer::kLnkExt);
44890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      base::win::TaskbarPinShortcutLink(shortcut_to_pin.value().c_str());
44990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
45090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
45190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
45223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // Update the icon if necessary.
453effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  base::FilePath icon_file = GetIconFilePath(web_app_path, shortcut_info.title);
454effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  CheckAndSaveIcon(icon_file, shortcut_info.favicon);
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DeletePlatformShortcuts(
4582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& web_app_path,
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const ShellIntegration::ShortcutInfo& shortcut_info) {
46090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GetShortcutLocationsAndDeleteShortcuts(
46190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      web_app_path, shortcut_info.profile_path, shortcut_info.title, NULL,
46290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      NULL);
463c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
464c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If there are no more shortcuts in the Chrome Apps subdirectory, remove it.
465c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::FilePath chrome_apps_dir;
466a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (ShellUtil::GetShortcutPath(
467a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_APPS_DIR,
468a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          BrowserDistribution::GetDistribution(),
469a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          ShellUtil::CURRENT_USER,
470a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          &chrome_apps_dir)) {
471a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (base::IsDirectoryEmpty(chrome_apps_dir))
4727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      base::DeleteFile(chrome_apps_dir, false);
473eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
474eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
475eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
476eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid DeleteAllShortcutsForProfile(const base::FilePath& profile_path) {
477eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  GetShortcutLocationsAndDeleteShortcuts(base::FilePath(), profile_path, L"",
478eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                         NULL, NULL);
479eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
480eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // If there are no more shortcuts in the Chrome Apps subdirectory, remove it.
481eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  base::FilePath chrome_apps_dir;
482a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (ShellUtil::GetShortcutPath(
483a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_APPS_DIR,
484a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          BrowserDistribution::GetDistribution(),
485a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          ShellUtil::CURRENT_USER,
486a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          &chrome_apps_dir)) {
487a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (base::IsDirectoryEmpty(chrome_apps_dir))
4887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      base::DeleteFile(chrome_apps_dir, false);
489c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
490c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
491c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
492c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)std::vector<base::FilePath> GetShortcutPaths(
493c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const ShellIntegration::ShortcutLocations& creation_locations) {
494c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Shortcut paths under which to create shortcuts.
495c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::vector<base::FilePath> shortcut_paths;
496c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Locations to add to shortcut_paths.
497c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  struct {
498c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    bool use_this_location;
499a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    ShellUtil::ShortcutLocation location_id;
500c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } locations[] = {
501c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {
502c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      creation_locations.on_desktop,
503a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      ShellUtil::SHORTCUT_LOCATION_DESKTOP
504c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }, {
505a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      creation_locations.applications_menu_location ==
506a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          ShellIntegration::APP_MENU_LOCATION_ROOT,
507a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      ShellUtil::SHORTCUT_LOCATION_START_MENU_ROOT
508c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }, {
509a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      creation_locations.applications_menu_location ==
510a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          ShellIntegration::APP_MENU_LOCATION_SUBDIR_CHROME,
511a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_DIR
512a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    }, {
513a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      creation_locations.applications_menu_location ==
514a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          ShellIntegration::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS,
515a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_APPS_DIR
516a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    }, {
517a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      // For Win7+, |in_quick_launch_bar| indicates that we are pinning to
518a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      // taskbar. This needs to be handled by callers.
519a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      creation_locations.in_quick_launch_bar &&
520a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          base::win::GetVersion() < base::win::VERSION_WIN7,
521a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      ShellUtil::SHORTCUT_LOCATION_QUICK_LAUNCH
522c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
523c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
524a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
525a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  BrowserDistribution* dist = BrowserDistribution::GetDistribution();
526c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Populate shortcut_paths.
527c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (int i = 0; i < arraysize(locations); ++i) {
528c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (locations[i].use_this_location) {
529c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::FilePath path;
530a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      if (!ShellUtil::GetShortcutPath(locations[i].location_id,
531a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                      dist,
532a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                      ShellUtil::CURRENT_USER,
533a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                      &path)) {
534a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        NOTREACHED();
535c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        continue;
536c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
537c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      shortcut_paths.push_back(path);
538c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
539c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
540c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return shortcut_paths;
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
543effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochbase::FilePath GetIconFilePath(const base::FilePath& web_app_path,
544effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                               const base::string16& title) {
545effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  return web_app_path.Append(GetSanitizedFileName(title))
546effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      .AddExtension(FILE_PATH_LITERAL(".ico"));
547effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
548effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace internals
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace web_app
552