Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / public / renderer / render_view_observer.cc
blob8289da949e1311a2769928a660a532edc75c5b44
1 // Copyright (c) 2011 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/public/renderer/render_view_observer.h"
7 #include "content/renderer/render_view_impl.h"
9 using blink::WebFrame;
11 namespace content {
13 RenderViewObserver::RenderViewObserver(RenderView* render_view)
14 : render_view_(render_view),
15 routing_id_(MSG_ROUTING_NONE) {
16 // |render_view| can be NULL on unit testing or if Observe() is used.
17 if (render_view) {
18 RenderViewImpl* impl = static_cast<RenderViewImpl*>(render_view);
19 routing_id_ = impl->routing_id();
20 // TODO(jam): bring this back DCHECK_NE(routing_id_, MSG_ROUTING_NONE);
21 impl->AddObserver(this);
25 RenderViewObserver::~RenderViewObserver() {
26 if (render_view_) {
27 RenderViewImpl* impl = static_cast<RenderViewImpl*>(render_view_);
28 impl->RemoveObserver(this);
32 void RenderViewObserver::OnDestruct() {
33 delete this;
36 bool RenderViewObserver::OnMessageReceived(const IPC::Message& message) {
37 return false;
40 bool RenderViewObserver::Send(IPC::Message* message) {
41 if (render_view_)
42 return render_view_->Send(message);
44 delete message;
45 return false;
48 RenderView* RenderViewObserver::render_view() const {
49 return render_view_;
52 void RenderViewObserver::RenderViewGone() {
53 render_view_ = NULL;
56 void RenderViewObserver::Observe(RenderView* render_view) {
57 RenderViewImpl* impl = static_cast<RenderViewImpl*>(render_view_);
58 if (impl) {
59 impl->RemoveObserver(this);
60 routing_id_ = 0;
63 render_view_ = render_view;
64 impl = static_cast<RenderViewImpl*>(render_view_);
65 if (impl) {
66 routing_id_ = impl->routing_id();
67 impl->AddObserver(this);
71 } // namespace content