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/http/http_response_info.h"
36 #include "net/url_request/url_request_status.h"
37 #include "third_party/WebKit/public/platform/WebReferrerPolicy.h"
38 #include "third_party/WebKit/public/platform/WebURLRequest.h"
39 #include "webkit/glue/resource_type.h"
40 #include "webkit/glue/webkit_glue_export.h"
43 class HttpResponseHeaders
;
46 namespace webkit_glue
{
47 class ResourceRequestBody
;
49 struct ResourceDevToolsInfo
: base::RefCounted
<ResourceDevToolsInfo
> {
50 typedef std::vector
<std::pair
<std::string
, std::string
> >
53 WEBKIT_GLUE_EXPORT
ResourceDevToolsInfo();
55 int32 http_status_code
;
56 std::string http_status_text
;
57 HeadersVector request_headers
;
58 HeadersVector response_headers
;
59 std::string request_headers_text
;
60 std::string response_headers_text
;
63 friend class base::RefCounted
<ResourceDevToolsInfo
>;
64 WEBKIT_GLUE_EXPORT
~ResourceDevToolsInfo();
67 struct ResourceResponseInfo
{
68 WEBKIT_GLUE_EXPORT
ResourceResponseInfo();
69 WEBKIT_GLUE_EXPORT
~ResourceResponseInfo();
71 // The time at which the request was made that resulted in this response.
72 // For cached responses, this time could be "far" in the past.
73 base::Time request_time
;
75 // The time at which the response headers were received. For cached
76 // responses, this time could be "far" in the past.
77 base::Time response_time
;
79 // The response headers or NULL if the URL type does not support headers.
80 scoped_refptr
<net::HttpResponseHeaders
> headers
;
82 // The mime type of the response. This may be a derived value.
83 std::string mime_type
;
85 // The character encoding of the response or none if not applicable to the
86 // response's mime type. This may be a derived value.
89 // An opaque string carrying security information pertaining to this
90 // response. This may include information about the SSL connection used.
91 std::string security_info
;
93 // Content length if available. -1 if not available
96 // Length of the encoded data transferred over the network. In case there is
97 // no data, contains -1.
98 int64 encoded_data_length
;
100 // The appcache this response was loaded from, or kNoCacheId.
103 // The manifest url of the appcache this response was loaded from.
104 // Note: this value is only populated for main resource requests.
105 GURL appcache_manifest_url
;
107 // Detailed timing information used by the WebTiming, HAR and Developer
108 // Tools. Includes socket ID and socket reuse information.
109 net::LoadTimingInfo load_timing
;
111 // Actual request and response headers, as obtained from the network stack.
112 // Only present if request had LOAD_REPORT_RAW_HEADERS in load_flags, and
113 // requesting renderer had CanReadRowCookies permission.
114 scoped_refptr
<ResourceDevToolsInfo
> devtools_info
;
116 // The path to a file that will contain the response body. It may only
117 // contain a portion of the response body at the time that the ResponseInfo
118 // becomes available.
119 base::FilePath download_file_path
;
121 // True if the response was delivered using SPDY.
122 bool was_fetched_via_spdy
;
124 // True if the response was delivered after NPN is negotiated.
125 bool was_npn_negotiated
;
127 // True if response could use alternate protocol. However, browser will
128 // ignore the alternate protocol when spdy is not enabled on browser side.
129 bool was_alternate_protocol_available
;
131 // Information about the type of connection used to fetch this response.
132 net::HttpResponseInfo::ConnectionInfo connection_info
;
134 // True if the response was fetched via an explicit proxy (as opposed to a
135 // transparent proxy). The proxy could be any type of proxy, HTTP or SOCKS.
136 // Note: we cannot tell if a transparent proxy may have been involved.
137 bool was_fetched_via_proxy
;
139 // NPN protocol negotiated with the server.
140 std::string npn_negotiated_protocol
;
142 // Remote address of the socket which fetched this resource.
143 net::HostPortPair socket_address
;
146 class ResourceLoaderBridge
{
148 // Structure used when calling
149 // WebKitPlatformSupportImpl::CreateResourceLoader().
150 struct WEBKIT_GLUE_EXPORT RequestInfo
{
154 // HTTP-style method name (e.g., "GET" or "POST").
157 // Absolute URL encoded in ASCII per the rules of RFC-2396.
160 // URL of the document in the top-level window, which may be checked by the
161 // third-party cookie blocking policy.
162 GURL first_party_for_cookies
;
164 // Optional parameter, a URL with similar constraints in how it must be
165 // encoded as the url member.
168 // The referrer policy that applies to the referrer.
169 WebKit::WebReferrerPolicy referrer_policy
;
171 // For HTTP(S) requests, the headers parameter can be a \r\n-delimited and
172 // \r\n-terminated list of MIME headers. They should be ASCII-encoded using
173 // the standard MIME header encoding rules. The headers parameter can also
174 // be null if no extra request headers need to be set.
177 // Composed of the values defined in url_request_load_flags.h.
180 // Process id of the process making the request.
183 // Indicates if the current request is the main frame load, a sub-frame
184 // load, or a sub objects load.
185 ResourceType::Type request_type
;
187 // Indicates the priority of this request, as determined by WebKit.
188 net::RequestPriority priority
;
190 // Used for plugin to browser requests.
191 uint32 request_context
;
193 // Identifies what appcache host this request is associated with.
194 int appcache_host_id
;
196 // Used to associated the bridge with a frame's network context.
199 // If true, then the response body will be downloaded to a file and the
200 // path to that file will be provided in ResponseInfo::download_file_path.
201 bool download_to_file
;
203 // True if the request was user initiated.
204 bool has_user_gesture
;
206 // Extra data associated with this request. We do not own this pointer.
207 WebKit::WebURLRequest::ExtraData
* extra_data
;
210 DISALLOW_COPY_AND_ASSIGN(RequestInfo
);
213 // See the SyncLoad method declared below. (The name of this struct is not
214 // suffixed with "Info" because it also contains the response data.)
215 struct SyncLoadResponse
: ResourceResponseInfo
{
219 // The response error code.
222 // The final URL of the response. This may differ from the request URL in
223 // the case of a server redirect.
226 // The response data.
230 // Generated by the bridge. This is implemented by our custom resource loader
231 // within webkit. The Peer and it's bridge should have identical lifetimes
232 // as they represent each end of a communication channel.
234 // These callbacks mirror net::URLRequest::Delegate and the order and
235 // conditions in which they will be called are identical. See url_request.h
236 // for more information.
239 // Called as upload progress is made.
240 // note: only for requests with LOAD_ENABLE_UPLOAD_PROGRESS set
241 virtual void OnUploadProgress(uint64 position
, uint64 size
) = 0;
243 // Called when a redirect occurs. The implementation may return false to
244 // suppress the redirect. The given ResponseInfo provides complete
245 // information about the redirect, and new_url is the URL that will be
246 // loaded if this method returns true. If this method returns true, the
247 // output parameter *has_new_first_party_for_cookies indicates whether the
248 // output parameter *new_first_party_for_cookies contains the new URL that
249 // should be consulted for the third-party cookie blocking policy.
250 virtual bool OnReceivedRedirect(const GURL
& new_url
,
251 const ResourceResponseInfo
& info
,
252 bool* has_new_first_party_for_cookies
,
253 GURL
* new_first_party_for_cookies
) = 0;
255 // Called when response headers are available (after all redirects have
257 virtual void OnReceivedResponse(const ResourceResponseInfo
& info
) = 0;
259 // Called when a chunk of response data is downloaded. This method may be
260 // called multiple times or not at all if an error occurs. This method is
261 // only called if RequestInfo::download_to_file was set to true, and in
262 // that case, OnReceivedData will not be called.
263 virtual void OnDownloadedData(int len
) = 0;
265 // Called when a chunk of response data is available. This method may
266 // be called multiple times or not at all if an error occurs.
267 // The encoded_data_length is the length of the encoded data transferred
268 // over the network, which could be different from data length (e.g. for
269 // gzipped content), or -1 if if unknown.
270 virtual void OnReceivedData(const char* data
,
272 int encoded_data_length
) = 0;
274 // Called when metadata generated by the renderer is retrieved from the
275 // cache. This method may be called zero or one times.
276 virtual void OnReceivedCachedMetadata(const char* data
, int len
) { }
278 // Called when the response is complete. This method signals completion of
279 // the resource load.
280 virtual void OnCompletedRequest(
282 bool was_ignored_by_handler
,
283 const std::string
& security_info
,
284 const base::TimeTicks
& completion_time
) = 0;
290 // use WebKitPlatformSupportImpl::CreateResourceLoader() for construction, but
291 // anybody can delete at any time, INCLUDING during processing of callbacks.
292 WEBKIT_GLUE_EXPORT
virtual ~ResourceLoaderBridge();
294 // Call this method before calling Start() to set the request body.
295 // May only be used with HTTP(S) POST requests.
296 virtual void SetRequestBody(ResourceRequestBody
* request_body
) = 0;
298 // Call this method to initiate the request. If this method succeeds, then
299 // the peer's methods will be called asynchronously to report various events.
300 virtual bool Start(Peer
* peer
) = 0;
302 // Call this method to cancel a request that is in progress. This method
303 // causes the request to immediately transition into the 'done' state. The
304 // OnCompletedRequest method will be called asynchronously; this assumes
305 // the peer is still valid.
306 virtual void Cancel() = 0;
308 // Call this method to suspend or resume a load that is in progress. This
309 // method may only be called after a successful call to the Start method.
310 virtual void SetDefersLoading(bool value
) = 0;
312 // Call this method when the priority of the requested resource changes after
313 // Start() has been called. This method may only be called after a successful
314 // call to the Start method.
315 virtual void DidChangePriority(net::RequestPriority new_priority
) = 0;
317 // Call this method to load the resource synchronously (i.e., in one shot).
318 // This is an alternative to the Start method. Be warned that this method
319 // will block the calling thread until the resource is fully downloaded or an
320 // error occurs. It could block the calling thread for a long time, so only
321 // use this if you really need it! There is also no way for the caller to
322 // interrupt this method. Errors are reported via the status field of the
323 // response parameter.
324 virtual void SyncLoad(SyncLoadResponse
* response
) = 0;
327 // Construction must go through
328 // WebKitPlatformSupportImpl::CreateResourceLoader()
329 // For HTTP(S) POST requests, the AppendDataToUpload and AppendFileToUpload
330 // methods may be called to construct the body of the request.
331 WEBKIT_GLUE_EXPORT
ResourceLoaderBridge();
334 DISALLOW_COPY_AND_ASSIGN(ResourceLoaderBridge
);
337 } // namespace webkit_glue
339 #endif // WEBKIT_GLUE_RESOURCE_LOADER_BRIDGE_H_