Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Side by Side Diff: chrome/browser/prerender/prerender_contents.cc

Issue 11348357: Add observer interface to PrerenderContents. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remediate to review Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 <functional> 8 #include <functional>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 else 66 else
67 message = new PrerenderMsg_RemovePrerenderURL(url); 67 message = new PrerenderMsg_RemovePrerenderURL(url);
68 render_process_host->Send(message); 68 render_process_host->Send(message);
69 } 69 }
70 70
71 } // namespace 71 } // namespace
72 72
73 class PrerenderContentsFactoryImpl : public PrerenderContents::Factory { 73 class PrerenderContentsFactoryImpl : public PrerenderContents::Factory {
74 public: 74 public:
75 virtual PrerenderContents* CreatePrerenderContents( 75 virtual PrerenderContents* CreatePrerenderContents(
76 PrerenderManager* prerender_manager, PrerenderTracker* prerender_tracker, 76 PrerenderManager* prerender_manager, Profile* profile,
77 Profile* profile, const GURL& url, const content::Referrer& referrer, 77 const GURL& url, const content::Referrer& referrer,
78 Origin origin, uint8 experiment_id) OVERRIDE { 78 Origin origin, uint8 experiment_id) OVERRIDE {
79 return new PrerenderContents(prerender_manager, prerender_tracker, profile, 79 return new PrerenderContents(prerender_manager, profile,
80 url, referrer, origin, experiment_id); 80 url, referrer, origin, experiment_id);
81 } 81 }
82 }; 82 };
83 83
84 // TabContentsDelegateImpl ----------------------------------------------------- 84 // TabContentsDelegateImpl -----------------------------------------------------
85 85
86 class PrerenderContents::TabContentsDelegateImpl 86 class PrerenderContents::TabContentsDelegateImpl
87 : public content::WebContentsDelegate { 87 : public content::WebContentsDelegate {
88 public: 88 public:
89 explicit TabContentsDelegateImpl(PrerenderContents* prerender_contents) : 89 explicit TabContentsDelegateImpl(PrerenderContents* prerender_contents) :
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 bool user_gesture) OVERRIDE { 160 bool user_gesture) OVERRIDE {
161 // TODO(mmenke): Consider supporting this if it is a common case during 161 // TODO(mmenke): Consider supporting this if it is a common case during
162 // prerenders. 162 // prerenders.
163 prerender_contents_->Destroy(FINAL_STATUS_REGISTER_PROTOCOL_HANDLER); 163 prerender_contents_->Destroy(FINAL_STATUS_REGISTER_PROTOCOL_HANDLER);
164 } 164 }
165 165
166 private: 166 private:
167 PrerenderContents* prerender_contents_; 167 PrerenderContents* prerender_contents_;
168 }; 168 };
169 169
170 PrerenderContents::Observer::Observer() {
171 }
172
173 PrerenderContents::Observer::~Observer() {
174 }
175
170 PrerenderContents::PendingPrerenderInfo::PendingPrerenderInfo( 176 PrerenderContents::PendingPrerenderInfo::PendingPrerenderInfo(
171 base::WeakPtr<PrerenderHandle> weak_prerender_handle, 177 base::WeakPtr<PrerenderHandle> weak_prerender_handle,
172 Origin origin, 178 Origin origin,
173 const GURL& url, 179 const GURL& url,
174 const content::Referrer& referrer, 180 const content::Referrer& referrer,
175 const gfx::Size& size) : weak_prerender_handle(weak_prerender_handle), 181 const gfx::Size& size) : weak_prerender_handle(weak_prerender_handle),
176 origin(origin), 182 origin(origin),
177 url(url), 183 url(url),
178 referrer(referrer), 184 referrer(referrer),
179 size(size) { 185 size(size) {
(...skipping 15 matching lines...) Expand all
195 prerender_contents_->web_contents()->GetController() 201 prerender_contents_->web_contents()->GetController()
196 .GetDefaultSessionStorageNamespace(); 202 .GetDefaultSessionStorageNamespace();
197 } 203 }
198 prerender_manager_->StartPendingPrerenders( 204 prerender_manager_->StartPendingPrerenders(
199 child_id_, &pending_prerenders_, session_storage_namespace); 205 child_id_, &pending_prerenders_, session_storage_namespace);
200 pending_prerenders_.clear(); 206 pending_prerenders_.clear();
201 } 207 }
202 208
203 PrerenderContents::PrerenderContents( 209 PrerenderContents::PrerenderContents(
204 PrerenderManager* prerender_manager, 210 PrerenderManager* prerender_manager,
205 PrerenderTracker* prerender_tracker,
206 Profile* profile, 211 Profile* profile,
207 const GURL& url, 212 const GURL& url,
208 const content::Referrer& referrer, 213 const content::Referrer& referrer,
209 Origin origin, 214 Origin origin,
210 uint8 experiment_id) 215 uint8 experiment_id)
211 : prerendering_has_started_(false), 216 : prerendering_has_started_(false),
212 prerender_manager_(prerender_manager), 217 prerender_manager_(prerender_manager),
213 prerender_tracker_(prerender_tracker),
214 prerender_url_(url), 218 prerender_url_(url),
215 referrer_(referrer), 219 referrer_(referrer),
216 profile_(profile), 220 profile_(profile),
217 page_id_(0), 221 page_id_(0),
218 session_storage_namespace_id_(-1), 222 session_storage_namespace_id_(-1),
219 has_stopped_loading_(false), 223 has_stopped_loading_(false),
220 has_finished_loading_(false), 224 has_finished_loading_(false),
221 final_status_(FINAL_STATUS_MAX), 225 final_status_(FINAL_STATUS_MAX),
222 match_complete_status_(MATCH_COMPLETE_DEFAULT), 226 match_complete_status_(MATCH_COMPLETE_DEFAULT),
223 prerendering_has_been_cancelled_(false), 227 prerendering_has_been_cancelled_(false),
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 render_view_host_observer_.reset( 307 render_view_host_observer_.reset(
304 new PrerenderRenderViewHostObserver(this, GetRenderViewHostMutable())); 308 new PrerenderRenderViewHostObserver(this, GetRenderViewHostMutable()));
305 309
306 child_id_ = GetRenderViewHost()->GetProcess()->GetID(); 310 child_id_ = GetRenderViewHost()->GetProcess()->GetID();
307 route_id_ = GetRenderViewHost()->GetRoutingID(); 311 route_id_ = GetRenderViewHost()->GetRoutingID();
308 312
309 // Register this with the ResourceDispatcherHost as a prerender 313 // Register this with the ResourceDispatcherHost as a prerender
310 // RenderViewHost. This must be done before the Navigate message to catch all 314 // RenderViewHost. This must be done before the Navigate message to catch all
311 // resource requests, but as it is on the same thread as the Navigate message 315 // resource requests, but as it is on the same thread as the Navigate message
312 // (IO) there is no race condition. 316 // (IO) there is no race condition.
313 prerender_tracker_->OnPrerenderingStarted( 317 prerender_manager()->prerender_tracker()->AddPrerenderContents(this);
314 child_id_, 318 NotifyPrerenderStart();
315 route_id_,
316 prerender_manager_);
317 319
318 // Close ourselves when the application is shutting down. 320 // Close ourselves when the application is shutting down.
319 notification_registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, 321 notification_registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
320 content::NotificationService::AllSources()); 322 content::NotificationService::AllSources());
321 323
322 // Register for our parent profile to shutdown, so we can shut ourselves down 324 // Register for our parent profile to shutdown, so we can shut ourselves down
323 // as well (should only be called for OTR profiles, as we should receive 325 // as well (should only be called for OTR profiles, as we should receive
324 // APP_TERMINATING before non-OTR profiles are destroyed). 326 // APP_TERMINATING before non-OTR profiles are destroyed).
325 // TODO(tburkard): figure out if this is needed. 327 // TODO(tburkard): figure out if this is needed.
326 notification_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, 328 notification_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 return child_id_ != -1; 361 return child_id_ != -1;
360 } 362 }
361 363
362 bool PrerenderContents::GetRouteId(int* route_id) const { 364 bool PrerenderContents::GetRouteId(int* route_id) const {
363 CHECK(route_id); 365 CHECK(route_id);
364 DCHECK_GE(route_id_, -1); 366 DCHECK_GE(route_id_, -1);
365 *route_id = route_id_; 367 *route_id = route_id_;
366 return route_id_ != -1; 368 return route_id_ != -1;
367 } 369 }
368 370
369 void PrerenderContents::set_final_status(FinalStatus final_status) { 371 void PrerenderContents::set_final_status(FinalStatus final_status) {
mmenke 2012/12/04 18:59:16 This should now be SetFinalStatus, and should have
gavinp 2012/12/04 22:26:07 It probably should have been SetFinalStatus a whil
370 DCHECK(final_status >= FINAL_STATUS_USED && final_status < FINAL_STATUS_MAX); 372 DCHECK(final_status >= FINAL_STATUS_USED && final_status < FINAL_STATUS_MAX);
371 DCHECK(final_status_ == FINAL_STATUS_MAX); 373 DCHECK(final_status_ == FINAL_STATUS_MAX);
372 374
373 final_status_ = final_status; 375 final_status_ = final_status;
376
377 if (!prerender_manager_->IsControlGroup() && prerendering_has_started())
378 NotifyPrerenderStop();
374 } 379 }
375 380
376 PrerenderContents::~PrerenderContents() { 381 PrerenderContents::~PrerenderContents() {
377 DCHECK(final_status_ != FINAL_STATUS_MAX); 382 DCHECK_NE(FINAL_STATUS_MAX, final_status_);
378 DCHECK(prerendering_has_been_cancelled_ || 383 DCHECK(
379 final_status_ == FINAL_STATUS_USED); 384 prerendering_has_been_cancelled_ || final_status_ == FINAL_STATUS_USED);
380 DCHECK(origin_ != ORIGIN_MAX); 385 DCHECK_NE(ORIGIN_MAX, origin_);
381 386
382 prerender_manager_->RecordFinalStatusWithMatchCompleteStatus( 387 prerender_manager_->RecordFinalStatusWithMatchCompleteStatus(
383 origin_, 388 origin_, experiment_id_, match_complete_status_, final_status_);
384 experiment_id_,
385 match_complete_status_,
386 final_status_);
387 389
388 if (child_id_ != -1 && route_id_ != -1) { 390 if (child_id_ != -1 && route_id_ != -1) {
389 prerender_tracker_->OnPrerenderingFinished(child_id_, route_id_);
390 for (std::vector<GURL>::const_iterator it = alias_urls_.begin(); 391 for (std::vector<GURL>::const_iterator it = alias_urls_.begin();
391 it != alias_urls_.end(); 392 it != alias_urls_.end();
392 ++it) { 393 ++it) {
393 InformRenderProcessAboutPrerender(*it, false, creator_child_id_); 394 InformRenderProcessAboutPrerender(*it, false, creator_child_id_);
394 } 395 }
395 } 396 }
396 397
397 // If we still have a WebContents, clean up anything we need to and then 398 // If we still have a WebContents, clean up anything we need to and then
398 // destroy it. 399 // destroy it.
399 if (prerender_contents_.get()) 400 if (prerender_contents_.get())
400 delete ReleasePrerenderContents(); 401 delete ReleasePrerenderContents();
401 } 402 }
402 403
404 void PrerenderContents::AddObserver(Observer* observer) {
mmenke 2012/12/04 18:59:16 Maybe a "DCHECK_NE(FINAL_STATUS_MAX, final_status_
gavinp 2012/12/04 22:26:07 ITYM DCHECK_EQ. Done.
405 observer_list_.AddObserver(observer);
406 }
407
403 void PrerenderContents::Observe(int type, 408 void PrerenderContents::Observe(int type,
404 const content::NotificationSource& source, 409 const content::NotificationSource& source,
405 const content::NotificationDetails& details) { 410 const content::NotificationDetails& details) {
406 switch (type) { 411 switch (type) {
407 case chrome::NOTIFICATION_PROFILE_DESTROYED: 412 case chrome::NOTIFICATION_PROFILE_DESTROYED:
408 Destroy(FINAL_STATUS_PROFILE_DESTROYED); 413 Destroy(FINAL_STATUS_PROFILE_DESTROYED);
409 return; 414 return;
410 415
411 case chrome::NOTIFICATION_APP_TERMINATING: 416 case chrome::NOTIFICATION_APP_TERMINATING:
412 Destroy(FINAL_STATUS_APP_TERMINATING); 417 Destroy(FINAL_STATUS_APP_TERMINATING);
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 WebContents* PrerenderContents::CreateWebContents( 482 WebContents* PrerenderContents::CreateWebContents(
478 SessionStorageNamespace* session_storage_namespace) { 483 SessionStorageNamespace* session_storage_namespace) {
479 // TODO(ajwong): Remove the temporary map once prerendering is aware of 484 // TODO(ajwong): Remove the temporary map once prerendering is aware of
480 // multiple session storage namespaces per tab. 485 // multiple session storage namespaces per tab.
481 content::SessionStorageNamespaceMap session_storage_namespace_map; 486 content::SessionStorageNamespaceMap session_storage_namespace_map;
482 session_storage_namespace_map[""] = session_storage_namespace; 487 session_storage_namespace_map[""] = session_storage_namespace;
483 return WebContents::CreateWithSessionStorage( 488 return WebContents::CreateWithSessionStorage(
484 profile_, NULL, MSG_ROUTING_NONE, NULL, session_storage_namespace_map); 489 profile_, NULL, MSG_ROUTING_NONE, NULL, session_storage_namespace_map);
485 } 490 }
486 491
492 void PrerenderContents::NotifyPrerenderStart() {
493 FOR_EACH_OBSERVER(Observer, observer_list_, OnPrerenderStart(this));
494 }
495
496 void PrerenderContents::NotifyPrerenderStop() {
497 FOR_EACH_OBSERVER(Observer, observer_list_, OnPrerenderStop(this));
498 observer_list_.Clear();
mmenke 2012/12/04 18:59:16 optional: Could DCHECK on final_status_ being / n
gavinp 2012/12/04 22:26:07 Done.
499 }
500
487 void PrerenderContents::OnUpdateFaviconURL( 501 void PrerenderContents::OnUpdateFaviconURL(
488 int32 page_id, 502 int32 page_id,
489 const std::vector<FaviconURL>& urls) { 503 const std::vector<FaviconURL>& urls) {
490 VLOG(1) << "PrerenderContents::OnUpdateFaviconURL" << icon_url_; 504 VLOG(1) << "PrerenderContents::OnUpdateFaviconURL" << icon_url_;
491 for (std::vector<FaviconURL>::const_iterator it = urls.begin(); 505 for (std::vector<FaviconURL>::const_iterator it = urls.begin();
492 it != urls.end(); ++it) { 506 it != urls.end(); ++it) {
493 if (it->icon_type == FaviconURL::FAVICON) { 507 if (it->icon_type == FaviconURL::FAVICON) {
494 icon_url_ = it->icon_url; 508 icon_url_ = it->icon_url;
495 VLOG(1) << icon_url_; 509 VLOG(1) << icon_url_;
496 return; 510 return;
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
573 587
574 void PrerenderContents::Destroy(FinalStatus final_status) { 588 void PrerenderContents::Destroy(FinalStatus final_status) {
575 if (prerendering_has_been_cancelled_) 589 if (prerendering_has_been_cancelled_)
576 return; 590 return;
577 591
578 if (child_id_ != -1 && route_id_ != -1) { 592 if (child_id_ != -1 && route_id_ != -1) {
579 // Cancel the prerender in the PrerenderTracker. This is needed 593 // Cancel the prerender in the PrerenderTracker. This is needed
580 // because destroy may be called directly from the UI thread without calling 594 // because destroy may be called directly from the UI thread without calling
581 // TryCancel(). This is difficult to completely avoid, since prerendering 595 // TryCancel(). This is difficult to completely avoid, since prerendering
582 // can be cancelled before a RenderView is created. 596 // can be cancelled before a RenderView is created.
583 bool is_cancelled = prerender_tracker_->TryCancel( 597 bool is_cancelled = prerender_manager()->prerender_tracker()->TryCancel(
584 child_id_, route_id_, final_status); 598 child_id_, route_id_, final_status);
585 CHECK(is_cancelled); 599 CHECK(is_cancelled);
586 600
587 // A different final status may have been set already from another thread. 601 // A different final status may have been set already from another thread.
588 // If so, use it instead. 602 // If so, use it instead.
589 if (!prerender_tracker_->GetFinalStatus(child_id_, route_id_, 603 if (!prerender_manager()->prerender_tracker()->
590 &final_status)) { 604 GetFinalStatus(child_id_, route_id_, &final_status)) {
591 NOTREACHED(); 605 NOTREACHED();
592 } 606 }
593 } 607 }
594 set_final_status(final_status); 608 set_final_status(final_status);
595 609
596 prerendering_has_been_cancelled_ = true; 610 prerendering_has_been_cancelled_ = true;
597 prerender_manager_->AddToHistory(this); 611 prerender_manager_->AddToHistory(this);
598 prerender_manager_->MoveEntryToPendingDelete(this, final_status); 612 prerender_manager_->MoveEntryToPendingDelete(this, final_status);
599 613
600 // We may destroy the PrerenderContents before we have initialized the 614 // We may destroy the PrerenderContents before we have initialized the
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
685 bool PrerenderContents::IsCrossSiteNavigationPending() const { 699 bool PrerenderContents::IsCrossSiteNavigationPending() const {
686 if (!prerender_contents_.get() || !prerender_contents_->web_contents()) 700 if (!prerender_contents_.get() || !prerender_contents_->web_contents())
687 return false; 701 return false;
688 const WebContents* web_contents = prerender_contents_->web_contents(); 702 const WebContents* web_contents = prerender_contents_->web_contents();
689 return (web_contents->GetSiteInstance() != 703 return (web_contents->GetSiteInstance() !=
690 web_contents->GetPendingSiteInstance()); 704 web_contents->GetPendingSiteInstance());
691 } 705 }
692 706
693 707
694 } // namespace prerender 708 } // namespace prerender
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698