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"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/threading/thread.h"
13 #include "base/threading/thread_restrictions.h"
14 #include "components/devtools_http_handler/devtools_http_handler.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/storage_partition.h"
17 #include "content/public/common/content_switches.h"
18 #include "content/public/common/main_function_params.h"
19 #include "content/public/common/url_constants.h"
20 #include "content/shell/browser/layout_test/layout_test_browser_context.h"
21 #include "content/shell/browser/shell.h"
22 #include "content/shell/browser/shell_browser_context.h"
23 #include "content/shell/browser/shell_devtools_manager_delegate.h"
24 #include "content/shell/browser/shell_net_log.h"
25 #include "content/shell/common/shell_switches.h"
26 #include "net/base/filename_util.h"
27 #include "net/base/net_module.h"
28 #include "net/grit/net_resources.h"
29 #include "ui/base/resource/resource_bundle.h"
32 #if defined(OS_ANDROID)
33 #include "components/crash/browser/crash_dump_manager_android.h"
34 #include "net/android/network_change_notifier_factory_android.h"
35 #include "net/base/network_change_notifier.h"
38 #if defined(USE_AURA) && defined(USE_X11)
39 #include "ui/events/devices/x11/touch_factory_x11.h"
41 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(OS_LINUX)
42 #include "ui/base/ime/input_method_initializer.h"
49 GURL
GetStartupURL() {
50 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
51 if (command_line
->HasSwitch(switches::kContentBrowserTest
))
53 const base::CommandLine::StringVector
& args
= command_line
->GetArgs();
55 #if defined(OS_ANDROID)
56 // Delay renderer creation on Android until surface is ready.
61 return GURL("https://www.google.com/");
64 if (url
.is_valid() && url
.has_scheme())
67 return net::FilePathToFileURL(base::FilePath(args
[0]));
70 base::StringPiece
PlatformResourceProvider(int key
) {
71 if (key
== IDR_DIR_HEADER_HTML
) {
72 base::StringPiece html_data
=
73 ui::ResourceBundle::GetSharedInstance().GetRawDataResource(
77 return base::StringPiece();
82 ShellBrowserMainParts::ShellBrowserMainParts(
83 const MainFunctionParams
& parameters
)
84 : parameters_(parameters
),
85 run_message_loop_(true),
86 devtools_http_handler_(nullptr) {
89 ShellBrowserMainParts::~ShellBrowserMainParts() {
90 DCHECK(!devtools_http_handler_
);
93 #if !defined(OS_MACOSX)
94 void ShellBrowserMainParts::PreMainMessageLoopStart() {
95 #if defined(USE_AURA) && defined(USE_X11)
96 ui::TouchFactory::SetTouchDeviceListFromCommandLine();
101 void ShellBrowserMainParts::PostMainMessageLoopStart() {
102 #if defined(OS_ANDROID)
103 base::MessageLoopForUI::current()->Start();
107 void ShellBrowserMainParts::PreEarlyInitialization() {
108 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(OS_LINUX)
109 ui::InitializeInputMethodForTesting();
111 #if defined(OS_ANDROID)
112 net::NetworkChangeNotifier::SetFactory(
113 new net::NetworkChangeNotifierFactoryAndroid());
117 void ShellBrowserMainParts::InitializeBrowserContexts() {
118 set_browser_context(new ShellBrowserContext(false, net_log_
.get()));
119 set_off_the_record_browser_context(
120 new ShellBrowserContext(true, net_log_
.get()));
123 void ShellBrowserMainParts::InitializeMessageLoopContext() {
124 Shell::CreateNewWindow(browser_context_
.get(),
130 void ShellBrowserMainParts::PreMainMessageLoopRun() {
131 #if defined(OS_ANDROID)
132 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
133 switches::kEnableCrashReporter
)) {
134 base::FilePath crash_dumps_dir
=
135 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
136 switches::kCrashDumpsDir
);
137 crash_dump_manager_
.reset(new breakpad::CrashDumpManager(crash_dumps_dir
));
141 net_log_
.reset(new ShellNetLog("content_shell"));
142 InitializeBrowserContexts();
144 net::NetModule::SetResourceProvider(PlatformResourceProvider
);
146 devtools_http_handler_
.reset(
147 ShellDevToolsManagerDelegate::CreateHttpHandler(browser_context_
.get()));
149 InitializeMessageLoopContext();
151 if (parameters_
.ui_task
) {
152 parameters_
.ui_task
->Run();
153 delete parameters_
.ui_task
;
154 run_message_loop_
= false;
158 bool ShellBrowserMainParts::MainMessageLoopRun(int* result_code
) {
159 return !run_message_loop_
;
162 void ShellBrowserMainParts::PostMainMessageLoopRun() {
163 devtools_http_handler_
.reset();
164 browser_context_
.reset();
165 off_the_record_browser_context_
.reset();