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