install_worker.cc revision 3551c9c881056c480085172ff9840cab31610854
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4//
5// This file contains the definitions of the installer functions that build
6// the WorkItemList used to install the application.
7
8#include "chrome/installer/setup/install_worker.h"
9
10#include <oaidl.h>
11#include <shlobj.h>
12#include <time.h>
13
14#include <vector>
15
16#include "base/bind.h"
17#include "base/command_line.h"
18#include "base/file_util.h"
19#include "base/files/file_path.h"
20#include "base/logging.h"
21#include "base/memory/scoped_ptr.h"
22#include "base/path_service.h"
23#include "base/strings/string16.h"
24#include "base/strings/string_util.h"
25#include "base/strings/utf_string_conversions.h"
26#include "base/version.h"
27#include "base/win/registry.h"
28#include "base/win/scoped_comptr.h"
29#include "base/win/windows_version.h"
30#include "chrome/common/chrome_constants.h"
31#include "chrome/common/chrome_switches.h"
32#include "chrome/installer/setup/install.h"
33#include "chrome/installer/setup/setup_constants.h"
34#include "chrome/installer/setup/setup_util.h"
35#include "chrome/installer/util/browser_distribution.h"
36#include "chrome/installer/util/callback_work_item.h"
37#include "chrome/installer/util/conditional_work_item_list.h"
38#include "chrome/installer/util/create_reg_key_work_item.h"
39#include "chrome/installer/util/google_update_constants.h"
40#include "chrome/installer/util/helper.h"
41#include "chrome/installer/util/install_util.h"
42#include "chrome/installer/util/installation_state.h"
43#include "chrome/installer/util/installer_state.h"
44#include "chrome/installer/util/l10n_string_util.h"
45#include "chrome/installer/util/product.h"
46#include "chrome/installer/util/set_reg_value_work_item.h"
47#include "chrome/installer/util/shell_util.h"
48#include "chrome/installer/util/util_constants.h"
49#include "chrome/installer/util/work_item_list.h"
50
51#if !defined(OMIT_CHROME_FRAME)
52#include "chrome_frame/chrome_tab.h"
53#endif
54
55using base::win::RegKey;
56
57namespace installer {
58
59namespace {
60
61enum ElevationPolicyId {
62  CURRENT_ELEVATION_POLICY,
63  OLD_ELEVATION_POLICY,
64};
65
66// The version identifying the work done by setup.exe --configure-user-settings
67// on user login by way of Active Setup.  Increase this value if the work done
68// in setup_main.cc's handling of kConfigureUserSettings changes and should be
69// executed again for all users.
70const wchar_t kActiveSetupVersion[] = L"24,0,0,0";
71
72// Although the UUID of the ChromeFrame class is used for the "current" value,
73// this is done only as a convenience; there is no need for the GUID of the Low
74// Rights policies to match the ChromeFrame class's GUID.  Hence, it is safe to
75// use this completely unrelated GUID for the "old" policies.
76const wchar_t kIELowRightsPolicyOldGuid[] =
77    L"{6C288DD7-76FB-4721-B628-56FAC252E199}";
78
79#if defined(OMIT_CHROME_FRAME)
80// For historical reasons, this GUID is the same as CLSID_ChromeFrame. Included
81// here to break the dependency on Chrome Frame when Chrome Frame is not being
82// built.
83// TODO(robertshield): Remove this when Chrome Frame works with Aura.
84const wchar_t kIELowRightsPolicyCurrentGuid[] =
85    L"{E0A900DF-9611-4446-86BD-4B1D47E7DB2A}";
86#endif
87
88const wchar_t kElevationPolicyKeyPath[] =
89    L"SOFTWARE\\Microsoft\\Internet Explorer\\Low Rights\\ElevationPolicy\\";
90
91void GetIELowRightsElevationPolicyKeyPath(ElevationPolicyId policy,
92                                          string16* key_path) {
93  DCHECK(policy == CURRENT_ELEVATION_POLICY || policy == OLD_ELEVATION_POLICY);
94  key_path->assign(kElevationPolicyKeyPath,
95                   arraysize(kElevationPolicyKeyPath) - 1);
96  if (policy == CURRENT_ELEVATION_POLICY) {
97#if defined(OMIT_CHROME_FRAME)
98    key_path->append(kIELowRightsPolicyCurrentGuid,
99                     arraysize(kIELowRightsPolicyCurrentGuid) - 1);
100#else
101    wchar_t cf_clsid[64];
102    int len = StringFromGUID2(__uuidof(ChromeFrame), &cf_clsid[0],
103                              arraysize(cf_clsid));
104    key_path->append(&cf_clsid[0], len - 1);
105#endif
106  } else {
107    key_path->append(kIELowRightsPolicyOldGuid,
108                     arraysize(kIELowRightsPolicyOldGuid)- 1);
109  }
110}
111
112// Local helper to call AddRegisterComDllWorkItems for all DLLs in a set of
113// products managed by a given package.
114// |old_version| can be NULL to indicate no Chrome is currently installed.
115void AddRegisterComDllWorkItemsForPackage(const InstallerState& installer_state,
116                                          const Version* old_version,
117                                          const Version& new_version,
118                                          WorkItemList* work_item_list) {
119  // First collect the list of DLLs to be registered from each product.
120  std::vector<base::FilePath> com_dll_list;
121  installer_state.AddComDllList(&com_dll_list);
122
123  // Then, if we got some, attempt to unregister the DLLs from the old
124  // version directory and then re-register them in the new one.
125  // Note that if we are migrating the install directory then we will not
126  // successfully unregister the old DLLs.
127  // TODO(robertshield): See whether we need to fix the migration case.
128  // TODO(robertshield): If we ever remove a DLL from a product, this will
129  // not unregister it on update. We should build the unregistration list from
130  // saved state instead of assuming it is the same as the registration list.
131  if (!com_dll_list.empty()) {
132    if (old_version) {
133      base::FilePath old_dll_path(installer_state.target_path().AppendASCII(
134          old_version->GetString()));
135
136      installer::AddRegisterComDllWorkItems(old_dll_path,
137                                            com_dll_list,
138                                            installer_state.system_install(),
139                                            false,  // Unregister
140                                            true,   // May fail
141                                            work_item_list);
142    }
143
144    base::FilePath dll_path(installer_state.target_path().AppendASCII(
145        new_version.GetString()));
146    installer::AddRegisterComDllWorkItems(dll_path,
147                                          com_dll_list,
148                                          installer_state.system_install(),
149                                          true,   // Register
150                                          false,  // Must succeed.
151                                          work_item_list);
152  }
153}
154
155void AddInstallerCopyTasks(const InstallerState& installer_state,
156                           const base::FilePath& setup_path,
157                           const base::FilePath& archive_path,
158                           const base::FilePath& temp_path,
159                           const Version& new_version,
160                           WorkItemList* install_list) {
161  DCHECK(install_list);
162  base::FilePath installer_dir(
163      installer_state.GetInstallerDirectory(new_version));
164  install_list->AddCreateDirWorkItem(installer_dir);
165
166  base::FilePath exe_dst(installer_dir.Append(setup_path.BaseName()));
167
168  if (exe_dst != setup_path) {
169    install_list->AddCopyTreeWorkItem(setup_path.value(), exe_dst.value(),
170                                      temp_path.value(), WorkItem::ALWAYS);
171  }
172
173  if (installer_state.RequiresActiveSetup()) {
174    // Make a copy of setup.exe with a different name so that Active Setup
175    // doesn't require an admin on XP thanks to Application Compatibility.
176    base::FilePath active_setup_exe(installer_dir.Append(kActiveSetupExe));
177    install_list->AddCopyTreeWorkItem(
178        setup_path.value(), active_setup_exe.value(), temp_path.value(),
179        WorkItem::ALWAYS);
180  }
181
182  // If only the App Host (not even the Chrome Binaries) is being installed,
183  // this must be a user-level App Host piggybacking on system-level Chrome
184  // Binaries. Only setup.exe is required, and only for uninstall.
185  if (installer_state.products().size() != 1 ||
186      !installer_state.FindProduct(BrowserDistribution::CHROME_APP_HOST)) {
187    base::FilePath archive_dst(installer_dir.Append(archive_path.BaseName()));
188    if (archive_path != archive_dst) {
189      // In the past, we copied rather than moved for system level installs so
190      // that the permissions of %ProgramFiles% would be picked up.  Now that
191      // |temp_path| is in %ProgramFiles% for system level installs (and in
192      // %LOCALAPPDATA% otherwise), there is no need to do this for the archive.
193      // Setup.exe, on the other hand, is created elsewhere so it must always be
194      // copied.
195      if (temp_path.IsParent(archive_path)) {
196        install_list->AddMoveTreeWorkItem(archive_path.value(),
197                                          archive_dst.value(),
198                                          temp_path.value(),
199                                          WorkItem::ALWAYS_MOVE);
200      } else {
201        // This may occur when setup is run out of an existing installation
202        // directory. For example, when quick-enabling user-level App Launcher
203        // from system-level Binaries. We can't (and don't want to) remove the
204        // system-level archive.
205        install_list->AddCopyTreeWorkItem(archive_path.value(),
206                                          archive_dst.value(),
207                                          temp_path.value(),
208                                          WorkItem::ALWAYS);
209      }
210    }
211  }
212}
213
214string16 GetRegCommandKey(BrowserDistribution* dist,
215                          const wchar_t* name) {
216  string16 cmd_key(dist->GetVersionKey());
217  cmd_key.append(1, base::FilePath::kSeparators[0])
218      .append(google_update::kRegCommandsKey)
219      .append(1, base::FilePath::kSeparators[0])
220      .append(name);
221  return cmd_key;
222}
223
224// Adds work items to create (or delete if uninstalling) app commands to launch
225// the app with a switch. The following criteria should be true:
226//  1. The switch takes one parameter.
227//  2. The command send pings.
228//  3. The command is web accessible.
229//  4. The command is run as the user.
230void AddCommandWithParameterWorkItems(const InstallerState& installer_state,
231                                      const InstallationState& machine_state,
232                                      const Version& new_version,
233                                      const Product& product,
234                                      const wchar_t* command_key,
235                                      const wchar_t* app,
236                                      const char* command_with_parameter,
237                                      WorkItemList* work_item_list) {
238  DCHECK(command_key);
239  DCHECK(app);
240  DCHECK(command_with_parameter);
241  DCHECK(work_item_list);
242
243  string16 full_cmd_key(GetRegCommandKey(product.distribution(), command_key));
244
245  if (installer_state.operation() == InstallerState::UNINSTALL) {
246    work_item_list->AddDeleteRegKeyWorkItem(
247        installer_state.root_key(), full_cmd_key)->set_log_message(
248            "removing " + WideToASCII(command_key) + " command");
249  } else {
250    CommandLine cmd_line(installer_state.target_path().Append(app));
251    cmd_line.AppendSwitchASCII(command_with_parameter, "%1");
252
253    AppCommand cmd(cmd_line.GetCommandLineString());
254    cmd.set_sends_pings(true);
255    cmd.set_is_web_accessible(true);
256    cmd.set_is_run_as_user(true);
257    cmd.AddWorkItems(installer_state.root_key(), full_cmd_key, work_item_list);
258  }
259}
260
261void AddInstallAppCommandWorkItems(const InstallerState& installer_state,
262                                   const InstallationState& machine_state,
263                                   const Version& new_version,
264                                   const Product& product,
265                                   WorkItemList* work_item_list) {
266  DCHECK(product.is_chrome_app_host());
267  AddCommandWithParameterWorkItems(installer_state, machine_state, new_version,
268                                   product, kCmdInstallApp,
269                                   installer::kChromeAppHostExe,
270                                   ::switches::kInstallFromWebstore,
271                                   work_item_list);
272}
273
274void AddInstallExtensionCommandWorkItem(const InstallerState& installer_state,
275                                        const InstallationState& machine_state,
276                                        const base::FilePath& setup_path,
277                                        const Version& new_version,
278                                        const Product& product,
279                                        WorkItemList* work_item_list) {
280  DCHECK(product.is_chrome());
281  AddCommandWithParameterWorkItems(installer_state, machine_state, new_version,
282                                   product, kCmdInstallExtension,
283                                   installer::kChromeExe,
284                                   ::switches::kLimitedInstallFromWebstore,
285                                   work_item_list);
286}
287
288// Returns the basic CommandLine to setup.exe for a quick-enable operation on
289// the binaries. This will unconditionally include --multi-install as well as
290// --verbose-logging if the current installation was launched with
291// --verbose-logging.  |setup_path| and |new_version| are optional only when
292// the operation is an uninstall.
293CommandLine GetGenericQuickEnableCommand(
294    const InstallerState& installer_state,
295    const InstallationState& machine_state,
296    const base::FilePath& setup_path,
297    const Version& new_version) {
298  // Only valid for multi-install operations.
299  DCHECK(installer_state.is_multi_install());
300  // Only valid when Chrome Binaries aren't being uninstalled.
301  DCHECK(installer_state.operation() != InstallerState::UNINSTALL ||
302         !installer_state.FindProduct(BrowserDistribution::CHROME_BINARIES));
303  // setup_path and new_version are required when not uninstalling.
304  DCHECK(installer_state.operation() == InstallerState::UNINSTALL ||
305         (!setup_path.empty() && new_version.IsValid()));
306
307  // The path to setup.exe contains the version of the Chrome Binaries, so it
308  // takes a little work to get it right.
309  base::FilePath binaries_setup_path;
310  if (installer_state.operation() == InstallerState::UNINSTALL) {
311    // One or more products are being uninstalled, but not Chrome Binaries.
312    // Use the path to the currently installed Chrome Binaries' setup.exe.
313    const ProductState* product_state = machine_state.GetProductState(
314        installer_state.system_install(),
315        BrowserDistribution::CHROME_BINARIES);
316    DCHECK(product_state);
317    binaries_setup_path = product_state->uninstall_command().GetProgram();
318  } else {
319    // Chrome Binaries are being installed, updated, or otherwise operated on.
320    // Use the path to the given |setup_path| in the normal location of
321    // multi-install Chrome Binaries of the given |version|.
322    binaries_setup_path = installer_state.GetInstallerDirectory(new_version)
323                              .Append(setup_path.BaseName());
324  }
325  DCHECK(!binaries_setup_path.empty());
326
327  CommandLine cmd_line(binaries_setup_path);
328  cmd_line.AppendSwitch(switches::kMultiInstall);
329  if (installer_state.verbose_logging())
330    cmd_line.AppendSwitch(switches::kVerboseLogging);
331  return cmd_line;
332}
333
334// Adds work items to add the "quick-enable-application-host" command to the
335// multi-installer binaries' version key on the basis of the current operation
336// (represented in |installer_state|) and the pre-existing machine configuration
337// (represented in |machine_state|).
338void AddQuickEnableApplicationLauncherWorkItems(
339    const InstallerState& installer_state,
340    const InstallationState& machine_state,
341    const base::FilePath& setup_path,
342    const Version& new_version,
343    WorkItemList* work_item_list) {
344  DCHECK(work_item_list);
345
346  bool will_have_chrome_binaries =
347      WillProductBePresentAfterSetup(installer_state, machine_state,
348                                     BrowserDistribution::CHROME_BINARIES);
349
350  // For system-level binaries there is no way to keep the command state in sync
351  // with the installation/uninstallation of the Application Launcher (which is
352  // always at user-level).  So we do not try to remove the command, i.e., it
353  // will always be installed if the Chrome Binaries are installed.
354  if (will_have_chrome_binaries) {
355    string16 cmd_key(GetRegCommandKey(
356                         BrowserDistribution::GetSpecificDistribution(
357                             BrowserDistribution::CHROME_BINARIES),
358                         kCmdQuickEnableApplicationHost));
359    CommandLine cmd_line(GetGenericQuickEnableCommand(installer_state,
360                                                      machine_state,
361                                                      setup_path,
362                                                      new_version));
363    // kMultiInstall and kVerboseLogging were processed above.
364    cmd_line.AppendSwitch(switches::kChromeAppLauncher);
365    cmd_line.AppendSwitch(switches::kEnsureGoogleUpdatePresent);
366    AppCommand cmd(cmd_line.GetCommandLineString());
367    cmd.set_sends_pings(true);
368    cmd.set_is_web_accessible(true);
369    cmd.set_is_run_as_user(true);
370    cmd.AddWorkItems(installer_state.root_key(), cmd_key, work_item_list);
371  }
372}
373
374void AddProductSpecificWorkItems(const InstallationState& original_state,
375                                 const InstallerState& installer_state,
376                                 const base::FilePath& setup_path,
377                                 const Version& new_version,
378                                 WorkItemList* list) {
379  const Products& products = installer_state.products();
380  for (Products::const_iterator it = products.begin(); it < products.end();
381       ++it) {
382    const Product& p = **it;
383    if (p.is_chrome_frame()) {
384      AddChromeFrameWorkItems(original_state, installer_state, setup_path,
385                              new_version, p, list);
386    }
387    if (p.is_chrome_app_host()) {
388      AddInstallAppCommandWorkItems(installer_state, original_state,
389                                    new_version, p, list);
390    }
391    if (p.is_chrome()) {
392      AddOsUpgradeWorkItems(installer_state, setup_path, new_version, p,
393                            list);
394      AddInstallExtensionCommandWorkItem(installer_state, original_state,
395                                         setup_path, new_version, p, list);
396    }
397    if (p.is_chrome_binaries()) {
398      AddQueryEULAAcceptanceWorkItems(
399          installer_state, setup_path, new_version, p, list);
400      AddQuickEnableChromeFrameWorkItems(
401          installer_state, original_state, setup_path, new_version, list);
402      AddQuickEnableApplicationLauncherWorkItems(
403          installer_state, original_state, setup_path, new_version, list);
404    }
405  }
406}
407
408// This is called when an MSI installation is run. It may be that a user is
409// attempting to install the MSI on top of a non-MSI managed installation.
410// If so, try and remove any existing uninstallation shortcuts, as we want the
411// uninstall to be managed entirely by the MSI machinery (accessible via the
412// Add/Remove programs dialog).
413void AddDeleteUninstallShortcutsForMSIWorkItems(
414    const InstallerState& installer_state,
415    const Product& product,
416    const base::FilePath& temp_path,
417    WorkItemList* work_item_list) {
418  DCHECK(installer_state.is_msi())
419      << "This must only be called for MSI installations!";
420
421  // First attempt to delete the old installation's ARP dialog entry.
422  HKEY reg_root = installer_state.root_key();
423  string16 uninstall_reg(product.distribution()->GetUninstallRegPath());
424
425  WorkItem* delete_reg_key = work_item_list->AddDeleteRegKeyWorkItem(
426      reg_root, uninstall_reg);
427  delete_reg_key->set_ignore_failure(true);
428
429  // Then attempt to delete the old installation's start menu shortcut.
430  base::FilePath uninstall_link;
431  if (installer_state.system_install()) {
432    PathService::Get(base::DIR_COMMON_START_MENU, &uninstall_link);
433  } else {
434    PathService::Get(base::DIR_START_MENU, &uninstall_link);
435  }
436
437  if (uninstall_link.empty()) {
438    LOG(ERROR) << "Failed to get location for shortcut.";
439  } else {
440    uninstall_link = uninstall_link.Append(
441        product.distribution()->GetStartMenuShortcutSubfolder(
442            BrowserDistribution::SUBFOLDER_CHROME));
443    uninstall_link = uninstall_link.Append(
444        product.distribution()->GetUninstallLinkName() + installer::kLnkExt);
445    VLOG(1) << "Deleting old uninstall shortcut (if present): "
446            << uninstall_link.value();
447    WorkItem* delete_link = work_item_list->AddDeleteTreeWorkItem(
448        uninstall_link, temp_path);
449    delete_link->set_ignore_failure(true);
450    delete_link->set_log_message(
451        "Failed to delete old uninstall shortcut.");
452  }
453}
454
455// Adds Chrome specific install work items to |install_list|.
456// |current_version| can be NULL to indicate no Chrome is currently installed.
457void AddChromeWorkItems(const InstallationState& original_state,
458                        const InstallerState& installer_state,
459                        const base::FilePath& setup_path,
460                        const base::FilePath& archive_path,
461                        const base::FilePath& src_path,
462                        const base::FilePath& temp_path,
463                        const Version* current_version,
464                        const Version& new_version,
465                        WorkItemList* install_list) {
466  const base::FilePath& target_path = installer_state.target_path();
467
468  if (current_version) {
469    // Delete the archive from an existing install to save some disk space.  We
470    // make this an unconditional work item since there's no need to roll this
471    // back; if installation fails we'll be moved to the "-full" channel anyway.
472    base::FilePath old_installer_dir(
473        installer_state.GetInstallerDirectory(*current_version));
474    base::FilePath old_archive(
475        old_installer_dir.Append(installer::kChromeArchive));
476    // Don't delete the archive that we are actually installing from.
477    if (archive_path != old_archive) {
478      install_list->AddDeleteTreeWorkItem(old_archive, temp_path)->
479          set_ignore_failure(true);
480    }
481  }
482
483  // Delete any new_chrome.exe if present (we will end up creating a new one
484  // if required) and then copy chrome.exe
485  base::FilePath new_chrome_exe(target_path.Append(installer::kChromeNewExe));
486
487  install_list->AddDeleteTreeWorkItem(new_chrome_exe, temp_path);
488
489  if (installer_state.IsChromeFrameRunning(original_state)) {
490    VLOG(1) << "Chrome Frame in use. Copying to new_chrome.exe";
491    install_list->AddCopyTreeWorkItem(
492        src_path.Append(installer::kChromeExe).value(),
493        new_chrome_exe.value(),
494        temp_path.value(),
495        WorkItem::ALWAYS);
496  } else {
497    install_list->AddCopyTreeWorkItem(
498        src_path.Append(installer::kChromeExe).value(),
499        target_path.Append(installer::kChromeExe).value(),
500        temp_path.value(),
501        WorkItem::NEW_NAME_IF_IN_USE,
502        new_chrome_exe.value());
503  }
504
505  // Extra executable for 64 bit systems.
506  // NOTE: We check for "not disabled" so that if the API call fails, we play it
507  // safe and copy the executable anyway.
508  // NOTE: the file wow_helper.exe is only needed for Vista and below.
509  if (base::win::OSInfo::GetInstance()->wow64_status() !=
510      base::win::OSInfo::WOW64_DISABLED &&
511      base::win::GetVersion() <= base::win::VERSION_VISTA) {
512    install_list->AddMoveTreeWorkItem(
513        src_path.Append(installer::kWowHelperExe).value(),
514        target_path.Append(installer::kWowHelperExe).value(),
515        temp_path.value(),
516        WorkItem::ALWAYS_MOVE);
517  }
518
519  // Install kVisualElementsManifest if it is present in |src_path|. No need to
520  // make this a conditional work item as if the file is not there now, it will
521  // never be.
522  if (base::PathExists(
523          src_path.Append(installer::kVisualElementsManifest))) {
524    install_list->AddMoveTreeWorkItem(
525        src_path.Append(installer::kVisualElementsManifest).value(),
526        target_path.Append(installer::kVisualElementsManifest).value(),
527        temp_path.value(),
528        WorkItem::ALWAYS_MOVE);
529  } else {
530    // We do not want to have an old VisualElementsManifest pointing to an old
531    // version directory. Delete it as there wasn't a new one to replace it.
532    install_list->AddDeleteTreeWorkItem(
533        target_path.Append(installer::kVisualElementsManifest),
534        temp_path);
535  }
536
537  // For the component build to work with the installer, we need to drop a
538  // config file and a manifest by chrome.exe. These files are only found in
539  // the archive if this is a component build.
540#if defined(COMPONENT_BUILD)
541  static const base::FilePath::CharType kChromeExeConfig[] =
542      FILE_PATH_LITERAL("chrome.exe.config");
543  static const base::FilePath::CharType kChromeExeManifest[] =
544      FILE_PATH_LITERAL("chrome.exe.manifest");
545  install_list->AddMoveTreeWorkItem(
546      src_path.Append(kChromeExeConfig).value(),
547      target_path.Append(kChromeExeConfig).value(),
548      temp_path.value(),
549      WorkItem::ALWAYS_MOVE);
550  install_list->AddMoveTreeWorkItem(
551      src_path.Append(kChromeExeManifest).value(),
552      target_path.Append(kChromeExeManifest).value(),
553      temp_path.value(),
554      WorkItem::ALWAYS_MOVE);
555#endif  // defined(COMPONENT_BUILD)
556
557  // In the past, we copied rather than moved for system level installs so that
558  // the permissions of %ProgramFiles% would be picked up.  Now that |temp_path|
559  // is in %ProgramFiles% for system level installs (and in %LOCALAPPDATA%
560  // otherwise), there is no need to do this.
561  // Note that we pass true for check_duplicates to avoid failing on in-use
562  // repair runs if the current_version is the same as the new_version.
563  bool check_for_duplicates = (current_version &&
564                               current_version->Equals(new_version));
565  install_list->AddMoveTreeWorkItem(
566      src_path.AppendASCII(new_version.GetString()).value(),
567      target_path.AppendASCII(new_version.GetString()).value(),
568      temp_path.value(),
569      check_for_duplicates ? WorkItem::CHECK_DUPLICATES :
570                             WorkItem::ALWAYS_MOVE);
571
572  // Delete any old_chrome.exe if present (ignore failure if it's in use).
573  install_list->AddDeleteTreeWorkItem(
574      target_path.Append(installer::kChromeOldExe), temp_path)->
575          set_ignore_failure(true);
576}
577
578// Probes COM machinery to get an instance of delegate_execute.exe's
579// CommandExecuteImpl class.  This is required so that COM purges its cache of
580// the path to the binary, which changes on updates.  This callback
581// unconditionally returns true since an install should not be aborted if the
582// probe fails.
583bool ProbeCommandExecuteCallback(const string16& command_execute_id,
584                                 const CallbackWorkItem& work_item) {
585  // Noop on rollback.
586  if (work_item.IsRollback())
587    return true;
588
589  CLSID class_id = {};
590
591  HRESULT hr = CLSIDFromString(command_execute_id.c_str(), &class_id);
592  if (FAILED(hr)) {
593    LOG(DFATAL) << "Failed converting \"" << command_execute_id << "\" to "
594                   "CLSID; hr=0x" << std::hex << hr;
595  } else {
596    base::win::ScopedComPtr<IUnknown> command_execute_impl;
597    hr = command_execute_impl.CreateInstance(class_id, NULL,
598                                             CLSCTX_LOCAL_SERVER);
599    if (hr != REGDB_E_CLASSNOTREG) {
600      LOG(ERROR) << "Unexpected result creating CommandExecuteImpl; hr=0x"
601                 << std::hex << hr;
602    }
603  }
604
605  return true;
606}
607
608void AddUninstallDelegateExecuteWorkItems(HKEY root,
609                                          const string16& delegate_execute_path,
610                                          WorkItemList* list) {
611  VLOG(1) << "Adding unregistration items for DelegateExecute verb handler in "
612          << root;
613  list->AddDeleteRegKeyWorkItem(root, delegate_execute_path);
614
615  // In the past, the ICommandExecuteImpl interface and a TypeLib were both
616  // registered.  Remove these since this operation may be updating a machine
617  // that had the old registrations.
618  list->AddDeleteRegKeyWorkItem(root,
619                                L"Software\\Classes\\Interface\\"
620                                L"{0BA0D4E9-2259-4963-B9AE-A839F7CB7544}");
621  list->AddDeleteRegKeyWorkItem(root,
622                                L"Software\\Classes\\TypeLib\\"
623#if defined(GOOGLE_CHROME_BUILD)
624                                L"{4E805ED8-EBA0-4601-9681-12815A56EBFD}"
625#else
626                                L"{7779FB70-B399-454A-AA1A-BAA850032B10}"
627#endif
628                                );
629}
630
631// Google Chrome Canary, between 20.0.1101.0 (crrev.com/132190) and 20.0.1106.0
632// (exclusively -- crrev.com/132596), registered a DelegateExecute class by
633// mistake (with the same GUID as Chrome). The fix stopped registering the bad
634// value, but didn't delete it. This is a problem for users who had installed
635// Canary before 20.0.1106.0 and now have a system-level Chrome, as the
636// left-behind Canary registrations in HKCU mask the HKLM registrations for the
637// same GUID. Cleanup those registrations if they still exist and belong to this
638// Canary (i.e., the registered delegate_execute's path is under |target_path|).
639void CleanupBadCanaryDelegateExecuteRegistration(
640    const base::FilePath& target_path,
641    WorkItemList* list) {
642  string16 google_chrome_delegate_execute_path(
643      L"Software\\Classes\\CLSID\\{5C65F4B0-3651-4514-B207-D10CB699B14B}");
644  string16 google_chrome_local_server_32(
645      google_chrome_delegate_execute_path + L"\\LocalServer32");
646
647  RegKey local_server_32_key;
648  string16 registered_server;
649  if (local_server_32_key.Open(HKEY_CURRENT_USER,
650                               google_chrome_local_server_32.c_str(),
651                               KEY_QUERY_VALUE) == ERROR_SUCCESS &&
652      local_server_32_key.ReadValue(L"ServerExecutable",
653                                    &registered_server) == ERROR_SUCCESS &&
654      target_path.IsParent(base::FilePath(registered_server))) {
655    scoped_ptr<WorkItemList> no_rollback_list(
656        WorkItem::CreateNoRollbackWorkItemList());
657    AddUninstallDelegateExecuteWorkItems(
658        HKEY_CURRENT_USER, google_chrome_delegate_execute_path,
659        no_rollback_list.get());
660    list->AddWorkItem(no_rollback_list.release());
661    VLOG(1) << "Added deletion items for bad Canary registrations.";
662  }
663}
664
665}  // namespace
666
667// This method adds work items to create (or update) Chrome uninstall entry in
668// either the Control Panel->Add/Remove Programs list or in the Omaha client
669// state key if running under an MSI installer.
670void AddUninstallShortcutWorkItems(const InstallerState& installer_state,
671                                   const base::FilePath& setup_path,
672                                   const Version& new_version,
673                                   const Product& product,
674                                   WorkItemList* install_list) {
675  HKEY reg_root = installer_state.root_key();
676  BrowserDistribution* browser_dist = product.distribution();
677  DCHECK(browser_dist);
678
679  // When we are installed via an MSI, we need to store our uninstall strings
680  // in the Google Update client state key. We do this even for non-MSI
681  // managed installs to avoid breaking the edge case whereby an MSI-managed
682  // install is updated by a non-msi installer (which would confuse the MSI
683  // machinery if these strings were not also updated). The UninstallString
684  // value placed in the client state key is also used by the mini_installer to
685  // locate the setup.exe instance used for binary patching.
686  // Do not quote the command line for the MSI invocation.
687  base::FilePath install_path(installer_state.target_path());
688  base::FilePath installer_path(
689      installer_state.GetInstallerDirectory(new_version));
690  installer_path = installer_path.Append(setup_path.BaseName());
691
692  CommandLine uninstall_arguments(CommandLine::NO_PROGRAM);
693  AppendUninstallCommandLineFlags(installer_state, product,
694                                  &uninstall_arguments);
695
696  // If Chrome Frame is installed in Ready Mode, add --chrome-frame to Chrome's
697  // uninstall entry. We skip this processing in case of uninstall since this
698  // means that Chrome Frame is being uninstalled, so there's no need to do any
699  // looping.
700  if (product.is_chrome() &&
701      installer_state.operation() != InstallerState::UNINSTALL) {
702    const Product* chrome_frame =
703        installer_state.FindProduct(BrowserDistribution::CHROME_FRAME);
704    if (chrome_frame && chrome_frame->HasOption(kOptionReadyMode))
705      chrome_frame->AppendProductFlags(&uninstall_arguments);
706  }
707
708  string16 update_state_key(browser_dist->GetStateKey());
709  install_list->AddCreateRegKeyWorkItem(reg_root, update_state_key);
710  install_list->AddSetRegValueWorkItem(reg_root, update_state_key,
711      installer::kUninstallStringField, installer_path.value(), true);
712  install_list->AddSetRegValueWorkItem(reg_root, update_state_key,
713      installer::kUninstallArgumentsField,
714      uninstall_arguments.GetCommandLineString(), true);
715
716  // MSI installations will manage their own uninstall shortcuts.
717  if (!installer_state.is_msi() && product.ShouldCreateUninstallEntry()) {
718    // We need to quote the command line for the Add/Remove Programs dialog.
719    CommandLine quoted_uninstall_cmd(installer_path);
720    DCHECK_EQ(quoted_uninstall_cmd.GetCommandLineString()[0], '"');
721    quoted_uninstall_cmd.AppendArguments(uninstall_arguments, false);
722
723    string16 uninstall_reg = browser_dist->GetUninstallRegPath();
724    install_list->AddCreateRegKeyWorkItem(reg_root, uninstall_reg);
725    install_list->AddSetRegValueWorkItem(reg_root, uninstall_reg,
726        installer::kUninstallDisplayNameField, browser_dist->GetDisplayName(),
727        true);
728    install_list->AddSetRegValueWorkItem(reg_root,
729        uninstall_reg, installer::kUninstallStringField,
730        quoted_uninstall_cmd.GetCommandLineString(), true);
731    install_list->AddSetRegValueWorkItem(reg_root,
732                                         uninstall_reg,
733                                         L"InstallLocation",
734                                         install_path.value(),
735                                         true);
736
737    BrowserDistribution* dist = product.distribution();
738    string16 chrome_icon = ShellUtil::FormatIconLocation(
739        install_path.Append(dist->GetIconFilename()).value(),
740        dist->GetIconIndex(BrowserDistribution::SHORTCUT_CHROME));
741    install_list->AddSetRegValueWorkItem(reg_root, uninstall_reg,
742                                         L"DisplayIcon", chrome_icon, true);
743    install_list->AddSetRegValueWorkItem(reg_root, uninstall_reg,
744                                         L"NoModify", static_cast<DWORD>(1),
745                                         true);
746    install_list->AddSetRegValueWorkItem(reg_root, uninstall_reg,
747                                         L"NoRepair", static_cast<DWORD>(1),
748                                         true);
749
750    install_list->AddSetRegValueWorkItem(reg_root, uninstall_reg,
751                                         L"Publisher",
752                                         browser_dist->GetPublisherName(),
753                                         true);
754    install_list->AddSetRegValueWorkItem(reg_root, uninstall_reg,
755                                         L"Version",
756                                         ASCIIToWide(new_version.GetString()),
757                                         true);
758    install_list->AddSetRegValueWorkItem(reg_root, uninstall_reg,
759                                         L"DisplayVersion",
760                                         ASCIIToWide(new_version.GetString()),
761                                         true);
762    install_list->AddSetRegValueWorkItem(reg_root, uninstall_reg,
763                                         L"InstallDate",
764                                         InstallUtil::GetCurrentDate(),
765                                         false);
766
767    const std::vector<uint16>& version_components = new_version.components();
768    if (version_components.size() == 4) {
769      // Our version should be in major.minor.build.rev.
770      install_list->AddSetRegValueWorkItem(reg_root, uninstall_reg,
771          L"VersionMajor", static_cast<DWORD>(version_components[2]), true);
772      install_list->AddSetRegValueWorkItem(reg_root, uninstall_reg,
773          L"VersionMinor", static_cast<DWORD>(version_components[3]), true);
774    }
775  }
776}
777
778// Create Version key for a product (if not already present) and sets the new
779// product version as the last step.
780void AddVersionKeyWorkItems(HKEY root,
781                            BrowserDistribution* dist,
782                            const Version& new_version,
783                            bool add_language_identifier,
784                            WorkItemList* list) {
785  // Create Version key for each distribution (if not already present) and set
786  // the new product version as the last step.
787  string16 version_key(dist->GetVersionKey());
788  list->AddCreateRegKeyWorkItem(root, version_key);
789
790  string16 product_name(dist->GetDisplayName());
791  list->AddSetRegValueWorkItem(root, version_key, google_update::kRegNameField,
792                               product_name, true);  // overwrite name also
793  list->AddSetRegValueWorkItem(root, version_key,
794                               google_update::kRegOopcrashesField,
795                               static_cast<DWORD>(1),
796                               false);  // set during first install
797  if (add_language_identifier) {
798    // Write the language identifier of the current translation.  Omaha's set of
799    // languages is a superset of Chrome's set of translations with this one
800    // exception: what Chrome calls "en-us", Omaha calls "en".  sigh.
801    string16 language(GetCurrentTranslation());
802    if (LowerCaseEqualsASCII(language, "en-us"))
803      language.resize(2);
804    list->AddSetRegValueWorkItem(root, version_key,
805                                 google_update::kRegLangField, language,
806                                 false);  // do not overwrite language
807  }
808  list->AddSetRegValueWorkItem(root, version_key,
809                               google_update::kRegVersionField,
810                               ASCIIToWide(new_version.GetString()),
811                               true);  // overwrite version
812}
813
814// Mirror oeminstall the first time anything is installed multi.  There is no
815// need to update the value on future install/update runs since this value never
816// changes.  Note that the value is removed by Google Update after EULA
817// acceptance is processed.
818void AddOemInstallWorkItems(const InstallationState& original_state,
819                            const InstallerState& installer_state,
820                            WorkItemList* install_list) {
821  DCHECK(installer_state.is_multi_install());
822  const bool system_install = installer_state.system_install();
823  if (!original_state.GetProductState(system_install,
824                                      BrowserDistribution::CHROME_BINARIES)) {
825    const HKEY root_key = installer_state.root_key();
826    string16 multi_key(
827        installer_state.multi_package_binaries_distribution()->GetStateKey());
828
829    // Copy the value from Chrome unless Chrome isn't installed or being
830    // installed.
831    BrowserDistribution::Type source_type;
832    if (installer_state.FindProduct(BrowserDistribution::CHROME_BROWSER)) {
833      source_type = BrowserDistribution::CHROME_BROWSER;
834    } else if (!installer_state.products().empty()) {
835      // Pick a product, any product.
836      source_type = installer_state.products()[0]->distribution()->GetType();
837    } else {
838      // Nothing is being installed?  Entirely unexpected, so do no harm.
839      LOG(ERROR) << "No products found in AddOemInstallWorkItems";
840      return;
841    }
842    const ProductState* source_product =
843        original_state.GetNonVersionedProductState(system_install, source_type);
844
845    string16 oem_install;
846    if (source_product->GetOemInstall(&oem_install)) {
847      VLOG(1) << "Mirroring oeminstall=\"" << oem_install << "\" from "
848              << BrowserDistribution::GetSpecificDistribution(source_type)->
849                     GetDisplayName();
850      install_list->AddCreateRegKeyWorkItem(root_key, multi_key);
851      // Always overwrite an old value.
852      install_list->AddSetRegValueWorkItem(root_key, multi_key,
853                                           google_update::kRegOemInstallField,
854                                           oem_install, true);
855    } else {
856      // Clear any old value.
857      install_list->AddDeleteRegValueWorkItem(
858          root_key, multi_key, google_update::kRegOemInstallField);
859    }
860  }
861}
862
863// Mirror eulaaccepted the first time anything is installed multi.  There is no
864// need to update the value on future install/update runs since
865// GoogleUpdateSettings::SetEULAConsent will modify the value for both the
866// relevant product and for the binaries.
867void AddEulaAcceptedWorkItems(const InstallationState& original_state,
868                              const InstallerState& installer_state,
869                              WorkItemList* install_list) {
870  DCHECK(installer_state.is_multi_install());
871  const bool system_install = installer_state.system_install();
872  if (!original_state.GetProductState(system_install,
873                                      BrowserDistribution::CHROME_BINARIES)) {
874    const HKEY root_key = installer_state.root_key();
875    string16 multi_key(
876        installer_state.multi_package_binaries_distribution()->GetStateKey());
877
878    // Copy the value from the product with the greatest value.
879    bool have_eula_accepted = false;
880    BrowserDistribution::Type product_type;
881    DWORD eula_accepted;
882    const Products& products = installer_state.products();
883    for (Products::const_iterator it = products.begin(); it < products.end();
884         ++it) {
885      const Product& product = **it;
886      if (product.is_chrome_binaries())
887        continue;
888      DWORD dword_value = 0;
889      BrowserDistribution::Type this_type = product.distribution()->GetType();
890      const ProductState* product_state =
891          original_state.GetNonVersionedProductState(
892              system_install, this_type);
893      if (product_state->GetEulaAccepted(&dword_value) &&
894          (!have_eula_accepted || dword_value > eula_accepted)) {
895        have_eula_accepted = true;
896        eula_accepted = dword_value;
897        product_type = this_type;
898      }
899    }
900
901    if (have_eula_accepted) {
902      VLOG(1) << "Mirroring eulaaccepted=" << eula_accepted << " from "
903              << BrowserDistribution::GetSpecificDistribution(product_type)->
904                     GetDisplayName();
905      install_list->AddCreateRegKeyWorkItem(root_key, multi_key);
906      install_list->AddSetRegValueWorkItem(
907          root_key, multi_key, google_update::kRegEULAAceptedField,
908          eula_accepted, true);
909    } else {
910      // Clear any old value.
911      install_list->AddDeleteRegValueWorkItem(
912          root_key, multi_key, google_update::kRegEULAAceptedField);
913    }
914  }
915}
916
917// Adds work items that make registry adjustments for Google Update.
918void AddGoogleUpdateWorkItems(const InstallationState& original_state,
919                              const InstallerState& installer_state,
920                              WorkItemList* install_list) {
921  // Is a multi-install product being installed or over-installed?
922  if (installer_state.operation() != InstallerState::MULTI_INSTALL &&
923      installer_state.operation() != InstallerState::MULTI_UPDATE) {
924    VLOG(1) << "AddGoogleUpdateWorkItems noop: " << installer_state.operation();
925    return;
926  }
927
928  const bool system_install = installer_state.system_install();
929  const HKEY root_key = installer_state.root_key();
930  string16 multi_key(
931      installer_state.multi_package_binaries_distribution()->GetStateKey());
932
933  // For system-level installs, make sure the ClientStateMedium key for the
934  // binaries exists.
935  if (system_install) {
936    install_list->AddCreateRegKeyWorkItem(
937        root_key,
938        installer_state.multi_package_binaries_distribution()->
939            GetStateMediumKey().c_str());
940  }
941
942  // Creating the ClientState key for binaries, if we're migrating to multi then
943  // copy over Chrome's brand code if it has one. Chrome Frame currently never
944  // has a brand code.
945  if (installer_state.state_type() != BrowserDistribution::CHROME_BINARIES) {
946    const ProductState* chrome_product_state =
947        original_state.GetNonVersionedProductState(
948            system_install, BrowserDistribution::CHROME_BROWSER);
949
950    const string16& brand(chrome_product_state->brand());
951    if (!brand.empty()) {
952      install_list->AddCreateRegKeyWorkItem(root_key, multi_key);
953      // Write Chrome's brand code to the multi key. Never overwrite the value
954      // if one is already present (although this shouldn't happen).
955      install_list->AddSetRegValueWorkItem(root_key,
956                                           multi_key,
957                                           google_update::kRegBrandField,
958                                           brand,
959                                           false);
960    }
961  }
962
963  AddOemInstallWorkItems(original_state, installer_state, install_list);
964  AddEulaAcceptedWorkItems(original_state, installer_state, install_list);
965  AddUsageStatsWorkItems(original_state, installer_state, install_list);
966
967  // TODO(grt): check for other keys/values we should put in the package's
968  // ClientState and/or Clients key.
969}
970
971void AddUsageStatsWorkItems(const InstallationState& original_state,
972                            const InstallerState& installer_state,
973                            WorkItemList* install_list) {
974  DCHECK(installer_state.operation() == InstallerState::MULTI_INSTALL ||
975         installer_state.operation() == InstallerState::MULTI_UPDATE);
976
977  HKEY root_key = installer_state.root_key();
978  bool value_found = false;
979  DWORD usagestats = 0;
980  const Products& products = installer_state.products();
981
982  // Search for an existing usagestats value for any product.
983  for (Products::const_iterator scan = products.begin(), end = products.end();
984       !value_found && scan != end; ++scan) {
985    if ((*scan)->is_chrome_binaries())
986      continue;
987    BrowserDistribution* dist = (*scan)->distribution();
988    const ProductState* product_state =
989        original_state.GetNonVersionedProductState(
990            installer_state.system_install(), dist->GetType());
991    value_found = product_state->GetUsageStats(&usagestats);
992  }
993
994  // If a value was found, write it in the appropriate location for the
995  // binaries and remove all values from the products.
996  if (value_found) {
997    string16 state_key(
998        installer_state.multi_package_binaries_distribution()->GetStateKey());
999    install_list->AddCreateRegKeyWorkItem(root_key, state_key);
1000    // Overwrite any existing value so that overinstalls (where Omaha writes a
1001    // new value into a product's state key) pick up the correct value.
1002    install_list->AddSetRegValueWorkItem(root_key, state_key,
1003                                         google_update::kRegUsageStatsField,
1004                                         usagestats, true);
1005
1006    for (Products::const_iterator scan = products.begin(), end = products.end();
1007         scan != end; ++scan) {
1008      if ((*scan)->is_chrome_binaries())
1009        continue;
1010      BrowserDistribution* dist = (*scan)->distribution();
1011      if (installer_state.system_install()) {
1012        install_list->AddDeleteRegValueWorkItem(
1013            root_key, dist->GetStateMediumKey(),
1014            google_update::kRegUsageStatsField);
1015        // Previous versions of Chrome also wrote a value in HKCU even for
1016        // system-level installs, so clean that up.
1017        install_list->AddDeleteRegValueWorkItem(
1018            HKEY_CURRENT_USER, dist->GetStateKey(),
1019            google_update::kRegUsageStatsField);
1020      }
1021      install_list->AddDeleteRegValueWorkItem(
1022          root_key, dist->GetStateKey(), google_update::kRegUsageStatsField);
1023    }
1024  }
1025}
1026
1027bool AppendPostInstallTasks(const InstallerState& installer_state,
1028                            const base::FilePath& setup_path,
1029                            const Version* current_version,
1030                            const Version& new_version,
1031                            const base::FilePath& temp_path,
1032                            WorkItemList* post_install_task_list) {
1033  DCHECK(post_install_task_list);
1034
1035  HKEY root = installer_state.root_key();
1036  const Products& products = installer_state.products();
1037  base::FilePath new_chrome_exe(
1038      installer_state.target_path().Append(installer::kChromeNewExe));
1039
1040  // Append work items that will only be executed if this was an update.
1041  // We update the 'opv' value with the current version that is active,
1042  // the 'cpv' value with the critical update version (if present), and the
1043  // 'cmd' value with the rename command to run.
1044  {
1045    scoped_ptr<WorkItemList> in_use_update_work_items(
1046        WorkItem::CreateConditionalWorkItemList(
1047            new ConditionRunIfFileExists(new_chrome_exe)));
1048    in_use_update_work_items->set_log_message("InUseUpdateWorkItemList");
1049
1050    // |critical_version| will be valid only if this in-use update includes a
1051    // version considered critical relative to the version being updated.
1052    Version critical_version(installer_state.DetermineCriticalVersion(
1053        current_version, new_version));
1054    base::FilePath installer_path(
1055        installer_state.GetInstallerDirectory(new_version).Append(
1056            setup_path.BaseName()));
1057
1058    CommandLine rename(installer_path);
1059    rename.AppendSwitch(switches::kRenameChromeExe);
1060    if (installer_state.system_install())
1061      rename.AppendSwitch(switches::kSystemLevel);
1062
1063    if (installer_state.verbose_logging())
1064      rename.AppendSwitch(switches::kVerboseLogging);
1065
1066    string16 version_key;
1067    for (size_t i = 0; i < products.size(); ++i) {
1068      BrowserDistribution* dist = products[i]->distribution();
1069      version_key = dist->GetVersionKey();
1070
1071      if (current_version) {
1072        in_use_update_work_items->AddSetRegValueWorkItem(root, version_key,
1073            google_update::kRegOldVersionField,
1074            ASCIIToWide(current_version->GetString()), true);
1075      }
1076      if (critical_version.IsValid()) {
1077        in_use_update_work_items->AddSetRegValueWorkItem(root, version_key,
1078            google_update::kRegCriticalVersionField,
1079            ASCIIToWide(critical_version.GetString()), true);
1080      } else {
1081        in_use_update_work_items->AddDeleteRegValueWorkItem(root, version_key,
1082            google_update::kRegCriticalVersionField);
1083      }
1084
1085      // Adding this registry entry for all products (but the binaries) is
1086      // overkill. However, as it stands, we don't have a way to know which
1087      // product will check the key and run the command, so we add it for all.
1088      // The first to run it will perform the operation and clean up the other
1089      // values.
1090      if (dist->GetType() != BrowserDistribution::CHROME_BINARIES) {
1091        CommandLine product_rename_cmd(rename);
1092        products[i]->AppendRenameFlags(&product_rename_cmd);
1093        in_use_update_work_items->AddSetRegValueWorkItem(
1094            root, version_key, google_update::kRegRenameCmdField,
1095            product_rename_cmd.GetCommandLineString(), true);
1096      }
1097    }
1098
1099    if (installer_state.FindProduct(BrowserDistribution::CHROME_FRAME)) {
1100      AddCopyIELowRightsPolicyWorkItems(installer_state,
1101                                        in_use_update_work_items.get());
1102    }
1103
1104    post_install_task_list->AddWorkItem(in_use_update_work_items.release());
1105  }
1106
1107  // Append work items that will be executed if this was NOT an in-use update.
1108  {
1109    scoped_ptr<WorkItemList> regular_update_work_items(
1110        WorkItem::CreateConditionalWorkItemList(
1111            new Not(new ConditionRunIfFileExists(new_chrome_exe))));
1112    regular_update_work_items->set_log_message("RegularUpdateWorkItemList");
1113
1114    // Since this was not an in-use-update, delete 'opv', 'cpv', and 'cmd' keys.
1115    for (size_t i = 0; i < products.size(); ++i) {
1116      BrowserDistribution* dist = products[i]->distribution();
1117      string16 version_key(dist->GetVersionKey());
1118      regular_update_work_items->AddDeleteRegValueWorkItem(root, version_key,
1119          google_update::kRegOldVersionField);
1120      regular_update_work_items->AddDeleteRegValueWorkItem(root, version_key,
1121          google_update::kRegCriticalVersionField);
1122      regular_update_work_items->AddDeleteRegValueWorkItem(root, version_key,
1123          google_update::kRegRenameCmdField);
1124    }
1125
1126    if (installer_state.FindProduct(BrowserDistribution::CHROME_FRAME)) {
1127      AddDeleteOldIELowRightsPolicyWorkItems(installer_state,
1128                                             regular_update_work_items.get());
1129    }
1130
1131    post_install_task_list->AddWorkItem(regular_update_work_items.release());
1132  }
1133
1134  AddRegisterComDllWorkItemsForPackage(installer_state, current_version,
1135                                       new_version, post_install_task_list);
1136
1137  // If we're told that we're an MSI install, make sure to set the marker
1138  // in the client state key so that future updates do the right thing.
1139  if (installer_state.is_msi()) {
1140    for (size_t i = 0; i < products.size(); ++i) {
1141      const Product* product = products[i];
1142      AddSetMsiMarkerWorkItem(installer_state, product->distribution(), true,
1143                              post_install_task_list);
1144
1145      // We want MSI installs to take over the Add/Remove Programs shortcut.
1146      // Make a best-effort attempt to delete any shortcuts left over from
1147      // previous non-MSI installations for the same type of install (system or
1148      // per user).
1149      if (product->ShouldCreateUninstallEntry()) {
1150        AddDeleteUninstallShortcutsForMSIWorkItems(installer_state, *product,
1151                                                   temp_path,
1152                                                   post_install_task_list);
1153      }
1154    }
1155  }
1156
1157  return true;
1158}
1159
1160void AddInstallWorkItems(const InstallationState& original_state,
1161                         const InstallerState& installer_state,
1162                         const base::FilePath& setup_path,
1163                         const base::FilePath& archive_path,
1164                         const base::FilePath& src_path,
1165                         const base::FilePath& temp_path,
1166                         const Version* current_version,
1167                         const Version& new_version,
1168                         WorkItemList* install_list) {
1169  DCHECK(install_list);
1170
1171  const base::FilePath& target_path = installer_state.target_path();
1172
1173  // A temp directory that work items need and the actual install directory.
1174  install_list->AddCreateDirWorkItem(temp_path);
1175  install_list->AddCreateDirWorkItem(target_path);
1176
1177  if (installer_state.FindProduct(BrowserDistribution::CHROME_BROWSER) ||
1178      installer_state.FindProduct(BrowserDistribution::CHROME_FRAME) ||
1179      installer_state.FindProduct(BrowserDistribution::CHROME_BINARIES)) {
1180    AddChromeWorkItems(original_state,
1181                       installer_state,
1182                       setup_path,
1183                       archive_path,
1184                       src_path,
1185                       temp_path,
1186                       current_version,
1187                       new_version,
1188                       install_list);
1189  }
1190
1191  if (installer_state.FindProduct(BrowserDistribution::CHROME_APP_HOST)) {
1192    install_list->AddCopyTreeWorkItem(
1193        src_path.Append(installer::kChromeAppHostExe).value(),
1194        target_path.Append(installer::kChromeAppHostExe).value(),
1195        temp_path.value(),
1196        WorkItem::ALWAYS,
1197        L"");
1198  }
1199
1200  // Copy installer in install directory
1201  AddInstallerCopyTasks(installer_state, setup_path, archive_path, temp_path,
1202                        new_version, install_list);
1203
1204  const HKEY root = installer_state.root_key();
1205  // Only set "lang" for user-level installs since for system-level, the install
1206  // language may not be related to a given user's runtime language.
1207  const bool add_language_identifier = !installer_state.system_install();
1208
1209  const Products& products = installer_state.products();
1210  for (Products::const_iterator it = products.begin(); it < products.end();
1211       ++it) {
1212    const Product& product = **it;
1213
1214    AddUninstallShortcutWorkItems(installer_state, setup_path, new_version,
1215                                  product, install_list);
1216
1217    AddVersionKeyWorkItems(root, product.distribution(), new_version,
1218                           add_language_identifier, install_list);
1219
1220    AddDelegateExecuteWorkItems(installer_state, target_path, new_version,
1221                                product, install_list);
1222
1223    AddActiveSetupWorkItems(installer_state, setup_path, new_version, product,
1224                            install_list);
1225  }
1226
1227  // TODO(huangs): Implement actual migration code and remove the hack below.
1228  // If installing Chrome without the legacy stand-alone App Launcher (to be
1229  // handled later), add "shadow" App Launcher registry keys so Google Update
1230  // would recognize the "dr" value in the App Launcher ClientState key.
1231  // Checking .is_multi_install() excludes Chrome Canary and stand-alone Chrome.
1232  if (installer_state.is_multi_install() &&
1233      installer_state.FindProduct(BrowserDistribution::CHROME_BROWSER) &&
1234      !installer_state.FindProduct(BrowserDistribution::CHROME_APP_HOST)) {
1235    BrowserDistribution* shadow_app_launcher_dist =
1236        BrowserDistribution::GetSpecificDistribution(
1237            BrowserDistribution::CHROME_APP_HOST);
1238    AddVersionKeyWorkItems(root, shadow_app_launcher_dist, new_version,
1239                           add_language_identifier, install_list);
1240  }
1241
1242  // Add any remaining work items that involve special settings for
1243  // each product.
1244  AddProductSpecificWorkItems(original_state, installer_state, setup_path,
1245                              new_version, install_list);
1246
1247  // Copy over brand, usagestats, and other values.
1248  AddGoogleUpdateWorkItems(original_state, installer_state, install_list);
1249
1250  // Append the tasks that run after the installation.
1251  AppendPostInstallTasks(installer_state,
1252                         setup_path,
1253                         current_version,
1254                         new_version,
1255                         temp_path,
1256                         install_list);
1257}
1258
1259void AddRegisterComDllWorkItems(const base::FilePath& dll_folder,
1260                                const std::vector<base::FilePath>& dll_list,
1261                                bool system_level,
1262                                bool do_register,
1263                                bool ignore_failures,
1264                                WorkItemList* work_item_list) {
1265  DCHECK(work_item_list);
1266  if (dll_list.empty()) {
1267    VLOG(1) << "No COM DLLs to register";
1268  } else {
1269    std::vector<base::FilePath>::const_iterator dll_iter(dll_list.begin());
1270    for (; dll_iter != dll_list.end(); ++dll_iter) {
1271      base::FilePath dll_path = dll_folder.Append(*dll_iter);
1272      WorkItem* work_item = work_item_list->AddSelfRegWorkItem(
1273          dll_path.value(), do_register, !system_level);
1274      DCHECK(work_item);
1275      work_item->set_ignore_failure(ignore_failures);
1276    }
1277  }
1278}
1279
1280void AddSetMsiMarkerWorkItem(const InstallerState& installer_state,
1281                             BrowserDistribution* dist,
1282                             bool set,
1283                             WorkItemList* work_item_list) {
1284  DCHECK(work_item_list);
1285  DWORD msi_value = set ? 1 : 0;
1286  WorkItem* set_msi_work_item = work_item_list->AddSetRegValueWorkItem(
1287      installer_state.root_key(), dist->GetStateKey(),
1288      google_update::kRegMSIField, msi_value, true);
1289  DCHECK(set_msi_work_item);
1290  set_msi_work_item->set_ignore_failure(true);
1291  set_msi_work_item->set_log_message("Could not write MSI marker!");
1292}
1293
1294void AddChromeFrameWorkItems(const InstallationState& original_state,
1295                             const InstallerState& installer_state,
1296                             const base::FilePath& setup_path,
1297                             const Version& new_version,
1298                             const Product& product,
1299                             WorkItemList* list) {
1300  DCHECK(product.is_chrome_frame());
1301  if (!installer_state.is_multi_install()) {
1302    VLOG(1) << "Not adding GCF specific work items for single install.";
1303    return;
1304  }
1305
1306  string16 version_key(product.distribution()->GetVersionKey());
1307  bool ready_mode = product.HasOption(kOptionReadyMode);
1308  HKEY root = installer_state.root_key();
1309  const bool is_install =
1310      (installer_state.operation() != InstallerState::UNINSTALL);
1311  bool update_chrome_uninstall_command = false;
1312  BrowserDistribution* dist =
1313      installer_state.multi_package_binaries_distribution();
1314  if (ready_mode) {
1315    // If GCF is being installed in ready mode, we write an entry to the
1316    // multi-install state key.  If the value already exists, we will not
1317    // overwrite it since the user might have opted out.
1318    list->AddCreateRegKeyWorkItem(root, dist->GetStateKey());
1319    list->AddSetRegValueWorkItem(root, dist->GetStateKey(),
1320        kChromeFrameReadyModeField,
1321        static_cast<int64>(is_install ? 1 : 0),  // The value we want to set.
1322        !is_install);  // Overwrite existing value.
1323    if (is_install) {
1324      base::FilePath installer_path(installer_state
1325          .GetInstallerDirectory(new_version).Append(setup_path.BaseName()));
1326
1327      CommandLine basic_cl(installer_path);
1328      basic_cl.AppendSwitch(switches::kChromeFrame);
1329      basic_cl.AppendSwitch(switches::kMultiInstall);
1330
1331      if (installer_state.system_install())
1332        basic_cl.AppendSwitch(switches::kSystemLevel);
1333
1334      CommandLine temp_opt_out(basic_cl);
1335      temp_opt_out.AppendSwitch(switches::kChromeFrameReadyModeTempOptOut);
1336
1337      CommandLine end_temp_opt_out(basic_cl);
1338      end_temp_opt_out.AppendSwitch(
1339          switches::kChromeFrameReadyModeEndTempOptOut);
1340
1341      CommandLine opt_out(installer_path);
1342      AppendUninstallCommandLineFlags(installer_state, product, &opt_out);
1343      // Force Uninstall silences the prompt to reboot to complete uninstall.
1344      opt_out.AppendSwitch(switches::kForceUninstall);
1345
1346      CommandLine opt_in(basic_cl);
1347      opt_in.AppendSwitch(switches::kChromeFrameReadyModeOptIn);
1348
1349      list->AddSetRegValueWorkItem(root, version_key,
1350                                   google_update::kRegCFTempOptOutCmdField,
1351                                   temp_opt_out.GetCommandLineString(), true);
1352      list->AddSetRegValueWorkItem(root, version_key,
1353                                   google_update::kRegCFEndTempOptOutCmdField,
1354                                   end_temp_opt_out.GetCommandLineString(),
1355                                   true);
1356      list->AddSetRegValueWorkItem(root, version_key,
1357                                   google_update::kRegCFOptOutCmdField,
1358                                   opt_out.GetCommandLineString(), true);
1359      list->AddSetRegValueWorkItem(root, version_key,
1360                                   google_update::kRegCFOptInCmdField,
1361                                   opt_in.GetCommandLineString(), true);
1362    } else {
1363      // If Chrome is not also being uninstalled, we need to update its command
1364      // line so that it doesn't include uninstalling Chrome Frame now.
1365      update_chrome_uninstall_command =
1366          (installer_state.FindProduct(BrowserDistribution::CHROME_BROWSER) ==
1367           NULL);
1368    }
1369  } else {
1370    // It doesn't matter here if we're installing or uninstalling Chrome Frame.
1371    // If ready mode isn't specified on the command line for installs, we need
1372    // to delete the ready mode flag from the registry if it exists - this
1373    // constitutes an opt-in for the user.  If we're uninstalling CF and ready
1374    // mode isn't specified on the command line, that means that CF wasn't
1375    // installed with ready mode enabled (the --ready-mode switch should be set
1376    // in the registry) so deleting the value should have no effect.
1377    // In both cases (install/uninstall), we need to make sure that Chrome's
1378    // uninstallation command line does not include the --chrome-frame switch
1379    // so that uninstalling Chrome will no longer uninstall Chrome Frame.
1380
1381    list->AddDeleteRegValueWorkItem(root, dist->GetStateKey(),
1382        kChromeFrameReadyModeField);
1383
1384    const Product* chrome =
1385        installer_state.FindProduct(BrowserDistribution::CHROME_BROWSER);
1386    if (chrome) {
1387      // Chrome is already a part of this installation run, so we can assume
1388      // that the uninstallation arguments will be updated correctly.
1389    } else {
1390      // Chrome is not a part of this installation run, so we have to explicitly
1391      // check if Chrome is installed, and if so, update its uninstallation
1392      // command lines.
1393      const ProductState* chrome_state = original_state.GetProductState(
1394          installer_state.system_install(),
1395          BrowserDistribution::CHROME_BROWSER);
1396      update_chrome_uninstall_command =
1397          (chrome_state != NULL) && chrome_state->is_multi_install();
1398    }
1399  }
1400
1401  if (!ready_mode || !is_install) {
1402    list->AddDeleteRegValueWorkItem(root, version_key,
1403                                    google_update::kRegCFTempOptOutCmdField);
1404    list->AddDeleteRegValueWorkItem(root, version_key,
1405                                    google_update::kRegCFEndTempOptOutCmdField);
1406    list->AddDeleteRegValueWorkItem(root, version_key,
1407                                    google_update::kRegCFOptOutCmdField);
1408    list->AddDeleteRegValueWorkItem(root, version_key,
1409                                    google_update::kRegCFOptInCmdField);
1410  }
1411
1412  if (update_chrome_uninstall_command) {
1413    // Chrome is not a part of this installation run, so we have to explicitly
1414    // check if Chrome is installed, and if so, update its uninstallation
1415    // command lines.
1416    const ProductState* chrome_state = original_state.GetProductState(
1417        installer_state.system_install(), BrowserDistribution::CHROME_BROWSER);
1418    if (chrome_state != NULL) {
1419      DCHECK(chrome_state->is_multi_install());
1420      Product chrome(BrowserDistribution::GetSpecificDistribution(
1421                         BrowserDistribution::CHROME_BROWSER));
1422      chrome.InitializeFromUninstallCommand(chrome_state->uninstall_command());
1423      AddUninstallShortcutWorkItems(installer_state, setup_path,
1424                                    chrome_state->version(), chrome, list);
1425    } else {
1426      NOTREACHED() << "What happened to Chrome?";
1427    }
1428  }
1429}
1430
1431void AddDelegateExecuteWorkItems(const InstallerState& installer_state,
1432                                 const base::FilePath& target_path,
1433                                 const Version& new_version,
1434                                 const Product& product,
1435                                 WorkItemList* list) {
1436  string16 handler_class_uuid;
1437  BrowserDistribution* dist = product.distribution();
1438  if (!dist->GetCommandExecuteImplClsid(&handler_class_uuid)) {
1439    if (InstallUtil::IsChromeSxSProcess()) {
1440      CleanupBadCanaryDelegateExecuteRegistration(target_path, list);
1441    } else {
1442      VLOG(1) << "No DelegateExecute verb handler processing to do for "
1443              << dist->GetDisplayName();
1444    }
1445    return;
1446  }
1447
1448  HKEY root = installer_state.root_key();
1449  string16 delegate_execute_path(L"Software\\Classes\\CLSID\\");
1450  delegate_execute_path.append(handler_class_uuid);
1451
1452  // Unconditionally remove registration regardless of whether or not it is
1453  // needed since builds after r132190 included it when it wasn't strictly
1454  // necessary.  Do this removal before adding in the new key to ensure that
1455  // the COM probe/flush below does its job.
1456  AddUninstallDelegateExecuteWorkItems(root, delegate_execute_path, list);
1457
1458  // Add work items to register the handler iff it is present.
1459  // See also shell_util.cc's GetProgIdEntries.
1460  if (installer_state.operation() != InstallerState::UNINSTALL) {
1461    VLOG(1) << "Adding registration items for DelegateExecute verb handler.";
1462
1463    // Force COM to flush its cache containing the path to the old handler.
1464    list->AddCallbackWorkItem(base::Bind(&ProbeCommandExecuteCallback,
1465                                         handler_class_uuid));
1466
1467    // The path to the exe (in the version directory).
1468    base::FilePath delegate_execute(target_path);
1469    if (new_version.IsValid())
1470      delegate_execute = delegate_execute.AppendASCII(new_version.GetString());
1471    delegate_execute = delegate_execute.Append(kDelegateExecuteExe);
1472
1473    // Command-line featuring the quoted path to the exe.
1474    string16 command(1, L'"');
1475    command.append(delegate_execute.value()).append(1, L'"');
1476
1477    // Register the CommandExecuteImpl class in Software\Classes\CLSID\...
1478    list->AddCreateRegKeyWorkItem(root, delegate_execute_path);
1479    list->AddSetRegValueWorkItem(root, delegate_execute_path, L"",
1480                                 L"CommandExecuteImpl Class", true);
1481    string16 subkey(delegate_execute_path);
1482    subkey.append(L"\\LocalServer32");
1483    list->AddCreateRegKeyWorkItem(root, subkey);
1484    list->AddSetRegValueWorkItem(root, subkey, L"", command, true);
1485    list->AddSetRegValueWorkItem(root, subkey, L"ServerExecutable",
1486                                 delegate_execute.value(), true);
1487
1488    subkey.assign(delegate_execute_path).append(L"\\Programmable");
1489    list->AddCreateRegKeyWorkItem(root, subkey);
1490  }
1491}
1492
1493void AddActiveSetupWorkItems(const InstallerState& installer_state,
1494                             const base::FilePath& setup_path,
1495                             const Version& new_version,
1496                             const Product& product,
1497                             WorkItemList* list) {
1498  DCHECK(installer_state.operation() != InstallerState::UNINSTALL);
1499  BrowserDistribution* dist = product.distribution();
1500
1501  if (!product.is_chrome() || !installer_state.system_install()) {
1502    const char* install_level =
1503        installer_state.system_install() ? "system" : "user";
1504    VLOG(1) << "No Active Setup processing to do for " << install_level
1505            << "-level " << dist->GetDisplayName();
1506    return;
1507  }
1508  DCHECK(installer_state.RequiresActiveSetup());
1509
1510  const HKEY root = HKEY_LOCAL_MACHINE;
1511  const string16 active_setup_path(InstallUtil::GetActiveSetupPath(dist));
1512
1513  VLOG(1) << "Adding registration items for Active Setup.";
1514  list->AddCreateRegKeyWorkItem(root, active_setup_path);
1515  list->AddSetRegValueWorkItem(root, active_setup_path, L"",
1516                               dist->GetDisplayName(), true);
1517
1518  base::FilePath active_setup_exe(installer_state.GetInstallerDirectory(
1519      new_version).Append(kActiveSetupExe));
1520  CommandLine cmd(active_setup_exe);
1521  cmd.AppendSwitch(installer::switches::kConfigureUserSettings);
1522  cmd.AppendSwitch(installer::switches::kVerboseLogging);
1523  cmd.AppendSwitch(installer::switches::kSystemLevel);
1524  product.AppendProductFlags(&cmd);
1525  list->AddSetRegValueWorkItem(root, active_setup_path, L"StubPath",
1526                               cmd.GetCommandLineString(), true);
1527
1528  // TODO(grt): http://crbug.com/75152 Write a reference to a localized
1529  // resource.
1530  list->AddSetRegValueWorkItem(root, active_setup_path, L"Localized Name",
1531                               dist->GetDisplayName(), true);
1532
1533  list->AddSetRegValueWorkItem(root, active_setup_path, L"IsInstalled",
1534                               static_cast<DWORD>(1U), true);
1535
1536  list->AddSetRegValueWorkItem(root, active_setup_path, L"Version",
1537                               kActiveSetupVersion, true);
1538}
1539
1540void AddDeleteOldIELowRightsPolicyWorkItems(
1541    const InstallerState& installer_state,
1542    WorkItemList* install_list) {
1543  DCHECK(install_list);
1544
1545  string16 key_path;
1546  GetIELowRightsElevationPolicyKeyPath(OLD_ELEVATION_POLICY, &key_path);
1547  install_list->AddDeleteRegKeyWorkItem(installer_state.root_key(), key_path);
1548}
1549
1550// Adds work items to copy the chrome_launcher IE low rights elevation policy
1551// from the primary policy GUID to the "old" policy GUID.  Take care not to
1552// perform the copy if there is already an old policy present, as the ones under
1553// the main kElevationPolicyGuid would then correspond to an intermediate
1554// version (current_version < pv < new_version).
1555void AddCopyIELowRightsPolicyWorkItems(const InstallerState& installer_state,
1556                                       WorkItemList* install_list) {
1557  DCHECK(install_list);
1558
1559  string16 current_key_path;
1560  string16 old_key_path;
1561
1562  GetIELowRightsElevationPolicyKeyPath(CURRENT_ELEVATION_POLICY,
1563                                       &current_key_path);
1564  GetIELowRightsElevationPolicyKeyPath(OLD_ELEVATION_POLICY, &old_key_path);
1565  // Do not clobber existing old policies.
1566  install_list->AddCopyRegKeyWorkItem(installer_state.root_key(),
1567                                      current_key_path, old_key_path,
1568                                      WorkItem::IF_NOT_PRESENT);
1569}
1570
1571void AppendUninstallCommandLineFlags(const InstallerState& installer_state,
1572                                     const Product& product,
1573                                     CommandLine* uninstall_cmd) {
1574  DCHECK(uninstall_cmd);
1575
1576  uninstall_cmd->AppendSwitch(installer::switches::kUninstall);
1577
1578  // Append the product-specific uninstall flags.
1579  product.AppendProductFlags(uninstall_cmd);
1580  if (installer_state.is_msi()) {
1581    uninstall_cmd->AppendSwitch(installer::switches::kMsi);
1582    // See comment in uninstall.cc where we check for the kDeleteProfile switch.
1583    if (product.is_chrome_frame()) {
1584      uninstall_cmd->AppendSwitch(installer::switches::kDeleteProfile);
1585    }
1586  }
1587  if (installer_state.system_install())
1588    uninstall_cmd->AppendSwitch(installer::switches::kSystemLevel);
1589  if (installer_state.verbose_logging())
1590    uninstall_cmd->AppendSwitch(installer::switches::kVerboseLogging);
1591}
1592
1593void RefreshElevationPolicy() {
1594  const wchar_t kIEFrameDll[] = L"ieframe.dll";
1595  const char kIERefreshPolicy[] = "IERefreshElevationPolicy";
1596
1597  HMODULE ieframe = LoadLibrary(kIEFrameDll);
1598  if (ieframe) {
1599    typedef HRESULT (__stdcall *IERefreshPolicy)();
1600    IERefreshPolicy ie_refresh_policy = reinterpret_cast<IERefreshPolicy>(
1601        GetProcAddress(ieframe, kIERefreshPolicy));
1602
1603    if (ie_refresh_policy) {
1604      ie_refresh_policy();
1605    } else {
1606      VLOG(1) << kIERefreshPolicy << " not supported.";
1607    }
1608
1609    FreeLibrary(ieframe);
1610  } else {
1611    VLOG(1) << "Cannot load " << kIEFrameDll;
1612  }
1613}
1614
1615void AddOsUpgradeWorkItems(const InstallerState& installer_state,
1616                           const base::FilePath& setup_path,
1617                           const Version& new_version,
1618                           const Product& product,
1619                           WorkItemList* install_list) {
1620  const HKEY root_key = installer_state.root_key();
1621  string16 cmd_key(GetRegCommandKey(product.distribution(), kCmdOnOsUpgrade));
1622
1623  if (installer_state.operation() == InstallerState::UNINSTALL) {
1624    install_list->AddDeleteRegKeyWorkItem(root_key, cmd_key)->
1625        set_log_message("Removing OS upgrade command");
1626  } else {
1627    // Register with Google Update to have setup.exe --on-os-upgrade called on
1628    // OS upgrade.
1629    CommandLine cmd_line(installer_state
1630        .GetInstallerDirectory(new_version)
1631        .Append(setup_path.BaseName()));
1632    // Add the main option to indicate OS upgrade flow.
1633    cmd_line.AppendSwitch(installer::switches::kOnOsUpgrade);
1634    // Add product-specific options.
1635    product.AppendProductFlags(&cmd_line);
1636    if (installer_state.system_install())
1637      cmd_line.AppendSwitch(installer::switches::kSystemLevel);
1638    // Log everything for now.
1639    cmd_line.AppendSwitch(installer::switches::kVerboseLogging);
1640
1641    AppCommand cmd(cmd_line.GetCommandLineString());
1642    cmd.set_is_auto_run_on_os_upgrade(true);
1643    cmd.AddWorkItems(installer_state.root_key(), cmd_key, install_list);
1644  }
1645}
1646
1647void AddQueryEULAAcceptanceWorkItems(const InstallerState& installer_state,
1648                                     const base::FilePath& setup_path,
1649                                     const Version& new_version,
1650                                     const Product& product,
1651                                     WorkItemList* work_item_list) {
1652  const HKEY root_key = installer_state.root_key();
1653  string16 cmd_key(GetRegCommandKey(product.distribution(),
1654                                    kCmdQueryEULAAcceptance));
1655  if (installer_state.operation() == InstallerState::UNINSTALL) {
1656    work_item_list->AddDeleteRegKeyWorkItem(root_key, cmd_key)->
1657        set_log_message("Removing query EULA acceptance command");
1658  } else {
1659    CommandLine cmd_line(installer_state
1660        .GetInstallerDirectory(new_version)
1661        .Append(setup_path.BaseName()));
1662    cmd_line.AppendSwitch(switches::kQueryEULAAcceptance);
1663    if (installer_state.system_install())
1664      cmd_line.AppendSwitch(installer::switches::kSystemLevel);
1665    if (installer_state.verbose_logging())
1666      cmd_line.AppendSwitch(installer::switches::kVerboseLogging);
1667    AppCommand cmd(cmd_line.GetCommandLineString());
1668    cmd.set_is_web_accessible(true);
1669    cmd.set_is_run_as_user(true);
1670    cmd.AddWorkItems(installer_state.root_key(), cmd_key, work_item_list);
1671  }
1672}
1673
1674void AddQuickEnableChromeFrameWorkItems(const InstallerState& installer_state,
1675                                        const InstallationState& machine_state,
1676                                        const base::FilePath& setup_path,
1677                                        const Version& new_version,
1678                                        WorkItemList* work_item_list) {
1679  DCHECK(work_item_list);
1680
1681  const bool system_install = installer_state.system_install();
1682  bool will_have_chrome_frame =
1683      WillProductBePresentAfterSetup(installer_state, machine_state,
1684                                     BrowserDistribution::CHROME_FRAME);
1685  bool will_have_chrome_binaries =
1686      WillProductBePresentAfterSetup(installer_state, machine_state,
1687                                     BrowserDistribution::CHROME_BINARIES);
1688
1689  string16 cmd_key(GetRegCommandKey(
1690                       BrowserDistribution::GetSpecificDistribution(
1691                           BrowserDistribution::CHROME_BINARIES),
1692                       kCmdQuickEnableCf));
1693
1694  if (will_have_chrome_frame) {
1695    // Chrome Frame is (to be) installed. Unconditionally remove the Quick
1696    // Enable command from the binaries. We do this even if multi-install Chrome
1697    // isn't installed since we don't want them left behind in any case.
1698    work_item_list->AddDeleteRegKeyWorkItem(
1699        installer_state.root_key(), cmd_key)->set_log_message(
1700            "removing " + WideToASCII(kCmdQuickEnableCf) + " command");
1701
1702  } else if (will_have_chrome_binaries) {
1703    // Chrome Frame isn't (to be) installed while some other multi-install
1704    // product is (to be) installed. Add the Quick Enable command to
1705    // the binaries.
1706    CommandLine cmd_line(GetGenericQuickEnableCommand(installer_state,
1707                                                      machine_state,
1708                                                      setup_path,
1709                                                      new_version));
1710    // kMultiInstall and kVerboseLogging were processed above.
1711    cmd_line.AppendSwitch(switches::kChromeFrameQuickEnable);
1712    if (installer_state.system_install())
1713      cmd_line.AppendSwitch(switches::kSystemLevel);
1714    AppCommand cmd(cmd_line.GetCommandLineString());
1715    cmd.set_sends_pings(true);
1716    cmd.set_is_web_accessible(true);
1717    cmd.AddWorkItems(installer_state.root_key(), cmd_key, work_item_list);
1718  }
1719}
1720
1721}  // namespace installer
1722