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. 4 5 #include "content/shell/renderer/shell_content_renderer_client.h" 6 7 #include "base/callback.h" 8 #include "base/command_line.h" 9 #include "base/debug/debugger.h" 10 #include "content/common/sandbox_win.h" 11 #include "content/public/common/content_constants.h" 12 #include "content/public/common/content_switches.h" 13 #include "content/public/renderer/render_view.h" 14 #include "content/public/test/layouttest_support.h" 15 #include "content/shell/common/shell_switches.h" 16 #include "content/shell/common/webkit_test_helpers.h" 17 #include "content/shell/renderer/shell_render_frame_observer.h" 18 #include "content/shell/renderer/shell_render_process_observer.h" 19 #include "content/shell/renderer/shell_render_view_observer.h" 20 #include "content/shell/renderer/test_runner/WebTestInterfaces.h" 21 #include "content/shell/renderer/test_runner/web_test_proxy.h" 22 #include "content/shell/renderer/webkit_test_runner.h" 23 #include "content/test/mock_webclipboard_impl.h" 24 #include "third_party/WebKit/public/platform/WebMediaStreamCenter.h" 25 #include "third_party/WebKit/public/web/WebPluginParams.h" 26 #include "third_party/WebKit/public/web/WebView.h" 27 #include "v8/include/v8.h" 28 29 #if defined(OS_WIN) 30 #include "content/public/renderer/render_font_warmup_win.h" 31 #include "third_party/WebKit/public/web/win/WebFontRendering.h" 32 #include "third_party/skia/include/ports/SkFontMgr.h" 33 #endif 34 35 using blink::WebAudioDevice; 36 using blink::WebClipboard; 37 using blink::WebLocalFrame; 38 using blink::WebMIDIAccessor; 39 using blink::WebMIDIAccessorClient; 40 using blink::WebMediaStreamCenter; 41 using blink::WebMediaStreamCenterClient; 42 using blink::WebPlugin; 43 using blink::WebPluginParams; 44 using blink::WebRTCPeerConnectionHandler; 45 using blink::WebRTCPeerConnectionHandlerClient; 46 using blink::WebThemeEngine; 47 48 namespace content { 49 50 #if defined(OS_WIN) 51 namespace { 52 53 // DirectWrite only has access to %WINDIR%\Fonts by default. For developer 54 // side-loading, support kRegisterFontFiles to allow access to additional fonts. 55 void RegisterSideloadedTypefaces(SkFontMgr* fontmgr) { 56 std::vector<std::string> files = GetSideloadFontFiles(); 57 for (std::vector<std::string>::const_iterator i(files.begin()); 58 i != files.end(); 59 ++i) { 60 SkTypeface* typeface = fontmgr->createFromFile(i->c_str()); 61 DoPreSandboxWarmupForTypeface(typeface); 62 blink::WebFontRendering::addSideloadedFontForTesting(typeface); 63 } 64 } 65 66 } // namespace 67 #endif // OS_WIN 68 69 ShellContentRendererClient::ShellContentRendererClient() { 70 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) { 71 EnableWebTestProxyCreation( 72 base::Bind(&ShellContentRendererClient::WebTestProxyCreated, 73 base::Unretained(this))); 74 } 75 76 #if defined(OS_WIN) 77 if (ShouldUseDirectWrite()) 78 RegisterSideloadedTypefaces(GetPreSandboxWarmupFontMgr()); 79 #endif 80 } 81 82 ShellContentRendererClient::~ShellContentRendererClient() { 83 } 84 85 void ShellContentRendererClient::RenderThreadStarted() { 86 shell_observer_.reset(new ShellRenderProcessObserver()); 87 #if defined(OS_MACOSX) 88 // We need to call this once before the sandbox was initialized to cache the 89 // value. 90 base::debug::BeingDebugged(); 91 #endif 92 } 93 94 void ShellContentRendererClient::RenderFrameCreated(RenderFrame* render_frame) { 95 new ShellRenderFrameObserver(render_frame); 96 } 97 98 void ShellContentRendererClient::RenderViewCreated(RenderView* render_view) { 99 new ShellRenderViewObserver(render_view); 100 101 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) 102 return; 103 WebKitTestRunner* test_runner = WebKitTestRunner::Get(render_view); 104 test_runner->Reset(); 105 render_view->GetWebView()->setSpellCheckClient( 106 test_runner->proxy()->GetSpellCheckClient()); 107 WebTestDelegate* delegate = 108 ShellRenderProcessObserver::GetInstance()->test_delegate(); 109 if (delegate == static_cast<WebTestDelegate*>(test_runner)) 110 ShellRenderProcessObserver::GetInstance()->SetMainWindow(render_view); 111 } 112 113 bool ShellContentRendererClient::OverrideCreatePlugin( 114 RenderFrame* render_frame, 115 WebLocalFrame* frame, 116 const WebPluginParams& params, 117 WebPlugin** plugin) { 118 return false; 119 } 120 121 WebMediaStreamCenter* 122 ShellContentRendererClient::OverrideCreateWebMediaStreamCenter( 123 WebMediaStreamCenterClient* client) { 124 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) 125 return NULL; 126 #if defined(ENABLE_WEBRTC) 127 WebTestInterfaces* interfaces = 128 ShellRenderProcessObserver::GetInstance()->test_interfaces(); 129 return interfaces->createMediaStreamCenter(client); 130 #else 131 return NULL; 132 #endif 133 } 134 135 WebRTCPeerConnectionHandler* 136 ShellContentRendererClient::OverrideCreateWebRTCPeerConnectionHandler( 137 WebRTCPeerConnectionHandlerClient* client) { 138 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) 139 return NULL; 140 #if defined(ENABLE_WEBRTC) 141 WebTestInterfaces* interfaces = 142 ShellRenderProcessObserver::GetInstance()->test_interfaces(); 143 return interfaces->createWebRTCPeerConnectionHandler(client); 144 #else 145 return NULL; 146 #endif 147 } 148 149 WebMIDIAccessor* 150 ShellContentRendererClient::OverrideCreateMIDIAccessor( 151 WebMIDIAccessorClient* client) { 152 WebTestInterfaces* interfaces = 153 ShellRenderProcessObserver::GetInstance()->test_interfaces(); 154 return interfaces->createMIDIAccessor(client); 155 } 156 157 WebAudioDevice* 158 ShellContentRendererClient::OverrideCreateAudioDevice( 159 double sample_rate) { 160 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) 161 return NULL; 162 WebTestInterfaces* interfaces = 163 ShellRenderProcessObserver::GetInstance()->test_interfaces(); 164 return interfaces->createAudioDevice(sample_rate); 165 } 166 167 WebClipboard* ShellContentRendererClient::OverrideWebClipboard() { 168 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) 169 return NULL; 170 if (!clipboard_) 171 clipboard_.reset(new MockWebClipboardImpl); 172 return clipboard_.get(); 173 } 174 175 WebThemeEngine* ShellContentRendererClient::OverrideThemeEngine() { 176 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) 177 return NULL; 178 return ShellRenderProcessObserver::GetInstance()->test_interfaces() 179 ->themeEngine(); 180 } 181 182 void ShellContentRendererClient::WebTestProxyCreated(RenderView* render_view, 183 WebTestProxyBase* proxy) { 184 WebKitTestRunner* test_runner = new WebKitTestRunner(render_view); 185 test_runner->set_proxy(proxy); 186 if (!ShellRenderProcessObserver::GetInstance()->test_delegate()) 187 ShellRenderProcessObserver::GetInstance()->SetTestDelegate(test_runner); 188 proxy->SetInterfaces( 189 ShellRenderProcessObserver::GetInstance()->test_interfaces()); 190 test_runner->proxy()->SetDelegate( 191 ShellRenderProcessObserver::GetInstance()->test_delegate()); 192 } 193 194 } // namespace content 195