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 CONTENT_PUBLIC_BROWSER_RESOURCE_DISPATCHER_HOST_DELEGATE_H_
6#define CONTENT_PUBLIC_BROWSER_RESOURCE_DISPATCHER_HOST_DELEGATE_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/memory/scoped_ptr.h"
12#include "content/common/content_export.h"
13#include "content/public/common/resource_type.h"
14
15class GURL;
16template <class T> class ScopedVector;
17
18namespace IPC {
19class Sender;
20}
21
22namespace net {
23class AuthChallengeInfo;
24class URLRequest;
25}
26
27namespace content {
28
29class AppCacheService;
30class ResourceContext;
31class ResourceDispatcherHostLoginDelegate;
32class ResourceThrottle;
33class StreamHandle;
34struct Referrer;
35struct ResourceResponse;
36
37// Interface that the embedder provides to ResourceDispatcherHost to allow
38// observing and modifying requests.
39class CONTENT_EXPORT ResourceDispatcherHostDelegate {
40 public:
41  // Called when a request begins. Return false to abort the request.
42  virtual bool ShouldBeginRequest(const std::string& method,
43                                  const GURL& url,
44                                  ResourceType resource_type,
45                                  ResourceContext* resource_context);
46
47  // Called after ShouldBeginRequest to allow the embedder to add resource
48  // throttles.
49  virtual void RequestBeginning(net::URLRequest* request,
50                                ResourceContext* resource_context,
51                                AppCacheService* appcache_service,
52                                ResourceType resource_type,
53                                ScopedVector<ResourceThrottle>* throttles);
54
55  // Allows an embedder to add additional resource handlers for a download.
56  // |must_download| is set if the request must be handled as a download.
57  virtual void DownloadStarting(net::URLRequest* request,
58                                ResourceContext* resource_context,
59                                int child_id,
60                                int route_id,
61                                int request_id,
62                                bool is_content_initiated,
63                                bool must_download,
64                                ScopedVector<ResourceThrottle>* throttles);
65
66  // Creates a ResourceDispatcherHostLoginDelegate that asks the user for a
67  // username and password.
68  virtual ResourceDispatcherHostLoginDelegate* CreateLoginDelegate(
69      net::AuthChallengeInfo* auth_info,
70      net::URLRequest* request);
71
72  // Launches the url for the given tab. Returns true if an attempt to handle
73  // the url was made, e.g. by launching an app. Note that this does not
74  // guarantee that the app successfully handled it.
75  virtual bool HandleExternalProtocol(const GURL& url,
76                                      int child_id,
77                                      int route_id);
78
79  // Returns true if we should force the given resource to be downloaded.
80  // Otherwise, the content layer decides.
81  virtual bool ShouldForceDownloadResource(const GURL& url,
82                                           const std::string& mime_type);
83
84  // Returns true and sets |origin| if a Stream should be created for the
85  // resource.
86  // If true is returned, a new Stream will be created and OnStreamCreated()
87  // will be called with
88  // - a StreamHandle instance for the Stream. The handle contains the URL for
89  //   reading the Stream etc.
90  // The Stream's origin will be set to |origin|.
91  //
92  // If the stream will be rendered in a BrowserPlugin, |payload| will contain
93  // the data that should be given to the old ResourceHandler to forward to the
94  // renderer process.
95  virtual bool ShouldInterceptResourceAsStream(net::URLRequest* request,
96                                               const std::string& mime_type,
97                                               GURL* origin,
98                                               std::string* payload);
99
100  // Informs the delegate that a Stream was created. The Stream can be read from
101  // the blob URL of the Stream, but can only be read once.
102  virtual void OnStreamCreated(net::URLRequest* request,
103                               scoped_ptr<content::StreamHandle> stream);
104
105  // Informs the delegate that a response has started.
106  virtual void OnResponseStarted(net::URLRequest* request,
107                                 ResourceContext* resource_context,
108                                 ResourceResponse* response,
109                                 IPC::Sender* sender);
110
111  // Informs the delegate that a request has been redirected.
112  virtual void OnRequestRedirected(const GURL& redirect_url,
113                                   net::URLRequest* request,
114                                   ResourceContext* resource_context,
115                                   ResourceResponse* response);
116
117  // Notification that a request has completed.
118  virtual void RequestComplete(net::URLRequest* url_request);
119
120 protected:
121  ResourceDispatcherHostDelegate();
122  virtual ~ResourceDispatcherHostDelegate();
123};
124
125}  // namespace content
126
127#endif  // CONTENT_PUBLIC_BROWSER_RESOURCE_DISPATCHER_HOST_DELEGATE_H_
128