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 "cc/base/switches.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/shell.h"
21 #include "content/shell/browser/shell_browser_context.h"
22 #include "content/shell/browser/shell_devtools_delegate.h"
23 #include "content/shell/browser/shell_net_log.h"
24 #include "content/shell/common/shell_switches.h"
25 #include "grit/net_resources.h"
26 #include "net/base/net_module.h"
27 #include "net/base/net_util.h"
28 #include "ui/base/resource/resource_bundle.h"
30 #include "webkit/browser/quota/quota_manager.h"
32 #if defined(ENABLE_PLUGINS)
33 #include "content/public/browser/plugin_service.h"
34 #include "content/shell/browser/shell_plugin_service_filter.h"
37 #if defined(OS_ANDROID)
38 #include "components/breakpad/browser/crash_dump_manager_android.h"
39 #include "net/android/network_change_notifier_factory_android.h"
40 #include "net/base/network_change_notifier.h"
43 #if defined(USE_AURA) && defined(USE_X11)
44 #include "ui/events/x/touch_factory_x11.h"
47 #if defined(OS_MACOSX)
48 #include "components/breakpad/app/breakpad_mac.h"
51 #if defined(OS_POSIX) && !defined(OS_MACOSX)
52 #include "components/breakpad/app/breakpad_linux.h"
56 #include "components/breakpad/app/breakpad_win.h"
63 // Default quota for each origin is 5MB.
64 const int kDefaultLayoutTestQuotaBytes
= 5 * 1024 * 1024;
66 GURL
GetStartupURL() {
67 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
68 if (command_line
->HasSwitch(switches::kContentBrowserTest
))
70 const CommandLine::StringVector
& args
= command_line
->GetArgs();
72 #if defined(OS_ANDROID)
73 // Delay renderer creation on Android until surface is ready.
78 return GURL("http://www.google.com/");
81 if (url
.is_valid() && url
.has_scheme())
84 return net::FilePathToFileURL(base::FilePath(args
[0]));
87 base::StringPiece
PlatformResourceProvider(int key
) {
88 if (key
== IDR_DIR_HEADER_HTML
) {
89 base::StringPiece html_data
=
90 ui::ResourceBundle::GetSharedInstance().GetRawDataResource(
94 return base::StringPiece();
99 ShellBrowserMainParts::ShellBrowserMainParts(
100 const MainFunctionParams
& parameters
)
101 : BrowserMainParts(), parameters_(parameters
), run_message_loop_(true) {}
103 ShellBrowserMainParts::~ShellBrowserMainParts() {
106 #if !defined(OS_MACOSX)
107 void ShellBrowserMainParts::PreMainMessageLoopStart() {
108 #if defined(USE_AURA) && defined(USE_X11)
109 ui::TouchFactory::SetTouchDeviceListFromCommandLine();
114 void ShellBrowserMainParts::PostMainMessageLoopStart() {
115 #if defined(OS_ANDROID)
116 base::MessageLoopForUI::current()->Start();
120 void ShellBrowserMainParts::PreEarlyInitialization() {
121 #if defined(OS_ANDROID)
122 net::NetworkChangeNotifier::SetFactory(
123 new net::NetworkChangeNotifierFactoryAndroid());
125 CommandLine::ForCurrentProcess()->AppendSwitch(
126 cc::switches::kCompositeToMailbox
);
130 void ShellBrowserMainParts::PreMainMessageLoopRun() {
131 if (CommandLine::ForCurrentProcess()->HasSwitch(
132 switches::kEnableCrashReporter
)) {
133 breakpad::InitCrashReporter();
134 #if defined(OS_ANDROID)
135 base::FilePath crash_dumps_dir
=
136 CommandLine::ForCurrentProcess()->GetSwitchValuePath(
137 switches::kCrashDumpsDir
);
138 crash_dump_manager_
.reset(new breakpad::CrashDumpManager(crash_dumps_dir
));
141 net_log_
.reset(new ShellNetLog());
142 browser_context_
.reset(new ShellBrowserContext(false, net_log_
.get()));
143 off_the_record_browser_context_
.reset(
144 new ShellBrowserContext(true, net_log_
.get()));
147 net::NetModule::SetResourceProvider(PlatformResourceProvider
);
149 devtools_delegate_
.reset(new ShellDevToolsDelegate(browser_context_
.get()));
151 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
)) {
152 Shell::CreateNewWindow(browser_context_
.get(),
159 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
)) {
160 quota::QuotaManager
* quota_manager
=
161 BrowserContext::GetDefaultStoragePartition(browser_context())
163 BrowserThread::PostTask(
166 base::Bind("a::QuotaManager::SetTemporaryGlobalOverrideQuota
,
168 kDefaultLayoutTestQuotaBytes
*
169 quota::QuotaManager::kPerHostTemporaryPortion
,
170 quota::QuotaCallback()));
171 #if defined(ENABLE_PLUGINS)
172 PluginService
* plugin_service
= PluginService::GetInstance();
173 plugin_service_filter_
.reset(new ShellPluginServiceFilter
);
174 plugin_service
->SetFilter(plugin_service_filter_
.get());
178 if (parameters_
.ui_task
) {
179 parameters_
.ui_task
->Run();
180 delete parameters_
.ui_task
;
181 run_message_loop_
= false;
185 bool ShellBrowserMainParts::MainMessageLoopRun(int* result_code
) {
186 return !run_message_loop_
;
189 void ShellBrowserMainParts::PostMainMessageLoopRun() {
190 #if defined(USE_AURA)
191 Shell::PlatformExit();
193 if (devtools_delegate_
)
194 devtools_delegate_
->Stop();
195 browser_context_
.reset();
196 off_the_record_browser_context_
.reset();