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

Unified Diff: content/browser/browser_main_runner.cc

Issue 9190018: Support sharing of ContentMain and BrowserMain code with embedded use cases. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 8 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/browser_main_runner.cc
===================================================================
--- content/browser/browser_main_runner.cc (revision 0)
+++ content/browser/browser_main_runner.cc (revision 0)
@@ -0,0 +1,171 @@
+// Copyright (c) 2011 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/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
+
+namespace {
+
+bool g_exited_main_message_loop = false;
+
+class BrowserMainRunnerImpl : public content::BrowserMainRunner {
+ public:
+ BrowserMainRunnerImpl();
+ ~BrowserMainRunnerImpl();
+
+ virtual int Initialize(const content::MainFunctionParams& parameters)
+ OVERRIDE;
+ virtual int Run() OVERRIDE;
+ virtual void Shutdown() OVERRIDE;
+
+ 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);
+};
+
+BrowserMainRunnerImpl::BrowserMainRunnerImpl()
+ : is_initialized_(false),
+ is_shutdown_(false) {
+}
+
+BrowserMainRunnerImpl::~BrowserMainRunnerImpl() {
+ if (is_initialized_ && !is_shutdown_)
+ Shutdown();
+}
+
+int BrowserMainRunnerImpl::Initialize(
+ const content::MainFunctionParams& parameters) {
+ 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.
+
+ // !!!!!!!!!! READ ME !!!!!!!!!!
+ // I (viettrungluu) am in the process of refactoring |BrowserMain()|. If you
+ // need to add something above this comment, read the documentation in
+ // browser_main.h. If you need to add something below, please do the
+ // following:
+ // - Figure out where you should add your code. Do NOT just pick a random
+ // location "which works".
+ // - Document the dependencies apart from compile-time-checkable ones. What
+ // must happen before your new code is executed? Does your new code need to
+ // run before something else? Are there performance reasons for executing
+ // your code at that point?
+ // - If you need to create a (persistent) object, heap allocate it and keep a
+ // |scoped_ptr| to it rather than allocating it on the stack. Otherwise
+ // I'll have to convert your code when I refactor.
+ // - Unless your new code is just a couple of lines, factor it out into a
+ // function with a well-defined purpose. Do NOT just add it inline in
+ // |BrowserMain()|.
+ // Thanks!
+
+ // TODO(viettrungluu): put the remainder into BrowserMainParts
+
+#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;
+}
+
+int BrowserMainRunnerImpl::Run() {
+ DCHECK(is_initialized_);
+ DCHECK(!is_shutdown_);
+ main_loop_->RunMainMessageLoopParts();
+ return main_loop_->GetResultCode();
+}
+
+void BrowserMainRunnerImpl::Shutdown() {
+ 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;
+}
+
+} // namespace
+
+namespace content {
+
+// static
+BrowserMainRunner* BrowserMainRunner::CreateMainRunner() {
+ return new BrowserMainRunnerImpl();
+}
+
+bool ExitedMainMessageLoop() {
+ return g_exited_main_message_loop;
+}
+
+} // namespace content
Property changes on: content\browser\browser_main_runner.cc
___________________________________________________________________
Added: svn:eol-style
+ LF

Powered by Google App Engine
This is Rietveld 408576698