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.
5 #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_RESOURCE_THROTTLE_H_
6 #define CHROME_BROWSER_PRERENDER_PRERENDER_RESOURCE_THROTTLE_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/weak_ptr.h"
11 #include "content/public/browser/resource_throttle.h"
12 #include "webkit/common/resource_type.h"
19 class PrerenderContents
;
20 class PrerenderTracker
;
22 // This class implements policy on resource requests in prerenders. It cancels
23 // prerenders on certain requests. It also defers certain requests until after
24 // the prerender is swapped in.
26 // TODO(davidben): Experiment with deferring network requests that
27 // would otherwise cancel the prerender.
28 class PrerenderResourceThrottle
29 : public content::ResourceThrottle
,
30 public base::SupportsWeakPtr
<PrerenderResourceThrottle
> {
32 PrerenderResourceThrottle(net::URLRequest
* request
,
33 PrerenderTracker
* tracker
);
35 // content::ResourceThrottle implementation:
36 virtual void WillStartRequest(bool* defer
) OVERRIDE
;
37 virtual void WillRedirectRequest(const GURL
& new_url
, bool* defer
) OVERRIDE
;
38 virtual const char* GetNameForLogging() const OVERRIDE
;
40 // Called by the PrerenderTracker when a prerender becomes visible.
41 // May only be called if currently throttling the resource.
44 // Called by the PrerenderTracker when a prerender is destroyed.
45 // May only be called if currently throttling the resource.
48 static void OverridePrerenderContentsForTesting(PrerenderContents
* contents
);
51 static void WillStartRequestOnUI(
52 const base::WeakPtr
<PrerenderResourceThrottle
>& throttle
,
53 const std::string
& method
,
54 int render_process_id
,
58 static void WillRedirectRequestOnUI(
59 const base::WeakPtr
<PrerenderResourceThrottle
>& throttle
,
60 const std::string
& follow_only_when_prerender_shown_header
,
61 ResourceType::Type resource_type
,
63 int render_process_id
,
67 // Helper to return the PrerenderContents given a render frame id. May return
69 static PrerenderContents
* PrerenderContentsFromRenderFrame(
70 int render_process_id
, int render_frame_id
);
72 // Helper method to call tracker_->AddResourceThrottleOnIOThread.
73 void AddResourceThrottle();
75 net::URLRequest
* request_
;
76 PrerenderTracker
* tracker_
;
78 DISALLOW_COPY_AND_ASSIGN(PrerenderResourceThrottle
);
81 } // namespace prerender
83 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_RESOURCE_THROTTLE_H_