| 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 <functional> | 8 #include <functional> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
| 33 #include "content/public/browser/web_contents_delegate.h" | 33 #include "content/public/browser/web_contents_delegate.h" |
| 34 #include "content/public/browser/web_contents_view.h" | 34 #include "content/public/browser/web_contents_view.h" |
| 35 #include "ui/gfx/rect.h" | 35 #include "ui/gfx/rect.h" |
| 36 | 36 |
| 37 using content::DownloadItem; | 37 using content::DownloadItem; |
| 38 using content::OpenURLParams; | 38 using content::OpenURLParams; |
| 39 using content::RenderViewHost; | 39 using content::RenderViewHost; |
| 40 using content::ResourceRedirectDetails; | 40 using content::ResourceRedirectDetails; |
| 41 using content::SessionStorageNamespace; | 41 using content::SessionStorageNamespace; |
| 42 using content::SessionStorageNamespaceMap; |
| 42 using content::WebContents; | 43 using content::WebContents; |
| 43 | 44 |
| 44 namespace prerender { | 45 namespace prerender { |
| 45 | 46 |
| 46 namespace { | 47 namespace { |
| 47 | 48 |
| 48 // 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 |
| 49 // 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 |
| 50 // 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 |
| 51 // 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 |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 pending_prerenders_.begin(); | 204 pending_prerenders_.begin(); |
| 204 it != pending_prerenders_.end(); | 205 it != pending_prerenders_.end(); |
| 205 ++it) { | 206 ++it) { |
| 206 if (it->weak_prerender_handle.get() == &prerender_handle) | 207 if (it->weak_prerender_handle.get() == &prerender_handle) |
| 207 return true; | 208 return true; |
| 208 } | 209 } |
| 209 return false; | 210 return false; |
| 210 } | 211 } |
| 211 | 212 |
| 212 void PrerenderContents::StartPendingPrerenders() { | 213 void PrerenderContents::StartPendingPrerenders() { |
| 213 SessionStorageNamespace* session_storage_namespace = NULL; | 214 const SessionStorageNamespaceMap& session_storage_namespace_map = |
| 214 if (prerender_contents_.get()) { | 215 prerender_contents_->web_contents()->GetController() |
| 215 // TODO(ajwong): This does not correctly handle storage for isolated apps. | 216 .GetSessionStorageNamespaceMap(); |
| 216 session_storage_namespace = | 217 DCHECK(child_id_ == -1 || !session_storage_namespace_map.empty()); |
| 217 prerender_contents_->web_contents()->GetController() | |
| 218 .GetSessionStorageNamespaceMap().find("")->second; | |
| 219 } | |
| 220 DCHECK(child_id_ == -1 || session_storage_namespace); | |
| 221 | 218 |
| 222 std::vector<PendingPrerenderInfo> pending_prerender_list; | 219 std::vector<PendingPrerenderInfo> pending_prerender_list; |
| 223 pending_prerender_list.swap(pending_prerenders_); | 220 pending_prerender_list.swap(pending_prerenders_); |
| 224 for (std::vector<PendingPrerenderInfo>::iterator it = | 221 for (std::vector<PendingPrerenderInfo>::iterator it = |
| 225 pending_prerender_list.begin(); | 222 pending_prerender_list.begin(); |
| 226 it != pending_prerender_list.end(); | 223 it != pending_prerender_list.end(); |
| 227 ++it) { | 224 ++it) { |
| 228 if (it->weak_prerender_handle && it->weak_prerender_handle->IsValid()) { | 225 if (it->weak_prerender_handle && it->weak_prerender_handle->IsValid()) { |
| 229 prerender_manager_->StartPendingPrerender( | 226 prerender_manager_->StartPendingPrerender( |
| 230 it->weak_prerender_handle.get(), ORIGIN_LINK_REL_PRERENDER, child_id_, | 227 it->weak_prerender_handle.get(), ORIGIN_LINK_REL_PRERENDER, child_id_, |
| 231 it->url, it->referrer, it->size, session_storage_namespace); | 228 it->url, it->referrer, it->size, session_storage_namespace_map); |
| 232 } | 229 } |
| 233 } | 230 } |
| 234 } | 231 } |
| 235 | 232 |
| 236 PrerenderContents::PendingPrerenderInfo::PendingPrerenderInfo( | 233 PrerenderContents::PendingPrerenderInfo::PendingPrerenderInfo( |
| 237 const base::WeakPtr<PrerenderHandle> weak_prerender_handle, | 234 const base::WeakPtr<PrerenderHandle> weak_prerender_handle, |
| 238 const GURL& url, | 235 const GURL& url, |
| 239 const content::Referrer& referrer, | 236 const content::Referrer& referrer, |
| 240 const gfx::Size& size) | 237 const gfx::Size& size) |
| 241 : weak_prerender_handle(weak_prerender_handle), | 238 : weak_prerender_handle(weak_prerender_handle), |
| (...skipping 13 matching lines...) Expand all Loading... |
| 255 const content::Referrer& referrer, | 252 const content::Referrer& referrer, |
| 256 Origin origin, | 253 Origin origin, |
| 257 uint8 experiment_id) | 254 uint8 experiment_id) |
| 258 : prerendering_has_started_(false), | 255 : prerendering_has_started_(false), |
| 259 prerender_manager_(prerender_manager), | 256 prerender_manager_(prerender_manager), |
| 260 prerender_tracker_(prerender_tracker), | 257 prerender_tracker_(prerender_tracker), |
| 261 prerender_url_(url), | 258 prerender_url_(url), |
| 262 referrer_(referrer), | 259 referrer_(referrer), |
| 263 profile_(profile), | 260 profile_(profile), |
| 264 page_id_(0), | 261 page_id_(0), |
| 265 session_storage_namespace_id_(-1), | |
| 266 has_stopped_loading_(false), | 262 has_stopped_loading_(false), |
| 267 has_finished_loading_(false), | 263 has_finished_loading_(false), |
| 268 final_status_(FINAL_STATUS_MAX), | 264 final_status_(FINAL_STATUS_MAX), |
| 269 match_complete_status_(MATCH_COMPLETE_DEFAULT), | 265 match_complete_status_(MATCH_COMPLETE_DEFAULT), |
| 270 prerendering_has_been_cancelled_(false), | 266 prerendering_has_been_cancelled_(false), |
| 271 child_id_(-1), | 267 child_id_(-1), |
| 272 route_id_(-1), | 268 route_id_(-1), |
| 273 origin_(origin), | 269 origin_(origin), |
| 274 experiment_id_(experiment_id), | 270 experiment_id_(experiment_id), |
| 275 creator_child_id_(-1) { | 271 creator_child_id_(-1) { |
| 276 DCHECK(prerender_manager != NULL); | 272 DCHECK(prerender_manager != NULL); |
| 277 } | 273 } |
| 278 | 274 |
| 279 void PrerenderContents::MakeIntoDummyReplacementOf( | 275 void PrerenderContents::MakeIntoDummyReplacementOf( |
| 280 const PrerenderContents* original_prerender_contents) { | 276 const PrerenderContents* original_prerender_contents) { |
| 281 load_start_time_ = original_prerender_contents->load_start_time_; | 277 load_start_time_ = original_prerender_contents->load_start_time_; |
| 282 session_storage_namespace_id_ = | 278 session_storage_namespace_ids_ = |
| 283 original_prerender_contents->session_storage_namespace_id_; | 279 original_prerender_contents->session_storage_namespace_ids_; |
| 284 } | 280 } |
| 285 | 281 |
| 286 bool PrerenderContents::Init() { | 282 bool PrerenderContents::Init() { |
| 287 return AddAliasURL(prerender_url_); | 283 return AddAliasURL(prerender_url_); |
| 288 } | 284 } |
| 289 | 285 |
| 290 // static | 286 // static |
| 291 PrerenderContents::Factory* PrerenderContents::CreateFactory() { | 287 PrerenderContents::Factory* PrerenderContents::CreateFactory() { |
| 292 return new PrerenderContentsFactoryImpl(); | 288 return new PrerenderContentsFactoryImpl(); |
| 293 } | 289 } |
| 294 | 290 |
| 295 void PrerenderContents::StartPrerendering( | 291 void PrerenderContents::StartPrerendering( |
| 296 int creator_child_id, | 292 int creator_child_id, |
| 297 const gfx::Size& size, | 293 const gfx::Size& size, |
| 298 SessionStorageNamespace* session_storage_namespace, | 294 const SessionStorageNamespaceMap& session_storage_namespace_map, |
| 299 bool is_control_group) { | 295 bool is_control_group) { |
| 300 DCHECK(profile_ != NULL); | 296 DCHECK(profile_ != NULL); |
| 301 DCHECK(!size.IsEmpty()); | 297 DCHECK(!size.IsEmpty()); |
| 302 DCHECK(!prerendering_has_started_); | 298 DCHECK(!prerendering_has_started_); |
| 303 DCHECK(prerender_contents_.get() == NULL); | 299 DCHECK(prerender_contents_.get() == NULL); |
| 304 DCHECK_EQ(-1, creator_child_id_); | 300 DCHECK_EQ(-1, creator_child_id_); |
| 305 DCHECK(size_.IsEmpty()); | 301 DCHECK(size_.IsEmpty()); |
| 306 DCHECK_EQ(1U, alias_urls_.size()); | 302 DCHECK_EQ(1U, alias_urls_.size()); |
| 307 | 303 |
| 308 creator_child_id_ = creator_child_id; | 304 creator_child_id_ = creator_child_id; |
| 309 session_storage_namespace_id_ = session_storage_namespace->id(); | |
| 310 size_ = size; | 305 size_ = size; |
| 306 for (SessionStorageNamespaceMap::const_iterator it = |
| 307 session_storage_namespace_map.begin(); |
| 308 it != session_storage_namespace_map.end(); |
| 309 ++it) { |
| 310 session_storage_namespace_ids_.insert(it->second->id()); |
| 311 } |
| 311 | 312 |
| 312 InformRenderProcessAboutPrerender(prerender_url_, true, | 313 InformRenderProcessAboutPrerender(prerender_url_, true, |
| 313 creator_child_id_); | 314 creator_child_id_); |
| 314 | 315 |
| 315 DCHECK(load_start_time_.is_null()); | 316 DCHECK(load_start_time_.is_null()); |
| 316 load_start_time_ = base::TimeTicks::Now(); | 317 load_start_time_ = base::TimeTicks::Now(); |
| 317 | 318 |
| 318 // Everything after this point sets up the WebContents object and associated | 319 // Everything after this point sets up the WebContents object and associated |
| 319 // RenderView for the prerender page. Don't do this for members of the | 320 // RenderView for the prerender page. Don't do this for members of the |
| 320 // control group. | 321 // control group. |
| 321 if (is_control_group) | 322 if (is_control_group) |
| 322 return; | 323 return; |
| 323 | 324 |
| 324 prerendering_has_started_ = true; | 325 prerendering_has_started_ = true; |
| 325 | 326 |
| 326 WebContents* new_contents = CreateWebContents(session_storage_namespace); | 327 WebContents* new_contents = CreateWebContents(session_storage_namespace_map); |
| 327 prerender_contents_.reset(new TabContents(new_contents)); | 328 prerender_contents_.reset(new TabContents(new_contents)); |
| 328 content::WebContentsObserver::Observe(new_contents); | 329 content::WebContentsObserver::Observe(new_contents); |
| 329 | 330 |
| 330 tab_contents_delegate_.reset(new TabContentsDelegateImpl(this)); | 331 tab_contents_delegate_.reset(new TabContentsDelegateImpl(this)); |
| 331 new_contents->SetDelegate(tab_contents_delegate_.get()); | 332 new_contents->SetDelegate(tab_contents_delegate_.get()); |
| 332 | 333 |
| 333 // Set the size of the prerender WebContents. | 334 // Set the size of the prerender WebContents. |
| 334 prerender_contents_->web_contents()->GetView()->SizeContents(size_); | 335 prerender_contents_->web_contents()->GetView()->SizeContents(size_); |
| 335 | 336 |
| 336 // Register as an observer of the RenderViewHost so we get messages. | 337 // Register as an observer of the RenderViewHost so we get messages. |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 NOTREACHED() << "Unexpected notification sent."; | 497 NOTREACHED() << "Unexpected notification sent."; |
| 497 break; | 498 break; |
| 498 } | 499 } |
| 499 } | 500 } |
| 500 | 501 |
| 501 void PrerenderContents::OnRenderViewHostCreated( | 502 void PrerenderContents::OnRenderViewHostCreated( |
| 502 RenderViewHost* new_render_view_host) { | 503 RenderViewHost* new_render_view_host) { |
| 503 } | 504 } |
| 504 | 505 |
| 505 WebContents* PrerenderContents::CreateWebContents( | 506 WebContents* PrerenderContents::CreateWebContents( |
| 506 SessionStorageNamespace* session_storage_namespace) { | 507 const SessionStorageNamespaceMap& session_storage_namespace_map) { |
| 507 // TODO(ajwong): Remove the temporary map once prerendering is aware of | 508 return WebContents::CreateWithSessionStorage(profile_, NULL, |
| 508 // multiple session storage namespaces per tab. | 509 MSG_ROUTING_NONE, NULL, |
| 509 content::SessionStorageNamespaceMap session_storage_namespace_map; | 510 session_storage_namespace_map); |
| 510 session_storage_namespace_map[""] = session_storage_namespace; | |
| 511 return WebContents::CreateWithSessionStorage( | |
| 512 profile_, NULL, MSG_ROUTING_NONE, NULL, session_storage_namespace_map); | |
| 513 } | 511 } |
| 514 | 512 |
| 515 void PrerenderContents::OnUpdateFaviconURL( | 513 void PrerenderContents::OnUpdateFaviconURL( |
| 516 int32 page_id, | 514 int32 page_id, |
| 517 const std::vector<FaviconURL>& urls) { | 515 const std::vector<FaviconURL>& urls) { |
| 518 VLOG(1) << "PrerenderContents::OnUpdateFaviconURL" << icon_url_; | 516 VLOG(1) << "PrerenderContents::OnUpdateFaviconURL" << icon_url_; |
| 519 for (std::vector<FaviconURL>::const_iterator it = urls.begin(); | 517 for (std::vector<FaviconURL>::const_iterator it = urls.begin(); |
| 520 it != urls.end(); ++it) { | 518 it != urls.end(); ++it) { |
| 521 if (it->icon_type == FaviconURL::FAVICON) { | 519 if (it->icon_type == FaviconURL::FAVICON) { |
| 522 icon_url_ = it->icon_url; | 520 icon_url_ = it->icon_url; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 554 PrerenderContents* other_pc) { | 552 PrerenderContents* other_pc) { |
| 555 for (std::vector<GURL>::const_iterator it = other_pc->alias_urls_.begin(); | 553 for (std::vector<GURL>::const_iterator it = other_pc->alias_urls_.begin(); |
| 556 it != other_pc->alias_urls_.end(); | 554 it != other_pc->alias_urls_.end(); |
| 557 ++it) { | 555 ++it) { |
| 558 alias_urls_.push_back(*it); | 556 alias_urls_.push_back(*it); |
| 559 } | 557 } |
| 560 } | 558 } |
| 561 | 559 |
| 562 bool PrerenderContents::Matches( | 560 bool PrerenderContents::Matches( |
| 563 const GURL& url, | 561 const GURL& url, |
| 564 const SessionStorageNamespace* session_storage_namespace) const { | 562 const SessionStorageNamespaceMap& session_storage_namespace_map) const { |
| 565 DCHECK(child_id_ == -1 || session_storage_namespace); | 563 DCHECK(child_id_ == -1 || !session_storage_namespace_map.empty()); |
| 566 if (session_storage_namespace && | 564 |
| 567 session_storage_namespace_id_ != session_storage_namespace->id()) | 565 // We must have the same session storage objects to match. |
| 566 if (session_storage_namespace_ids_.size() != |
| 567 session_storage_namespace_map.size()) { |
| 568 return false; | 568 return false; |
| 569 } |
| 570 |
| 571 for (SessionStorageNamespaceMap::const_iterator it = |
| 572 session_storage_namespace_map.begin(); |
| 573 it != session_storage_namespace_map.end(); |
| 574 ++it) { |
| 575 if (session_storage_namespace_ids_.find(it->second->id()) == |
| 576 session_storage_namespace_ids_.end()) { |
| 577 return false; |
| 578 } |
| 579 } |
| 580 |
| 569 return std::count_if(alias_urls_.begin(), alias_urls_.end(), | 581 return std::count_if(alias_urls_.begin(), alias_urls_.end(), |
| 570 std::bind2nd(std::equal_to<GURL>(), url)) != 0; | 582 std::bind2nd(std::equal_to<GURL>(), url)) != 0; |
| 571 } | 583 } |
| 572 | 584 |
| 573 void PrerenderContents::RenderViewGone(base::TerminationStatus status) { | 585 void PrerenderContents::RenderViewGone(base::TerminationStatus status) { |
| 574 Destroy(FINAL_STATUS_RENDERER_CRASHED); | 586 Destroy(FINAL_STATUS_RENDERER_CRASHED); |
| 575 } | 587 } |
| 576 | 588 |
| 577 void PrerenderContents::DidStopLoading( | 589 void PrerenderContents::DidStopLoading( |
| 578 content::RenderViewHost* render_view_host) { | 590 content::RenderViewHost* render_view_host) { |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 715 bool PrerenderContents::IsCrossSiteNavigationPending() const { | 727 bool PrerenderContents::IsCrossSiteNavigationPending() const { |
| 716 if (!prerender_contents_.get() || !prerender_contents_->web_contents()) | 728 if (!prerender_contents_.get() || !prerender_contents_->web_contents()) |
| 717 return false; | 729 return false; |
| 718 const WebContents* web_contents = prerender_contents_->web_contents(); | 730 const WebContents* web_contents = prerender_contents_->web_contents(); |
| 719 return (web_contents->GetSiteInstance() != | 731 return (web_contents->GetSiteInstance() != |
| 720 web_contents->GetPendingSiteInstance()); | 732 web_contents->GetPendingSiteInstance()); |
| 721 } | 733 } |
| 722 | 734 |
| 723 | 735 |
| 724 } // namespace prerender | 736 } // namespace prerender |
| OLD | NEW |