[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / content / shell / shell_browser_main_parts.cc
blobdc29b3f828610414a423fd1bfda003b30ba479e8
1 // Copyright (c) 2012 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/shell_browser_main_parts.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/file_path.h"
10 #include "base/message_loop.h"
11 #include "base/string_number_conversions.h"
12 #include "base/threading/thread.h"
13 #include "base/threading/thread_restrictions.h"
14 #include "content/public/common/content_switches.h"
15 #include "content/public/common/main_function_params.h"
16 #include "content/public/common/url_constants.h"
17 #include "content/shell/shell.h"
18 #include "content/shell/shell_browser_context.h"
19 #include "content/shell/shell_devtools_delegate.h"
20 #include "content/shell/shell_switches.h"
21 #include "googleurl/src/gurl.h"
22 #include "grit/net_resources.h"
23 #include "net/base/net_module.h"
24 #include "net/base/net_util.h"
25 #include "ui/base/resource/resource_bundle.h"
27 #if defined(OS_ANDROID)
28 #include "net/base/network_change_notifier.h"
29 #include "net/android/network_change_notifier_factory_android.h"
30 #endif
32 #if defined(USE_AURA) && defined(USE_X11)
33 #include "ui/base/touch/touch_factory.h"
34 #endif
36 namespace content {
38 namespace {
40 static GURL GetStartupURL() {
41 CommandLine* command_line = CommandLine::ForCurrentProcess();
42 if (command_line->HasSwitch(switches::kContentBrowserTest))
43 return GURL();
44 const CommandLine::StringVector& args = command_line->GetArgs();
46 #if defined(OS_ANDROID)
47 // Delay renderer creation on Android until surface is ready.
48 return GURL();
49 #endif
51 if (args.empty())
52 return GURL("http://www.google.com/");
54 GURL url(args[0]);
55 if (url.is_valid() && url.has_scheme())
56 return url;
58 return net::FilePathToFileURL(FilePath(args[0]));
61 base::StringPiece PlatformResourceProvider(int key) {
62 if (key == IDR_DIR_HEADER_HTML) {
63 base::StringPiece html_data =
64 ui::ResourceBundle::GetSharedInstance().GetRawDataResource(
65 IDR_DIR_HEADER_HTML);
66 return html_data;
68 return base::StringPiece();
71 } // namespace
73 ShellBrowserMainParts::ShellBrowserMainParts(
74 const MainFunctionParams& parameters)
75 : BrowserMainParts(),
76 parameters_(parameters),
77 run_message_loop_(true),
78 devtools_delegate_(NULL) {
81 ShellBrowserMainParts::~ShellBrowserMainParts() {
84 #if !defined(OS_MACOSX)
85 void ShellBrowserMainParts::PreMainMessageLoopStart() {
86 #if defined(USE_AURA) && defined(USE_X11)
87 ui::TouchFactory::SetTouchDeviceListFromCommandLine();
88 #endif
90 #endif
92 void ShellBrowserMainParts::PostMainMessageLoopStart() {
93 #if defined(OS_ANDROID)
94 MessageLoopForUI::current()->Start();
95 #endif
98 void ShellBrowserMainParts::PreEarlyInitialization() {
99 #if defined(OS_ANDROID)
100 net::NetworkChangeNotifier::SetFactory(
101 new net::NetworkChangeNotifierFactoryAndroid());
102 #endif
105 void ShellBrowserMainParts::PreMainMessageLoopRun() {
106 browser_context_.reset(new ShellBrowserContext(false));
107 off_the_record_browser_context_.reset(new ShellBrowserContext(true));
109 Shell::PlatformInitialize();
110 net::NetModule::SetResourceProvider(PlatformResourceProvider);
112 int port = 0;
113 // On android the port number isn't used.
114 #if !defined(OS_ANDROID)
115 // See if the user specified a port on the command line (useful for
116 // automation). If not, use an ephemeral port by specifying 0.
117 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
118 if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) {
119 int temp_port;
120 std::string port_str =
121 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort);
122 if (base::StringToInt(port_str, &temp_port) &&
123 temp_port > 0 && temp_port < 65535) {
124 port = temp_port;
125 } else {
126 DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
129 #endif
130 devtools_delegate_ = new ShellDevToolsDelegate(browser_context_.get(), port);
132 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) {
133 Shell::CreateNewWindow(browser_context_.get(),
134 GetStartupURL(),
135 NULL,
136 MSG_ROUTING_NONE,
137 NULL);
140 if (parameters_.ui_task) {
141 parameters_.ui_task->Run();
142 delete parameters_.ui_task;
143 run_message_loop_ = false;
147 bool ShellBrowserMainParts::MainMessageLoopRun(int* result_code) {
148 return !run_message_loop_;
151 void ShellBrowserMainParts::PostMainMessageLoopRun() {
152 #if defined(USE_AURA)
153 Shell::PlatformExit();
154 #endif
155 if (devtools_delegate_)
156 devtools_delegate_->Stop();
157 browser_context_.reset();
158 off_the_record_browser_context_.reset();
161 } // namespace