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/public/browser/web_contents_observer.h"
6
7#include "content/browser/web_contents/web_contents_impl.h"
8#include "content/public/browser/navigation_details.h"
9#include "content/public/browser/render_view_host.h"
10
11namespace content {
12
13WebContentsObserver::WebContentsObserver(WebContents* web_contents)
14    : web_contents_(NULL) {
15  Observe(web_contents);
16}
17
18WebContentsObserver::WebContentsObserver()
19    : web_contents_(NULL) {
20}
21
22WebContentsObserver::~WebContentsObserver() {
23  if (web_contents_)
24    web_contents_->RemoveObserver(this);
25}
26
27WebContents* WebContentsObserver::web_contents() const {
28  return web_contents_;
29}
30
31void WebContentsObserver::Observe(WebContents* web_contents) {
32  if (web_contents == web_contents_) {
33    // Early exit to avoid infinite loops if we're in the middle of a callback.
34    return;
35  }
36  if (web_contents_)
37    web_contents_->RemoveObserver(this);
38  web_contents_ = static_cast<WebContentsImpl*>(web_contents);
39  if (web_contents_) {
40    web_contents_->AddObserver(this);
41  }
42}
43
44bool WebContentsObserver::OnMessageReceived(
45    const IPC::Message& message,
46    RenderFrameHost* render_frame_host) {
47  return false;
48}
49
50bool WebContentsObserver::OnMessageReceived(const IPC::Message& message) {
51  return false;
52}
53
54bool WebContentsObserver::Send(IPC::Message* message) {
55  if (!web_contents_) {
56    delete message;
57    return false;
58  }
59
60  return web_contents_->Send(message);
61}
62
63int WebContentsObserver::routing_id() const {
64  if (!web_contents_)
65    return MSG_ROUTING_NONE;
66
67  return web_contents_->GetRoutingID();
68}
69
70void WebContentsObserver::ResetWebContents() {
71  web_contents_->RemoveObserver(this);
72  web_contents_ = NULL;
73}
74
75}  // namespace content
76