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"
45 #if !defined(OS_CHROMEOS)
46 #include "ui/base/ime/input_method_initializer.h"
54 // Default quota for each origin is 5MB.
55 const int kDefaultLayoutTestQuotaBytes
= 5 * 1024 * 1024;
57 GURL
GetStartupURL() {
58 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
59 if (command_line
->HasSwitch(switches::kContentBrowserTest
))
61 const CommandLine::StringVector
& args
= command_line
->GetArgs();
63 #if defined(OS_ANDROID)
64 // Delay renderer creation on Android until surface is ready.
69 return GURL("http://www.google.com/");
72 if (url
.is_valid() && url
.has_scheme())
75 return net::FilePathToFileURL(base::FilePath(args
[0]));
78 base::StringPiece
PlatformResourceProvider(int key
) {
79 if (key
== IDR_DIR_HEADER_HTML
) {
80 base::StringPiece html_data
=
81 ui::ResourceBundle::GetSharedInstance().GetRawDataResource(
85 return base::StringPiece();
90 ShellBrowserMainParts::ShellBrowserMainParts(
91 const MainFunctionParams
& parameters
)
92 : BrowserMainParts(), parameters_(parameters
), run_message_loop_(true) {}
94 ShellBrowserMainParts::~ShellBrowserMainParts() {
97 #if !defined(OS_MACOSX)
98 void ShellBrowserMainParts::PreMainMessageLoopStart() {
99 #if defined(USE_AURA) && defined(USE_X11)
100 ui::TouchFactory::SetTouchDeviceListFromCommandLine();
105 void ShellBrowserMainParts::PostMainMessageLoopStart() {
106 #if defined(OS_ANDROID)
107 base::MessageLoopForUI::current()->Start();
111 void ShellBrowserMainParts::PreEarlyInitialization() {
112 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(USE_X11)
113 ui::InitializeInputMethodForTesting();
115 #if defined(OS_ANDROID)
116 net::NetworkChangeNotifier::SetFactory(
117 new net::NetworkChangeNotifierFactoryAndroid());
119 CommandLine::ForCurrentProcess()->AppendSwitch(
120 cc::switches::kCompositeToMailbox
);
124 void ShellBrowserMainParts::PreMainMessageLoopRun() {
125 #if defined(OS_ANDROID)
126 if (CommandLine::ForCurrentProcess()->HasSwitch(
127 switches::kEnableCrashReporter
)) {
128 base::FilePath crash_dumps_dir
=
129 CommandLine::ForCurrentProcess()->GetSwitchValuePath(
130 switches::kCrashDumpsDir
);
131 crash_dump_manager_
.reset(new breakpad::CrashDumpManager(crash_dumps_dir
));
134 net_log_
.reset(new ShellNetLog());
135 browser_context_
.reset(new ShellBrowserContext(false, net_log_
.get()));
136 off_the_record_browser_context_
.reset(
137 new ShellBrowserContext(true, net_log_
.get()));
140 net::NetModule::SetResourceProvider(PlatformResourceProvider
);
142 devtools_delegate_
.reset(new ShellDevToolsDelegate(browser_context_
.get()));
144 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
)) {
145 Shell::CreateNewWindow(browser_context_
.get(),
152 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
)) {
153 quota::QuotaManager
* quota_manager
=
154 BrowserContext::GetDefaultStoragePartition(browser_context())
156 BrowserThread::PostTask(
159 base::Bind("a::QuotaManager::SetTemporaryGlobalOverrideQuota
,
161 kDefaultLayoutTestQuotaBytes
*
162 quota::QuotaManager::kPerHostTemporaryPortion
,
163 quota::QuotaCallback()));
164 #if defined(ENABLE_PLUGINS)
165 PluginService
* plugin_service
= PluginService::GetInstance();
166 plugin_service_filter_
.reset(new ShellPluginServiceFilter
);
167 plugin_service
->SetFilter(plugin_service_filter_
.get());
171 if (parameters_
.ui_task
) {
172 parameters_
.ui_task
->Run();
173 delete parameters_
.ui_task
;
174 run_message_loop_
= false;
178 bool ShellBrowserMainParts::MainMessageLoopRun(int* result_code
) {
179 return !run_message_loop_
;
182 void ShellBrowserMainParts::PostMainMessageLoopRun() {
183 if (devtools_delegate_
)
184 devtools_delegate_
->Stop();
185 browser_context_
.reset();
186 off_the_record_browser_context_
.reset();