Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / installer / setup / install.cc
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 #include "chrome/installer/setup/install.h"
6
7 #include <windows.h>
8 #include <shlobj.h>
9 #include <time.h>
10
11 #include <string>
12
13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h"
15 #include "base/logging.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/numerics/safe_conversions.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/win/shortcut.h"
22 #include "base/win/windows_version.h"
23 #include "chrome/common/chrome_constants.h"
24 #include "chrome/common/chrome_switches.h"
25 #include "chrome/installer/setup/install_worker.h"
26 #include "chrome/installer/setup/setup_constants.h"
27 #include "chrome/installer/util/auto_launch_util.h"
28 #include "chrome/installer/util/browser_distribution.h"
29 #include "chrome/installer/util/create_reg_key_work_item.h"
30 #include "chrome/installer/util/delete_after_reboot_helper.h"
31 #include "chrome/installer/util/google_update_constants.h"
32 #include "chrome/installer/util/helper.h"
33 #include "chrome/installer/util/install_util.h"
34 #include "chrome/installer/util/master_preferences.h"
35 #include "chrome/installer/util/master_preferences_constants.h"
36 #include "chrome/installer/util/set_reg_value_work_item.h"
37 #include "chrome/installer/util/shell_util.h"
38 #include "chrome/installer/util/util_constants.h"
39 #include "chrome/installer/util/work_item_list.h"
40
41
42 namespace {
43
44 void LogShortcutOperation(ShellUtil::ShortcutLocation location,
45                           BrowserDistribution* dist,
46                           const ShellUtil::ShortcutProperties& properties,
47                           ShellUtil::ShortcutOperation operation,
48                           bool failed) {
49   // ShellUtil::SHELL_SHORTCUT_UPDATE_EXISTING should not be used at install and
50   // thus this method does not handle logging a message for it.
51   DCHECK(operation != ShellUtil::SHELL_SHORTCUT_UPDATE_EXISTING);
52   std::string message;
53   if (failed)
54     message.append("Failed: ");
55   message.append(
56       (operation == ShellUtil::SHELL_SHORTCUT_CREATE_ALWAYS ||
57        operation == ShellUtil::SHELL_SHORTCUT_CREATE_IF_NO_SYSTEM_LEVEL) ?
58       "Creating " : "Overwriting ");
59   if (failed && operation == ShellUtil::SHELL_SHORTCUT_REPLACE_EXISTING)
60     message.append("(maybe the shortcut doesn't exist?) ");
61   message.append((properties.level == ShellUtil::CURRENT_USER) ? "per-user " :
62                                                                  "all-users ");
63   switch (location) {
64     case ShellUtil::SHORTCUT_LOCATION_DESKTOP:
65       message.append("Desktop ");
66       break;
67     case ShellUtil::SHORTCUT_LOCATION_QUICK_LAUNCH:
68       message.append("Quick Launch ");
69       break;
70     case ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_DIR:
71       message.append("Start menu/" +
72                      base::UTF16ToUTF8(dist->GetStartMenuShortcutSubfolder(
73                                      BrowserDistribution::SUBFOLDER_CHROME)) +
74                       " ");
75       break;
76     case ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_APPS_DIR:
77       message.append("Start menu/" +
78                      base::UTF16ToUTF8(dist->GetStartMenuShortcutSubfolder(
79                                      BrowserDistribution::SUBFOLDER_APPS)) +
80                      " ");
81       break;
82     default:
83       NOTREACHED();
84   }
85
86   message.push_back('"');
87   if (properties.has_shortcut_name())
88     message.append(base::UTF16ToUTF8(properties.shortcut_name));
89   else
90     message.append(base::UTF16ToUTF8(dist->GetDisplayName()));
91   message.push_back('"');
92
93   message.append(" shortcut to ");
94   message.append(base::UTF16ToUTF8(properties.target.value()));
95   if (properties.has_arguments())
96     message.append(base::UTF16ToUTF8(properties.arguments));
97
98   if (properties.pin_to_taskbar &&
99       base::win::GetVersion() >= base::win::VERSION_WIN7) {
100     message.append(" and pinning to the taskbar.");
101   } else {
102     message.push_back('.');
103   }
104
105   if (failed)
106     LOG(WARNING) << message;
107   else
108     VLOG(1) << message;
109 }
110
111 void ExecuteAndLogShortcutOperation(
112     ShellUtil::ShortcutLocation location,
113     BrowserDistribution* dist,
114     const ShellUtil::ShortcutProperties& properties,
115     ShellUtil::ShortcutOperation operation) {
116   LogShortcutOperation(location, dist, properties, operation, false);
117   if (!ShellUtil::CreateOrUpdateShortcut(location, dist, properties,
118                                          operation)) {
119     LogShortcutOperation(location, dist, properties, operation, true);
120   }
121 }
122
123 void AddChromeToMediaPlayerList() {
124   base::string16 reg_path(installer::kMediaPlayerRegPath);
125   // registry paths can also be appended like file system path
126   reg_path.push_back(base::FilePath::kSeparators[0]);
127   reg_path.append(installer::kChromeExe);
128   VLOG(1) << "Adding Chrome to Media player list at " << reg_path;
129   scoped_ptr<WorkItem> work_item(WorkItem::CreateCreateRegKeyWorkItem(
130       HKEY_LOCAL_MACHINE, reg_path, WorkItem::kWow64Default));
131
132   // if the operation fails we log the error but still continue
133   if (!work_item.get()->Do())
134     LOG(ERROR) << "Could not add Chrome to media player inclusion list.";
135 }
136
137 // Copy master_preferences file provided to installer, in the same folder
138 // as chrome.exe so Chrome first run can find it. This function will be called
139 // only on the first install of Chrome.
140 void CopyPreferenceFileForFirstRun(
141     const installer::InstallerState& installer_state,
142     const base::FilePath& prefs_source_path) {
143   base::FilePath prefs_dest_path(installer_state.target_path().AppendASCII(
144       installer::kDefaultMasterPrefs));
145   if (!base::CopyFile(prefs_source_path, prefs_dest_path)) {
146     VLOG(1) << "Failed to copy master preferences from:"
147             << prefs_source_path.value() << " gle: " << ::GetLastError();
148   }
149 }
150
151 // This function installs a new version of Chrome to the specified location.
152 //
153 // setup_path: Path to the executable (setup.exe) as it will be copied
154 //           to Chrome install folder after install is complete
155 // archive_path: Path to the archive (chrome.7z) as it will be copied
156 //               to Chrome install folder after install is complete
157 // src_path: the path that contains a complete and unpacked Chrome package
158 //           to be installed.
159 // temp_path: the path of working directory used during installation. This path
160 //            does not need to exist.
161 // new_version: new Chrome version that needs to be installed
162 // current_version: returns the current active version (if any)
163 //
164 // This function makes best effort to do installation in a transactional
165 // manner. If failed it tries to rollback all changes on the file system
166 // and registry. For example, if package exists before calling the
167 // function, it rolls back all new file and directory changes under
168 // package. If package does not exist before calling the function
169 // (typical new install), the function creates package during install
170 // and removes the whole directory during rollback.
171 installer::InstallStatus InstallNewVersion(
172     const installer::InstallationState& original_state,
173     const installer::InstallerState& installer_state,
174     const base::FilePath& setup_path,
175     const base::FilePath& archive_path,
176     const base::FilePath& src_path,
177     const base::FilePath& temp_path,
178     const Version& new_version,
179     scoped_ptr<Version>* current_version) {
180   DCHECK(current_version);
181
182   installer_state.UpdateStage(installer::BUILDING);
183
184   current_version->reset(installer_state.GetCurrentVersion(original_state));
185   scoped_ptr<WorkItemList> install_list(WorkItem::CreateWorkItemList());
186
187   AddInstallWorkItems(original_state,
188                       installer_state,
189                       setup_path,
190                       archive_path,
191                       src_path,
192                       temp_path,
193                       current_version->get(),
194                       new_version,
195                       install_list.get());
196
197   base::FilePath new_chrome_exe(
198       installer_state.target_path().Append(installer::kChromeNewExe));
199
200   installer_state.UpdateStage(installer::EXECUTING);
201
202   if (!install_list->Do()) {
203     installer_state.UpdateStage(installer::ROLLINGBACK);
204     installer::InstallStatus result =
205         base::PathExists(new_chrome_exe) && current_version->get() &&
206         new_version.Equals(*current_version->get()) ?
207         installer::SAME_VERSION_REPAIR_FAILED :
208         installer::INSTALL_FAILED;
209     LOG(ERROR) << "Install failed, rolling back... result: " << result;
210     install_list->Rollback();
211     LOG(ERROR) << "Rollback complete. ";
212     return result;
213   }
214
215   installer_state.UpdateStage(installer::REFRESHING_POLICY);
216
217   installer::RefreshElevationPolicy();
218
219   if (!current_version->get()) {
220     VLOG(1) << "First install of version " << new_version.GetString();
221     return installer::FIRST_INSTALL_SUCCESS;
222   }
223
224   if (new_version.Equals(**current_version)) {
225     VLOG(1) << "Install repaired of version " << new_version.GetString();
226     return installer::INSTALL_REPAIRED;
227   }
228
229   if (new_version.CompareTo(**current_version) > 0) {
230     if (base::PathExists(new_chrome_exe)) {
231       VLOG(1) << "Version updated to " << new_version.GetString()
232               << " while running " << (*current_version)->GetString();
233       return installer::IN_USE_UPDATED;
234     }
235     VLOG(1) << "Version updated to " << new_version.GetString();
236     return installer::NEW_VERSION_UPDATED;
237   }
238
239   LOG(ERROR) << "Not sure how we got here while updating"
240              << ", new version: " << new_version.GetString()
241              << ", old version: " << (*current_version)->GetString();
242
243   return installer::INSTALL_FAILED;
244 }
245
246 // Deletes the old "Uninstall Google Chrome" shortcut in the Start menu which
247 // was installed prior to Chrome 24.
248 void CleanupLegacyShortcuts(const installer::InstallerState& installer_state,
249                             BrowserDistribution* dist,
250                             const base::FilePath& chrome_exe) {
251   ShellUtil::ShellChange shortcut_level = installer_state.system_install() ?
252       ShellUtil::SYSTEM_LEVEL : ShellUtil::CURRENT_USER;
253   base::FilePath uninstall_shortcut_path;
254   ShellUtil::GetShortcutPath(ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_DIR,
255                              dist, shortcut_level, &uninstall_shortcut_path);
256   uninstall_shortcut_path = uninstall_shortcut_path.Append(
257       dist->GetUninstallLinkName() + installer::kLnkExt);
258   base::DeleteFile(uninstall_shortcut_path, false);
259 }
260
261 // Returns the appropriate shortcut operations for App Launcher,
262 // based on state of installation and master_preferences.
263 installer::InstallShortcutOperation GetAppLauncherShortcutOperation(
264     const installer::InstallationState& original_state,
265     const installer::InstallerState& installer_state) {
266   const installer::ProductState* original_app_host_state =
267       original_state.GetProductState(installer_state.system_install(),
268                                      BrowserDistribution::CHROME_APP_HOST);
269   bool app_launcher_exists = original_app_host_state &&
270       original_app_host_state->uninstall_command()
271           .HasSwitch(installer::switches::kChromeAppLauncher);
272   if (!app_launcher_exists)
273     return installer::INSTALL_SHORTCUT_CREATE_ALL;
274
275   return installer::INSTALL_SHORTCUT_REPLACE_EXISTING;
276 }
277
278 }  // end namespace
279
280 namespace installer {
281
282 void EscapeXmlAttributeValueInSingleQuotes(base::string16* att_value) {
283   base::ReplaceChars(*att_value, base::ASCIIToUTF16("&"),
284                      base::ASCIIToUTF16("&amp;"), att_value);
285   base::ReplaceChars(*att_value, base::ASCIIToUTF16("'"),
286                      base::ASCIIToUTF16("&apos;"), att_value);
287   base::ReplaceChars(*att_value, base::ASCIIToUTF16("<"),
288                      base::ASCIIToUTF16("&lt;"), att_value);
289 }
290
291 bool CreateVisualElementsManifest(const base::FilePath& src_path,
292                                   const Version& version) {
293   // Construct the relative path to the versioned VisualElements directory.
294   base::string16 elements_dir(base::ASCIIToUTF16(version.GetString()));
295   elements_dir.push_back(base::FilePath::kSeparators[0]);
296   elements_dir.append(installer::kVisualElements);
297
298   // Some distributions of Chromium may not include visual elements. Only
299   // proceed if this distribution does.
300   if (!base::PathExists(src_path.Append(elements_dir))) {
301     VLOG(1) << "No visual elements found, not writing "
302             << installer::kVisualElementsManifest << " to " << src_path.value();
303     return true;
304   } else {
305     // A printf_p-style format string for generating the visual elements
306     // manifest. Required arguments, in order, are:
307     //   - Localized display name for the product.
308     //   - Relative path to the VisualElements directory.
309     static const char kManifestTemplate[] =
310         "<Application>\r\n"
311         "  <VisualElements\r\n"
312         "      DisplayName='%1$ls'\r\n"
313         "      Logo='%2$ls\\Logo.png'\r\n"
314         "      SmallLogo='%2$ls\\SmallLogo.png'\r\n"
315         "      ForegroundText='light'\r\n"
316         "      BackgroundColor='#323232'>\r\n"
317         "    <DefaultTile ShowName='allLogos'/>\r\n"
318         "    <SplashScreen Image='%2$ls\\splash-620x300.png'/>\r\n"
319         "  </VisualElements>\r\n"
320         "</Application>";
321
322     const base::string16 manifest_template(
323         base::ASCIIToUTF16(kManifestTemplate));
324
325     BrowserDistribution* dist = BrowserDistribution::GetSpecificDistribution(
326         BrowserDistribution::CHROME_BROWSER);
327     // TODO(grt): http://crbug.com/75152 Write a reference to a localized
328     // resource for |display_name|.
329     base::string16 display_name(dist->GetDisplayName());
330     EscapeXmlAttributeValueInSingleQuotes(&display_name);
331
332     // Fill the manifest with the desired values.
333     base::string16 manifest16(base::StringPrintf(
334         manifest_template.c_str(), display_name.c_str(), elements_dir.c_str()));
335
336     // Write the manifest to |src_path|.
337     const std::string manifest(base::UTF16ToUTF8(manifest16));
338     int size = base::checked_cast<int>(manifest.size());
339     if (base::WriteFile(
340         src_path.Append(installer::kVisualElementsManifest),
341             manifest.c_str(), size) == size) {
342       VLOG(1) << "Successfully wrote " << installer::kVisualElementsManifest
343               << " to " << src_path.value();
344       return true;
345     } else {
346       PLOG(ERROR) << "Error writing " << installer::kVisualElementsManifest
347                   << " to " << src_path.value();
348       return false;
349     }
350   }
351 }
352
353 void CreateOrUpdateShortcuts(
354     const base::FilePath& target,
355     const installer::Product& product,
356     const MasterPreferences& prefs,
357     InstallShortcutLevel install_level,
358     InstallShortcutOperation install_operation) {
359   bool do_not_create_any_shortcuts = false;
360   prefs.GetBool(master_preferences::kDoNotCreateAnyShortcuts,
361                 &do_not_create_any_shortcuts);
362   if (do_not_create_any_shortcuts)
363     return;
364
365   // Extract shortcut preferences from |prefs|.
366   bool do_not_create_desktop_shortcut = false;
367   bool do_not_create_quick_launch_shortcut = false;
368   bool do_not_create_taskbar_shortcut = false;
369   bool alternate_desktop_shortcut = false;
370   prefs.GetBool(master_preferences::kDoNotCreateDesktopShortcut,
371                 &do_not_create_desktop_shortcut);
372   prefs.GetBool(master_preferences::kDoNotCreateQuickLaunchShortcut,
373                 &do_not_create_quick_launch_shortcut);
374   prefs.GetBool(master_preferences::kDoNotCreateTaskbarShortcut,
375                 &do_not_create_taskbar_shortcut);
376   prefs.GetBool(master_preferences::kAltShortcutText,
377                 &alternate_desktop_shortcut);
378
379   BrowserDistribution* dist = product.distribution();
380
381   // The default operation on update is to overwrite shortcuts with the
382   // currently desired properties, but do so only for shortcuts that still
383   // exist.
384   ShellUtil::ShortcutOperation shortcut_operation;
385   switch (install_operation) {
386     case INSTALL_SHORTCUT_CREATE_ALL:
387       shortcut_operation = ShellUtil::SHELL_SHORTCUT_CREATE_ALWAYS;
388       break;
389     case INSTALL_SHORTCUT_CREATE_EACH_IF_NO_SYSTEM_LEVEL:
390       shortcut_operation = ShellUtil::SHELL_SHORTCUT_CREATE_IF_NO_SYSTEM_LEVEL;
391       break;
392     default:
393       DCHECK(install_operation == INSTALL_SHORTCUT_REPLACE_EXISTING);
394       shortcut_operation = ShellUtil::SHELL_SHORTCUT_REPLACE_EXISTING;
395       break;
396   }
397
398   // Shortcuts are always installed per-user unless specified.
399   ShellUtil::ShellChange shortcut_level = (install_level == ALL_USERS ?
400       ShellUtil::SYSTEM_LEVEL : ShellUtil::CURRENT_USER);
401
402   // |base_properties|: The basic properties to set on every shortcut installed
403   // (to be refined on a per-shortcut basis).
404   ShellUtil::ShortcutProperties base_properties(shortcut_level);
405   product.AddDefaultShortcutProperties(target, &base_properties);
406
407   if (!do_not_create_desktop_shortcut ||
408       shortcut_operation == ShellUtil::SHELL_SHORTCUT_REPLACE_EXISTING) {
409     ShellUtil::ShortcutProperties desktop_properties(base_properties);
410     if (alternate_desktop_shortcut) {
411       desktop_properties.set_shortcut_name(
412           dist->GetShortcutName(
413               BrowserDistribution::SHORTCUT_CHROME_ALTERNATE));
414     }
415     ExecuteAndLogShortcutOperation(
416         ShellUtil::SHORTCUT_LOCATION_DESKTOP, dist, desktop_properties,
417         shortcut_operation);
418
419     // On update there is no harm in always trying to update the alternate
420     // Desktop shortcut.
421     if (!alternate_desktop_shortcut &&
422         shortcut_operation == ShellUtil::SHELL_SHORTCUT_REPLACE_EXISTING) {
423       desktop_properties.set_shortcut_name(
424           dist->GetShortcutName(
425               BrowserDistribution::SHORTCUT_CHROME_ALTERNATE));
426       ExecuteAndLogShortcutOperation(
427           ShellUtil::SHORTCUT_LOCATION_DESKTOP, dist, desktop_properties,
428           shortcut_operation);
429     }
430   }
431
432   if (!do_not_create_quick_launch_shortcut ||
433       shortcut_operation == ShellUtil::SHELL_SHORTCUT_REPLACE_EXISTING) {
434     // There is no such thing as an all-users Quick Launch shortcut, always
435     // install the per-user shortcut.
436     ShellUtil::ShortcutProperties quick_launch_properties(base_properties);
437     quick_launch_properties.level = ShellUtil::CURRENT_USER;
438     ExecuteAndLogShortcutOperation(
439         ShellUtil::SHORTCUT_LOCATION_QUICK_LAUNCH, dist,
440         quick_launch_properties, shortcut_operation);
441   }
442
443   ShellUtil::ShortcutProperties start_menu_properties(base_properties);
444   // IMPORTANT: Only the default (no arguments and default browserappid) browser
445   // shortcut in the Start menu (Start screen on Win8+) should be made dual
446   // mode.
447   start_menu_properties.set_dual_mode(true);
448   if (!do_not_create_taskbar_shortcut &&
449       (shortcut_operation == ShellUtil::SHELL_SHORTCUT_CREATE_ALWAYS ||
450        shortcut_operation ==
451            ShellUtil::SHELL_SHORTCUT_CREATE_IF_NO_SYSTEM_LEVEL)) {
452     start_menu_properties.set_pin_to_taskbar(true);
453   }
454   ExecuteAndLogShortcutOperation(
455       ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_DIR, dist,
456       start_menu_properties, shortcut_operation);
457 }
458
459 void RegisterChromeOnMachine(const installer::InstallerState& installer_state,
460                              const installer::Product& product,
461                              bool make_chrome_default) {
462   DCHECK(product.is_chrome());
463
464   // Try to add Chrome to Media Player shim inclusion list. We don't do any
465   // error checking here because this operation will fail if user doesn't
466   // have admin rights and we want to ignore the error.
467   AddChromeToMediaPlayerList();
468
469   // Make Chrome the default browser if desired when possible. Otherwise, only
470   // register it with Windows.
471   BrowserDistribution* dist = product.distribution();
472   const base::string16 chrome_exe(
473       installer_state.target_path().Append(installer::kChromeExe).value());
474   VLOG(1) << "Registering Chrome as browser: " << chrome_exe;
475   if (make_chrome_default && ShellUtil::CanMakeChromeDefaultUnattended()) {
476     int level = ShellUtil::CURRENT_USER;
477     if (installer_state.system_install())
478       level = level | ShellUtil::SYSTEM_LEVEL;
479     ShellUtil::MakeChromeDefault(dist, level, chrome_exe, true);
480   } else {
481     ShellUtil::RegisterChromeBrowser(dist, chrome_exe, base::string16(), false);
482   }
483 }
484
485 InstallStatus InstallOrUpdateProduct(
486     const installer::InstallationState& original_state,
487     const installer::InstallerState& installer_state,
488     const base::FilePath& setup_path,
489     const base::FilePath& archive_path,
490     const base::FilePath& install_temp_path,
491     const base::FilePath& src_path,
492     const base::FilePath& prefs_path,
493     const MasterPreferences& prefs,
494     const Version& new_version) {
495   DCHECK(!installer_state.products().empty());
496
497   // TODO(robertshield): Removing the pending on-reboot moves should be done
498   // elsewhere.
499   // Remove any scheduled MOVEFILE_DELAY_UNTIL_REBOOT entries in the target of
500   // this installation. These may have been added during a previous uninstall of
501   // the same version.
502   LOG_IF(ERROR, !RemoveFromMovesPendingReboot(installer_state.target_path()))
503       << "Error accessing pending moves value.";
504
505   // Create VisualElementManifest.xml in |src_path| (if required) so that it
506   // looks as if it had been extracted from the archive when calling
507   // InstallNewVersion() below.
508   installer_state.UpdateStage(installer::CREATING_VISUAL_MANIFEST);
509   CreateVisualElementsManifest(src_path, new_version);
510
511   scoped_ptr<Version> existing_version;
512   InstallStatus result = InstallNewVersion(original_state, installer_state,
513       setup_path, archive_path, src_path, install_temp_path, new_version,
514       &existing_version);
515
516   // TODO(robertshield): Everything below this line should instead be captured
517   // by WorkItems.
518   if (!InstallUtil::GetInstallReturnCode(result)) {
519     installer_state.UpdateStage(installer::UPDATING_CHANNELS);
520
521     // Update the modifiers on the channel values for the product(s) being
522     // installed and for the binaries in case of multi-install.
523     installer_state.UpdateChannels();
524
525     installer_state.UpdateStage(installer::COPYING_PREFERENCES_FILE);
526
527     if (result == FIRST_INSTALL_SUCCESS && !prefs_path.empty())
528       CopyPreferenceFileForFirstRun(installer_state, prefs_path);
529
530     installer_state.UpdateStage(installer::CREATING_SHORTCUTS);
531
532     const installer::Product* app_launcher_product =
533         installer_state.FindProduct(BrowserDistribution::CHROME_APP_HOST);
534     // Creates shortcuts for App Launcher.
535     if (app_launcher_product) {
536       // TODO(huangs): Remove this check once we have system-level App Host.
537       DCHECK(!installer_state.system_install());
538       const base::FilePath app_host_exe(
539           installer_state.target_path().Append(kChromeAppHostExe));
540       InstallShortcutOperation app_launcher_shortcut_operation =
541           GetAppLauncherShortcutOperation(original_state, installer_state);
542
543       // Always install per-user shortcuts for App Launcher.
544       CreateOrUpdateShortcuts(app_host_exe, *app_launcher_product, prefs,
545                               CURRENT_USER, app_launcher_shortcut_operation);
546     }
547
548     const installer::Product* chrome_product =
549         installer_state.FindProduct(BrowserDistribution::CHROME_BROWSER);
550     // Creates shortcuts for Chrome.
551     if (chrome_product) {
552       BrowserDistribution* chrome_dist = chrome_product->distribution();
553       const base::FilePath chrome_exe(
554           installer_state.target_path().Append(kChromeExe));
555       CleanupLegacyShortcuts(installer_state, chrome_dist, chrome_exe);
556
557       // Install per-user shortcuts on user-level installs and all-users
558       // shortcuts on system-level installs. Note that Active Setup will take
559       // care of installing missing per-user shortcuts on system-level install
560       // (i.e., quick launch, taskbar pin, and possibly deleted all-users
561       // shortcuts).
562       InstallShortcutLevel install_level = installer_state.system_install() ?
563           ALL_USERS : CURRENT_USER;
564
565       InstallShortcutOperation install_operation =
566           INSTALL_SHORTCUT_REPLACE_EXISTING;
567       if (result == installer::FIRST_INSTALL_SUCCESS ||
568           result == installer::INSTALL_REPAIRED ||
569           !original_state.GetProductState(installer_state.system_install(),
570                                           chrome_dist->GetType())) {
571         // Always create the shortcuts on a new install, a repair install, and
572         // when the Chrome product is being added to the current install.
573         install_operation = INSTALL_SHORTCUT_CREATE_ALL;
574       }
575
576       CreateOrUpdateShortcuts(chrome_exe, *chrome_product, prefs, install_level,
577                               install_operation);
578     }
579
580     if (chrome_product) {
581       // Register Chrome and, if requested, make Chrome the default browser.
582       installer_state.UpdateStage(installer::REGISTERING_CHROME);
583
584       bool make_chrome_default = false;
585       prefs.GetBool(master_preferences::kMakeChromeDefault,
586                     &make_chrome_default);
587
588       // If this is not the user's first Chrome install, but they have chosen
589       // Chrome to become their default browser on the download page, we must
590       // force it here because the master_preferences file will not get copied
591       // into the build.
592       bool force_chrome_default_for_user = false;
593       if (result == NEW_VERSION_UPDATED ||
594           result == INSTALL_REPAIRED) {
595         prefs.GetBool(master_preferences::kMakeChromeDefaultForUser,
596                       &force_chrome_default_for_user);
597       }
598
599       RegisterChromeOnMachine(installer_state, *chrome_product,
600           make_chrome_default || force_chrome_default_for_user);
601
602       // Configure auto-launch.
603       if (result == FIRST_INSTALL_SUCCESS) {
604         installer_state.UpdateStage(installer::CONFIGURE_AUTO_LAUNCH);
605
606         // Add auto-launch key if specified in master_preferences.
607         bool auto_launch_chrome = false;
608         prefs.GetBool(
609             installer::master_preferences::kAutoLaunchChrome,
610             &auto_launch_chrome);
611         if (auto_launch_chrome) {
612           auto_launch_util::EnableForegroundStartAtLogin(
613               base::ASCIIToUTF16(chrome::kInitialProfile),
614               installer_state.target_path());
615         }
616       }
617     }
618
619     installer_state.UpdateStage(installer::REMOVING_OLD_VERSIONS);
620
621     installer_state.RemoveOldVersionDirectories(
622         new_version,
623         existing_version.get(),
624         install_temp_path);
625   }
626
627   return result;
628 }
629
630 void HandleOsUpgradeForBrowser(const installer::InstallerState& installer_state,
631                                const installer::Product& chrome) {
632   DCHECK(chrome.is_chrome());
633   // Upon upgrading to Windows 8, we need to fix Chrome shortcuts and register
634   // Chrome, so that Metro Chrome would work if Chrome is the default browser.
635   if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
636     VLOG(1) << "Updating and registering shortcuts.";
637     // Read master_preferences copied beside chrome.exe at install.
638     MasterPreferences prefs(
639         installer_state.target_path().AppendASCII(kDefaultMasterPrefs));
640
641     // Unfortunately, if this is a system-level install, we can't update the
642     // shortcuts of each individual user (this only matters if this is an OS
643     // upgrade from XP/Vista to Win7+ as some properties are only set on
644     // shortcuts as of Win7).
645     // At least attempt to update potentially existing all-users shortcuts.
646     InstallShortcutLevel level = installer_state.system_install() ?
647         ALL_USERS : CURRENT_USER;
648     base::FilePath chrome_exe(installer_state.target_path().Append(kChromeExe));
649     CreateOrUpdateShortcuts(
650         chrome_exe, chrome, prefs, level, INSTALL_SHORTCUT_REPLACE_EXISTING);
651     RegisterChromeOnMachine(installer_state, chrome, false);
652   }
653 }
654
655 // NOTE: Should the work done here, on Active Setup, change: kActiveSetupVersion
656 // in install_worker.cc needs to be increased for Active Setup to invoke this
657 // again for all users of this install.
658 void HandleActiveSetupForBrowser(const base::FilePath& installation_root,
659                                  const installer::Product& chrome,
660                                  bool force) {
661   DCHECK(chrome.is_chrome());
662   // Only create shortcuts on Active Setup if the first run sentinel is not
663   // present for this user (as some shortcuts used to be installed on first
664   // run and this could otherwise re-install shortcuts for users that have
665   // already deleted them in the past).
666   // Decide whether to create the shortcuts or simply replace existing
667   // shortcuts; if the decision is to create them, only shortcuts whose matching
668   // all-users shortcut isn't present on the system will be created.
669   InstallShortcutOperation install_operation =
670       (!force && InstallUtil::IsFirstRunSentinelPresent() ?
671            INSTALL_SHORTCUT_REPLACE_EXISTING :
672            INSTALL_SHORTCUT_CREATE_EACH_IF_NO_SYSTEM_LEVEL);
673
674   // Read master_preferences copied beside chrome.exe at install.
675   MasterPreferences prefs(installation_root.AppendASCII(kDefaultMasterPrefs));
676   base::FilePath chrome_exe(installation_root.Append(kChromeExe));
677   CreateOrUpdateShortcuts(
678       chrome_exe, chrome, prefs, CURRENT_USER, install_operation);
679 }
680
681 }  // namespace installer