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)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/setup/install.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <windows.h>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <shlobj.h>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <time.h>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/numerics/safe_conversions.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/path_service.h"
197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/strings/string_util.h"
207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/strings/stringprintf.h"
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/win/shortcut.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/win/windows_version.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_constants.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/setup/install_worker.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/setup/setup_constants.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/util/auto_launch_util.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/util/browser_distribution.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/util/create_reg_key_work_item.h"
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/util/delete_after_reboot_helper.h"
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/util/google_update_constants.h"
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/util/helper.h"
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/util/install_util.h"
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/util/master_preferences.h"
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/util/master_preferences_constants.h"
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/util/set_reg_value_work_item.h"
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/util/shell_util.h"
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/util/util_constants.h"
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/util/work_item_list.h"
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void LogShortcutOperation(ShellUtil::ShortcutLocation location,
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          BrowserDistribution* dist,
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          const ShellUtil::ShortcutProperties& properties,
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          ShellUtil::ShortcutOperation operation,
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          bool failed) {
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ShellUtil::SHELL_SHORTCUT_UPDATE_EXISTING should not be used at install and
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // thus this method does not handle logging a message for it.
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(operation != ShellUtil::SHELL_SHORTCUT_UPDATE_EXISTING);
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string message;
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (failed)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    message.append("Failed: ");
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  message.append(
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      (operation == ShellUtil::SHELL_SHORTCUT_CREATE_ALWAYS ||
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       operation == ShellUtil::SHELL_SHORTCUT_CREATE_IF_NO_SYSTEM_LEVEL) ?
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "Creating " : "Overwriting ");
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (failed && operation == ShellUtil::SHELL_SHORTCUT_REPLACE_EXISTING)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    message.append("(maybe the shortcut doesn't exist?) ");
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  message.append((properties.level == ShellUtil::CURRENT_USER) ? "per-user " :
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                                 "all-users ");
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (location) {
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case ShellUtil::SHORTCUT_LOCATION_DESKTOP:
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      message.append("Desktop ");
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case ShellUtil::SHORTCUT_LOCATION_QUICK_LAUNCH:
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      message.append("Quick Launch ");
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
71a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    case ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_DIR:
72a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      message.append("Start menu/" +
73cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                     base::UTF16ToUTF8(dist->GetStartMenuShortcutSubfolder(
74a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                     BrowserDistribution::SUBFOLDER_CHROME)) +
75a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                      " ");
76a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      break;
77a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    case ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_APPS_DIR:
78a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      message.append("Start menu/" +
79cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                     base::UTF16ToUTF8(dist->GetStartMenuShortcutSubfolder(
80a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                     BrowserDistribution::SUBFOLDER_APPS)) +
81a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                     " ");
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NOTREACHED();
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  message.push_back('"');
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (properties.has_shortcut_name())
89cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    message.append(base::UTF16ToUTF8(properties.shortcut_name));
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  else
91cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    message.append(base::UTF16ToUTF8(dist->GetDisplayName()));
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  message.push_back('"');
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  message.append(" shortcut to ");
95cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  message.append(base::UTF16ToUTF8(properties.target.value()));
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (properties.has_arguments())
97cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    message.append(base::UTF16ToUTF8(properties.arguments));
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (properties.pin_to_taskbar &&
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::win::GetVersion() >= base::win::VERSION_WIN7) {
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    message.append(" and pinning to the taskbar.");
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    message.push_back('.');
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (failed)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(WARNING) << message;
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  else
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    VLOG(1) << message;
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ExecuteAndLogShortcutOperation(
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ShellUtil::ShortcutLocation location,
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    BrowserDistribution* dist,
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const ShellUtil::ShortcutProperties& properties,
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ShellUtil::ShortcutOperation operation) {
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LogShortcutOperation(location, dist, properties, operation, false);
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!ShellUtil::CreateOrUpdateShortcut(location, dist, properties,
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                         operation)) {
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LogShortcutOperation(location, dist, properties, operation, true);
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void AddChromeToMediaPlayerList() {
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::string16 reg_path(installer::kMediaPlayerRegPath);
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // registry paths can also be appended like file system path
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  reg_path.push_back(base::FilePath::kSeparators[0]);
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  reg_path.append(installer::kChromeExe);
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  VLOG(1) << "Adding Chrome to Media player list at " << reg_path;
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<WorkItem> work_item(WorkItem::CreateCreateRegKeyWorkItem(
131cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      HKEY_LOCAL_MACHINE, reg_path, WorkItem::kWow64Default));
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // if the operation fails we log the error but still continue
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!work_item.get()->Do())
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Could not add Chrome to media player inclusion list.";
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copy master_preferences file provided to installer, in the same folder
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// as chrome.exe so Chrome first run can find it. This function will be called
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// only on the first install of Chrome.
141cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void CopyPreferenceFileForFirstRun(
142cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const installer::InstallerState& installer_state,
143cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const base::FilePath& prefs_source_path) {
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath prefs_dest_path(installer_state.target_path().AppendASCII(
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      installer::kDefaultMasterPrefs));
1467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!base::CopyFile(prefs_source_path, prefs_dest_path)) {
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    VLOG(1) << "Failed to copy master preferences from:"
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            << prefs_source_path.value() << " gle: " << ::GetLastError();
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This function installs a new version of Chrome to the specified location.
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// setup_path: Path to the executable (setup.exe) as it will be copied
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//           to Chrome install folder after install is complete
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// archive_path: Path to the archive (chrome.7z) as it will be copied
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//               to Chrome install folder after install is complete
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// src_path: the path that contains a complete and unpacked Chrome package
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//           to be installed.
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// temp_path: the path of working directory used during installation. This path
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//            does not need to exist.
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// new_version: new Chrome version that needs to be installed
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// current_version: returns the current active version (if any)
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This function makes best effort to do installation in a transactional
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// manner. If failed it tries to rollback all changes on the file system
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and registry. For example, if package exists before calling the
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// function, it rolls back all new file and directory changes under
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// package. If package does not exist before calling the function
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (typical new install), the function creates package during install
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and removes the whole directory during rollback.
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)installer::InstallStatus InstallNewVersion(
173cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const installer::InstallationState& original_state,
174cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const installer::InstallerState& installer_state,
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& setup_path,
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& archive_path,
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& src_path,
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& temp_path,
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const Version& new_version,
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_ptr<Version>* current_version) {
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(current_version);
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  installer_state.UpdateStage(installer::BUILDING);
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  current_version->reset(installer_state.GetCurrentVersion(original_state));
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<WorkItemList> install_list(WorkItem::CreateWorkItemList());
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AddInstallWorkItems(original_state,
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      installer_state,
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      setup_path,
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      archive_path,
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      src_path,
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      temp_path,
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      current_version->get(),
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      new_version,
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      install_list.get());
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath new_chrome_exe(
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      installer_state.target_path().Append(installer::kChromeNewExe));
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  installer_state.UpdateStage(installer::EXECUTING);
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!install_list->Do()) {
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    installer_state.UpdateStage(installer::ROLLINGBACK);
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    installer::InstallStatus result =
2067dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        base::PathExists(new_chrome_exe) && current_version->get() &&
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        new_version.Equals(*current_version->get()) ?
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        installer::SAME_VERSION_REPAIR_FAILED :
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        installer::INSTALL_FAILED;
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Install failed, rolling back... result: " << result;
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    install_list->Rollback();
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Rollback complete. ";
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return result;
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  installer_state.UpdateStage(installer::REFRESHING_POLICY);
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  installer::RefreshElevationPolicy();
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!current_version->get()) {
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    VLOG(1) << "First install of version " << new_version.GetString();
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return installer::FIRST_INSTALL_SUCCESS;
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (new_version.Equals(**current_version)) {
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    VLOG(1) << "Install repaired of version " << new_version.GetString();
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return installer::INSTALL_REPAIRED;
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (new_version.CompareTo(**current_version) > 0) {
2317dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    if (base::PathExists(new_chrome_exe)) {
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      VLOG(1) << "Version updated to " << new_version.GetString()
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              << " while running " << (*current_version)->GetString();
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return installer::IN_USE_UPDATED;
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    VLOG(1) << "Version updated to " << new_version.GetString();
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return installer::NEW_VERSION_UPDATED;
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LOG(ERROR) << "Not sure how we got here while updating"
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             << ", new version: " << new_version.GetString()
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             << ", old version: " << (*current_version)->GetString();
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return installer::INSTALL_FAILED;
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// Deletes the old "Uninstall Google Chrome" shortcut in the Start menu which
2486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// was installed prior to Chrome 24.
249cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void CleanupLegacyShortcuts(const installer::InstallerState& installer_state,
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            BrowserDistribution* dist,
2512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                            const base::FilePath& chrome_exe) {
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellUtil::ShellChange shortcut_level = installer_state.system_install() ?
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ShellUtil::SYSTEM_LEVEL : ShellUtil::CURRENT_USER;
2542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath uninstall_shortcut_path;
255a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ShellUtil::GetShortcutPath(ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_DIR,
256a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                             dist, shortcut_level, &uninstall_shortcut_path);
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uninstall_shortcut_path = uninstall_shortcut_path.Append(
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      dist->GetUninstallLinkName() + installer::kLnkExt);
2597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  base::DeleteFile(uninstall_shortcut_path, false);
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns the appropriate shortcut operations for App Launcher,
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// based on state of installation and master_preferences.
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)installer::InstallShortcutOperation GetAppLauncherShortcutOperation(
265cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const installer::InstallationState& original_state,
266cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const installer::InstallerState& installer_state) {
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const installer::ProductState* original_app_host_state =
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      original_state.GetProductState(installer_state.system_install(),
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     BrowserDistribution::CHROME_APP_HOST);
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool app_launcher_exists = original_app_host_state &&
2712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      original_app_host_state->uninstall_command()
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          .HasSwitch(installer::switches::kChromeAppLauncher);
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!app_launcher_exists)
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return installer::INSTALL_SHORTCUT_CREATE_ALL;
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return installer::INSTALL_SHORTCUT_REPLACE_EXISTING;
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // end namespace
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace installer {
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void EscapeXmlAttributeValueInSingleQuotes(base::string16* att_value) {
284cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::ReplaceChars(*att_value, base::ASCIIToUTF16("&"),
285cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                     base::ASCIIToUTF16("&amp;"), att_value);
286cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::ReplaceChars(*att_value, base::ASCIIToUTF16("'"),
287cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                     base::ASCIIToUTF16("&apos;"), att_value);
288cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::ReplaceChars(*att_value, base::ASCIIToUTF16("<"),
289cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                     base::ASCIIToUTF16("&lt;"), att_value);
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool CreateVisualElementsManifest(const base::FilePath& src_path,
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  const Version& version) {
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Construct the relative path to the versioned VisualElements directory.
295cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::string16 elements_dir(base::ASCIIToUTF16(version.GetString()));
2962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  elements_dir.push_back(base::FilePath::kSeparators[0]);
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  elements_dir.append(installer::kVisualElements);
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Some distributions of Chromium may not include visual elements. Only
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // proceed if this distribution does.
3017dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!base::PathExists(src_path.Append(elements_dir))) {
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    VLOG(1) << "No visual elements found, not writing "
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            << installer::kVisualElementsManifest << " to " << src_path.value();
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // A printf_p-style format string for generating the visual elements
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // manifest. Required arguments, in order, are:
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //   - Localized display name for the product.
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //   - Relative path to the VisualElements directory.
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static const char kManifestTemplate[] =
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "<Application>\r\n"
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "  <VisualElements\r\n"
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "      DisplayName='%1$ls'\r\n"
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "      Logo='%2$ls\\Logo.png'\r\n"
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "      SmallLogo='%2$ls\\SmallLogo.png'\r\n"
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "      ForegroundText='light'\r\n"
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "      BackgroundColor='#323232'>\r\n"
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "    <DefaultTile ShowName='allLogos'/>\r\n"
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "    <SplashScreen Image='%2$ls\\splash-620x300.png'/>\r\n"
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "  </VisualElements>\r\n"
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "</Application>";
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
323cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const base::string16 manifest_template(
324cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        base::ASCIIToUTF16(kManifestTemplate));
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    BrowserDistribution* dist = BrowserDistribution::GetSpecificDistribution(
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        BrowserDistribution::CHROME_BROWSER);
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // TODO(grt): http://crbug.com/75152 Write a reference to a localized
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // resource for |display_name|.
3305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::string16 display_name(dist->GetDisplayName());
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EscapeXmlAttributeValueInSingleQuotes(&display_name);
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Fill the manifest with the desired values.
3345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::string16 manifest16(base::StringPrintf(
3355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        manifest_template.c_str(), display_name.c_str(), elements_dir.c_str()));
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Write the manifest to |src_path|.
338cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const std::string manifest(base::UTF16ToUTF8(manifest16));
3395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    int size = base::checked_cast<int>(manifest.size());
340a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (base::WriteFile(
34168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)        src_path.Append(installer::kVisualElementsManifest),
34268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)            manifest.c_str(), size) == size) {
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      VLOG(1) << "Successfully wrote " << installer::kVisualElementsManifest
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              << " to " << src_path.value();
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return true;
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      PLOG(ERROR) << "Error writing " << installer::kVisualElementsManifest
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  << " to " << src_path.value();
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void CreateOrUpdateShortcuts(
3552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& target,
356cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const installer::Product& product,
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const MasterPreferences& prefs,
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    InstallShortcutLevel install_level,
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    InstallShortcutOperation install_operation) {
3602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool do_not_create_any_shortcuts = false;
3612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  prefs.GetBool(master_preferences::kDoNotCreateAnyShortcuts,
3622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                &do_not_create_any_shortcuts);
3632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (do_not_create_any_shortcuts)
3642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
3652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Extract shortcut preferences from |prefs|.
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool do_not_create_desktop_shortcut = false;
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool do_not_create_quick_launch_shortcut = false;
3692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool do_not_create_taskbar_shortcut = false;
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool alternate_desktop_shortcut = false;
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  prefs.GetBool(master_preferences::kDoNotCreateDesktopShortcut,
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                &do_not_create_desktop_shortcut);
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  prefs.GetBool(master_preferences::kDoNotCreateQuickLaunchShortcut,
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                &do_not_create_quick_launch_shortcut);
3752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  prefs.GetBool(master_preferences::kDoNotCreateTaskbarShortcut,
3762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                &do_not_create_taskbar_shortcut);
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  prefs.GetBool(master_preferences::kAltShortcutText,
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                &alternate_desktop_shortcut);
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  BrowserDistribution* dist = product.distribution();
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The default operation on update is to overwrite shortcuts with the
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // currently desired properties, but do so only for shortcuts that still
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // exist.
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellUtil::ShortcutOperation shortcut_operation;
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (install_operation) {
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case INSTALL_SHORTCUT_CREATE_ALL:
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shortcut_operation = ShellUtil::SHELL_SHORTCUT_CREATE_ALWAYS;
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case INSTALL_SHORTCUT_CREATE_EACH_IF_NO_SYSTEM_LEVEL:
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shortcut_operation = ShellUtil::SHELL_SHORTCUT_CREATE_IF_NO_SYSTEM_LEVEL;
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DCHECK(install_operation == INSTALL_SHORTCUT_REPLACE_EXISTING);
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shortcut_operation = ShellUtil::SHELL_SHORTCUT_REPLACE_EXISTING;
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Shortcuts are always installed per-user unless specified.
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellUtil::ShellChange shortcut_level = (install_level == ALL_USERS ?
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ShellUtil::SYSTEM_LEVEL : ShellUtil::CURRENT_USER);
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |base_properties|: The basic properties to set on every shortcut installed
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // (to be refined on a per-shortcut basis).
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellUtil::ShortcutProperties base_properties(shortcut_level);
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  product.AddDefaultShortcutProperties(target, &base_properties);
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!do_not_create_desktop_shortcut ||
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shortcut_operation == ShellUtil::SHELL_SHORTCUT_REPLACE_EXISTING) {
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ShellUtil::ShortcutProperties desktop_properties(base_properties);
4113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (alternate_desktop_shortcut) {
4123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      desktop_properties.set_shortcut_name(
4133551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          dist->GetShortcutName(
4143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)              BrowserDistribution::SHORTCUT_CHROME_ALTERNATE));
4153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    }
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ExecuteAndLogShortcutOperation(
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ShellUtil::SHORTCUT_LOCATION_DESKTOP, dist, desktop_properties,
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        shortcut_operation);
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // On update there is no harm in always trying to update the alternate
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Desktop shortcut.
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!alternate_desktop_shortcut &&
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        shortcut_operation == ShellUtil::SHELL_SHORTCUT_REPLACE_EXISTING) {
4243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      desktop_properties.set_shortcut_name(
4253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          dist->GetShortcutName(
4263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)              BrowserDistribution::SHORTCUT_CHROME_ALTERNATE));
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ExecuteAndLogShortcutOperation(
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          ShellUtil::SHORTCUT_LOCATION_DESKTOP, dist, desktop_properties,
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          shortcut_operation);
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!do_not_create_quick_launch_shortcut ||
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shortcut_operation == ShellUtil::SHELL_SHORTCUT_REPLACE_EXISTING) {
4352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // There is no such thing as an all-users Quick Launch shortcut, always
4362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // install the per-user shortcut.
4372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ShellUtil::ShortcutProperties quick_launch_properties(base_properties);
4382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    quick_launch_properties.level = ShellUtil::CURRENT_USER;
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ExecuteAndLogShortcutOperation(
4402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        ShellUtil::SHORTCUT_LOCATION_QUICK_LAUNCH, dist,
4412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        quick_launch_properties, shortcut_operation);
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellUtil::ShortcutProperties start_menu_properties(base_properties);
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // IMPORTANT: Only the default (no arguments and default browserappid) browser
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // shortcut in the Start menu (Start screen on Win8+) should be made dual
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // mode.
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  start_menu_properties.set_dual_mode(true);
4492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!do_not_create_taskbar_shortcut &&
4502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      (shortcut_operation == ShellUtil::SHELL_SHORTCUT_CREATE_ALWAYS ||
4512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       shortcut_operation ==
4522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)           ShellUtil::SHELL_SHORTCUT_CREATE_IF_NO_SYSTEM_LEVEL)) {
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    start_menu_properties.set_pin_to_taskbar(true);
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
455a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ExecuteAndLogShortcutOperation(
456a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_DIR, dist,
457a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      start_menu_properties, shortcut_operation);
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
460cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void RegisterChromeOnMachine(const installer::InstallerState& installer_state,
461cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                             const installer::Product& product,
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             bool make_chrome_default) {
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(product.is_chrome());
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Try to add Chrome to Media Player shim inclusion list. We don't do any
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // error checking here because this operation will fail if user doesn't
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // have admin rights and we want to ignore the error.
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AddChromeToMediaPlayerList();
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Make Chrome the default browser if desired when possible. Otherwise, only
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // register it with Windows.
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  BrowserDistribution* dist = product.distribution();
4735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const base::string16 chrome_exe(
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      installer_state.target_path().Append(installer::kChromeExe).value());
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  VLOG(1) << "Registering Chrome as browser: " << chrome_exe;
4762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (make_chrome_default && ShellUtil::CanMakeChromeDefaultUnattended()) {
4772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int level = ShellUtil::CURRENT_USER;
4782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (installer_state.system_install())
4792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      level = level | ShellUtil::SYSTEM_LEVEL;
4802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ShellUtil::MakeChromeDefault(dist, level, chrome_exe, true);
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
4825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ShellUtil::RegisterChromeBrowser(dist, chrome_exe, base::string16(), false);
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)InstallStatus InstallOrUpdateProduct(
487cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const installer::InstallationState& original_state,
488cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const installer::InstallerState& installer_state,
4892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& setup_path,
4902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& archive_path,
4912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& install_temp_path,
4922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& src_path,
4932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& prefs_path,
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const MasterPreferences& prefs,
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const Version& new_version) {
4965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(!installer_state.products().empty());
4975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(robertshield): Removing the pending on-reboot moves should be done
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // elsewhere.
5005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Remove any scheduled MOVEFILE_DELAY_UNTIL_REBOOT entries in the target of
5015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // this installation. These may have been added during a previous uninstall of
5025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // the same version.
5035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  LOG_IF(ERROR, !RemoveFromMovesPendingReboot(installer_state.target_path()))
5045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      << "Error accessing pending moves value.";
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Create VisualElementManifest.xml in |src_path| (if required) so that it
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // looks as if it had been extracted from the archive when calling
5085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // InstallNewVersion() below.
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  installer_state.UpdateStage(installer::CREATING_VISUAL_MANIFEST);
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CreateVisualElementsManifest(src_path, new_version);
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<Version> existing_version;
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  InstallStatus result = InstallNewVersion(original_state, installer_state,
5145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      setup_path, archive_path, src_path, install_temp_path, new_version,
5155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &existing_version);
5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(robertshield): Everything below this line should instead be captured
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // by WorkItems.
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!InstallUtil::GetInstallReturnCode(result)) {
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    installer_state.UpdateStage(installer::UPDATING_CHANNELS);
5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Update the modifiers on the channel values for the product(s) being
5235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // installed and for the binaries in case of multi-install.
5245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    installer_state.UpdateChannels();
5255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    installer_state.UpdateStage(installer::COPYING_PREFERENCES_FILE);
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (result == FIRST_INSTALL_SUCCESS && !prefs_path.empty())
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CopyPreferenceFileForFirstRun(installer_state, prefs_path);
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    installer_state.UpdateStage(installer::CREATING_SHORTCUTS);
5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
533cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const installer::Product* app_launcher_product =
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        installer_state.FindProduct(BrowserDistribution::CHROME_APP_HOST);
5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Creates shortcuts for App Launcher.
5362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (app_launcher_product) {
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // TODO(huangs): Remove this check once we have system-level App Host.
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DCHECK(!installer_state.system_install());
5392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const base::FilePath app_host_exe(
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          installer_state.target_path().Append(kChromeAppHostExe));
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      InstallShortcutOperation app_launcher_shortcut_operation =
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          GetAppLauncherShortcutOperation(original_state, installer_state);
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Always install per-user shortcuts for App Launcher.
5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CreateOrUpdateShortcuts(app_host_exe, *app_launcher_product, prefs,
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              CURRENT_USER, app_launcher_shortcut_operation);
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
549cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const installer::Product* chrome_product =
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        installer_state.FindProduct(BrowserDistribution::CHROME_BROWSER);
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Creates shortcuts for Chrome.
5525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (chrome_product) {
5532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      BrowserDistribution* chrome_dist = chrome_product->distribution();
5542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const base::FilePath chrome_exe(
5555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          installer_state.target_path().Append(kChromeExe));
5562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      CleanupLegacyShortcuts(installer_state, chrome_dist, chrome_exe);
5572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Install per-user shortcuts on user-level installs and all-users
5592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // shortcuts on system-level installs. Note that Active Setup will take
5602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // care of installing missing per-user shortcuts on system-level install
5612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // (i.e., quick launch, taskbar pin, and possibly deleted all-users
5622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // shortcuts).
5632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      InstallShortcutLevel install_level = installer_state.system_install() ?
5642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          ALL_USERS : CURRENT_USER;
5655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      InstallShortcutOperation install_operation =
5675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          INSTALL_SHORTCUT_REPLACE_EXISTING;
5685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (result == installer::FIRST_INSTALL_SUCCESS ||
5692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          result == installer::INSTALL_REPAIRED ||
5702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          !original_state.GetProductState(installer_state.system_install(),
5712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                          chrome_dist->GetType())) {
5722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        // Always create the shortcuts on a new install, a repair install, and
5732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        // when the Chrome product is being added to the current install.
5745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        install_operation = INSTALL_SHORTCUT_CREATE_ALL;
5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
5765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      CreateOrUpdateShortcuts(chrome_exe, *chrome_product, prefs, install_level,
5785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              install_operation);
5795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
5805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (chrome_product) {
5825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Register Chrome and, if requested, make Chrome the default browser.
5835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      installer_state.UpdateStage(installer::REGISTERING_CHROME);
5845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bool make_chrome_default = false;
5865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      prefs.GetBool(master_preferences::kMakeChromeDefault,
5875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    &make_chrome_default);
5885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // If this is not the user's first Chrome install, but they have chosen
5905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Chrome to become their default browser on the download page, we must
5915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // force it here because the master_preferences file will not get copied
5925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // into the build.
5935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bool force_chrome_default_for_user = false;
5945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (result == NEW_VERSION_UPDATED ||
5955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          result == INSTALL_REPAIRED) {
5965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        prefs.GetBool(master_preferences::kMakeChromeDefaultForUser,
5975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      &force_chrome_default_for_user);
5985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
5995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      RegisterChromeOnMachine(installer_state, *chrome_product,
6015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          make_chrome_default || force_chrome_default_for_user);
6025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Configure auto-launch.
6045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (result == FIRST_INSTALL_SUCCESS) {
6055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        installer_state.UpdateStage(installer::CONFIGURE_AUTO_LAUNCH);
6065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // Add auto-launch key if specified in master_preferences.
6085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        bool auto_launch_chrome = false;
6095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        prefs.GetBool(
6105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            installer::master_preferences::kAutoLaunchChrome,
6115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            &auto_launch_chrome);
6125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (auto_launch_chrome) {
6135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          auto_launch_util::EnableForegroundStartAtLogin(
614cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)              base::ASCIIToUTF16(chrome::kInitialProfile),
6155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              installer_state.target_path());
6165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
6175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
6185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    installer_state.UpdateStage(installer::REMOVING_OLD_VERSIONS);
6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    installer_state.RemoveOldVersionDirectories(
6235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        new_version,
6245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        existing_version.get(),
6255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        install_temp_path);
6265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return result;
6295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
631cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void HandleOsUpgradeForBrowser(const installer::InstallerState& installer_state,
632cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                               const installer::Product& chrome) {
6335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(chrome.is_chrome());
6345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Upon upgrading to Windows 8, we need to fix Chrome shortcuts and register
6355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Chrome, so that Metro Chrome would work if Chrome is the default browser.
6365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
6375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    VLOG(1) << "Updating and registering shortcuts.";
6385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Read master_preferences copied beside chrome.exe at install.
6395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MasterPreferences prefs(
6405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        installer_state.target_path().AppendASCII(kDefaultMasterPrefs));
6415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Unfortunately, if this is a system-level install, we can't update the
6435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // shortcuts of each individual user (this only matters if this is an OS
6442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // upgrade from XP/Vista to Win7+ as some properties are only set on
6455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // shortcuts as of Win7).
6465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // At least attempt to update potentially existing all-users shortcuts.
6475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    InstallShortcutLevel level = installer_state.system_install() ?
6485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ALL_USERS : CURRENT_USER;
6492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::FilePath chrome_exe(installer_state.target_path().Append(kChromeExe));
6505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CreateOrUpdateShortcuts(
6515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        chrome_exe, chrome, prefs, level, INSTALL_SHORTCUT_REPLACE_EXISTING);
6525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    RegisterChromeOnMachine(installer_state, chrome, false);
6535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// NOTE: Should the work done here, on Active Setup, change: kActiveSetupVersion
6575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in install_worker.cc needs to be increased for Active Setup to invoke this
6585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// again for all users of this install.
6592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void HandleActiveSetupForBrowser(const base::FilePath& installation_root,
660cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                 const installer::Product& chrome,
6615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 bool force) {
6625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(chrome.is_chrome());
6635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Only create shortcuts on Active Setup if the first run sentinel is not
6645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // present for this user (as some shortcuts used to be installed on first
6655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // run and this could otherwise re-install shortcuts for users that have
6665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // already deleted them in the past).
6672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Decide whether to create the shortcuts or simply replace existing
6682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // shortcuts; if the decision is to create them, only shortcuts whose matching
6692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // all-users shortcut isn't present on the system will be created.
6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  InstallShortcutOperation install_operation =
671effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      (!force && InstallUtil::IsFirstRunSentinelPresent() ?
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           INSTALL_SHORTCUT_REPLACE_EXISTING :
6735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           INSTALL_SHORTCUT_CREATE_EACH_IF_NO_SYSTEM_LEVEL);
6745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Read master_preferences copied beside chrome.exe at install.
6765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MasterPreferences prefs(installation_root.AppendASCII(kDefaultMasterPrefs));
6772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath chrome_exe(installation_root.Append(kChromeExe));
6785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CreateOrUpdateShortcuts(
6795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      chrome_exe, chrome, prefs, CURRENT_USER, install_operation);
6805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace installer
683