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/browser/lifetime/application_lifetime.h"
6
7#include "base/bind.h"
8#include "base/prefs/pref_service.h"
9#include "base/win/metro.h"
10#include "base/win/windows_version.h"
11#include "chrome/browser/browser_process.h"
12#include "chrome/browser/first_run/upgrade_util.h"
13#include "chrome/browser/ui/browser_finder.h"
14#include "chrome/common/pref_names.h"
15#include "ui/views/widget/widget.h"
16
17#if defined(USE_AURA)
18#include "base/environment.h"
19#include "base/files/file_path.h"
20#include "base/path_service.h"
21#include "chrome/browser/metro_utils/metro_chrome_win.h"
22#include "chrome/browser/metro_viewer/chrome_metro_viewer_process_host_aurawin.h"
23#include "chrome/browser/shell_integration.h"
24#include "chrome/common/chrome_constants.h"
25#include "chrome/installer/util/util_constants.h"
26#include "content/public/browser/web_contents.h"
27#endif
28
29namespace chrome {
30
31#if !defined(USE_AURA)
32void HandleAppExitingForPlatform() {
33  views::Widget::CloseAllSecondaryWidgets();
34}
35#endif
36
37// Following set of functions, which are used to switch chrome mode after
38// restart are used for in places where either user explicitly wants to switch
39// mode or some functionality is not available in either mode and we ask user
40// to switch mode.
41// Here mode refers to Windows 8 modes such as Metro (also called immersive)
42// and desktop mode (Classic or traditional).
43
44// Mode switch based on current mode which is devised from current process.
45void AttemptRestartWithModeSwitch() {
46#if defined(USE_AURA)
47  // This function should be called only from non aura code path.
48  // In aura/ash windows world browser process is always non metro.
49  NOTREACHED();
50#else
51  // The kRestartSwitchMode preference does not exists for Windows 7 and older
52  // operating systems so there is no need for OS version check.
53  PrefService* prefs = g_browser_process->local_state();
54  if (base::win::IsMetroProcess()) {
55    prefs->SetString(prefs::kRelaunchMode,
56                     upgrade_util::kRelaunchModeDesktop);
57  } else {
58    prefs->SetString(prefs::kRelaunchMode,
59                     upgrade_util::kRelaunchModeMetro);
60  }
61  AttemptRestart();
62#endif
63}
64
65#if defined(USE_AURA)
66void ActivateDesktopHelper(AshExecutionStatus ash_execution_status) {
67  scoped_ptr<base::Environment> env(base::Environment::Create());
68  std::string version_str;
69
70  // Get the version variable and remove it from the environment.
71  if (!env->GetVar(chrome::kChromeVersionEnvVar, &version_str))
72    version_str.clear();
73
74  base::FilePath exe_path;
75  if (!PathService::Get(base::FILE_EXE, &exe_path))
76    return;
77
78  base::FilePath path(exe_path.DirName());
79
80  // The relauncher is ordinarily in the version directory.  When running in a
81  // build tree however (where CHROME_VERSION is not set in the environment)
82  // look for it in Chrome's directory.
83  if (!version_str.empty())
84    path = path.AppendASCII(version_str);
85
86  path = path.Append(installer::kDelegateExecuteExe);
87
88  // Actually launching the process needs to happen in the metro viewer,
89  // otherwise it won't automatically transition to desktop.  So we have
90  // to send an IPC to the viewer to do the ShellExecute.
91  ChromeMetroViewerProcessHost::HandleActivateDesktop(
92      path, ash_execution_status == ASH_TERMINATE);
93}
94#endif
95
96void AttemptRestartToDesktopMode() {
97  PrefService* prefs = g_browser_process->local_state();
98  prefs->SetString(prefs::kRelaunchMode,
99                   upgrade_util::kRelaunchModeDesktop);
100
101  AttemptRestart();
102}
103
104void AttemptRestartToMetroMode() {
105  PrefService* prefs = g_browser_process->local_state();
106  prefs->SetString(prefs::kRelaunchMode,
107                   upgrade_util::kRelaunchModeMetro);
108  AttemptRestart();
109}
110
111}  // namespace chrome
112