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 #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_RESOURCE_THROTTLE_H_ 6 #define CHROME_BROWSER_PRERENDER_PRERENDER_RESOURCE_THROTTLE_H_ 7 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 "content/public/common/resource_type.h" 13 14 namespace net { 15 class URLRequest; 16 } 17 18 namespace prerender { 19 class PrerenderContents; 20 21 // This class implements policy on resource requests in prerenders. It cancels 22 // prerenders on certain requests. It also defers certain requests until after 23 // the prerender is swapped in. 24 // 25 // TODO(davidben): Experiment with deferring network requests that 26 // would otherwise cancel the prerender. 27 class PrerenderResourceThrottle 28 : public content::ResourceThrottle, 29 public base::SupportsWeakPtr<PrerenderResourceThrottle> { 30 public: 31 explicit PrerenderResourceThrottle(net::URLRequest* request); 32 33 // content::ResourceThrottle implementation: 34 virtual void WillStartRequest(bool* defer) OVERRIDE; 35 virtual void WillRedirectRequest(const GURL& new_url, bool* defer) OVERRIDE; 36 virtual const char* GetNameForLogging() const OVERRIDE; 37 38 // Called by the PrerenderContents when a prerender becomes visible. 39 // May only be called if currently throttling the resource. 40 void Resume(); 41 42 static void OverridePrerenderContentsForTesting(PrerenderContents* contents); 43 44 private: 45 // Helper method to cancel the request. May only be called if currently 46 // throttling the resource. 47 void Cancel(); 48 49 static void WillStartRequestOnUI( 50 const base::WeakPtr<PrerenderResourceThrottle>& throttle, 51 const std::string& method, 52 content::ResourceType resource_type, 53 int render_process_id, 54 int render_frame_id, 55 const GURL& url); 56 57 static void WillRedirectRequestOnUI( 58 const base::WeakPtr<PrerenderResourceThrottle>& throttle, 59 const std::string& follow_only_when_prerender_shown_header, 60 content::ResourceType resource_type, 61 bool async, 62 int render_process_id, 63 int render_frame_id, 64 const GURL& new_url); 65 66 // Helper to return the PrerenderContents given a render frame id. May return 67 // NULL if it's gone. 68 static PrerenderContents* PrerenderContentsFromRenderFrame( 69 int render_process_id, int render_frame_id); 70 71 net::URLRequest* request_; 72 73 DISALLOW_COPY_AND_ASSIGN(PrerenderResourceThrottle); 74 }; 75 76 } // namespace prerender 77 78 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_RESOURCE_THROTTLE_H_ 79