[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / content / shell / shell_content_renderer_client.cc
blob8fff64a476079d0148fd43e962e8d51084970523
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_content_renderer_client.h"
7 #include "base/callback.h"
8 #include "base/command_line.h"
9 #include "content/public/common/content_constants.h"
10 #include "content/public/common/content_switches.h"
11 #include "content/public/common/url_constants.h"
12 #include "content/public/test/layouttest_support.h"
13 #include "content/shell/shell_render_process_observer.h"
14 #include "content/shell/shell_switches.h"
15 #include "content/shell/webkit_test_runner.h"
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginParams.h"
18 #include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestPlugin.h"
19 #include "third_party/WebKit/Tools/DumpRenderTree/chromium/TestRunner/public/WebTestProxy.h"
20 #include "v8/include/v8.h"
22 using WebKit::WebFrame;
23 using WebTestRunner::WebTestPlugin;
24 using WebTestRunner::WebTestProxyBase;
26 namespace content {
28 namespace {
30 bool IsLocalhost(const std::string& host) {
31 return host == "127.0.0.1" || host == "localhost";
34 bool HostIsUsedBySomeTestsToGenerateError(const std::string& host) {
35 return host == "255.255.255.255";
38 bool IsExternalPage(const GURL& url) {
39 return !url.host().empty() &&
40 (url.SchemeIs(chrome::kHttpScheme) ||
41 url.SchemeIs(chrome::kHttpsScheme)) &&
42 !IsLocalhost(url.host()) &&
43 !HostIsUsedBySomeTestsToGenerateError(url.host());
46 } // namespace
48 ShellContentRendererClient::ShellContentRendererClient() {
49 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) {
50 EnableWebTestProxyCreation(
51 base::Bind(&ShellContentRendererClient::WebTestProxyCreated,
52 base::Unretained(this)));
56 ShellContentRendererClient::~ShellContentRendererClient() {
59 void ShellContentRendererClient::RenderThreadStarted() {
60 shell_observer_.reset(new ShellRenderProcessObserver());
63 bool ShellContentRendererClient::OverrideCreatePlugin(
64 RenderView* render_view,
65 WebKit::WebFrame* frame,
66 const WebKit::WebPluginParams& params,
67 WebKit::WebPlugin** plugin) {
68 std::string mime_type = params.mimeType.utf8();
69 if (mime_type == content::kBrowserPluginMimeType) {
70 // Allow browser plugin in content_shell only if it is forced by flag.
71 // Returning true here disables the plugin.
72 return !CommandLine::ForCurrentProcess()->HasSwitch(
73 switches::kEnableBrowserPluginForAllViewTypes);
75 if (params.mimeType == WebTestPlugin::mimeType()) {
76 *plugin = WebTestPlugin::create(
77 frame,
78 params,
79 ShellRenderProcessObserver::GetInstance()->test_delegate());
80 return true;
82 return false;
85 bool ShellContentRendererClient::WillSendRequest(
86 WebFrame* frame,
87 PageTransition transition_type,
88 const GURL& url,
89 const GURL& first_party_for_cookies,
90 GURL* new_url) {
91 CommandLine* command_line = CommandLine::ForCurrentProcess();
92 if (!command_line->HasSwitch(switches::kDumpRenderTree))
93 return false;
94 ShellRenderProcessObserver* render_process_observer =
95 ShellRenderProcessObserver::GetInstance();
96 if (!command_line->HasSwitch(switches::kAllowExternalPages) &&
97 IsExternalPage(url) && !IsExternalPage(first_party_for_cookies)) {
98 render_process_observer->test_delegate()->printMessage(
99 std::string("Blocked access to external URL " + url.spec() + "\n"));
100 *new_url = GURL();
101 return true;
103 *new_url = render_process_observer->test_delegate()->rewriteLayoutTestsURL(
104 url.spec());
105 return true;
108 void ShellContentRendererClient::WebTestProxyCreated(RenderView* render_view,
109 WebTestProxyBase* proxy) {
110 WebKitTestRunner* test_runner = new WebKitTestRunner(render_view);
111 if (!ShellRenderProcessObserver::GetInstance()->test_delegate()) {
112 ShellRenderProcessObserver::GetInstance()->SetMainWindow(render_view,
113 test_runner,
114 test_runner);
116 test_runner->set_proxy(proxy);
117 proxy->setDelegate(
118 ShellRenderProcessObserver::GetInstance()->test_delegate());
119 proxy->setInterfaces(
120 ShellRenderProcessObserver::GetInstance()->test_interfaces());
123 } // namespace content