| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_RESOURCE_THROTTLE_H_ | 5 #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_RESOURCE_THROTTLE_H_ |
| 6 #define CHROME_BROWSER_PRERENDER_PRERENDER_RESOURCE_THROTTLE_H_ | 6 #define CHROME_BROWSER_PRERENDER_PRERENDER_RESOURCE_THROTTLE_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "content/public/browser/resource_throttle.h" | 11 #include "content/public/browser/resource_throttle.h" |
| 12 #include "webkit/common/resource_type.h" | 12 #include "webkit/common/resource_type.h" |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 class URLRequest; | 15 class URLRequest; |
| 16 } | 16 } |
| 17 | 17 |
| 18 namespace prerender { | 18 namespace prerender { |
| 19 class PrerenderContents; | 19 class PrerenderContents; |
| 20 class PrerenderTracker; | |
| 21 | 20 |
| 22 // This class implements policy on resource requests in prerenders. It cancels | 21 // This class implements policy on resource requests in prerenders. It cancels |
| 23 // prerenders on certain requests. It also defers certain requests until after | 22 // prerenders on certain requests. It also defers certain requests until after |
| 24 // the prerender is swapped in. | 23 // the prerender is swapped in. |
| 25 // | 24 // |
| 26 // TODO(davidben): Experiment with deferring network requests that | 25 // TODO(davidben): Experiment with deferring network requests that |
| 27 // would otherwise cancel the prerender. | 26 // would otherwise cancel the prerender. |
| 28 class PrerenderResourceThrottle | 27 class PrerenderResourceThrottle |
| 29 : public content::ResourceThrottle, | 28 : public content::ResourceThrottle, |
| 30 public base::SupportsWeakPtr<PrerenderResourceThrottle> { | 29 public base::SupportsWeakPtr<PrerenderResourceThrottle> { |
| 31 public: | 30 public: |
| 32 PrerenderResourceThrottle(net::URLRequest* request, | 31 explicit PrerenderResourceThrottle(net::URLRequest* request); |
| 33 PrerenderTracker* tracker); | |
| 34 | 32 |
| 35 // content::ResourceThrottle implementation: | 33 // content::ResourceThrottle implementation: |
| 36 virtual void WillStartRequest(bool* defer) OVERRIDE; | 34 virtual void WillStartRequest(bool* defer) OVERRIDE; |
| 37 virtual void WillRedirectRequest(const GURL& new_url, bool* defer) OVERRIDE; | 35 virtual void WillRedirectRequest(const GURL& new_url, bool* defer) OVERRIDE; |
| 38 virtual const char* GetNameForLogging() const OVERRIDE; | 36 virtual const char* GetNameForLogging() const OVERRIDE; |
| 39 | 37 |
| 40 // Called by the PrerenderTracker when a prerender becomes visible. | 38 // Called by the PrerenderContents when a prerender becomes visible. |
| 41 // May only be called if currently throttling the resource. | 39 // May only be called if currently throttling the resource. |
| 42 void Resume(); | 40 void Resume(); |
| 43 | 41 |
| 44 // Called by the PrerenderTracker when a prerender is destroyed. | |
| 45 // May only be called if currently throttling the resource. | |
| 46 void Cancel(); | |
| 47 | |
| 48 static void OverridePrerenderContentsForTesting(PrerenderContents* contents); | 42 static void OverridePrerenderContentsForTesting(PrerenderContents* contents); |
| 49 | 43 |
| 50 private: | 44 private: |
| 45 // Helper method to cancel the request. May only be called if currently |
| 46 // throttling the resource. |
| 47 void Cancel(); |
| 48 |
| 51 static void WillStartRequestOnUI( | 49 static void WillStartRequestOnUI( |
| 52 const base::WeakPtr<PrerenderResourceThrottle>& throttle, | 50 const base::WeakPtr<PrerenderResourceThrottle>& throttle, |
| 53 const std::string& method, | 51 const std::string& method, |
| 54 int render_process_id, | 52 int render_process_id, |
| 55 int render_frame_id, | 53 int render_frame_id, |
| 56 const GURL& url); | 54 const GURL& url); |
| 57 | 55 |
| 58 static void WillRedirectRequestOnUI( | 56 static void WillRedirectRequestOnUI( |
| 59 const base::WeakPtr<PrerenderResourceThrottle>& throttle, | 57 const base::WeakPtr<PrerenderResourceThrottle>& throttle, |
| 60 const std::string& follow_only_when_prerender_shown_header, | 58 const std::string& follow_only_when_prerender_shown_header, |
| 61 ResourceType::Type resource_type, | 59 ResourceType::Type resource_type, |
| 62 bool async, | 60 bool async, |
| 63 int render_process_id, | 61 int render_process_id, |
| 64 int render_frame_id, | 62 int render_frame_id, |
| 65 const GURL& new_url); | 63 const GURL& new_url); |
| 66 | 64 |
| 67 // Helper to return the PrerenderContents given a render frame id. May return | 65 // Helper to return the PrerenderContents given a render frame id. May return |
| 68 // NULL if it's gone. | 66 // NULL if it's gone. |
| 69 static PrerenderContents* PrerenderContentsFromRenderFrame( | 67 static PrerenderContents* PrerenderContentsFromRenderFrame( |
| 70 int render_process_id, int render_frame_id); | 68 int render_process_id, int render_frame_id); |
| 71 | 69 |
| 72 // Helper method to call tracker_->AddResourceThrottleOnIOThread. | |
| 73 void AddResourceThrottle(); | |
| 74 | |
| 75 net::URLRequest* request_; | 70 net::URLRequest* request_; |
| 76 PrerenderTracker* tracker_; | |
| 77 | 71 |
| 78 DISALLOW_COPY_AND_ASSIGN(PrerenderResourceThrottle); | 72 DISALLOW_COPY_AND_ASSIGN(PrerenderResourceThrottle); |
| 79 }; | 73 }; |
| 80 | 74 |
| 81 } // namespace prerender | 75 } // namespace prerender |
| 82 | 76 |
| 83 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_RESOURCE_THROTTLE_H_ | 77 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_RESOURCE_THROTTLE_H_ |
| OLD | NEW |