1// Copyright 2013 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_view_observer.h"
6
7#include "base/command_line.h"
8#include "content/public/renderer/render_thread.h"
9#include "content/public/renderer/render_view.h"
10#include "content/public/renderer/render_view_observer.h"
11#include "content/shell/common/shell_messages.h"
12#include "content/shell/common/shell_switches.h"
13#include "content/shell/renderer/ipc_echo.h"
14#include "third_party/WebKit/public/web/WebTestingSupport.h"
15#include "third_party/WebKit/public/web/WebView.h"
16
17namespace content {
18
19ShellRenderViewObserver::ShellRenderViewObserver(RenderView* render_view)
20    : RenderViewObserver(render_view) {
21}
22
23ShellRenderViewObserver::~ShellRenderViewObserver() {
24}
25
26void ShellRenderViewObserver::DidClearWindowObject(
27    blink::WebLocalFrame* frame) {
28  if (CommandLine::ForCurrentProcess()->HasSwitch(
29          switches::kExposeInternalsForTesting)) {
30    blink::WebTestingSupport::injectInternalsObject(frame);
31  }
32
33  if (CommandLine::ForCurrentProcess()->HasSwitch(
34          switches::kExposeIpcEcho)) {
35    RenderView* view = render_view();
36    if (!ipc_echo_)
37      ipc_echo_.reset(new IPCEcho(view->GetWebView()->mainFrame()->document(),
38                                  RenderThread::Get(), view->GetRoutingID()));
39    ipc_echo_->Install(view->GetWebView()->mainFrame());
40  }
41}
42
43bool ShellRenderViewObserver::OnMessageReceived(const IPC::Message& message) {
44  bool handled = true;
45  IPC_BEGIN_MESSAGE_MAP(ShellRenderViewObserver, message)
46    IPC_MESSAGE_HANDLER(ShellViewMsg_EchoPong, OnEchoPong)
47    IPC_MESSAGE_UNHANDLED(handled = false)
48  IPC_END_MESSAGE_MAP()
49
50  return handled;
51}
52
53void ShellRenderViewObserver::OnEchoPong(int id, const std::string& body) {
54  ipc_echo_->DidRespondEcho(id, body.size());
55}
56
57}  // namespace content
58