OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_RESOURCE_HANDLER_H_ |
| 6 #define CHROME_BROWSER_PRERENDER_PRERENDER_RESOURCE_HANDLER_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <string> |
| 10 |
| 11 #include "base/callback.h" |
| 12 #include "chrome/browser/prerender/prerender_manager.h" |
| 13 #include "chrome/browser/renderer_host/resource_handler.h" |
| 14 |
| 15 class ChromeURLRequestContext; |
| 16 namespace net { |
| 17 class URLRequest; |
| 18 } |
| 19 |
| 20 // The PrerenderResourceHandler initiates prerendering of web pages |
| 21 // under the following conditions: |
| 22 // - The profile which initiated the request allows prerendering. |
| 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. |
| 25 // - The response status code is a 200. |
| 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 { |
| 30 public: |
| 31 // Timer is a simple interface with one method: GetTime(). |
| 32 // It is used so unit tests can provide a simulated Timer for faster |
| 33 // and more consistent behavior. |
| 34 class Timer { |
| 35 public: |
| 36 virtual ~Timer(); |
| 37 |
| 38 // Returns the current time. |
| 39 virtual base::Time GetTime() const = 0; |
| 40 }; |
| 41 |
| 42 // Creates a new PrerenderResourceHandler if appropriate for the |
| 43 // given |request| and |context|, otherwise NULL is returned. The |
| 44 // caller is resposible for deleting the returned handler. |
| 45 // |
| 46 // |next_handler| is the backup handler that this handler delegates to |
| 47 // for the majority of the commands, and must be non-NULL. |
| 48 static PrerenderResourceHandler* MaybeCreate( |
| 49 const net::URLRequest& request, |
| 50 ChromeURLRequestContext* context, |
| 51 ResourceHandler* next_handler); |
| 52 |
| 53 // OnResponseStarted will ask the |prerender_manager_| to start |
| 54 // prerendering the requested resource if it is of an appropriate |
| 55 // content type. The next handler is still invoked. |
| 56 virtual bool OnResponseStarted(int request_id, |
| 57 ResourceResponse* response); |
| 58 |
| 59 // The following methods simply delegate to the next_handler. |
| 60 virtual bool OnUploadProgress(int request_id, |
| 61 uint64 position, |
| 62 uint64 size); |
| 63 virtual bool OnRequestRedirected(int request_id, const GURL& url, |
| 64 ResourceResponse* response, |
| 65 bool* defer); |
| 66 virtual bool OnWillStart(int request_id, const GURL& url, bool* defer); |
| 67 |
| 68 virtual bool OnWillRead(int request_id, |
| 69 net::IOBuffer** buf, |
| 70 int* buf_size, |
| 71 int min_size); |
| 72 |
| 73 virtual bool OnReadCompleted(int request_id, int* bytes_read); |
| 74 |
| 75 virtual bool OnResponseCompleted(int request_id, |
| 76 const URLRequestStatus& status, |
| 77 const std::string& security_info); |
| 78 |
| 79 virtual void OnRequestClosed(); |
| 80 |
| 81 private: |
| 82 friend class PrerenderResourceHandlerTest; |
| 83 typedef Callback1<const GURL&>::Type PrerenderCallback; |
| 84 |
| 85 static const int kDefaultPrerenderDurationSeconds; |
| 86 |
| 87 PrerenderResourceHandler(ResourceHandler* next_handler, |
| 88 PrerenderManager* prerender_manager); |
| 89 PrerenderResourceHandler(ResourceHandler* next_handler, |
| 90 PrerenderCallback* callback); |
| 91 virtual ~PrerenderResourceHandler(); |
| 92 |
| 93 void RunCallbackFromUIThread(const GURL& url); |
| 94 void StartPrerender(const GURL& url); |
| 95 void set_timer(Timer* timer); |
| 96 void set_prerender_duration(base::TimeDelta prerender_duration_); |
| 97 |
| 98 GURL url_; |
| 99 scoped_refptr<ResourceHandler> next_handler_; |
| 100 scoped_refptr<PrerenderManager> prerender_manager_; |
| 101 scoped_ptr<PrerenderCallback> prerender_callback_; |
| 102 scoped_ptr<Timer> timer_; |
| 103 base::TimeDelta prerender_duration_; |
| 104 |
| 105 DISALLOW_COPY_AND_ASSIGN(PrerenderResourceHandler); |
| 106 }; |
| 107 |
| 108 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_RESOURCE_HANDLER_H_ |
OLD | NEW |