chrome_resource_dispatcher_host_delegate.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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 "base/metrics/field_trial.h"
13#include "chrome/common/metrics/variations/variation_ids.h"
14#include "content/public/browser/resource_dispatcher_host_delegate.h"
15
16class DelayedResourceQueue;
17class DownloadRequestLimiter;
18class SafeBrowsingService;
19
20namespace extensions {
21class UserScriptListener;
22}
23
24namespace prerender {
25class PrerenderTracker;
26}
27
28// Implements ResourceDispatcherHostDelegate. Currently used by the Prerender
29// system to abort requests and add to the load flags when a request begins.
30class ChromeResourceDispatcherHostDelegate
31    : public content::ResourceDispatcherHostDelegate,
32      public base::FieldTrialList::Observer {
33 public:
34  // This class does not take ownership of the tracker but merely holds a
35  // reference to it to avoid accessing g_browser_process.
36  // |prerender_tracker| must outlive |this|.
37  explicit ChromeResourceDispatcherHostDelegate(
38      prerender::PrerenderTracker* prerender_tracker);
39  virtual ~ChromeResourceDispatcherHostDelegate();
40
41  // ResourceDispatcherHostDelegate implementation.
42  virtual bool ShouldBeginRequest(
43      int child_id,
44      int route_id,
45      const std::string& method,
46      const GURL& url,
47      ResourceType::Type resource_type,
48      content::ResourceContext* resource_context,
49      const content::Referrer& referrer) OVERRIDE;
50  virtual void RequestBeginning(
51      net::URLRequest* request,
52      content::ResourceContext* resource_context,
53      appcache::AppCacheService* appcache_service,
54      ResourceType::Type resource_type,
55      int child_id,
56      int route_id,
57      bool is_continuation_of_transferred_request,
58      ScopedVector<content::ResourceThrottle>* throttles) OVERRIDE;
59  virtual void DownloadStarting(
60      net::URLRequest* request,
61      content::ResourceContext* resource_context,
62      int child_id,
63      int route_id,
64      int request_id,
65      bool is_content_initiated,
66      ScopedVector<content::ResourceThrottle>* throttles) OVERRIDE;
67  virtual bool AcceptSSLClientCertificateRequest(
68        net::URLRequest* request,
69        net::SSLCertRequestInfo* cert_request_info) OVERRIDE;
70  virtual bool AcceptAuthRequest(net::URLRequest* request,
71                                 net::AuthChallengeInfo* auth_info) OVERRIDE;
72  virtual content::ResourceDispatcherHostLoginDelegate* CreateLoginDelegate(
73      net::AuthChallengeInfo* auth_info, net::URLRequest* request) OVERRIDE;
74  virtual bool HandleExternalProtocol(const GURL& url,
75                                      int child_id,
76                                      int route_id) OVERRIDE;
77  virtual bool ShouldForceDownloadResource(
78      const GURL& url, const std::string& mime_type) OVERRIDE;
79  virtual void OnResponseStarted(
80      net::URLRequest* request,
81      content::ResourceContext* resource_context,
82      content::ResourceResponse* response,
83      IPC::Sender* sender) OVERRIDE;
84  virtual void OnRequestRedirected(
85      const GURL& redirect_url,
86      net::URLRequest* request,
87      content::ResourceContext* resource_context,
88      content::ResourceResponse* response) OVERRIDE;
89
90  // base::FieldTrialList::Observer implementation.
91  // This will add the variation ID associated with |trial_name| and
92  // |group_name| to the variation ID cache.
93  virtual void OnFieldTrialGroupFinalized(
94      const std::string& trial_name,
95      const std::string& group_name) OVERRIDE;
96
97 private:
98  void AppendStandardResourceThrottles(
99      const net::URLRequest* request,
100      content::ResourceContext* resource_context,
101      int child_id,
102      int route_id,
103      ResourceType::Type resource_type,
104      ScopedVector<content::ResourceThrottle>* throttles);
105
106  // Adds Chrome experiment and metrics state as custom headers to |request|.
107  // This is a best-effort attempt, and does not interrupt the request if the
108  // headers can not be appended.
109  void AppendChromeMetricsHeaders(
110      net::URLRequest* request,
111      content::ResourceContext* resource_context,
112      ResourceType::Type resource_type);
113
114#if defined(ENABLE_ONE_CLICK_SIGNIN)
115  // Append headers required to tell Gaia whether the sync interstitial
116  // should be shown or not.  This header is only added for valid Gaia URLs.
117  void AppendChromeSyncGaiaHeader(
118      net::URLRequest* request,
119      content::ResourceContext* resource_context);
120#endif
121
122  // Prepares the variation IDs cache with initial values if not already done.
123  // This method also registers the caller with the FieldTrialList to receive
124  // new variation IDs.
125  void InitVariationIDsCacheIfNeeded();
126
127  // Takes whatever is currently in |variation_ids_set_| and recreates
128  // |variation_ids_header_| with it.
129  void UpdateVariationIDsHeaderValue();
130
131  scoped_refptr<DownloadRequestLimiter> download_request_limiter_;
132  scoped_refptr<SafeBrowsingService> safe_browsing_;
133  scoped_refptr<extensions::UserScriptListener> user_script_listener_;
134  prerender::PrerenderTracker* prerender_tracker_;
135
136  // Whether or not we've initialized the Cache.
137  bool variation_ids_cache_initialized_;
138
139  // Keep a cache of variation IDs that are transmitted in headers to Google.
140  // This consists of a list of valid IDs, and the actual transmitted header.
141  // Note that since this cache is both initialized and accessed from the IO
142  // thread, we do not need to synchronize its uses.
143  std::set<chrome_variations::VariationID> variation_ids_set_;
144  std::string variation_ids_header_;
145
146  DISALLOW_COPY_AND_ASSIGN(ChromeResourceDispatcherHostDelegate);
147};
148
149#endif  // CHROME_BROWSER_RENDERER_HOST_CHROME_RESOURCE_DISPATCHER_HOST_DELEGATE_H_
150