Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/memory/linked_ptr.h" | |
| 10 #include "base/process_util.h" | 11 #include "base/process_util.h" |
| 11 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/history/history_tab_helper.h" | 13 #include "chrome/browser/history/history_tab_helper.h" |
| 13 #include "chrome/browser/history/history_types.h" | 14 #include "chrome/browser/history/history_types.h" |
| 14 #include "chrome/browser/prerender/prerender_final_status.h" | 15 #include "chrome/browser/prerender/prerender_final_status.h" |
| 16 #include "chrome/browser/prerender/prerender_handle.h" | |
| 15 #include "chrome/browser/prerender/prerender_manager.h" | 17 #include "chrome/browser/prerender/prerender_manager.h" |
| 16 #include "chrome/browser/prerender/prerender_render_view_host_observer.h" | 18 #include "chrome/browser/prerender/prerender_render_view_host_observer.h" |
| 17 #include "chrome/browser/prerender/prerender_tracker.h" | 19 #include "chrome/browser/prerender/prerender_tracker.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/ui/browser.h" | 21 #include "chrome/browser/ui/browser.h" |
| 20 #include "chrome/browser/ui/browser_finder.h" | 22 #include "chrome/browser/ui/browser_finder.h" |
| 21 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 23 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 22 #include "chrome/common/chrome_notification_types.h" | 24 #include "chrome/common/chrome_notification_types.h" |
| 23 #include "chrome/common/icon_messages.h" | 25 #include "chrome/common/icon_messages.h" |
| 24 #include "chrome/common/prerender_messages.h" | 26 #include "chrome/common/prerender_messages.h" |
| 25 #include "chrome/common/url_constants.h" | 27 #include "chrome/common/url_constants.h" |
| 26 #include "content/public/browser/resource_request_details.h" | 28 #include "content/public/browser/resource_request_details.h" |
| 27 #include "content/public/browser/browser_child_process_host.h" | 29 #include "content/public/browser/browser_child_process_host.h" |
| 28 #include "content/public/browser/notification_service.h" | 30 #include "content/public/browser/notification_service.h" |
| 29 #include "content/public/browser/render_process_host.h" | 31 #include "content/public/browser/render_process_host.h" |
| 30 #include "content/public/browser/render_view_host.h" | 32 #include "content/public/browser/render_view_host.h" |
| 31 #include "content/public/browser/web_contents.h" | 33 #include "content/public/browser/web_contents.h" |
| 32 #include "content/public/browser/web_contents_delegate.h" | 34 #include "content/public/browser/web_contents_delegate.h" |
| 33 #include "content/public/browser/web_contents_view.h" | 35 #include "content/public/browser/web_contents_view.h" |
| 34 #include "ui/gfx/rect.h" | 36 #include "ui/gfx/rect.h" |
| 35 | 37 |
| 36 using content::DownloadItem; | 38 using content::DownloadItem; |
| 37 using content::OpenURLParams; | 39 using content::OpenURLParams; |
| 38 using content::RenderViewHost; | 40 using content::RenderViewHost; |
| 39 using content::ResourceRedirectDetails; | 41 using content::ResourceRedirectDetails; |
| 42 using content::SessionStorageNamespace; | |
| 40 using content::WebContents; | 43 using content::WebContents; |
| 41 | 44 |
| 42 namespace prerender { | 45 namespace prerender { |
| 43 | 46 |
| 44 namespace { | 47 namespace { |
| 45 | 48 |
| 46 // Compares URLs ignoring any ref for the purposes of matching URLs when | |
| 47 // prerendering. | |
| 48 struct PrerenderURLPredicate { | |
| 49 explicit PrerenderURLPredicate(const GURL& url) | |
| 50 : url_(url) { | |
| 51 } | |
| 52 | |
| 53 bool operator()(const GURL& url) const { | |
| 54 return url.scheme() == url_.scheme() && | |
| 55 url.host() == url_.host() && | |
| 56 url.port() == url_.port() && | |
| 57 url.path() == url_.path() && | |
| 58 url.query() == url_.query(); | |
| 59 } | |
| 60 GURL url_; | |
| 61 }; | |
| 62 | |
| 63 // Tells the render process at |child_id| whether |url| is a new prerendered | 49 // Tells the render process at |child_id| whether |url| is a new prerendered |
| 64 // page, or whether |url| is being removed as a prerendered page. Currently | 50 // page, or whether |url| is being removed as a prerendered page. Currently |
| 65 // this will only inform the render process that created the prerendered page | 51 // this will only inform the render process that created the prerendered page |
| 66 // with <link rel="prerender"> tags about it. This means that if the user | 52 // with <link rel="prerender"> tags about it. This means that if the user |
| 67 // clicks on a link for a prerendered URL in a different page, the prerender | 53 // clicks on a link for a prerendered URL in a different page, the prerender |
| 68 // will not be swapped in. | 54 // will not be swapped in. |
| 69 void InformRenderProcessAboutPrerender(const GURL& url, | 55 void InformRenderProcessAboutPrerender(const GURL& url, |
| 70 bool is_add, | 56 bool is_add, |
| 71 int child_id) { | 57 int child_id) { |
| 72 if (child_id < 0) | 58 if (child_id < 0) |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 89 public: | 75 public: |
| 90 virtual PrerenderContents* CreatePrerenderContents( | 76 virtual PrerenderContents* CreatePrerenderContents( |
| 91 PrerenderManager* prerender_manager, PrerenderTracker* prerender_tracker, | 77 PrerenderManager* prerender_manager, PrerenderTracker* prerender_tracker, |
| 92 Profile* profile, const GURL& url, const content::Referrer& referrer, | 78 Profile* profile, const GURL& url, const content::Referrer& referrer, |
| 93 Origin origin, uint8 experiment_id) OVERRIDE { | 79 Origin origin, uint8 experiment_id) OVERRIDE { |
| 94 return new PrerenderContents(prerender_manager, prerender_tracker, profile, | 80 return new PrerenderContents(prerender_manager, prerender_tracker, profile, |
| 95 url, referrer, origin, experiment_id); | 81 url, referrer, origin, experiment_id); |
| 96 } | 82 } |
| 97 }; | 83 }; |
| 98 | 84 |
| 85 bool PrerenderContents::LessThanByLoadStartTime::operator ()( | |
| 86 const PrerenderContents* lhs, | |
| 87 const PrerenderContents* rhs) const { | |
| 88 DCHECK(!lhs->load_start_time().is_null()); | |
| 89 DCHECK(!rhs->load_start_time().is_null()); | |
| 90 return lhs->load_start_time() < rhs->load_start_time(); | |
| 91 } | |
| 92 | |
| 99 struct PrerenderContents::PendingPrerenderInfo { | 93 struct PrerenderContents::PendingPrerenderInfo { |
| 100 PendingPrerenderInfo(const GURL& url, | 94 PendingPrerenderInfo(linked_ptr<PrerenderHandle> prerender_handle, |
|
mmenke
2012/06/22 16:12:45
I assume this is to allow copying. linked_ptr is
| |
| 95 const GURL& url, | |
| 101 const content::Referrer& referrer, | 96 const content::Referrer& referrer, |
| 102 const gfx::Size& size); | 97 const gfx::Size& size); |
| 98 | |
| 99 linked_ptr<PrerenderHandle> prerender_handle; | |
|
dominich
2012/06/22 15:36:16
You don't see those very often. I would have expec
| |
| 103 const GURL url; | 100 const GURL url; |
| 104 const content::Referrer referrer; | 101 const content::Referrer referrer; |
| 105 const gfx::Size size; | 102 const gfx::Size size; |
| 106 }; | 103 }; |
| 107 | 104 |
| 108 PrerenderContents::PendingPrerenderInfo::PendingPrerenderInfo( | 105 PrerenderContents::PendingPrerenderInfo::PendingPrerenderInfo( |
| 106 linked_ptr<PrerenderHandle> prerender_handle, | |
| 109 const GURL& url, | 107 const GURL& url, |
| 110 const content::Referrer& referrer, | 108 const content::Referrer& referrer, |
| 111 const gfx::Size& size) | 109 const gfx::Size& size) |
| 112 : url(url), | 110 : prerender_handle(prerender_handle), |
| 111 url(url), | |
| 113 referrer(referrer), | 112 referrer(referrer), |
| 114 size(size) { | 113 size(size) { |
| 115 } | 114 } |
| 116 | 115 |
| 117 // TabContentsDelegateImpl ----------------------------------------------------- | 116 // TabContentsDelegateImpl ----------------------------------------------------- |
| 118 | 117 |
| 119 class PrerenderContents::TabContentsDelegateImpl | 118 class PrerenderContents::TabContentsDelegateImpl |
| 120 : public content::WebContentsDelegate { | 119 : public content::WebContentsDelegate { |
| 121 public: | 120 public: |
| 122 explicit TabContentsDelegateImpl(PrerenderContents* prerender_contents) : | 121 explicit TabContentsDelegateImpl(PrerenderContents* prerender_contents) : |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 199 private: | 198 private: |
| 200 typedef std::vector<scoped_refptr<history::HistoryAddPageArgs> > | 199 typedef std::vector<scoped_refptr<history::HistoryAddPageArgs> > |
| 201 AddPageVector; | 200 AddPageVector; |
| 202 | 201 |
| 203 // Caches pages to be added to the history. | 202 // Caches pages to be added to the history. |
| 204 AddPageVector add_page_vector_; | 203 AddPageVector add_page_vector_; |
| 205 | 204 |
| 206 PrerenderContents* prerender_contents_; | 205 PrerenderContents* prerender_contents_; |
| 207 }; | 206 }; |
| 208 | 207 |
| 209 void PrerenderContents::AddPendingPrerender(const GURL& url, | 208 void PrerenderContents::AddPendingPrerender( |
| 210 const content::Referrer& referrer, | 209 scoped_ptr<PrerenderHandle> prerender_handle, |
| 211 const gfx::Size& size) { | 210 const GURL& url, |
| 212 pending_prerender_list_.push_back(PendingPrerenderInfo(url, referrer, size)); | 211 const content::Referrer& referrer, |
| 212 const gfx::Size& size) { | |
| 213 pending_prerender_list_.push_back( | |
| 214 PendingPrerenderInfo( | |
| 215 make_linked_ptr(prerender_handle.release()), | |
|
dominich
2012/06/22 15:36:16
what is this voodoo?
So you have a prerender hand
| |
| 216 url, referrer, size)); | |
| 213 } | 217 } |
| 214 | 218 |
| 215 bool PrerenderContents::IsPendingEntry(const GURL& url) const { | 219 bool PrerenderContents::IsPendingEntry( |
| 220 const PrerenderHandle* prerender_handle) const { | |
| 216 for (PendingPrerenderList::const_iterator it = | 221 for (PendingPrerenderList::const_iterator it = |
|
dominich
2012/06/22 15:36:16
what no std::count_if?
| |
| 217 pending_prerender_list_.begin(); | 222 pending_prerender_list_.begin(); |
| 218 it != pending_prerender_list_.end(); | 223 it != pending_prerender_list_.end(); |
| 219 ++it) { | 224 ++it) { |
| 220 if (it->url == url) | 225 if (it->prerender_handle == prerender_handle) |
| 221 return true; | 226 return true; |
| 222 } | 227 } |
| 223 return false; | 228 return false; |
| 224 } | 229 } |
| 225 | 230 |
| 226 void PrerenderContents::StartPendingPrerenders() { | 231 void PrerenderContents::StartPendingPrerenders() { |
| 227 PendingPrerenderList pending_prerender_list; | 232 PendingPrerenderList pending_prerender_list; |
| 228 pending_prerender_list.swap(pending_prerender_list_); | 233 pending_prerender_list.swap(pending_prerender_list_); |
| 229 for (PendingPrerenderList::iterator it = pending_prerender_list.begin(); | 234 for (PendingPrerenderList::iterator it = pending_prerender_list.begin(); |
| 230 it != pending_prerender_list.end(); | 235 it != pending_prerender_list.end(); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 242 const content::Referrer& referrer, | 247 const content::Referrer& referrer, |
| 243 Origin origin, | 248 Origin origin, |
| 244 uint8 experiment_id) | 249 uint8 experiment_id) |
| 245 : prerendering_has_started_(false), | 250 : prerendering_has_started_(false), |
| 246 prerender_manager_(prerender_manager), | 251 prerender_manager_(prerender_manager), |
| 247 prerender_tracker_(prerender_tracker), | 252 prerender_tracker_(prerender_tracker), |
| 248 prerender_url_(url), | 253 prerender_url_(url), |
| 249 referrer_(referrer), | 254 referrer_(referrer), |
| 250 profile_(profile), | 255 profile_(profile), |
| 251 page_id_(0), | 256 page_id_(0), |
| 257 client_count_(1), | |
| 252 has_stopped_loading_(false), | 258 has_stopped_loading_(false), |
| 253 has_finished_loading_(false), | 259 has_finished_loading_(false), |
| 254 final_status_(FINAL_STATUS_MAX), | 260 final_status_(FINAL_STATUS_MAX), |
| 255 match_complete_status_(MATCH_COMPLETE_DEFAULT), | 261 match_complete_status_(MATCH_COMPLETE_DEFAULT), |
| 256 prerendering_has_been_cancelled_(false), | 262 prerendering_has_been_cancelled_(false), |
| 257 child_id_(-1), | 263 child_id_(-1), |
| 258 route_id_(-1), | 264 route_id_(-1), |
| 259 origin_(origin), | 265 origin_(origin), |
| 260 experiment_id_(experiment_id), | 266 experiment_id_(experiment_id), |
| 261 creator_child_id_(-1) { | 267 creator_child_id_(-1) { |
| 262 DCHECK(prerender_manager != NULL); | 268 DCHECK(prerender_manager != NULL); |
| 263 } | 269 } |
| 264 | 270 |
| 271 PrerenderContents* PrerenderContents::CreateDummyReplacement() const { | |
| 272 PrerenderContents* dummy = | |
| 273 new PrerenderContents(prerender_manager_, prerender_tracker_, profile_, | |
| 274 prerender_url_, referrer_, origin_, experiment_id_); | |
| 275 dummy->load_start_time_ = load_start_time_; | |
| 276 dummy->client_count_ = client_count_; | |
|
dominich
2012/06/22 15:36:16
do we not need to copy the child_id, route_id, fin
| |
| 277 return dummy; | |
| 278 } | |
| 279 | |
| 265 bool PrerenderContents::Init() { | 280 bool PrerenderContents::Init() { |
| 266 return AddAliasURL(prerender_url_); | 281 return AddAliasURL(prerender_url_); |
| 267 } | 282 } |
| 268 | 283 |
| 269 // static | 284 // static |
| 270 PrerenderContents::Factory* PrerenderContents::CreateFactory() { | 285 PrerenderContents::Factory* PrerenderContents::CreateFactory() { |
| 271 return new PrerenderContentsFactoryImpl(); | 286 return new PrerenderContentsFactoryImpl(); |
| 272 } | 287 } |
| 273 | 288 |
| 274 void PrerenderContents::StartPrerendering( | 289 void PrerenderContents::StartPrerendering( |
| 275 int creator_child_id, | 290 int creator_child_id, |
| 276 const gfx::Size& size, | 291 const gfx::Size& size, |
| 277 content::SessionStorageNamespace* session_storage_namespace, | 292 SessionStorageNamespace* session_storage_namespace, |
| 278 bool is_control_group) { | 293 bool is_control_group) { |
| 279 DCHECK(profile_ != NULL); | 294 DCHECK(profile_ != NULL); |
| 280 DCHECK(!prerendering_has_started_); | 295 DCHECK(!prerendering_has_started_); |
| 281 DCHECK(prerender_contents_.get() == NULL); | 296 DCHECK(prerender_contents_.get() == NULL); |
| 282 DCHECK_EQ(-1, creator_child_id_); | 297 DCHECK_EQ(-1, creator_child_id_); |
| 283 DCHECK(size_.IsEmpty()); | 298 DCHECK(size_.IsEmpty()); |
| 284 DCHECK_EQ(1U, alias_urls_.size()); | 299 DCHECK_EQ(1U, alias_urls_.size()); |
| 285 | 300 |
| 286 creator_child_id_ = creator_child_id; | 301 creator_child_id_ = creator_child_id; |
| 287 | 302 |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 495 NOTREACHED() << "Unexpected notification sent."; | 510 NOTREACHED() << "Unexpected notification sent."; |
| 496 break; | 511 break; |
| 497 } | 512 } |
| 498 } | 513 } |
| 499 | 514 |
| 500 void PrerenderContents::OnRenderViewHostCreated( | 515 void PrerenderContents::OnRenderViewHostCreated( |
| 501 RenderViewHost* new_render_view_host) { | 516 RenderViewHost* new_render_view_host) { |
| 502 } | 517 } |
| 503 | 518 |
| 504 WebContents* PrerenderContents::CreateWebContents( | 519 WebContents* PrerenderContents::CreateWebContents( |
| 505 content::SessionStorageNamespace* session_storage_namespace) { | 520 SessionStorageNamespace* session_storage_namespace) { |
| 506 return WebContents::Create(profile_, NULL, MSG_ROUTING_NONE, NULL, | 521 return WebContents::Create(profile_, NULL, MSG_ROUTING_NONE, NULL, |
| 507 session_storage_namespace); | 522 session_storage_namespace); |
| 508 } | 523 } |
| 509 | 524 |
| 510 void PrerenderContents::OnUpdateFaviconURL( | 525 void PrerenderContents::OnUpdateFaviconURL( |
| 511 int32 page_id, | 526 int32 page_id, |
| 512 const std::vector<FaviconURL>& urls) { | 527 const std::vector<FaviconURL>& urls) { |
| 513 VLOG(1) << "PrerenderContents::OnUpdateFaviconURL" << icon_url_; | 528 VLOG(1) << "PrerenderContents::OnUpdateFaviconURL" << icon_url_; |
| 514 for (std::vector<FaviconURL>::const_iterator it = urls.begin(); | 529 for (std::vector<FaviconURL>::const_iterator it = urls.begin(); |
| 515 it != urls.end(); ++it) { | 530 it != urls.end(); ++it) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 547 | 562 |
| 548 void PrerenderContents::AddAliasURLsFromOtherPrerenderContents( | 563 void PrerenderContents::AddAliasURLsFromOtherPrerenderContents( |
| 549 PrerenderContents* other_pc) { | 564 PrerenderContents* other_pc) { |
| 550 for (std::vector<GURL>::const_iterator it = other_pc->alias_urls_.begin(); | 565 for (std::vector<GURL>::const_iterator it = other_pc->alias_urls_.begin(); |
| 551 it != other_pc->alias_urls_.end(); | 566 it != other_pc->alias_urls_.end(); |
| 552 ++it) { | 567 ++it) { |
| 553 alias_urls_.push_back(*it); | 568 alias_urls_.push_back(*it); |
| 554 } | 569 } |
| 555 } | 570 } |
| 556 | 571 |
| 557 bool PrerenderContents::MatchesURL(const GURL& url, GURL* matching_url) const { | 572 void PrerenderContents::IncrementClientCount() { |
| 558 std::vector<GURL>::const_iterator matching_url_iterator = | 573 DCHECK_GT(0, client_count_); |
| 559 std::find_if(alias_urls_.begin(), | 574 ++client_count_; |
| 560 alias_urls_.end(), | 575 } |
| 561 PrerenderURLPredicate(url)); | 576 |
| 562 if (matching_url_iterator != alias_urls_.end()) { | 577 void PrerenderContents::DecrementClientCount() { |
|
dominich
2012/06/22 15:36:16
DCHECK_GT(0, client_count_);
| |
| 563 if (matching_url) | 578 if (--client_count_ == 0) |
| 564 *matching_url = *matching_url_iterator; | 579 Destroy(FINAL_STATUS_CANCELLED); |
| 565 return true; | 580 } |
| 566 } | 581 |
| 567 return false; | 582 bool PrerenderContents::Matches( |
| 583 const GURL& url, | |
| 584 const SessionStorageNamespace* session_storage_namespace) { | |
| 585 SessionStorageNamespace* my_session_storage_namespace = | |
| 586 child_id_ == -1 ? NULL | |
| 587 : GetRenderViewHostMutable()->GetSessionStorageNamespace() ; | |
| 588 | |
| 589 if (session_storage_namespace != my_session_storage_namespace) | |
| 590 return false; | |
| 591 | |
| 592 return implicit_cast<bool>( | |
| 593 std::count_if(alias_urls_.begin(), alias_urls_.end(), | |
| 594 std::bind2nd(std::equal_to<GURL>(), url))); | |
|
dominich
2012/06/22 15:36:16
This will break fragment mismatches. There's a ver
| |
| 568 } | 595 } |
| 569 | 596 |
| 570 void PrerenderContents::OnJSOutOfMemory() { | 597 void PrerenderContents::OnJSOutOfMemory() { |
| 571 Destroy(FINAL_STATUS_JS_OUT_OF_MEMORY); | 598 Destroy(FINAL_STATUS_JS_OUT_OF_MEMORY); |
| 572 } | 599 } |
| 573 | 600 |
| 574 void PrerenderContents::RenderViewGone(base::TerminationStatus status) { | 601 void PrerenderContents::RenderViewGone(base::TerminationStatus status) { |
| 575 Destroy(FINAL_STATUS_RENDERER_CRASHED); | 602 Destroy(FINAL_STATUS_RENDERER_CRASHED); |
| 576 } | 603 } |
| 577 | 604 |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 723 bool PrerenderContents::IsCrossSiteNavigationPending() const { | 750 bool PrerenderContents::IsCrossSiteNavigationPending() const { |
| 724 if (!prerender_contents_.get() || !prerender_contents_->web_contents()) | 751 if (!prerender_contents_.get() || !prerender_contents_->web_contents()) |
| 725 return false; | 752 return false; |
| 726 const WebContents* web_contents = prerender_contents_->web_contents(); | 753 const WebContents* web_contents = prerender_contents_->web_contents(); |
| 727 return (web_contents->GetSiteInstance() != | 754 return (web_contents->GetSiteInstance() != |
| 728 web_contents->GetPendingSiteInstance()); | 755 web_contents->GetPendingSiteInstance()); |
| 729 } | 756 } |
| 730 | 757 |
| 731 | 758 |
| 732 } // namespace prerender | 759 } // namespace prerender |
| OLD | NEW |