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.
5 #include "chrome/browser/prerender/prerender_handle.h"
9 #include "base/logging.h"
10 #include "chrome/browser/prerender/prerender_contents.h"
11 #include "content/public/browser/web_contents.h"
15 PrerenderHandle::Observer::Observer() {
18 PrerenderHandle::Observer::~Observer() {
21 PrerenderHandle::~PrerenderHandle() {
22 if (prerender_data_
.get()) {
23 prerender_data_
->contents()->RemoveObserver(this);
27 void PrerenderHandle::SetObserver(Observer
* observer
) {
28 DCHECK_NE(static_cast<Observer
*>(NULL
), observer
);
32 void PrerenderHandle::OnNavigateAway() {
33 DCHECK(CalledOnValidThread());
34 if (prerender_data_
.get())
35 prerender_data_
->OnHandleNavigatedAway(this);
38 void PrerenderHandle::OnCancel() {
39 DCHECK(CalledOnValidThread());
40 if (prerender_data_
.get())
41 prerender_data_
->OnHandleCanceled(this);
44 bool PrerenderHandle::IsPrerendering() const {
45 DCHECK(CalledOnValidThread());
46 return prerender_data_
.get() != NULL
;
49 bool PrerenderHandle::IsFinishedLoading() const {
50 DCHECK(CalledOnValidThread());
51 if (!prerender_data_
.get())
53 return prerender_data_
->contents()->has_finished_loading();
56 bool PrerenderHandle::IsAbandoned() const {
57 DCHECK(CalledOnValidThread());
58 return prerender_data_
&& !prerender_data_
->abandon_time().is_null();
61 PrerenderContents
* PrerenderHandle::contents() const {
62 DCHECK(CalledOnValidThread());
63 return prerender_data_
? prerender_data_
->contents() : NULL
;
66 bool PrerenderHandle::Matches(
68 const content::SessionStorageNamespace
* session_storage_namespace
) const {
69 DCHECK(CalledOnValidThread());
70 if (!prerender_data_
.get())
72 return prerender_data_
->contents()->Matches(url
, session_storage_namespace
);
75 PrerenderHandle::PrerenderHandle(
76 PrerenderManager::PrerenderData
* prerender_data
)
78 weak_ptr_factory_(this) {
80 prerender_data_
= prerender_data
->AsWeakPtr();
81 prerender_data
->OnHandleCreated(this);
85 void PrerenderHandle::OnPrerenderStart(PrerenderContents
* prerender_contents
) {
86 DCHECK(CalledOnValidThread());
87 DCHECK(prerender_data_
.get());
88 DCHECK_EQ(prerender_data_
->contents(), prerender_contents
);
90 observer_
->OnPrerenderStart(this);
93 void PrerenderHandle::OnPrerenderStopLoading(
94 PrerenderContents
* prerender_contents
) {
95 DCHECK(CalledOnValidThread());
96 DCHECK(prerender_data_
.get());
97 DCHECK_EQ(prerender_data_
->contents(), prerender_contents
);
99 observer_
->OnPrerenderStopLoading(this);
102 void PrerenderHandle::OnPrerenderDomContentLoaded(
103 PrerenderContents
* prerender_contents
) {
104 DCHECK(CalledOnValidThread());
105 DCHECK(prerender_data_
.get());
106 DCHECK_EQ(prerender_data_
->contents(), prerender_contents
);
108 observer_
->OnPrerenderDomContentLoaded(this);
111 void PrerenderHandle::OnPrerenderStop(PrerenderContents
* prerender_contents
) {
112 DCHECK(CalledOnValidThread());
114 observer_
->OnPrerenderStop(this);
117 void PrerenderHandle::OnPrerenderCreatedMatchCompleteReplacement(
118 PrerenderContents
* contents
, PrerenderContents
* replacement
) {
119 DCHECK(CalledOnValidThread());
121 // This should occur in the middle of the surgery on the PrerenderData, and
122 // so we expect to not have our new contents in our PrerenderData yet. The
124 // PrerenderManager::PrerenderData::MakeIntoMatchCompleteReplacement, so
125 // this method only needs to switch observing.
127 contents
->RemoveObserver(this);
128 replacement
->AddObserver(this);
130 observer_
->OnPrerenderCreatedMatchCompleteReplacement(this);
133 bool PrerenderHandle::RepresentingSamePrerenderAs(
134 PrerenderHandle
* other
) const {
135 return other
&& other
->prerender_data_
.get() && prerender_data_
.get()
136 && prerender_data_
.get() == other
->prerender_data_
.get();
139 } // namespace prerender