chrome_resource_dispatcher_host_delegate.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1// Copyright (c) 2012 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_RENDERER_HOST_CHROME_RESOURCE_DISPATCHER_HOST_DELEGATE_H_
6#define CHROME_BROWSER_RENDERER_HOST_CHROME_RESOURCE_DISPATCHER_HOST_DELEGATE_H_
7
8#include <set>
9
10#include "base/compiler_specific.h"
11#include "base/memory/ref_counted.h"
12#include "chrome/browser/external_protocol/external_protocol_handler.h"
13#include "content/public/browser/resource_dispatcher_host_delegate.h"
14
15class DelayedResourceQueue;
16class DownloadRequestLimiter;
17class SafeBrowsingService;
18
19namespace extensions {
20class UserScriptListener;
21}
22
23namespace prerender {
24class PrerenderTracker;
25}
26
27// Implements ResourceDispatcherHostDelegate. Currently used by the Prerender
28// system to abort requests and add to the load flags when a request begins.
29class ChromeResourceDispatcherHostDelegate
30    : public content::ResourceDispatcherHostDelegate {
31 public:
32  // This class does not take ownership of the tracker but merely holds a
33  // reference to it to avoid accessing g_browser_process.
34  // |prerender_tracker| must outlive |this|.
35  explicit ChromeResourceDispatcherHostDelegate(
36      prerender::PrerenderTracker* prerender_tracker);
37  virtual ~ChromeResourceDispatcherHostDelegate();
38
39  // ResourceDispatcherHostDelegate implementation.
40  virtual bool ShouldBeginRequest(
41      int child_id,
42      int route_id,
43      const std::string& method,
44      const GURL& url,
45      ResourceType::Type resource_type,
46      content::ResourceContext* resource_context) OVERRIDE;
47  virtual void RequestBeginning(
48      net::URLRequest* request,
49      content::ResourceContext* resource_context,
50      appcache::AppCacheService* appcache_service,
51      ResourceType::Type resource_type,
52      int child_id,
53      int route_id,
54      ScopedVector<content::ResourceThrottle>* throttles) OVERRIDE;
55  virtual void DownloadStarting(
56      net::URLRequest* request,
57      content::ResourceContext* resource_context,
58      int child_id,
59      int route_id,
60      int request_id,
61      bool is_content_initiated,
62      bool must_download,
63      ScopedVector<content::ResourceThrottle>* throttles) OVERRIDE;
64  virtual content::ResourceDispatcherHostLoginDelegate* CreateLoginDelegate(
65      net::AuthChallengeInfo* auth_info, net::URLRequest* request) OVERRIDE;
66  virtual bool HandleExternalProtocol(const GURL& url,
67                                      int child_id,
68                                      int route_id) OVERRIDE;
69  virtual bool ShouldForceDownloadResource(
70      const GURL& url, const std::string& mime_type) OVERRIDE;
71  virtual bool ShouldInterceptResourceAsStream(
72      content::ResourceContext* resource_context,
73      const GURL& url,
74      const std::string& mime_type,
75      GURL* origin,
76      std::string* target_id) OVERRIDE;
77  virtual void OnStreamCreated(
78      content::ResourceContext* resource_context,
79      int render_process_id,
80      int render_view_id,
81      const std::string& target_id,
82      scoped_ptr<content::StreamHandle> stream,
83      int64 expected_content_size) OVERRIDE;
84  virtual void OnResponseStarted(
85      net::URLRequest* request,
86      content::ResourceContext* resource_context,
87      content::ResourceResponse* response,
88      IPC::Sender* sender) OVERRIDE;
89  virtual void OnRequestRedirected(
90      const GURL& redirect_url,
91      net::URLRequest* request,
92      content::ResourceContext* resource_context,
93      content::ResourceResponse* response) OVERRIDE;
94  virtual void RequestComplete(net::URLRequest* url_request) OVERRIDE;
95
96  // Called on the UI thread. Allows switching out the
97  // ExternalProtocolHandler::Delegate for testing code.
98  static void SetExternalProtocolHandlerDelegateForTesting(
99      ExternalProtocolHandler::Delegate* delegate);
100
101 private:
102  void AppendStandardResourceThrottles(
103      net::URLRequest* request,
104      content::ResourceContext* resource_context,
105      ResourceType::Type resource_type,
106      ScopedVector<content::ResourceThrottle>* throttles);
107
108  // Adds Chrome experiment and metrics state as custom headers to |request|.
109  // This is a best-effort attempt, and does not interrupt the request if the
110  // headers can not be appended.
111  void AppendChromeMetricsHeaders(
112      net::URLRequest* request,
113      content::ResourceContext* resource_context,
114      ResourceType::Type resource_type);
115
116#if defined(ENABLE_ONE_CLICK_SIGNIN)
117  // Append headers required to tell Gaia whether the sync interstitial
118  // should be shown or not.  This header is only added for valid Gaia URLs.
119  void AppendChromeSyncGaiaHeader(
120      net::URLRequest* request,
121      content::ResourceContext* resource_context);
122#endif
123
124  scoped_refptr<DownloadRequestLimiter> download_request_limiter_;
125  scoped_refptr<SafeBrowsingService> safe_browsing_;
126  scoped_refptr<extensions::UserScriptListener> user_script_listener_;
127  prerender::PrerenderTracker* prerender_tracker_;
128
129  DISALLOW_COPY_AND_ASSIGN(ChromeResourceDispatcherHostDelegate);
130};
131
132#endif  // CHROME_BROWSER_RENDERER_HOST_CHROME_RESOURCE_DISPATCHER_HOST_DELEGATE_H_
133