Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(143)

Side by Side Diff: content/browser/browser_main_runner.cc

Issue 9375017: Support sharing of ContentMain and BrowserMain code with embedded use cases (try #3). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/browser_main.h" 5 #include "content/public/browser/browser_main_runner.h"
6 6
7 #include "base/allocator/allocator_shim.h" 7 #include "base/allocator/allocator_shim.h"
8 #include "base/base_switches.h" 8 #include "base/base_switches.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/debug/trace_event.h" 10 #include "base/debug/trace_event.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/metrics/histogram.h" 12 #include "base/metrics/histogram.h"
13 #include "content/browser/browser_main_loop.h" 13 #include "content/browser/browser_main_loop.h"
14 #include "content/browser/notification_service_impl.h" 14 #include "content/browser/notification_service_impl.h"
15 #include "content/common/child_process.h" 15 #include "content/common/child_process.h"
16 #include "content/public/common/content_switches.h" 16 #include "content/public/common/content_switches.h"
17 #include "content/public/common/main_function_params.h" 17 #include "content/public/common/main_function_params.h"
18 18
19 #if defined(OS_WIN) 19 #if defined(OS_WIN)
20 #include "base/win/scoped_com_initializer.h" 20 #include "base/win/scoped_com_initializer.h"
21 #endif 21 #endif
22 22
23 bool g_exited_main_message_loop = false;
24
23 namespace { 25 namespace {
24 26
25 bool g_exited_main_message_loop = false; 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 };
26 138
27 } // namespace 139 } // namespace
28 140
29 namespace content { 141 namespace content {
30 142
31 bool ExitedMainMessageLoop() { 143 // static
32 return g_exited_main_message_loop; 144 BrowserMainRunner* BrowserMainRunner::Create() {
145 return new BrowserMainRunnerImpl();
33 } 146 }
34 147
35 } // namespace content 148 } // namespace content
36
37 // Main routine for running as the Browser process.
38 int BrowserMain(const content::MainFunctionParams& parameters) {
39 TRACE_EVENT_BEGIN_ETW("BrowserMain", 0, "");
40
41 // ChildProcess:: is a misnomer unless you consider context. Use
42 // of --wait-for-debugger only makes sense when Chrome itself is a
43 // child process (e.g. when launched by PyAuto).
44 if (parameters.command_line.HasSwitch(switches::kWaitForDebugger))
45 ChildProcess::WaitForDebugger("Browser");
46
47 NotificationServiceImpl main_notification_service;
48
49 scoped_ptr<content::BrowserMainLoop> main_loop(
50 new content::BrowserMainLoop(parameters));
51
52 main_loop->Init();
53
54 main_loop->EarlyInitialization();
55
56 // Must happen before we try to use a message loop or display any UI.
57 main_loop->InitializeToolkit();
58
59 main_loop->MainMessageLoopStart();
60
61 // WARNING: If we get a WM_ENDSESSION, objects created on the stack here
62 // are NOT deleted. If you need something to run during WM_ENDSESSION add it
63 // to browser_shutdown::Shutdown or BrowserProcess::EndSession.
64
65 // !!!!!!!!!! READ ME !!!!!!!!!!
66 // I (viettrungluu) am in the process of refactoring |BrowserMain()|. If you
67 // need to add something above this comment, read the documentation in
68 // browser_main.h. If you need to add something below, please do the
69 // following:
70 // - Figure out where you should add your code. Do NOT just pick a random
71 // location "which works".
72 // - Document the dependencies apart from compile-time-checkable ones. What
73 // must happen before your new code is executed? Does your new code need to
74 // run before something else? Are there performance reasons for executing
75 // your code at that point?
76 // - If you need to create a (persistent) object, heap allocate it and keep a
77 // |scoped_ptr| to it rather than allocating it on the stack. Otherwise
78 // I'll have to convert your code when I refactor.
79 // - Unless your new code is just a couple of lines, factor it out into a
80 // function with a well-defined purpose. Do NOT just add it inline in
81 // |BrowserMain()|.
82 // Thanks!
83
84 // TODO(viettrungluu): put the remainder into BrowserMainParts
85
86 #if defined(OS_WIN)
87 #if !defined(NO_TCMALLOC)
88 // When linking shared libraries, NO_TCMALLOC is defined, and dynamic
89 // allocator selection is not supported.
90
91 // Make this call before going multithreaded, or spawning any subprocesses.
92 base::allocator::SetupSubprocessAllocator();
93 #endif
94
95 base::win::ScopedCOMInitializer com_initializer;
96 #endif // OS_WIN
97
98 base::StatisticsRecorder statistics;
99
100 main_loop->RunMainMessageLoopParts(&g_exited_main_message_loop);
101
102 TRACE_EVENT_END_ETW("BrowserMain", 0, 0);
103
104 return main_loop->GetResultCode();
105 }
OLDNEW
« no previous file with comments | « content/browser/browser_main_loop.cc ('k') | content/browser/renderer_host/render_process_host_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698