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 "content/public/browser/browser_thread.h"
15 #include "content/public/browser/storage_partition.h"
16 #include "content/public/common/content_switches.h"
17 #include "content/public/common/main_function_params.h"
18 #include "content/public/common/url_constants.h"
19 #include "content/shell/browser/shell.h"
20 #include "content/shell/browser/shell_browser_context.h"
21 #include "content/shell/browser/shell_devtools_delegate.h"
22 #include "content/shell/browser/shell_net_log.h"
23 #include "content/shell/common/shell_switches.h"
24 #include "net/base/filename_util.h"
25 #include "net/base/net_module.h"
26 #include "net/grit/net_resources.h"
27 #include "ui/base/resource/resource_bundle.h"
29 #include "webkit/browser/quota/quota_manager.h"
31 #if defined(ENABLE_PLUGINS)
32 #include "content/public/browser/plugin_service.h"
33 #include "content/shell/browser/shell_plugin_service_filter.h"
36 #if defined(OS_ANDROID)
37 #include "components/breakpad/browser/crash_dump_manager_android.h"
38 #include "net/android/network_change_notifier_factory_android.h"
39 #include "net/base/network_change_notifier.h"
42 #if defined(USE_AURA) && defined(USE_X11)
43 #include "ui/events/x/touch_factory_x11.h"
45 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(OS_LINUX)
46 #include "ui/base/ime/input_method_initializer.h"
53 // Default quota for each origin is 5MB.
54 const int kDefaultLayoutTestQuotaBytes
= 5 * 1024 * 1024;
56 GURL
GetStartupURL() {
57 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
58 if (command_line
->HasSwitch(switches::kContentBrowserTest
))
60 const CommandLine::StringVector
& args
= command_line
->GetArgs();
62 #if defined(OS_ANDROID)
63 // Delay renderer creation on Android until surface is ready.
68 return GURL("http://www.google.com/");
71 if (url
.is_valid() && url
.has_scheme())
74 return net::FilePathToFileURL(base::FilePath(args
[0]));
77 base::StringPiece
PlatformResourceProvider(int key
) {
78 if (key
== IDR_DIR_HEADER_HTML
) {
79 base::StringPiece html_data
=
80 ui::ResourceBundle::GetSharedInstance().GetRawDataResource(
84 return base::StringPiece();
89 ShellBrowserMainParts::ShellBrowserMainParts(
90 const MainFunctionParams
& parameters
)
91 : BrowserMainParts(), parameters_(parameters
), run_message_loop_(true) {}
93 ShellBrowserMainParts::~ShellBrowserMainParts() {
96 #if !defined(OS_MACOSX)
97 void ShellBrowserMainParts::PreMainMessageLoopStart() {
98 #if defined(USE_AURA) && defined(USE_X11)
99 ui::TouchFactory::SetTouchDeviceListFromCommandLine();
104 void ShellBrowserMainParts::PostMainMessageLoopStart() {
105 #if defined(OS_ANDROID)
106 base::MessageLoopForUI::current()->Start();
110 void ShellBrowserMainParts::PreEarlyInitialization() {
111 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(OS_LINUX)
112 ui::InitializeInputMethodForTesting();
114 #if defined(OS_ANDROID)
115 net::NetworkChangeNotifier::SetFactory(
116 new net::NetworkChangeNotifierFactoryAndroid());
120 void ShellBrowserMainParts::PreMainMessageLoopRun() {
121 #if defined(OS_ANDROID)
122 if (CommandLine::ForCurrentProcess()->HasSwitch(
123 switches::kEnableCrashReporter
)) {
124 base::FilePath crash_dumps_dir
=
125 CommandLine::ForCurrentProcess()->GetSwitchValuePath(
126 switches::kCrashDumpsDir
);
127 crash_dump_manager_
.reset(new breakpad::CrashDumpManager(crash_dumps_dir
));
130 net_log_
.reset(new ShellNetLog("content_shell"));
131 browser_context_
.reset(new ShellBrowserContext(false, net_log_
.get()));
132 off_the_record_browser_context_
.reset(
133 new ShellBrowserContext(true, net_log_
.get()));
136 net::NetModule::SetResourceProvider(PlatformResourceProvider
);
138 devtools_delegate_
.reset(new ShellDevToolsDelegate(browser_context_
.get()));
140 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
)) {
141 Shell::CreateNewWindow(browser_context_
.get(),
148 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
)) {
149 storage::QuotaManager
* quota_manager
=
150 BrowserContext::GetDefaultStoragePartition(browser_context())
152 BrowserThread::PostTask(
155 base::Bind(&storage::QuotaManager::SetTemporaryGlobalOverrideQuota
,
157 kDefaultLayoutTestQuotaBytes
*
158 storage::QuotaManager::kPerHostTemporaryPortion
,
159 storage::QuotaCallback()));
160 #if defined(ENABLE_PLUGINS)
161 PluginService
* plugin_service
= PluginService::GetInstance();
162 plugin_service_filter_
.reset(new ShellPluginServiceFilter
);
163 plugin_service
->SetFilter(plugin_service_filter_
.get());
167 if (parameters_
.ui_task
) {
168 parameters_
.ui_task
->Run();
169 delete parameters_
.ui_task
;
170 run_message_loop_
= false;
174 bool ShellBrowserMainParts::MainMessageLoopRun(int* result_code
) {
175 return !run_message_loop_
;
178 void ShellBrowserMainParts::PostMainMessageLoopRun() {
179 if (devtools_delegate_
)
180 devtools_delegate_
->Stop();
181 browser_context_
.reset();
182 off_the_record_browser_context_
.reset();