OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/tab_contents/tab_contents_observer.h" | 5 #include "content/browser/tab_contents/tab_contents_observer.h" |
6 | 6 |
7 #include "content/browser/renderer_host/render_view_host.h" | 7 #include "content/browser/renderer_host/render_view_host.h" |
8 #include "content/browser/tab_contents/tab_contents.h" | 8 #include "content/browser/tab_contents/tab_contents.h" |
9 | 9 |
| 10 TabContentsObserver::Registrar::Registrar(TabContentsObserver* observer) |
| 11 : observer_(observer), tab_(NULL) { |
| 12 } |
| 13 |
| 14 TabContentsObserver::Registrar::~Registrar() { |
| 15 if (tab_) |
| 16 tab_->RemoveObserver(observer_); |
| 17 } |
| 18 |
| 19 void TabContentsObserver::Registrar::Observe(TabContents* tab) { |
| 20 observer_->SetTabContents(tab); |
| 21 if (tab_) |
| 22 tab_->RemoveObserver(observer_); |
| 23 tab_ = tab; |
| 24 if (tab_) |
| 25 tab_->AddObserver(observer_); |
| 26 } |
| 27 |
10 void TabContentsObserver::NavigateToPendingEntry() { | 28 void TabContentsObserver::NavigateToPendingEntry() { |
11 } | 29 } |
12 | 30 |
13 void TabContentsObserver::DidNavigateMainFramePostCommit( | 31 void TabContentsObserver::DidNavigateMainFramePostCommit( |
14 const NavigationController::LoadCommittedDetails& details, | 32 const NavigationController::LoadCommittedDetails& details, |
15 const ViewHostMsg_FrameNavigate_Params& params) { | 33 const ViewHostMsg_FrameNavigate_Params& params) { |
16 } | 34 } |
17 | 35 |
18 void TabContentsObserver::DidNavigateAnyFramePostCommit( | 36 void TabContentsObserver::DidNavigateAnyFramePostCommit( |
19 const NavigationController::LoadCommittedDetails& details, | 37 const NavigationController::LoadCommittedDetails& details, |
20 const ViewHostMsg_FrameNavigate_Params& params) { | 38 const ViewHostMsg_FrameNavigate_Params& params) { |
21 } | 39 } |
22 | 40 |
23 void TabContentsObserver::OnProvisionalChangeToMainFrameUrl(const GURL& url) { | 41 void TabContentsObserver::OnProvisionalChangeToMainFrameUrl(const GURL& url) { |
24 } | 42 } |
25 | 43 |
26 void TabContentsObserver::DidStartLoading() { | 44 void TabContentsObserver::DidStartLoading() { |
27 } | 45 } |
28 | 46 |
29 void TabContentsObserver::DidStopLoading() { | 47 void TabContentsObserver::DidStopLoading() { |
30 } | 48 } |
31 | 49 |
32 void TabContentsObserver::RenderViewGone() { | 50 void TabContentsObserver::RenderViewGone() { |
33 } | 51 } |
34 | 52 |
35 void TabContentsObserver::StopNavigation() { | 53 void TabContentsObserver::StopNavigation() { |
36 } | 54 } |
37 | 55 |
38 TabContentsObserver::TabContentsObserver(TabContents* tab_contents) | 56 TabContentsObserver::TabContentsObserver(TabContents* tab_contents) { |
39 : tab_contents_(tab_contents), | 57 SetTabContents(tab_contents); |
40 routing_id_(tab_contents->render_view_host()->routing_id()) { | |
41 tab_contents_->AddObserver(this); | 58 tab_contents_->AddObserver(this); |
42 } | 59 } |
43 | 60 |
| 61 TabContentsObserver::TabContentsObserver() |
| 62 : tab_contents_(NULL), routing_id_(MSG_ROUTING_NONE) { |
| 63 } |
| 64 |
44 TabContentsObserver::~TabContentsObserver() { | 65 TabContentsObserver::~TabContentsObserver() { |
45 if (tab_contents_) | 66 if (tab_contents_) |
46 tab_contents_->RemoveObserver(this); | 67 tab_contents_->RemoveObserver(this); |
47 } | 68 } |
48 | 69 |
49 void TabContentsObserver::OnTabContentsDestroyed(TabContents* tab) { | 70 void TabContentsObserver::OnTabContentsDestroyed(TabContents* tab) { |
50 } | 71 } |
51 | 72 |
52 bool TabContentsObserver::OnMessageReceived(const IPC::Message& message) { | 73 bool TabContentsObserver::OnMessageReceived(const IPC::Message& message) { |
53 return false; | 74 return false; |
54 } | 75 } |
55 | 76 |
56 bool TabContentsObserver::Send(IPC::Message* message) { | 77 bool TabContentsObserver::Send(IPC::Message* message) { |
57 if (!tab_contents_ || !tab_contents_->render_view_host()) { | 78 if (!tab_contents_ || !tab_contents_->render_view_host()) { |
58 delete message; | 79 delete message; |
59 return false; | 80 return false; |
60 } | 81 } |
61 | 82 |
62 return tab_contents_->render_view_host()->Send(message); | 83 return tab_contents_->render_view_host()->Send(message); |
63 } | 84 } |
64 | 85 |
| 86 void TabContentsObserver::SetTabContents(TabContents* tab_contents) { |
| 87 tab_contents_ = tab_contents; |
| 88 if (tab_contents_) |
| 89 routing_id_ = tab_contents->render_view_host()->routing_id(); |
| 90 } |
| 91 |
65 void TabContentsObserver::TabContentsDestroyed() { | 92 void TabContentsObserver::TabContentsDestroyed() { |
66 // Do cleanup so that 'this' can safely be deleted from | 93 // Do cleanup so that 'this' can safely be deleted from |
67 // OnTabContentsDestroyed. | 94 // OnTabContentsDestroyed. |
68 tab_contents_->RemoveObserver(this); | 95 tab_contents_->RemoveObserver(this); |
69 TabContents* tab = tab_contents_; | 96 TabContents* tab = tab_contents_; |
70 tab_contents_ = NULL; | 97 tab_contents_ = NULL; |
71 OnTabContentsDestroyed(tab); | 98 OnTabContentsDestroyed(tab); |
72 } | 99 } |
OLD | NEW |