• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
PrerenderPendingSwapThrottle(net::URLRequest * request,PrerenderTracker * tracker)17 PrerenderPendingSwapThrottle::PrerenderPendingSwapThrottle(
18     net::URLRequest* request,
19     PrerenderTracker* tracker)
20     : request_(request),
21       tracker_(tracker),
22       throttled_(false) {
23 }
24 
WillStartRequest(bool * defer)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 render_process_id = info->GetChildID();
35   int render_frame_id = info->GetRenderFrameID();
36 
37   // Check if this request is for a URL we intend to swap in.
38   if (!tracker_->IsPendingSwapRequestOnIOThread(
39           render_process_id, render_frame_id, request_->url())) {
40     return;
41   }
42 
43   *defer = true;
44   throttled_ = true;
45   tracker_->AddPendingSwapThrottleOnIOThread(
46       render_process_id, render_frame_id, request_->url(), this->AsWeakPtr());
47 }
48 
GetNameForLogging() const49 const char* PrerenderPendingSwapThrottle::GetNameForLogging() const {
50   return "PrerenderPendingSwapThrottle";
51 }
52 
Resume()53 void PrerenderPendingSwapThrottle::Resume() {
54   DCHECK(throttled_);
55 
56   throttled_ = false;
57   controller()->Resume();
58 }
59 
Cancel()60 void PrerenderPendingSwapThrottle::Cancel() {
61   DCHECK(throttled_);
62 
63   throttled_ = false;
64   controller()->Cancel();
65 }
66 
67 }  // namespace prerender
68