summaryrefslogtreecommitdiffstats
path: root/content/browser/browser_main_runner.cc
diff options
context:
space:
mode:
authormarshall@chromium.org <marshall@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-02-06 16:05:13 +0000
committermarshall@chromium.org <marshall@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-02-06 16:05:13 +0000
commit6f2b39bccfbb74a7633deff531a33f03efe9accf (patch)
tree4e27e58cc514dd3d553f790bb0ca299b33245e2c /content/browser/browser_main_runner.cc
parentb3cc0c25614566f1261b9c3759d6a076e327fd6c (diff)
downloadchromium_src-6f2b39bccfbb74a7633deff531a33f03efe9accf.zip
chromium_src-6f2b39bccfbb74a7633deff531a33f03efe9accf.tar.gz
chromium_src-6f2b39bccfbb74a7633deff531a33f03efe9accf.tar.bz2
Support sharing of ContentMain and BrowserMain code with embedded use cases.
For the browser use case it is convenient to have a single ContentMain entry point function that handles all initialization, run and shutdown. For embedded use cases it is often necessary to integrate with existing application message loops where initialization and shutdown must be handled separately. To support sharing of this code the following changes were required: 1. Refactor the ContentMain function to create a ContentMainRunner class containing separate initialization, run and shutdown functions. 2. Refactor the BrowserMain function and BrowserMainLoop class to create a BrowserMainRunner class containing separate initialization, run and shutdown functions. 3. Add a new BrowserMainParts::GetMainMessageLoop method. This is necessary to support creation of a custom MessageLoop implementation while sharing BrowserMainRunner initialization and shutdown code. BUG=112507 TEST=none Review URL: https://chromiumcodereview.appspot.com/9190018 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@120574 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/browser/browser_main_runner.cc')
-rw-r--r--content/browser/browser_main_runner.cc138
1 files changed, 138 insertions, 0 deletions
diff --git a/content/browser/browser_main_runner.cc b/content/browser/browser_main_runner.cc
new file mode 100644
index 0000000..b7aaf74
--- /dev/null
+++ b/content/browser/browser_main_runner.cc
@@ -0,0 +1,138 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/public/browser/browser_main_runner.h"
+
+#include "base/allocator/allocator_shim.h"
+#include "base/base_switches.h"
+#include "base/command_line.h"
+#include "base/debug/trace_event.h"
+#include "base/logging.h"
+#include "base/metrics/histogram.h"
+#include "content/browser/browser_main_loop.h"
+#include "content/browser/notification_service_impl.h"
+#include "content/common/child_process.h"
+#include "content/public/common/content_switches.h"
+#include "content/public/common/main_function_params.h"
+
+#if defined(OS_WIN)
+#include "base/win/scoped_com_initializer.h"
+#endif
+
+bool g_exited_main_message_loop = false;
+
+namespace {
+
+class BrowserMainRunnerImpl : public content::BrowserMainRunner {
+ public:
+ BrowserMainRunnerImpl()
+ : is_initialized_(false),
+ is_shutdown_(false) {
+ }
+
+ ~BrowserMainRunnerImpl() {
+ if (is_initialized_ && !is_shutdown_)
+ Shutdown();
+ }
+
+ virtual int Initialize(const content::MainFunctionParams& parameters)
+ OVERRIDE {
+ is_initialized_ = true;
+
+ // ChildProcess:: is a misnomer unless you consider context. Use
+ // of --wait-for-debugger only makes sense when Chrome itself is a
+ // child process (e.g. when launched by PyAuto).
+ if (parameters.command_line.HasSwitch(switches::kWaitForDebugger))
+ ChildProcess::WaitForDebugger("Browser");
+
+ notification_service_.reset(new NotificationServiceImpl);
+
+ main_loop_.reset(new content::BrowserMainLoop(parameters));
+
+ main_loop_->Init();
+
+ main_loop_->EarlyInitialization();
+
+ // Must happen before we try to use a message loop or display any UI.
+ main_loop_->InitializeToolkit();
+
+ main_loop_->MainMessageLoopStart();
+
+ // WARNING: If we get a WM_ENDSESSION, objects created on the stack here
+ // are NOT deleted. If you need something to run during WM_ENDSESSION add it
+ // to browser_shutdown::Shutdown or BrowserProcess::EndSession.
+
+#if defined(OS_WIN)
+#if !defined(NO_TCMALLOC)
+ // When linking shared libraries, NO_TCMALLOC is defined, and dynamic
+ // allocator selection is not supported.
+
+ // Make this call before going multithreaded, or spawning any subprocesses.
+ base::allocator::SetupSubprocessAllocator();
+#endif
+
+ com_initializer_.reset(new base::win::ScopedCOMInitializer);
+#endif // OS_WIN
+
+ statistics_.reset(new base::StatisticsRecorder);
+
+ main_loop_->CreateThreads();
+
+ // Return -1 to indicate no early termination.
+ return -1;
+ }
+
+ virtual int Run() OVERRIDE {
+ DCHECK(is_initialized_);
+ DCHECK(!is_shutdown_);
+ main_loop_->RunMainMessageLoopParts();
+ return main_loop_->GetResultCode();
+ }
+
+ virtual void Shutdown() OVERRIDE {
+ DCHECK(is_initialized_);
+ DCHECK(!is_shutdown_);
+ g_exited_main_message_loop = true;
+ main_loop_->ShutdownThreadsAndCleanUp();
+
+ statistics_.reset(NULL);
+
+#if defined(OS_WIN)
+ com_initializer_.reset(NULL);
+#endif
+
+ main_loop_.reset(NULL);
+
+ notification_service_.reset(NULL);
+
+ is_shutdown_ = true;
+ }
+
+ protected:
+ // True if the runner has been initialized.
+ bool is_initialized_;
+
+ // True if the runner has been shut down.
+ bool is_shutdown_;
+
+ scoped_ptr<NotificationServiceImpl> notification_service_;
+ scoped_ptr<content::BrowserMainLoop> main_loop_;
+#if defined(OS_WIN)
+ scoped_ptr<base::win::ScopedCOMInitializer> com_initializer_;
+#endif
+ scoped_ptr<base::StatisticsRecorder> statistics_;
+
+ DISALLOW_COPY_AND_ASSIGN(BrowserMainRunnerImpl);
+};
+
+} // namespace
+
+namespace content {
+
+// static
+BrowserMainRunner* BrowserMainRunner::Create() {
+ return new BrowserMainRunnerImpl();
+}
+
+} // namespace content