OLD | NEW |
| (Empty) |
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 "content/public/browser/browser_main_runner.h" | |
6 | |
7 #include "base/allocator/allocator_shim.h" | |
8 #include "base/base_switches.h" | |
9 #include "base/command_line.h" | |
10 #include "base/debug/trace_event.h" | |
11 #include "base/logging.h" | |
12 #include "base/metrics/histogram.h" | |
13 #include "content/browser/browser_main_loop.h" | |
14 #include "content/browser/notification_service_impl.h" | |
15 #include "content/common/child_process.h" | |
16 #include "content/public/common/content_switches.h" | |
17 #include "content/public/common/main_function_params.h" | |
18 | |
19 #if defined(OS_WIN) | |
20 #include "base/win/scoped_com_initializer.h" | |
21 #endif | |
22 | |
23 bool g_exited_main_message_loop = false; | |
24 | |
25 namespace { | |
26 | |
27 class BrowserMainRunnerImpl : public content::BrowserMainRunner { | |
28 public: | |
29 BrowserMainRunnerImpl() | |
30 : is_initialized_(false), | |
31 is_shutdown_(false), | |
32 created_threads_(false) { | |
33 } | |
34 | |
35 ~BrowserMainRunnerImpl() { | |
36 if (is_initialized_ && !is_shutdown_) | |
37 Shutdown(); | |
38 } | |
39 | |
40 virtual int Initialize(const content::MainFunctionParams& parameters) | |
41 OVERRIDE { | |
42 is_initialized_ = true; | |
43 | |
44 // ChildProcess:: is a misnomer unless you consider context. Use | |
45 // of --wait-for-debugger only makes sense when Chrome itself is a | |
46 // child process (e.g. when launched by PyAuto). | |
47 if (parameters.command_line.HasSwitch(switches::kWaitForDebugger)) | |
48 ChildProcess::WaitForDebugger("Browser"); | |
49 | |
50 notification_service_.reset(new NotificationServiceImpl); | |
51 | |
52 main_loop_.reset(new content::BrowserMainLoop(parameters)); | |
53 | |
54 main_loop_->Init(); | |
55 | |
56 main_loop_->EarlyInitialization(); | |
57 | |
58 // Must happen before we try to use a message loop or display any UI. | |
59 main_loop_->InitializeToolkit(); | |
60 | |
61 main_loop_->MainMessageLoopStart(); | |
62 | |
63 // WARNING: If we get a WM_ENDSESSION, objects created on the stack here | |
64 // are NOT deleted. If you need something to run during WM_ENDSESSION add it | |
65 // to browser_shutdown::Shutdown or BrowserProcess::EndSession. | |
66 | |
67 #if defined(OS_WIN) | |
68 #if !defined(NO_TCMALLOC) | |
69 // When linking shared libraries, NO_TCMALLOC is defined, and dynamic | |
70 // allocator selection is not supported. | |
71 | |
72 // Make this call before going multithreaded, or spawning any subprocesses. | |
73 base::allocator::SetupSubprocessAllocator(); | |
74 #endif | |
75 | |
76 com_initializer_.reset(new base::win::ScopedCOMInitializer); | |
77 #endif // OS_WIN | |
78 | |
79 statistics_.reset(new base::StatisticsRecorder); | |
80 | |
81 main_loop_->CreateThreads(); | |
82 int result_code = main_loop_->GetResultCode(); | |
83 if (result_code > 0) | |
84 return result_code; | |
85 created_threads_ = true; | |
86 | |
87 // Return -1 to indicate no early termination. | |
88 return -1; | |
89 } | |
90 | |
91 virtual int Run() OVERRIDE { | |
92 DCHECK(is_initialized_); | |
93 DCHECK(!is_shutdown_); | |
94 main_loop_->RunMainMessageLoopParts(); | |
95 return main_loop_->GetResultCode(); | |
96 } | |
97 | |
98 virtual void Shutdown() OVERRIDE { | |
99 DCHECK(is_initialized_); | |
100 DCHECK(!is_shutdown_); | |
101 g_exited_main_message_loop = true; | |
102 | |
103 if (created_threads_) | |
104 main_loop_->ShutdownThreadsAndCleanUp(); | |
105 | |
106 statistics_.reset(NULL); | |
107 | |
108 #if defined(OS_WIN) | |
109 com_initializer_.reset(NULL); | |
110 #endif | |
111 | |
112 main_loop_.reset(NULL); | |
113 | |
114 notification_service_.reset(NULL); | |
115 | |
116 is_shutdown_ = true; | |
117 } | |
118 | |
119 protected: | |
120 // True if the runner has been initialized. | |
121 bool is_initialized_; | |
122 | |
123 // True if the runner has been shut down. | |
124 bool is_shutdown_; | |
125 | |
126 // True if the non-UI threads were created. | |
127 bool created_threads_; | |
128 | |
129 scoped_ptr<NotificationServiceImpl> notification_service_; | |
130 scoped_ptr<content::BrowserMainLoop> main_loop_; | |
131 #if defined(OS_WIN) | |
132 scoped_ptr<base::win::ScopedCOMInitializer> com_initializer_; | |
133 #endif | |
134 scoped_ptr<base::StatisticsRecorder> statistics_; | |
135 | |
136 DISALLOW_COPY_AND_ASSIGN(BrowserMainRunnerImpl); | |
137 }; | |
138 | |
139 } // namespace | |
140 | |
141 namespace content { | |
142 | |
143 // static | |
144 BrowserMainRunner* BrowserMainRunner::Create() { | |
145 return new BrowserMainRunnerImpl(); | |
146 } | |
147 | |
148 } // namespace content | |
OLD | NEW |