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.
5 // The intent of this file is to provide a type-neutral abstraction between
6 // Chrome and WebKit for resource loading. This pure-virtual interface is
7 // implemented by the embedder.
9 // One of these objects will be created by WebKit for each request. WebKit
10 // will own the pointer to the bridge, and will delete it when the request is
13 // In turn, the bridge's owner on the WebKit end will implement the Peer
14 // interface, which we will use to communicate notifications back.
16 #ifndef WEBKIT_GLUE_RESOURCE_LOADER_BRIDGE_H_
17 #define WEBKIT_GLUE_RESOURCE_LOADER_BRIDGE_H_
22 #include "build/build_config.h"
24 #include "base/file_descriptor_posix.h"
26 #include "base/files/file_path.h"
27 #include "base/memory/ref_counted.h"
28 #include "base/platform_file.h"
29 #include "base/time.h"
30 #include "base/values.h"
31 #include "googleurl/src/gurl.h"
32 #include "net/base/host_port_pair.h"
33 #include "net/base/load_timing_info.h"
34 #include "net/base/request_priority.h"
35 #include "net/url_request/url_request_status.h"
36 #include "third_party/WebKit/Source/Platform/chromium/public/WebReferrerPolicy.h"
37 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLRequest.h"
38 #include "webkit/glue/resource_type.h"
39 #include "webkit/glue/webkit_glue_export.h"
42 class HttpResponseHeaders
;
45 namespace webkit_glue
{
46 class ResourceRequestBody
;
48 struct ResourceDevToolsInfo
: base::RefCounted
<ResourceDevToolsInfo
> {
49 typedef std::vector
<std::pair
<std::string
, std::string
> >
52 WEBKIT_GLUE_EXPORT
ResourceDevToolsInfo();
54 int32 http_status_code
;
55 std::string http_status_text
;
56 HeadersVector request_headers
;
57 HeadersVector response_headers
;
58 std::string request_headers_text
;
59 std::string response_headers_text
;
62 friend class base::RefCounted
<ResourceDevToolsInfo
>;
63 WEBKIT_GLUE_EXPORT
~ResourceDevToolsInfo();
66 struct ResourceResponseInfo
{
67 WEBKIT_GLUE_EXPORT
ResourceResponseInfo();
68 WEBKIT_GLUE_EXPORT
~ResourceResponseInfo();
70 // The time at which the request was made that resulted in this response.
71 // For cached responses, this time could be "far" in the past.
72 base::Time request_time
;
74 // The time at which the response headers were received. For cached
75 // responses, this time could be "far" in the past.
76 base::Time response_time
;
78 // The response headers or NULL if the URL type does not support headers.
79 scoped_refptr
<net::HttpResponseHeaders
> headers
;
81 // The mime type of the response. This may be a derived value.
82 std::string mime_type
;
84 // The character encoding of the response or none if not applicable to the
85 // response's mime type. This may be a derived value.
88 // An opaque string carrying security information pertaining to this
89 // response. This may include information about the SSL connection used.
90 std::string security_info
;
92 // Content length if available. -1 if not available
95 // Length of the encoded data transferred over the network. In case there is
96 // no data, contains -1.
97 int64 encoded_data_length
;
99 // The appcache this response was loaded from, or kNoCacheId.
102 // The manifest url of the appcache this response was loaded from.
103 // Note: this value is only populated for main resource requests.
104 GURL appcache_manifest_url
;
106 // Detailed timing information used by the WebTiming, HAR and Developer
107 // Tools. Includes socket ID and socket reuse information.
108 net::LoadTimingInfo load_timing
;
110 // Actual request and response headers, as obtained from the network stack.
111 // Only present if request had LOAD_REPORT_RAW_HEADERS in load_flags, and
112 // requesting renderer had CanReadRowCookies permission.
113 scoped_refptr
<ResourceDevToolsInfo
> devtools_info
;
115 // The path to a file that will contain the response body. It may only
116 // contain a portion of the response body at the time that the ResponseInfo
117 // becomes available.
118 base::FilePath download_file_path
;
120 // True if the response was delivered using SPDY.
121 bool was_fetched_via_spdy
;
123 // True if the response was delivered after NPN is negotiated.
124 bool was_npn_negotiated
;
126 // True if response could use alternate protocol. However, browser will
127 // ignore the alternate protocol when spdy is not enabled on browser side.
128 bool was_alternate_protocol_available
;
130 // True if the response was fetched via an explicit proxy (as opposed to a
131 // transparent proxy). The proxy could be any type of proxy, HTTP or SOCKS.
132 // Note: we cannot tell if a transparent proxy may have been involved.
133 bool was_fetched_via_proxy
;
135 // NPN protocol negotiated with the server.
136 std::string npn_negotiated_protocol
;
138 // Remote address of the socket which fetched this resource.
139 net::HostPortPair socket_address
;
142 class ResourceLoaderBridge
{
144 // Structure used when calling
145 // WebKitPlatformSupportImpl::CreateResourceLoader().
146 struct WEBKIT_GLUE_EXPORT RequestInfo
{
150 // HTTP-style method name (e.g., "GET" or "POST").
153 // Absolute URL encoded in ASCII per the rules of RFC-2396.
156 // URL of the document in the top-level window, which may be checked by the
157 // third-party cookie blocking policy.
158 GURL first_party_for_cookies
;
160 // Optional parameter, a URL with similar constraints in how it must be
161 // encoded as the url member.
164 // The referrer policy that applies to the referrer.
165 WebKit::WebReferrerPolicy referrer_policy
;
167 // For HTTP(S) requests, the headers parameter can be a \r\n-delimited and
168 // \r\n-terminated list of MIME headers. They should be ASCII-encoded using
169 // the standard MIME header encoding rules. The headers parameter can also
170 // be null if no extra request headers need to be set.
173 // Composed of the values defined in url_request_load_flags.h.
176 // Process id of the process making the request.
179 // Indicates if the current request is the main frame load, a sub-frame
180 // load, or a sub objects load.
181 ResourceType::Type request_type
;
183 // Indicates the priority of this request, as determined by WebKit.
184 net::RequestPriority priority
;
186 // Used for plugin to browser requests.
187 uint32 request_context
;
189 // Identifies what appcache host this request is associated with.
190 int appcache_host_id
;
192 // Used to associated the bridge with a frame's network context.
195 // If true, then the response body will be downloaded to a file and the
196 // path to that file will be provided in ResponseInfo::download_file_path.
197 bool download_to_file
;
199 // True if the request was user initiated.
200 bool has_user_gesture
;
202 // Extra data associated with this request. We do not own this pointer.
203 WebKit::WebURLRequest::ExtraData
* extra_data
;
206 DISALLOW_COPY_AND_ASSIGN(RequestInfo
);
209 // See the SyncLoad method declared below. (The name of this struct is not
210 // suffixed with "Info" because it also contains the response data.)
211 struct SyncLoadResponse
: ResourceResponseInfo
{
215 // The response error code.
218 // The final URL of the response. This may differ from the request URL in
219 // the case of a server redirect.
222 // The response data.
226 // Generated by the bridge. This is implemented by our custom resource loader
227 // within webkit. The Peer and it's bridge should have identical lifetimes
228 // as they represent each end of a communication channel.
230 // These callbacks mirror net::URLRequest::Delegate and the order and
231 // conditions in which they will be called are identical. See url_request.h
232 // for more information.
235 // Called as upload progress is made.
236 // note: only for requests with LOAD_ENABLE_UPLOAD_PROGRESS set
237 virtual void OnUploadProgress(uint64 position
, uint64 size
) = 0;
239 // Called when a redirect occurs. The implementation may return false to
240 // suppress the redirect. The given ResponseInfo provides complete
241 // information about the redirect, and new_url is the URL that will be
242 // loaded if this method returns true. If this method returns true, the
243 // output parameter *has_new_first_party_for_cookies indicates whether the
244 // output parameter *new_first_party_for_cookies contains the new URL that
245 // should be consulted for the third-party cookie blocking policy.
246 virtual bool OnReceivedRedirect(const GURL
& new_url
,
247 const ResourceResponseInfo
& info
,
248 bool* has_new_first_party_for_cookies
,
249 GURL
* new_first_party_for_cookies
) = 0;
251 // Called when response headers are available (after all redirects have
253 virtual void OnReceivedResponse(const ResourceResponseInfo
& info
) = 0;
255 // Called when a chunk of response data is downloaded. This method may be
256 // called multiple times or not at all if an error occurs. This method is
257 // only called if RequestInfo::download_to_file was set to true, and in
258 // that case, OnReceivedData will not be called.
259 virtual void OnDownloadedData(int len
) = 0;
261 // Called when a chunk of response data is available. This method may
262 // be called multiple times or not at all if an error occurs.
263 // The encoded_data_length is the length of the encoded data transferred
264 // over the network, which could be different from data length (e.g. for
265 // gzipped content), or -1 if if unknown.
266 virtual void OnReceivedData(const char* data
,
268 int encoded_data_length
) = 0;
270 // Called when metadata generated by the renderer is retrieved from the
271 // cache. This method may be called zero or one times.
272 virtual void OnReceivedCachedMetadata(const char* data
, int len
) { }
274 // Called when the response is complete. This method signals completion of
275 // the resource load.
276 virtual void OnCompletedRequest(
278 bool was_ignored_by_handler
,
279 const std::string
& security_info
,
280 const base::TimeTicks
& completion_time
) = 0;
286 // use WebKitPlatformSupportImpl::CreateResourceLoader() for construction, but
287 // anybody can delete at any time, INCLUDING during processing of callbacks.
288 WEBKIT_GLUE_EXPORT
virtual ~ResourceLoaderBridge();
290 // Call this method before calling Start() to set the request body.
291 // May only be used with HTTP(S) POST requests.
292 virtual void SetRequestBody(ResourceRequestBody
* request_body
) = 0;
294 // Call this method to initiate the request. If this method succeeds, then
295 // the peer's methods will be called asynchronously to report various events.
296 virtual bool Start(Peer
* peer
) = 0;
298 // Call this method to cancel a request that is in progress. This method
299 // causes the request to immediately transition into the 'done' state. The
300 // OnCompletedRequest method will be called asynchronously; this assumes
301 // the peer is still valid.
302 virtual void Cancel() = 0;
304 // Call this method to suspend or resume a load that is in progress. This
305 // method may only be called after a successful call to the Start method.
306 virtual void SetDefersLoading(bool value
) = 0;
308 // Call this method when the priority of the requested resource changes after
309 // Start() has been called. This method may only be called after a successful
310 // call to the Start method.
311 virtual void DidChangePriority(net::RequestPriority new_priority
) = 0;
313 // Call this method to load the resource synchronously (i.e., in one shot).
314 // This is an alternative to the Start method. Be warned that this method
315 // will block the calling thread until the resource is fully downloaded or an
316 // error occurs. It could block the calling thread for a long time, so only
317 // use this if you really need it! There is also no way for the caller to
318 // interrupt this method. Errors are reported via the status field of the
319 // response parameter.
320 virtual void SyncLoad(SyncLoadResponse
* response
) = 0;
323 // Construction must go through
324 // WebKitPlatformSupportImpl::CreateResourceLoader()
325 // For HTTP(S) POST requests, the AppendDataToUpload and AppendFileToUpload
326 // methods may be called to construct the body of the request.
327 WEBKIT_GLUE_EXPORT
ResourceLoaderBridge();
330 DISALLOW_COPY_AND_ASSIGN(ResourceLoaderBridge
);
333 } // namespace webkit_glue
335 #endif // WEBKIT_GLUE_RESOURCE_LOADER_BRIDGE_H_