1 // Copyright 2014 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.
5 #include "ios/web/app/web_main_runner.h"
7 #include "base/at_exit.h"
8 #include "base/command_line.h"
9 #include "base/i18n/icu_util.h"
10 #include "base/logging.h"
11 #include "base/metrics/statistics_recorder.h"
12 #include "ios/web/app/web_main_loop.h"
13 #include "ios/web/public/web_client.h"
14 #include "ui/base/ui_base_paths.h"
18 class WebMainRunnerImpl : public WebMainRunner {
21 : is_initialized_(false),
23 completed_basic_startup_(false),
26 ~WebMainRunnerImpl() override {
27 if (is_initialized_ && !is_shutdown_) {
32 int Initialize(const WebMainParams& params) override {
33 ////////////////////////////////////////////////////////////////////////
34 // ContentMainRunnerImpl::Initialize()
36 is_initialized_ = true;
37 delegate_ = params.delegate;
39 // TODO(rohitrao): Chrome for iOS initializes this in main(), because it's
40 // needed for breakpad. Are we really going to require that all embedders
41 // initialize an AtExitManager in main()?
42 exit_manager_.reset(new base::AtExitManager);
44 // There is no way to pass commandline flags to process on iOS, so the
45 // CommandLine is always initialized empty. Embedders can add switches in
46 // |BasicStartupComplete|.
47 base::CommandLine::Init(0, nullptr);
49 delegate_->BasicStartupComplete();
51 completed_basic_startup_ = true;
53 // TODO(rohitrao): Should we instead require that all embedders call
56 SetWebClient(&empty_web_client_);
59 crypto::EarlySetupForNSSInit();
62 // TODO(rohitrao): Desktop calls content::RegisterContentSchemes(true) here.
63 // Do we need similar scheme registration on iOS?
64 ui::RegisterPathProvider();
66 CHECK(base::i18n::InitializeICU());
68 ////////////////////////////////////////////////////////////
69 // BrowserMainRunnerImpl::Initialize()
70 base::StatisticsRecorder::Initialize();
72 main_loop_.reset(new WebMainLoop());
74 main_loop_->EarlyInitialization();
75 main_loop_->MainMessageLoopStart();
76 main_loop_->CreateStartupTasks();
77 int result_code = main_loop_->GetResultCode();
81 // Return -1 to indicate no early termination.
85 void ShutDown() override {
86 ////////////////////////////////////////////////////////////////////
87 // BrowserMainRunner::Shutdown()
89 DCHECK(is_initialized_);
90 DCHECK(!is_shutdown_);
91 main_loop_->ShutdownThreadsAndCleanUp();
92 main_loop_.reset(nullptr);
94 ////////////////////////////////////////////////////////////////////
95 // ContentMainRunner::Shutdown()
97 if (completed_basic_startup_ && delegate_) {
98 delegate_->ProcessExiting();
101 exit_manager_.reset(nullptr);
107 // True if we have started to initialize the runner.
108 bool is_initialized_;
110 // True if the runner has been shut down.
113 // True if basic startup was completed.
114 bool completed_basic_startup_;
116 // The delegate will outlive this object.
117 WebMainDelegate* delegate_;
119 // Used if the embedder doesn't set one.
120 WebClient empty_web_client_;
122 scoped_ptr<base::AtExitManager> exit_manager_;
123 scoped_ptr<WebMainLoop> main_loop_;
125 DISALLOW_COPY_AND_ASSIGN(WebMainRunnerImpl);
129 WebMainRunner* WebMainRunner::Create() {
130 return new WebMainRunnerImpl();