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