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 <map>
9#include <set>
10
11#include "base/compiler_specific.h"
12#include "base/memory/ref_counted.h"
13#include "chrome/browser/external_protocol/external_protocol_handler.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:
33  // This class does not take ownership of the tracker but merely holds a
34  // reference to it to avoid accessing g_browser_process.
35  // |prerender_tracker| must outlive |this|.
36  explicit ChromeResourceDispatcherHostDelegate(
37      prerender::PrerenderTracker* prerender_tracker);
38  virtual ~ChromeResourceDispatcherHostDelegate();
39
40  // ResourceDispatcherHostDelegate implementation.
41  virtual bool ShouldBeginRequest(
42      const std::string& method,
43      const GURL& url,
44      content::ResourceType resource_type,
45      content::ResourceContext* resource_context) OVERRIDE;
46  virtual void RequestBeginning(
47      net::URLRequest* request,
48      content::ResourceContext* resource_context,
49      content::AppCacheService* appcache_service,
50      content::ResourceType resource_type,
51      ScopedVector<content::ResourceThrottle>* throttles) OVERRIDE;
52  virtual void DownloadStarting(
53      net::URLRequest* request,
54      content::ResourceContext* resource_context,
55      int child_id,
56      int route_id,
57      int request_id,
58      bool is_content_initiated,
59      bool must_download,
60      ScopedVector<content::ResourceThrottle>* throttles) OVERRIDE;
61  virtual content::ResourceDispatcherHostLoginDelegate* CreateLoginDelegate(
62      net::AuthChallengeInfo* auth_info, net::URLRequest* request) OVERRIDE;
63  virtual bool HandleExternalProtocol(const GURL& url,
64                                      int child_id,
65                                      int route_id) OVERRIDE;
66  virtual bool ShouldForceDownloadResource(
67      const GURL& url, const std::string& mime_type) OVERRIDE;
68  virtual bool ShouldInterceptResourceAsStream(
69      net::URLRequest* request,
70      const std::string& mime_type,
71      GURL* origin,
72      std::string* payload) OVERRIDE;
73  virtual void OnStreamCreated(
74      net::URLRequest* request,
75      scoped_ptr<content::StreamHandle> stream) OVERRIDE;
76  virtual void OnResponseStarted(
77      net::URLRequest* request,
78      content::ResourceContext* resource_context,
79      content::ResourceResponse* response,
80      IPC::Sender* sender) OVERRIDE;
81  virtual void OnRequestRedirected(
82      const GURL& redirect_url,
83      net::URLRequest* request,
84      content::ResourceContext* resource_context,
85      content::ResourceResponse* response) OVERRIDE;
86  virtual void RequestComplete(net::URLRequest* url_request) OVERRIDE;
87
88  // Called on the UI thread. Allows switching out the
89  // ExternalProtocolHandler::Delegate for testing code.
90  static void SetExternalProtocolHandlerDelegateForTesting(
91      ExternalProtocolHandler::Delegate* delegate);
92
93 private:
94#if defined(ENABLE_EXTENSIONS)
95  struct StreamTargetInfo {
96    std::string extension_id;
97    std::string view_id;
98  };
99#endif
100
101  void AppendStandardResourceThrottles(
102      net::URLRequest* request,
103      content::ResourceContext* resource_context,
104      content::ResourceType resource_type,
105      ScopedVector<content::ResourceThrottle>* throttles);
106
107#if defined(ENABLE_ONE_CLICK_SIGNIN)
108  // Append headers required to tell Gaia whether the sync interstitial
109  // should be shown or not.  This header is only added for valid Gaia URLs.
110  void AppendChromeSyncGaiaHeader(
111      net::URLRequest* request,
112      content::ResourceContext* resource_context);
113#endif
114
115  scoped_refptr<DownloadRequestLimiter> download_request_limiter_;
116  scoped_refptr<SafeBrowsingService> safe_browsing_;
117#if defined(ENABLE_EXTENSIONS)
118  scoped_refptr<extensions::UserScriptListener> user_script_listener_;
119  std::map<net::URLRequest*, StreamTargetInfo> stream_target_info_;
120#endif
121  prerender::PrerenderTracker* prerender_tracker_;
122
123  DISALLOW_COPY_AND_ASSIGN(ChromeResourceDispatcherHostDelegate);
124};
125
126#endif  // CHROME_BROWSER_RENDERER_HOST_CHROME_RESOURCE_DISPATCHER_HOST_DELEGATE_H_
127