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
15using content::ResourceType;
16
17namespace prerender {
18
19PrerenderPendingSwapThrottle::PrerenderPendingSwapThrottle(
20    net::URLRequest* request,
21    PrerenderTracker* tracker)
22    : request_(request),
23      tracker_(tracker),
24      throttled_(false) {
25}
26
27void PrerenderPendingSwapThrottle::WillStartRequest(bool* defer) {
28  DCHECK(!throttled_);
29  const content::ResourceRequestInfo* info =
30      content::ResourceRequestInfo::ForRequest(request_);
31
32  // We only care about main frame loads.
33  if (info->GetResourceType() != content::RESOURCE_TYPE_MAIN_FRAME)
34    return;
35
36  int render_process_id = info->GetChildID();
37  int render_frame_id = info->GetRenderFrameID();
38
39  // Check if this request is for a URL we intend to swap in.
40  if (!tracker_->IsPendingSwapRequestOnIOThread(
41          render_process_id, render_frame_id, request_->url())) {
42    return;
43  }
44
45  *defer = true;
46  throttled_ = true;
47  tracker_->AddPendingSwapThrottleOnIOThread(
48      render_process_id, render_frame_id, request_->url(), this->AsWeakPtr());
49}
50
51const char* PrerenderPendingSwapThrottle::GetNameForLogging() const {
52  return "PrerenderPendingSwapThrottle";
53}
54
55void PrerenderPendingSwapThrottle::Resume() {
56  DCHECK(throttled_);
57
58  throttled_ = false;
59  controller()->Resume();
60}
61
62void PrerenderPendingSwapThrottle::Cancel() {
63  DCHECK(throttled_);
64
65  throttled_ = false;
66  controller()->Cancel();
67}
68
69}  // namespace prerender
70