OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_HANDLER_H_ | 5 #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_RESOURCE_HANDLER_H_ |
6 #define CHROME_BROWSER_PRERENDER_PRERENDER_RESOURCE_HANDLER_H_ | 6 #define CHROME_BROWSER_PRERENDER_PRERENDER_RESOURCE_HANDLER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "chrome/browser/prerender/prerender_manager.h" | 12 #include "chrome/browser/prerender/prerender_manager.h" |
13 #include "chrome/browser/renderer_host/resource_handler.h" | 13 #include "chrome/browser/renderer_host/resource_handler.h" |
14 | 14 |
15 class ChromeURLRequestContext; | 15 class ChromeURLRequestContext; |
16 namespace net { | 16 namespace net { |
17 class URLRequest; | 17 class URLRequest; |
18 } | 18 } |
19 | 19 |
20 // The PrerenderResourceHandler initiates prerendering of web pages | 20 // The PrerenderResourceHandler initiates prerendering of web pages |
21 // under the following conditions: | 21 // under the following conditions: |
22 // - The profile which initiated the request allows prerendering. | 22 // - The profile which initiated the request allows prerendering. |
23 // - The initial request is a GET for a PREFETCH resource type. | 23 // - The initial request is a GET for a PREFETCH resource type. |
24 // - The final URL (after redirects) has a scheme of http or https. | 24 // - The final URL (after redirects) has a scheme of http or https. |
25 // - The response status code is a 200. | 25 // - The response status code is a 200. |
26 // - The MIME type of the response (sniffed or explicit) is text/html. | 26 // - The MIME type of the response (sniffed or explicit) is text/html. |
27 // - The resource will not need to revalidate within 30 seconds. This prevents | |
28 // no-cache or very quickly refreshing resources from being prerendered. | |
29 class PrerenderResourceHandler : public ResourceHandler { | 27 class PrerenderResourceHandler : public ResourceHandler { |
30 public: | 28 public: |
31 typedef base::Time (*GetCurrentTimeFunction)(); | |
32 | |
33 // Creates a new PrerenderResourceHandler if appropriate for the | 29 // Creates a new PrerenderResourceHandler if appropriate for the |
34 // given |request| and |context|, otherwise NULL is returned. The | 30 // given |request| and |context|, otherwise NULL is returned. The |
35 // caller is resposible for deleting the returned handler. | 31 // caller is resposible for deleting the returned handler. |
36 // | 32 // |
37 // |next_handler| is the backup handler that this handler delegates to | 33 // |next_handler| is the backup handler that this handler delegates to |
38 // for the majority of the commands, and must be non-NULL. | 34 // for the majority of the commands, and must be non-NULL. |
39 static PrerenderResourceHandler* MaybeCreate( | 35 static PrerenderResourceHandler* MaybeCreate( |
40 const net::URLRequest& request, | 36 const net::URLRequest& request, |
41 ChromeURLRequestContext* context, | 37 ChromeURLRequestContext* context, |
42 ResourceHandler* next_handler); | 38 ResourceHandler* next_handler); |
(...skipping 24 matching lines...) Expand all Loading... |
67 const net::URLRequestStatus& status, | 63 const net::URLRequestStatus& status, |
68 const std::string& security_info); | 64 const std::string& security_info); |
69 | 65 |
70 virtual void OnRequestClosed(); | 66 virtual void OnRequestClosed(); |
71 | 67 |
72 private: | 68 private: |
73 friend class PrerenderResourceHandlerTest; | 69 friend class PrerenderResourceHandlerTest; |
74 typedef Callback2<const GURL&, const std::vector<GURL>&>::Type | 70 typedef Callback2<const GURL&, const std::vector<GURL>&>::Type |
75 PrerenderCallback; | 71 PrerenderCallback; |
76 | 72 |
77 static const int kDefaultPrerenderDurationSeconds; | |
78 | |
79 PrerenderResourceHandler(ResourceHandler* next_handler, | 73 PrerenderResourceHandler(ResourceHandler* next_handler, |
80 PrerenderManager* prerender_manager); | 74 PrerenderManager* prerender_manager); |
81 PrerenderResourceHandler(ResourceHandler* next_handler, | 75 PrerenderResourceHandler(ResourceHandler* next_handler, |
82 PrerenderCallback* callback); | 76 PrerenderCallback* callback); |
83 virtual ~PrerenderResourceHandler(); | 77 virtual ~PrerenderResourceHandler(); |
84 | 78 |
85 void RunCallbackFromUIThread(const GURL& url, | 79 void RunCallbackFromUIThread(const GURL& url, |
86 const std::vector<GURL>& alias_urls); | 80 const std::vector<GURL>& alias_urls); |
87 void StartPrerender(const GURL& url, | 81 void StartPrerender(const GURL& url, |
88 const std::vector<GURL>& alias_urls); | 82 const std::vector<GURL>& alias_urls); |
89 void set_prerender_duration(base::TimeDelta prerender_duration); | |
90 void set_get_current_time_function(GetCurrentTimeFunction get_current_time); | |
91 | 83 |
92 // The set of URLs that are aliases to the URL to be prerendered, | 84 // The set of URLs that are aliases to the URL to be prerendered, |
93 // as a result of redirects. | 85 // as a result of redirects. |
94 std::vector<GURL> alias_urls_; | 86 std::vector<GURL> alias_urls_; |
95 GURL url_; | 87 GURL url_; |
96 scoped_refptr<ResourceHandler> next_handler_; | 88 scoped_refptr<ResourceHandler> next_handler_; |
97 scoped_refptr<PrerenderManager> prerender_manager_; | 89 scoped_refptr<PrerenderManager> prerender_manager_; |
98 scoped_ptr<PrerenderCallback> prerender_callback_; | 90 scoped_ptr<PrerenderCallback> prerender_callback_; |
99 base::TimeDelta prerender_duration_; | |
100 GetCurrentTimeFunction get_current_time_; | |
101 | 91 |
102 DISALLOW_COPY_AND_ASSIGN(PrerenderResourceHandler); | 92 DISALLOW_COPY_AND_ASSIGN(PrerenderResourceHandler); |
103 }; | 93 }; |
104 | 94 |
105 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_RESOURCE_HANDLER_H_ | 95 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_RESOURCE_HANDLER_H_ |
OLD | NEW |