prerender_resource_handler.h revision ddb351dbec246cf1fab5ec20d2d5520909041de1
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 "content/browser/renderer_host/resource_handler.h"
14
15class ChromeURLRequestContext;
16namespace net {
17class URLRequest;
18}
19
20namespace prerender {
21
22// The PrerenderResourceHandler initiates prerendering of web pages
23// under the following conditions:
24//   - The profile which initiated the request allows prerendering.
25//   - The initial request is a GET for a PREFETCH resource type.
26//   - The final URL (after redirects) has a scheme of http or https.
27//   - The response status code is a 200.
28//   - The MIME type of the response (sniffed or explicit) is text/html.
29class PrerenderResourceHandler : public ResourceHandler {
30 public:
31  // Creates a new PrerenderResourceHandler if appropriate for the
32  // given |request| and |context|, otherwise NULL is returned. The
33  // caller is resposible for deleting the returned handler.
34  //
35  // |next_handler| is the backup handler that this handler delegates to
36  // for the majority of the commands, and must be non-NULL.
37  static PrerenderResourceHandler* MaybeCreate(
38      const net::URLRequest& request,
39      ChromeURLRequestContext* context,
40      ResourceHandler* next_handler,
41      bool is_from_prerender, int child_id, int route_id);
42
43  // OnResponseStarted will ask the |prerender_manager_| to start
44  // prerendering the requested resource if it is of an appropriate
45  // content type. The next handler is still invoked.
46  virtual bool OnResponseStarted(int request_id,
47                                 ResourceResponse* response);
48
49  // The following methods simply delegate to the next_handler.
50  virtual bool OnUploadProgress(int request_id,
51                                uint64 position,
52                                uint64 size);
53  virtual bool OnRequestRedirected(int request_id, const GURL& url,
54                                   ResourceResponse* response,
55                                   bool* defer);
56  virtual bool OnWillStart(int request_id, const GURL& url, bool* defer);
57
58  virtual bool OnWillRead(int request_id,
59                          net::IOBuffer** buf,
60                          int* buf_size,
61                          int min_size);
62
63  virtual bool OnReadCompleted(int request_id, int* bytes_read);
64
65  virtual bool OnResponseCompleted(int request_id,
66                                   const net::URLRequestStatus& status,
67                                   const std::string& security_info);
68
69  virtual void OnRequestClosed();
70
71 private:
72  friend class PrerenderResourceHandlerTest;
73  typedef Callback5<const std::pair<int, int>&,
74                    const GURL&,
75                    const std::vector<GURL>&,
76                    const GURL&,
77                    bool>::Type PrerenderCallback;
78
79  PrerenderResourceHandler(const net::URLRequest& request,
80                           ResourceHandler* next_handler,
81                           PrerenderManager* prerender_manager,
82                           bool make_pending, int child_id, int route_id);
83
84  // This constructor is only used from unit tests.
85  PrerenderResourceHandler(const net::URLRequest& request,
86                           ResourceHandler* next_handler,
87                           PrerenderCallback* callback);
88
89  virtual ~PrerenderResourceHandler();
90
91  void RunCallbackFromUIThread(const std::pair<int, int>& child_route_id_pair,
92                               const GURL& url,
93                               const std::vector<GURL>& alias_urls,
94                               const GURL& referrer,
95                               bool make_pending);
96  void StartPrerender(const std::pair<int, int>& child_route_id_pair,
97                      const GURL& url,
98                      const std::vector<GURL>& alias_urls,
99                      const GURL& referrer,
100                      bool make_pending);
101
102  // The set of URLs that are aliases to the URL to be prerendered,
103  // as a result of redirects, including the final URL.
104  std::vector<GURL> alias_urls_;
105  GURL url_;
106  scoped_refptr<ResourceHandler> next_handler_;
107  scoped_refptr<PrerenderManager> prerender_manager_;
108  scoped_ptr<PrerenderCallback> prerender_callback_;
109
110  // Used to obtain the referrer, but only after any redirections occur, as they
111  // can result in the referrer being cleared.
112  const net::URLRequest& request_;
113
114  int child_id_;
115  int route_id_;
116
117  // True if we want to make this a pending prerender for later
118  bool make_pending_;
119
120  DISALLOW_COPY_AND_ASSIGN(PrerenderResourceHandler);
121};
122
123}  // namespace prerender
124
125#endif  // CHROME_BROWSER_PRERENDER_PRERENDER_RESOURCE_HANDLER_H_
126