1 // Copyright 2013 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. 4 5 #include "chrome/browser/prerender/prerender_pending_swap_throttle.h" 6 7 #include "chrome/browser/prerender/prerender_final_status.h" 8 #include "chrome/browser/prerender/prerender_manager.h" 9 #include "chrome/browser/prerender/prerender_tracker.h" 10 #include "chrome/browser/prerender/prerender_util.h" 11 #include "content/public/browser/resource_controller.h" 12 #include "content/public/browser/resource_request_info.h" 13 #include "net/url_request/url_request.h" 14 15 namespace prerender { 16 17 PrerenderPendingSwapThrottle::PrerenderPendingSwapThrottle( 18 net::URLRequest* request, 19 PrerenderTracker* tracker) 20 : request_(request), 21 tracker_(tracker), 22 throttled_(false) { 23 } 24 25 void PrerenderPendingSwapThrottle::WillStartRequest(bool* defer) { 26 DCHECK(!throttled_); 27 const content::ResourceRequestInfo* info = 28 content::ResourceRequestInfo::ForRequest(request_); 29 30 // We only care about main frame loads. 31 if (info->GetResourceType() != ResourceType::MAIN_FRAME) 32 return; 33 34 int child_id = info->GetChildID(); 35 int route_id = info->GetRouteID(); 36 37 // Check if this request is for a URL we intend to swap in. 38 if (!tracker_->IsPendingSwapRequestOnIOThread(child_id, route_id, 39 request_->url())) { 40 return; 41 } 42 43 *defer = true; 44 throttled_ = true; 45 tracker_->AddPendingSwapThrottleOnIOThread(child_id, route_id, 46 request_->url(), 47 this->AsWeakPtr()); 48 } 49 50 const char* PrerenderPendingSwapThrottle::GetNameForLogging() const { 51 return "PrerenderPendingSwapThrottle"; 52 } 53 54 void PrerenderPendingSwapThrottle::Resume() { 55 DCHECK(throttled_); 56 57 throttled_ = false; 58 controller()->Resume(); 59 } 60 61 void PrerenderPendingSwapThrottle::Cancel() { 62 DCHECK(throttled_); 63 64 throttled_ = false; 65 controller()->Cancel(); 66 } 67 68 } // namespace prerender 69