Refactor management of overview window copy lifetime into a separate class.
[chromium-blink-merge.git] / content / shell / browser / shell_browser_main_parts.cc
blobf579d762c44834470f061f333673d35d990b8698
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/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"
29 #include "url/gurl.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"
35 #endif
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"
41 #endif
43 #if defined(USE_AURA) && defined(USE_X11)
44 #include "ui/events/x/touch_factory_x11.h"
45 #endif
47 #if defined(OS_MACOSX)
48 #include "components/breakpad/app/breakpad_mac.h"
49 #endif
51 #if defined(OS_POSIX) && !defined(OS_MACOSX)
52 #include "components/breakpad/app/breakpad_linux.h"
53 #endif
55 #if defined(OS_WIN)
56 #include "components/breakpad/app/breakpad_win.h"
57 #endif
59 namespace content {
61 namespace {
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))
69 return GURL();
70 const CommandLine::StringVector& args = command_line->GetArgs();
72 #if defined(OS_ANDROID)
73 // Delay renderer creation on Android until surface is ready.
74 return GURL();
75 #endif
77 if (args.empty())
78 return GURL("http://www.google.com/");
80 GURL url(args[0]);
81 if (url.is_valid() && url.has_scheme())
82 return url;
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(
91 IDR_DIR_HEADER_HTML);
92 return html_data;
94 return base::StringPiece();
97 } // namespace
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();
110 #endif
112 #endif
114 void ShellBrowserMainParts::PostMainMessageLoopStart() {
115 #if defined(OS_ANDROID)
116 base::MessageLoopForUI::current()->Start();
117 #endif
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);
127 #endif
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));
139 #endif
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()));
146 Shell::Initialize();
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(),
153 GetStartupURL(),
154 NULL,
155 MSG_ROUTING_NONE,
156 gfx::Size());
159 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) {
160 quota::QuotaManager* quota_manager =
161 BrowserContext::GetDefaultStoragePartition(browser_context())
162 ->GetQuotaManager();
163 BrowserThread::PostTask(
164 BrowserThread::IO,
165 FROM_HERE,
166 base::Bind(&quota::QuotaManager::SetTemporaryGlobalOverrideQuota,
167 quota_manager,
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());
175 #endif
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();
192 #endif
193 if (devtools_delegate_)
194 devtools_delegate_->Stop();
195 browser_context_.reset();
196 off_the_record_browser_context_.reset();
199 } // namespace