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