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