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_render_process_observer.h" 6 7 #include "base/command_line.h" 8 #include "content/public/common/content_client.h" 9 #include "content/public/renderer/render_thread.h" 10 #include "content/public/renderer/render_view.h" 11 #include "content/public/test/layouttest_support.h" 12 #include "content/shell/common/shell_messages.h" 13 #include "content/shell/common/shell_switches.h" 14 #include "content/shell/renderer/shell_content_renderer_client.h" 15 #include "content/shell/renderer/test_runner/WebTestInterfaces.h" 16 #include "content/shell/renderer/webkit_test_runner.h" 17 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" 18 #include "third_party/WebKit/public/web/WebView.h" 19 #include "v8/include/v8.h" 20 21 using blink::WebFrame; 22 using blink::WebRuntimeFeatures; 23 24 namespace content { 25 26 namespace { 27 ShellRenderProcessObserver* g_instance = NULL; 28 } 29 30 // static 31 ShellRenderProcessObserver* ShellRenderProcessObserver::GetInstance() { 32 return g_instance; 33 } 34 35 ShellRenderProcessObserver::ShellRenderProcessObserver() 36 : main_test_runner_(NULL), 37 test_delegate_(NULL) { 38 CHECK(!g_instance); 39 g_instance = this; 40 RenderThread::Get()->AddObserver(this); 41 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) 42 return; 43 EnableRendererLayoutTestMode(); 44 } 45 46 ShellRenderProcessObserver::~ShellRenderProcessObserver() { 47 CHECK(g_instance == this); 48 g_instance = NULL; 49 } 50 51 void ShellRenderProcessObserver::SetTestDelegate(WebTestDelegate* delegate) { 52 test_interfaces_->setDelegate(delegate); 53 test_delegate_ = delegate; 54 } 55 56 void ShellRenderProcessObserver::SetMainWindow(RenderView* view) { 57 WebKitTestRunner* test_runner = WebKitTestRunner::Get(view); 58 test_interfaces_->setWebView(view->GetWebView(), test_runner->proxy()); 59 main_test_runner_ = test_runner; 60 } 61 62 void ShellRenderProcessObserver::WebKitInitialized() { 63 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) 64 return; 65 66 // We always expose GC to layout tests. 67 std::string flags("--expose-gc"); 68 v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size())); 69 70 if (!CommandLine::ForCurrentProcess()->HasSwitch( 71 switches::kStableReleaseMode)) { 72 WebRuntimeFeatures::enableTestOnlyFeatures(true); 73 } 74 75 test_interfaces_.reset(new WebTestInterfaces); 76 test_interfaces_->resetAll(); 77 } 78 79 void ShellRenderProcessObserver::OnRenderProcessShutdown() { 80 test_interfaces_.reset(); 81 } 82 83 bool ShellRenderProcessObserver::OnControlMessageReceived( 84 const IPC::Message& message) { 85 bool handled = true; 86 IPC_BEGIN_MESSAGE_MAP(ShellRenderProcessObserver, message) 87 IPC_MESSAGE_HANDLER(ShellViewMsg_SetWebKitSourceDir, OnSetWebKitSourceDir) 88 IPC_MESSAGE_UNHANDLED(handled = false) 89 IPC_END_MESSAGE_MAP() 90 91 return handled; 92 } 93 94 void ShellRenderProcessObserver::OnSetWebKitSourceDir( 95 const base::FilePath& webkit_source_dir) { 96 webkit_source_dir_ = webkit_source_dir; 97 } 98 99 } // namespace content 100