summaryrefslogtreecommitdiffstats
path: root/content/public/browser/web_contents_observer.cc
blob: ecbb3bfd4cf9c9fd10a6478bfd43d647a5e419e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/public/browser/web_contents_observer.h"

#include "content/browser/renderer_host/render_view_host.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "content/public/browser/navigation_details.h"

namespace content {

WebContentsObserver::WebContentsObserver(WebContents* web_contents)
    : tab_contents_(NULL) {
  Observe(web_contents);
}

WebContentsObserver::WebContentsObserver()
    : tab_contents_(NULL) {
}

WebContentsObserver::~WebContentsObserver() {
  if (tab_contents_)
    tab_contents_->RemoveObserver(this);
}

WebContents* WebContentsObserver::web_contents() const {
  return tab_contents_;
}

void WebContentsObserver::Observe(WebContents* web_contents) {
  if (tab_contents_)
    tab_contents_->RemoveObserver(this);
  tab_contents_ = static_cast<TabContents*>(web_contents);
  if (tab_contents_) {
    tab_contents_->AddObserver(this);
  }
}

bool WebContentsObserver::OnMessageReceived(const IPC::Message& message) {
  return false;
}

bool WebContentsObserver::Send(IPC::Message* message) {
  if (!tab_contents_ || !tab_contents_->GetRenderViewHost()) {
    delete message;
    return false;
  }

  return tab_contents_->GetRenderViewHost()->Send(message);
}

int WebContentsObserver::routing_id() const {
  if (!tab_contents_ || !tab_contents_->GetRenderViewHost())
    return MSG_ROUTING_NONE;

  return tab_contents_->GetRenderViewHost()->routing_id();
}

void WebContentsObserver::TabContentsDestroyed() {
  // Do cleanup so that 'this' can safely be deleted from WebContentsDestroyed.
  tab_contents_->RemoveObserver(this);
  TabContents* tab = tab_contents_;
  tab_contents_ = NULL;
  WebContentsDestroyed(tab);
}

}  // namespace content