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.
8 #include "base/at_exit.h"
9 #include "base/command_line.h"
10 #include "chrome/app/breakpad_win.h"
11 #include "chrome/app/client_util.h"
12 #include "chrome/app/metro_driver_win.h"
13 #include "content/public/app/startup_helper_win.h"
14 #include "content/public/common/result_codes.h"
15 #include "sandbox/win/src/sandbox_factory.h"
19 // TODO(jschuh): Remove this after we narrow down the cause of the crashes.
22 static void NTAPI
UpdateCount(PVOID module
, DWORD reason
, PVOID reserved
) {
23 if (reason
== DLL_THREAD_ATTACH
) {
24 // We hit the threshold, but the loader would eat an exception fired now.
25 // So schedule an APC to fire the exception once loading is done.
26 if (::InterlockedIncrement(&count_
) > cap_
)
27 ::QueueUserAPC(CrashProcessCallback
, ::GetCurrentThread(), NULL
);
28 } else if (reason
== DLL_THREAD_DETACH
) {
29 ::InterlockedDecrement(&count_
);
33 static void SetCap(LONG cap
) { cap_
= cap
; }
36 static void CALLBACK
CrashProcessCallback(ULONG_PTR
) { __debugbreak(); }
38 static volatile LONG count_
;
39 static volatile LONG cap_
;
42 LONG
volatile ThreadTracker::count_
= 1;
43 LONG
volatile ThreadTracker::cap_
= LONG_MAX
;
47 // Magic required to get our function called on thread attach and detach.
49 #pragma data_seg(push, old_seg)
50 #pragma data_seg(".CRT$XLB")
51 PIMAGE_TLS_CALLBACK p_thread_callback
= ThreadTracker::UpdateCount
;
52 #pragma data_seg(pop, old_seg)
54 #pragma comment(linker, "/INCLUDE:__tls_used")
55 #pragma comment(linker, "/INCLUDE:_p_thread_callback")
58 int RunChrome(HINSTANCE instance
) {
60 // We restarted because of a previous crash. Ask user if we should relaunch.
61 if (ShowRestartDialogIfCrashed(&exit_now
)) {
63 return content::RESULT_CODE_NORMAL_EXIT
;
66 // Initialize the sandbox services.
67 sandbox::SandboxInterfaceInfo sandbox_info
= {0};
68 content::InitializeSandboxInfo(&sandbox_info
);
70 // Cap the threads for any sandboxed process.
71 if (sandbox_info
.target_services
)
72 ThreadTracker::SetCap(200);
74 // Load and launch the chrome dll. *Everything* happens inside.
75 MainDllLoader
* loader
= MakeMainDllLoader();
76 int rc
= loader
->Launch(instance
, &sandbox_info
);
77 loader
->RelaunchChromeBrowserWithNewCommandLineIfNeeded();
82 int APIENTRY
wWinMain(HINSTANCE instance
, HINSTANCE prev
, wchar_t*, int) {
83 // Initialize the commandline singleton from the environment.
84 CommandLine::Init(0, NULL
);
85 // The exit manager is in charge of calling the dtors of singletons.
86 base::AtExitManager exit_manager
;
88 MetroDriver metro_driver
;
89 if (metro_driver
.in_metro_mode())
90 return metro_driver
.RunInMetro(instance
, &RunChrome
);
91 // Not in metro mode, proceed as normal.
92 return RunChrome(instance
);