Add ICU message format support
[chromium-blink-merge.git] / content / shell / browser / shell_browser_main_parts.cc
blob68f52e2b4d688284ceb36670d6942be27107631b
1 // Copyright 2013 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 "content/shell/browser/shell_browser_main_parts.h"
7 #include "base/base_switches.h"
8 #include "base/bind.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/threading/thread.h"
14 #include "base/threading/thread_restrictions.h"
15 #include "components/devtools_http_handler/devtools_http_handler.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/storage_partition.h"
18 #include "content/public/common/content_switches.h"
19 #include "content/public/common/main_function_params.h"
20 #include "content/public/common/url_constants.h"
21 #include "content/shell/browser/layout_test/layout_test_browser_context.h"
22 #include "content/shell/browser/shell.h"
23 #include "content/shell/browser/shell_browser_context.h"
24 #include "content/shell/browser/shell_devtools_manager_delegate.h"
25 #include "content/shell/browser/shell_net_log.h"
26 #include "content/shell/common/shell_switches.h"
27 #include "net/base/filename_util.h"
28 #include "net/base/net_module.h"
29 #include "net/grit/net_resources.h"
30 #include "ui/base/resource/resource_bundle.h"
31 #include "url/gurl.h"
33 #if defined(OS_ANDROID)
34 #include "components/crash/browser/crash_dump_manager_android.h"
35 #include "net/android/network_change_notifier_factory_android.h"
36 #include "net/base/network_change_notifier.h"
37 #endif
39 #if defined(USE_AURA) && defined(USE_X11)
40 #include "ui/events/devices/x11/touch_factory_x11.h"
41 #endif
42 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(OS_LINUX)
43 #include "ui/base/ime/input_method_initializer.h"
44 #endif
46 namespace content {
48 namespace {
50 GURL GetStartupURL() {
51 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
52 if (command_line->HasSwitch(switches::kContentBrowserTest))
53 return GURL();
54 const base::CommandLine::StringVector& args = command_line->GetArgs();
56 #if defined(OS_ANDROID)
57 // Delay renderer creation on Android until surface is ready.
58 return GURL();
59 #endif
61 if (args.empty())
62 return GURL("https://www.google.com/");
64 GURL url(args[0]);
65 if (url.is_valid() && url.has_scheme())
66 return url;
68 return net::FilePathToFileURL(
69 base::MakeAbsoluteFilePath(base::FilePath(args[0])));
72 base::StringPiece PlatformResourceProvider(int key) {
73 if (key == IDR_DIR_HEADER_HTML) {
74 base::StringPiece html_data =
75 ui::ResourceBundle::GetSharedInstance().GetRawDataResource(
76 IDR_DIR_HEADER_HTML);
77 return html_data;
79 return base::StringPiece();
82 } // namespace
84 ShellBrowserMainParts::ShellBrowserMainParts(
85 const MainFunctionParams& parameters)
86 : parameters_(parameters),
87 run_message_loop_(true),
88 devtools_http_handler_(nullptr) {
91 ShellBrowserMainParts::~ShellBrowserMainParts() {
92 DCHECK(!devtools_http_handler_);
95 #if !defined(OS_MACOSX)
96 void ShellBrowserMainParts::PreMainMessageLoopStart() {
97 #if defined(USE_AURA) && defined(USE_X11)
98 ui::TouchFactory::SetTouchDeviceListFromCommandLine();
99 #endif
101 #endif
103 void ShellBrowserMainParts::PostMainMessageLoopStart() {
104 #if defined(OS_ANDROID)
105 base::MessageLoopForUI::current()->Start();
106 #endif
109 void ShellBrowserMainParts::PreEarlyInitialization() {
110 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(OS_LINUX)
111 ui::InitializeInputMethodForTesting();
112 #endif
113 #if defined(OS_ANDROID)
114 net::NetworkChangeNotifier::SetFactory(
115 new net::NetworkChangeNotifierFactoryAndroid());
116 #endif
119 void ShellBrowserMainParts::InitializeBrowserContexts() {
120 set_browser_context(new ShellBrowserContext(false, net_log_.get()));
121 set_off_the_record_browser_context(
122 new ShellBrowserContext(true, net_log_.get()));
125 void ShellBrowserMainParts::InitializeMessageLoopContext() {
126 Shell::CreateNewWindow(browser_context_.get(),
127 GetStartupURL(),
128 NULL,
129 gfx::Size());
132 void ShellBrowserMainParts::PreMainMessageLoopRun() {
133 #if defined(OS_ANDROID)
134 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
135 switches::kEnableCrashReporter)) {
136 base::FilePath crash_dumps_dir =
137 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
138 switches::kCrashDumpsDir);
139 crash_dump_manager_.reset(new breakpad::CrashDumpManager(crash_dumps_dir));
141 #endif
143 net_log_.reset(new ShellNetLog("content_shell"));
144 InitializeBrowserContexts();
145 Shell::Initialize();
146 net::NetModule::SetResourceProvider(PlatformResourceProvider);
148 devtools_http_handler_.reset(
149 ShellDevToolsManagerDelegate::CreateHttpHandler(browser_context_.get()));
151 InitializeMessageLoopContext();
153 if (parameters_.ui_task) {
154 parameters_.ui_task->Run();
155 delete parameters_.ui_task;
156 run_message_loop_ = false;
160 bool ShellBrowserMainParts::MainMessageLoopRun(int* result_code) {
161 return !run_message_loop_;
164 void ShellBrowserMainParts::PostMainMessageLoopRun() {
165 devtools_http_handler_.reset();
166 browser_context_.reset();
167 off_the_record_browser_context_.reset();
170 } // namespace