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 "chrome/browser/prerender/prerender_contents.h" | 5 #include "chrome/browser/prerender/prerender_contents.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/process_util.h" | 10 #include "base/process_util.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 } | 53 } |
54 GURL url_; | 54 GURL url_; |
55 }; | 55 }; |
56 | 56 |
57 } // end namespace | 57 } // end namespace |
58 | 58 |
59 class PrerenderContentsFactoryImpl : public PrerenderContents::Factory { | 59 class PrerenderContentsFactoryImpl : public PrerenderContents::Factory { |
60 public: | 60 public: |
61 virtual PrerenderContents* CreatePrerenderContents( | 61 virtual PrerenderContents* CreatePrerenderContents( |
62 PrerenderManager* prerender_manager, PrerenderTracker* prerender_tracker, | 62 PrerenderManager* prerender_manager, PrerenderTracker* prerender_tracker, |
63 Profile* profile, const GURL& url, const GURL& referrer) OVERRIDE { | 63 Profile* profile, const GURL& url, const GURL& referrer, |
| 64 Origin origin) OVERRIDE { |
64 return new PrerenderContents(prerender_manager, prerender_tracker, profile, | 65 return new PrerenderContents(prerender_manager, prerender_tracker, profile, |
65 url, referrer); | 66 url, referrer, origin); |
66 } | 67 } |
67 }; | 68 }; |
68 | 69 |
69 // TabContentsDelegateImpl ----------------------------------------------------- | 70 // TabContentsDelegateImpl ----------------------------------------------------- |
70 | 71 |
71 class PrerenderContents::TabContentsDelegateImpl | 72 class PrerenderContents::TabContentsDelegateImpl |
72 : public TabContentsDelegate { | 73 : public TabContentsDelegate { |
73 public: | 74 public: |
74 explicit TabContentsDelegateImpl(PrerenderContents* prerender_contents) : | 75 explicit TabContentsDelegateImpl(PrerenderContents* prerender_contents) : |
75 prerender_contents_(prerender_contents) { | 76 prerender_contents_(prerender_contents) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 // Caches pages to be added to the history. | 118 // Caches pages to be added to the history. |
118 AddPageVector add_page_vector_; | 119 AddPageVector add_page_vector_; |
119 | 120 |
120 PrerenderContents* prerender_contents_; | 121 PrerenderContents* prerender_contents_; |
121 }; | 122 }; |
122 | 123 |
123 PrerenderContents::PrerenderContents(PrerenderManager* prerender_manager, | 124 PrerenderContents::PrerenderContents(PrerenderManager* prerender_manager, |
124 PrerenderTracker* prerender_tracker, | 125 PrerenderTracker* prerender_tracker, |
125 Profile* profile, | 126 Profile* profile, |
126 const GURL& url, | 127 const GURL& url, |
127 const GURL& referrer) | 128 const GURL& referrer, |
| 129 Origin origin) |
128 : prerender_manager_(prerender_manager), | 130 : prerender_manager_(prerender_manager), |
129 prerender_tracker_(prerender_tracker), | 131 prerender_tracker_(prerender_tracker), |
130 prerender_url_(url), | 132 prerender_url_(url), |
131 referrer_(referrer), | 133 referrer_(referrer), |
132 profile_(profile), | 134 profile_(profile), |
133 page_id_(0), | 135 page_id_(0), |
134 has_stopped_loading_(false), | 136 has_stopped_loading_(false), |
135 final_status_(FINAL_STATUS_MAX), | 137 final_status_(FINAL_STATUS_MAX), |
136 prerendering_has_started_(false), | 138 prerendering_has_started_(false), |
137 prerendering_has_been_cancelled_(false), | 139 prerendering_has_been_cancelled_(false), |
138 child_id_(-1), | 140 child_id_(-1), |
139 route_id_(-1), | 141 route_id_(-1), |
140 starting_page_id_(-1) { | 142 starting_page_id_(-1), |
| 143 origin_(origin) { |
141 DCHECK(prerender_manager != NULL); | 144 DCHECK(prerender_manager != NULL); |
142 } | 145 } |
143 | 146 |
144 bool PrerenderContents::Init() { | 147 bool PrerenderContents::Init() { |
145 return AddAliasURL(prerender_url_); | 148 return AddAliasURL(prerender_url_); |
146 } | 149 } |
147 | 150 |
148 // static | 151 // static |
149 PrerenderContents::Factory* PrerenderContents::CreateFactory() { | 152 PrerenderContents::Factory* PrerenderContents::CreateFactory() { |
150 return new PrerenderContentsFactoryImpl(); | 153 return new PrerenderContentsFactoryImpl(); |
151 } | 154 } |
152 | 155 |
153 void PrerenderContents::StartPrerendering( | 156 void PrerenderContents::StartPrerendering( |
154 const RenderViewHost* source_render_view_host) { | 157 const RenderViewHost* source_render_view_host) { |
155 DCHECK(profile_ != NULL); | 158 DCHECK(profile_ != NULL); |
156 DCHECK(!prerendering_has_started_); | 159 DCHECK(!prerendering_has_started_); |
157 DCHECK(prerender_contents_.get() == NULL); | 160 DCHECK(prerender_contents_.get() == NULL); |
158 DCHECK(source_render_view_host != NULL); | |
159 DCHECK(source_render_view_host->view() != NULL); | |
160 | 161 |
161 prerendering_has_started_ = true; | 162 prerendering_has_started_ = true; |
162 TabContents* new_contents = new TabContents(profile_, NULL, MSG_ROUTING_NONE, | 163 TabContents* new_contents = new TabContents(profile_, NULL, MSG_ROUTING_NONE, |
163 NULL, NULL); | 164 NULL, NULL); |
164 prerender_contents_.reset(new TabContentsWrapper(new_contents)); | 165 prerender_contents_.reset(new TabContentsWrapper(new_contents)); |
165 TabContentsObserver::Observe(new_contents); | 166 TabContentsObserver::Observe(new_contents); |
166 prerender_contents_->download_tab_helper()->set_delegate(this); | 167 prerender_contents_->download_tab_helper()->set_delegate(this); |
167 | 168 |
168 TabContents* source_tc = | 169 if (source_render_view_host) { |
169 source_render_view_host->delegate()->GetAsTabContents(); | 170 DCHECK(source_render_view_host->view() != NULL); |
170 if (source_tc) { | 171 TabContents* source_tc = |
171 // So that history merging will work, get the max page ID | 172 source_render_view_host->delegate()->GetAsTabContents(); |
172 // of the old page, and add a safety margin of 10 to it (for things | 173 if (source_tc) { |
173 // such as redirects). | 174 // So that history merging will work, get the max page ID |
174 starting_page_id_ = source_tc->GetMaxPageID(); | 175 // of the old page, and add a safety margin of 10 to it (for things |
175 if (starting_page_id_ < 0) | 176 // such as redirects). |
176 starting_page_id_ = 0; | 177 starting_page_id_ = source_tc->GetMaxPageID(); |
177 starting_page_id_ += kPrerenderPageIdOffset; | 178 if (starting_page_id_ < 0) |
178 prerender_contents_->controller().set_max_restored_page_id( | 179 starting_page_id_ = 0; |
179 starting_page_id_); | 180 starting_page_id_ += kPrerenderPageIdOffset; |
| 181 prerender_contents_->controller().set_max_restored_page_id( |
| 182 starting_page_id_); |
180 | 183 |
181 tab_contents_delegate_.reset(new TabContentsDelegateImpl(this)); | 184 tab_contents_delegate_.reset(new TabContentsDelegateImpl(this)); |
182 new_contents->set_delegate(tab_contents_delegate_.get()); | 185 new_contents->set_delegate(tab_contents_delegate_.get()); |
183 | 186 |
184 // Set the size of the new TC to that of the old TC. | 187 // Set the size of the new TC to that of the old TC. |
185 gfx::Rect tab_bounds; | 188 gfx::Rect tab_bounds; |
186 source_tc->view()->GetContainerBounds(&tab_bounds); | 189 source_tc->view()->GetContainerBounds(&tab_bounds); |
187 prerender_contents_->view()->SizeContents(tab_bounds.size()); | 190 prerender_contents_->view()->SizeContents(tab_bounds.size()); |
| 191 } |
188 } | 192 } |
189 | 193 |
190 // Register as an observer of the RenderViewHost so we get messages. | 194 // Register as an observer of the RenderViewHost so we get messages. |
191 render_view_host_observer_.reset( | 195 render_view_host_observer_.reset( |
192 new PrerenderRenderViewHostObserver(this, render_view_host_mutable())); | 196 new PrerenderRenderViewHostObserver(this, render_view_host_mutable())); |
193 | 197 |
194 child_id_ = render_view_host()->process()->id(); | 198 child_id_ = render_view_host()->process()->id(); |
195 route_id_ = render_view_host()->routing_id(); | 199 route_id_ = render_view_host()->routing_id(); |
196 | 200 |
197 // Register this with the ResourceDispatcherHost as a prerender | 201 // Register this with the ResourceDispatcherHost as a prerender |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 | 269 |
266 FinalStatus PrerenderContents::final_status() const { | 270 FinalStatus PrerenderContents::final_status() const { |
267 return final_status_; | 271 return final_status_; |
268 } | 272 } |
269 | 273 |
270 PrerenderContents::~PrerenderContents() { | 274 PrerenderContents::~PrerenderContents() { |
271 DCHECK(final_status_ != FINAL_STATUS_MAX); | 275 DCHECK(final_status_ != FINAL_STATUS_MAX); |
272 DCHECK(prerendering_has_been_cancelled_ || | 276 DCHECK(prerendering_has_been_cancelled_ || |
273 final_status_ == FINAL_STATUS_USED || | 277 final_status_ == FINAL_STATUS_USED || |
274 final_status_ == FINAL_STATUS_CONTROL_GROUP); | 278 final_status_ == FINAL_STATUS_CONTROL_GROUP); |
| 279 DCHECK(origin_ != ORIGIN_MAX); |
275 | 280 |
276 // If we haven't even started prerendering, we were just in the control | 281 // If we haven't even started prerendering, we were just in the control |
277 // group, which means we do not want to record the status. | 282 // group, which means we do not want to record the status. |
278 if (prerendering_has_started()) | 283 if (prerendering_has_started()) |
279 RecordFinalStatus(final_status_); | 284 RecordFinalStatus(origin_, final_status_); |
280 | 285 |
281 if (child_id_ != -1 && route_id_ != -1) | 286 if (child_id_ != -1 && route_id_ != -1) |
282 prerender_tracker_->OnPrerenderingFinished(child_id_, route_id_); | 287 prerender_tracker_->OnPrerenderingFinished(child_id_, route_id_); |
283 | 288 |
284 // If we still have a TabContents, clean up anything we need to and then | 289 // If we still have a TabContents, clean up anything we need to and then |
285 // destroy it. | 290 // destroy it. |
286 if (prerender_contents_.get()) | 291 if (prerender_contents_.get()) |
287 delete ReleasePrerenderContents(); | 292 delete ReleasePrerenderContents(); |
288 | 293 |
289 // The following URLs are no longer rendering. | 294 // The following URLs are no longer rendering. |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
570 return NULL; | 575 return NULL; |
571 DictionaryValue* dict_value = new DictionaryValue(); | 576 DictionaryValue* dict_value = new DictionaryValue(); |
572 dict_value->SetString("url", prerender_url_.spec()); | 577 dict_value->SetString("url", prerender_url_.spec()); |
573 base::TimeTicks current_time = base::TimeTicks::Now(); | 578 base::TimeTicks current_time = base::TimeTicks::Now(); |
574 base::TimeDelta duration = current_time - load_start_time_; | 579 base::TimeDelta duration = current_time - load_start_time_; |
575 dict_value->SetInteger("duration", duration.InSeconds()); | 580 dict_value->SetInteger("duration", duration.InSeconds()); |
576 return dict_value; | 581 return dict_value; |
577 } | 582 } |
578 | 583 |
579 } // namespace prerender | 584 } // namespace prerender |
OLD | NEW |