Home | History | Annotate | Download | only in app
      1 // Copyright (c) 2011 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 <windows.h>
      6 #include <tchar.h>
      7 
      8 #include <string>
      9 
     10 #include "base/at_exit.h"
     11 #include "base/command_line.h"
     12 #include "base/files/file_path.h"
     13 #include "base/win/windows_version.h"
     14 #include "chrome/app/client_util.h"
     15 #include "chrome/browser/chrome_process_finder_win.h"
     16 #include "chrome/browser/policy/policy_path_parser.h"
     17 #include "chrome/common/chrome_constants.h"
     18 #include "chrome/common/chrome_paths_internal.h"
     19 #include "chrome/common/chrome_switches.h"
     20 #include "chrome_elf/chrome_elf_main.h"
     21 #include "components/startup_metric_utils/startup_metric_utils.h"
     22 #include "content/public/common/result_codes.h"
     23 #include "ui/gfx/win/dpi.h"
     24 
     25 namespace {
     26 
     27 void CheckSafeModeLaunch() {
     28   unsigned short k1 = ::GetAsyncKeyState(VK_CONTROL);
     29   unsigned short k2 = ::GetAsyncKeyState(VK_MENU);
     30   const unsigned short kPressedMask = 0x8000;
     31   if ((k1 & kPressedMask) && (k2 & kPressedMask))
     32     ::SetEnvironmentVariableA(chrome::kSafeModeEnvVar, "1");
     33 }
     34 
     35 // List of switches that it's safe to rendezvous early with. Fast start should
     36 // not be done if a command line contains a switch not in this set.
     37 // Note this is currently stored as a list of two because it's probably faster
     38 // to iterate over this small array than building a map for constant time
     39 // lookups.
     40 const char* const kFastStartSwitches[] = {
     41   switches::kProfileDirectory,
     42   switches::kShowAppList,
     43 };
     44 
     45 bool IsFastStartSwitch(const std::string& command_line_switch) {
     46   for (size_t i = 0; i < arraysize(kFastStartSwitches); ++i) {
     47     if (command_line_switch == kFastStartSwitches[i])
     48       return true;
     49   }
     50   return false;
     51 }
     52 
     53 bool ContainsNonFastStartFlag(const CommandLine& command_line) {
     54   const CommandLine::SwitchMap& switches = command_line.GetSwitches();
     55   if (switches.size() > arraysize(kFastStartSwitches))
     56     return true;
     57   for (CommandLine::SwitchMap::const_iterator it = switches.begin();
     58        it != switches.end(); ++it) {
     59     if (!IsFastStartSwitch(it->first))
     60       return true;
     61   }
     62   return false;
     63 }
     64 
     65 bool AttemptFastNotify(const CommandLine& command_line) {
     66   if (ContainsNonFastStartFlag(command_line))
     67     return false;
     68 
     69   base::FilePath user_data_dir;
     70   if (!chrome::GetDefaultUserDataDirectory(&user_data_dir))
     71     return false;
     72   policy::path_parser::CheckUserDataDirPolicy(&user_data_dir);
     73 
     74   HWND chrome = chrome::FindRunningChromeWindow(user_data_dir);
     75   if (!chrome)
     76     return false;
     77   return chrome::AttemptToNotifyRunningChrome(chrome, true) ==
     78       chrome::NOTIFY_SUCCESS;
     79 }
     80 
     81 }  // namespace
     82 
     83 #if !defined(ADDRESS_SANITIZER)
     84 int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prev, wchar_t*, int) {
     85 #else
     86 // The AddressSanitizer build should be a console program as it prints out stuff
     87 // on stderr.
     88 int main() {
     89   HINSTANCE instance = GetModuleHandle(NULL);
     90 #endif
     91   startup_metric_utils::RecordExeMainEntryTime();
     92 
     93   // Signal Chrome Elf that Chrome has begun to start.
     94   SignalChromeElf();
     95 
     96   // Initialize the commandline singleton from the environment.
     97   CommandLine::Init(0, NULL);
     98   // The exit manager is in charge of calling the dtors of singletons.
     99   base::AtExitManager exit_manager;
    100 
    101   // We don't want to set DPI awareness on Vista because we don't support
    102   // DirectWrite there. GDI fonts are kerned very badly, so better to leave
    103   // DPI-unaware and at effective 1.0. See also ShouldUseDirectWrite().
    104   if (base::win::GetVersion() > base::win::VERSION_VISTA)
    105     gfx::EnableHighDPISupport();
    106 
    107   if (AttemptFastNotify(*CommandLine::ForCurrentProcess()))
    108     return 0;
    109 
    110   CheckSafeModeLaunch();
    111 
    112   // Load and launch the chrome dll. *Everything* happens inside.
    113   MainDllLoader* loader = MakeMainDllLoader();
    114   int rc = loader->Launch(instance);
    115   loader->RelaunchChromeBrowserWithNewCommandLineIfNeeded();
    116   delete loader;
    117   return rc;
    118 }
    119