Removing flow to demote App Launcher to App Host, so app_host.exe can be deleted...
[chromium-blink-merge.git] / webkit / glue / resource_loader_bridge.h
blob2deec26b92e77aa01720febe378bfb3c09192874
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 // 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.
8 //
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
11 // no longer needed.
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_
19 #include <utility>
20 #include <vector>
22 #include "build/build_config.h"
23 #if defined(OS_POSIX)
24 #include "base/file_descriptor_posix.h"
25 #endif
26 #include "base/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/url_request/url_request_status.h"
34 #include "third_party/WebKit/Source/Platform/chromium/public/WebReferrerPolicy.h"
35 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLRequest.h"
36 #include "webkit/glue/resource_type.h"
37 #include "webkit/glue/webkit_glue_export.h"
39 namespace net {
40 class HttpResponseHeaders;
43 namespace webkit_glue {
44 class ResourceRequestBody;
46 // Structure containing timing information for the request. It addresses
47 // http://groups.google.com/group/http-archive-specification/web/har-1-1-spec
48 // and http://dev.w3.org/2006/webapi/WebTiming/ needs.
50 // All the values for starts and ends are given in milliseconds and are
51 // offsets with respect to the given base time.
52 struct ResourceLoadTimingInfo {
53 WEBKIT_GLUE_EXPORT ResourceLoadTimingInfo();
54 WEBKIT_GLUE_EXPORT ~ResourceLoadTimingInfo();
56 // All the values in this struct are given as offsets in ticks wrt
57 // this base tick count.
58 base::TimeTicks base_ticks;
60 // The value of Time::Now() when base_ticks was set.
61 base::Time base_time;
63 // The time that proxy processing started. For requests with no proxy phase,
64 // this time is -1.
65 int32 proxy_start;
67 // The time that proxy processing ended. For reused sockets this time
68 // is -1.
69 int32 proxy_end;
71 // The time that DNS lookup started. For reused sockets this time is -1.
72 int32 dns_start;
74 // The time that DNS lookup ended. For reused sockets this time is -1.
75 int32 dns_end;
77 // The time that establishing connection started. Connect time includes
78 // DNS, blocking, TCP, TCP retries and SSL time.
79 int32 connect_start;
81 // The time that establishing connection ended. Connect time includes
82 // DNS, blocking, TCP, TCP retries and SSL time.
83 int32 connect_end;
85 // The time at which SSL handshake started. For non-HTTPS requests this
86 // is 0.
87 int32 ssl_start;
89 // The time at which SSL handshake ended. For non-HTTPS requests this is 0.
90 int32 ssl_end;
92 // The time that HTTP request started. For non-HTTP requests this is 0.
93 int32 send_start;
95 // The time that HTTP request ended. For non-HTTP requests this is 0.
96 int32 send_end;
98 // The time at which receiving HTTP headers started. For non-HTTP requests
99 // this is 0.
100 int32 receive_headers_start;
102 // The time at which receiving HTTP headers ended. For non-HTTP requests
103 // this is 0.
104 int32 receive_headers_end;
107 struct ResourceDevToolsInfo : base::RefCounted<ResourceDevToolsInfo> {
108 typedef std::vector<std::pair<std::string, std::string> >
109 HeadersVector;
111 WEBKIT_GLUE_EXPORT ResourceDevToolsInfo();
113 int32 http_status_code;
114 std::string http_status_text;
115 HeadersVector request_headers;
116 HeadersVector response_headers;
117 std::string request_headers_text;
118 std::string response_headers_text;
120 private:
121 friend class base::RefCounted<ResourceDevToolsInfo>;
122 WEBKIT_GLUE_EXPORT ~ResourceDevToolsInfo();
125 struct ResourceResponseInfo {
126 WEBKIT_GLUE_EXPORT ResourceResponseInfo();
127 WEBKIT_GLUE_EXPORT ~ResourceResponseInfo();
129 // The time at which the request was made that resulted in this response.
130 // For cached responses, this time could be "far" in the past.
131 base::Time request_time;
133 // The time at which the response headers were received. For cached
134 // responses, this time could be "far" in the past.
135 base::Time response_time;
137 // The response headers or NULL if the URL type does not support headers.
138 scoped_refptr<net::HttpResponseHeaders> headers;
140 // The mime type of the response. This may be a derived value.
141 std::string mime_type;
143 // The character encoding of the response or none if not applicable to the
144 // response's mime type. This may be a derived value.
145 std::string charset;
147 // An opaque string carrying security information pertaining to this
148 // response. This may include information about the SSL connection used.
149 std::string security_info;
151 // Content length if available. -1 if not available
152 int64 content_length;
154 // Length of the encoded data transferred over the network. In case there is
155 // no data, contains -1.
156 int64 encoded_data_length;
158 // The appcache this response was loaded from, or kNoCacheId.
159 int64 appcache_id;
161 // The manifest url of the appcache this response was loaded from.
162 // Note: this value is only populated for main resource requests.
163 GURL appcache_manifest_url;
165 // Connection identifier from the underlying network stack. In case there
166 // is no associated connection, contains 0.
167 uint32 connection_id;
169 // Determines whether physical connection reused.
170 bool connection_reused;
172 // Detailed timing information used by the WebTiming, HAR and Developer
173 // Tools.
174 ResourceLoadTimingInfo load_timing;
176 // Actual request and response headers, as obtained from the network stack.
177 // Only present if request had LOAD_REPORT_RAW_HEADERS in load_flags, and
178 // requesting renderer had CanReadRowCookies permission.
179 scoped_refptr<ResourceDevToolsInfo> devtools_info;
181 // The path to a file that will contain the response body. It may only
182 // contain a portion of the response body at the time that the ResponseInfo
183 // becomes available.
184 FilePath download_file_path;
186 // True if the response was delivered using SPDY.
187 bool was_fetched_via_spdy;
189 // True if the response was delivered after NPN is negotiated.
190 bool was_npn_negotiated;
192 // True if response could use alternate protocol. However, browser will
193 // ignore the alternate protocol when spdy is not enabled on browser side.
194 bool was_alternate_protocol_available;
196 // True if the response was fetched via an explicit proxy (as opposed to a
197 // transparent proxy). The proxy could be any type of proxy, HTTP or SOCKS.
198 // Note: we cannot tell if a transparent proxy may have been involved.
199 bool was_fetched_via_proxy;
201 // NPN protocol negotiated with the server.
202 std::string npn_negotiated_protocol;
204 // Remote address of the socket which fetched this resource.
205 net::HostPortPair socket_address;
208 class ResourceLoaderBridge {
209 public:
210 // Structure used when calling
211 // WebKitPlatformSupportImpl::CreateResourceLoader().
212 struct WEBKIT_GLUE_EXPORT RequestInfo {
213 RequestInfo();
214 ~RequestInfo();
216 // HTTP-style method name (e.g., "GET" or "POST").
217 std::string method;
219 // Absolute URL encoded in ASCII per the rules of RFC-2396.
220 GURL url;
222 // URL of the document in the top-level window, which may be checked by the
223 // third-party cookie blocking policy.
224 GURL first_party_for_cookies;
226 // Optional parameter, a URL with similar constraints in how it must be
227 // encoded as the url member.
228 GURL referrer;
230 // The referrer policy that applies to the referrer.
231 WebKit::WebReferrerPolicy referrer_policy;
233 // For HTTP(S) requests, the headers parameter can be a \r\n-delimited and
234 // \r\n-terminated list of MIME headers. They should be ASCII-encoded using
235 // the standard MIME header encoding rules. The headers parameter can also
236 // be null if no extra request headers need to be set.
237 std::string headers;
239 // Composed of the values defined in url_request_load_flags.h.
240 int load_flags;
242 // Process id of the process making the request.
243 int requestor_pid;
245 // Indicates if the current request is the main frame load, a sub-frame
246 // load, or a sub objects load.
247 ResourceType::Type request_type;
249 // Used for plugin to browser requests.
250 uint32 request_context;
252 // Identifies what appcache host this request is associated with.
253 int appcache_host_id;
255 // Used to associated the bridge with a frame's network context.
256 int routing_id;
258 // If true, then the response body will be downloaded to a file and the
259 // path to that file will be provided in ResponseInfo::download_file_path.
260 bool download_to_file;
262 // True if the request was user initiated.
263 bool has_user_gesture;
265 // Extra data associated with this request. We do not own this pointer.
266 WebKit::WebURLRequest::ExtraData* extra_data;
268 private:
269 DISALLOW_COPY_AND_ASSIGN(RequestInfo);
272 // See the SyncLoad method declared below. (The name of this struct is not
273 // suffixed with "Info" because it also contains the response data.)
274 struct SyncLoadResponse : ResourceResponseInfo {
275 SyncLoadResponse();
276 ~SyncLoadResponse();
278 // The response error code.
279 int error_code;
281 // The final URL of the response. This may differ from the request URL in
282 // the case of a server redirect.
283 GURL url;
285 // The response data.
286 std::string data;
289 // Generated by the bridge. This is implemented by our custom resource loader
290 // within webkit. The Peer and it's bridge should have identical lifetimes
291 // as they represent each end of a communication channel.
293 // These callbacks mirror net::URLRequest::Delegate and the order and
294 // conditions in which they will be called are identical. See url_request.h
295 // for more information.
296 class Peer {
297 public:
298 // Called as upload progress is made.
299 // note: only for requests with LOAD_ENABLE_UPLOAD_PROGRESS set
300 virtual void OnUploadProgress(uint64 position, uint64 size) = 0;
302 // Called when a redirect occurs. The implementation may return false to
303 // suppress the redirect. The given ResponseInfo provides complete
304 // information about the redirect, and new_url is the URL that will be
305 // loaded if this method returns true. If this method returns true, the
306 // output parameter *has_new_first_party_for_cookies indicates whether the
307 // output parameter *new_first_party_for_cookies contains the new URL that
308 // should be consulted for the third-party cookie blocking policy.
309 virtual bool OnReceivedRedirect(const GURL& new_url,
310 const ResourceResponseInfo& info,
311 bool* has_new_first_party_for_cookies,
312 GURL* new_first_party_for_cookies) = 0;
314 // Called when response headers are available (after all redirects have
315 // been followed).
316 virtual void OnReceivedResponse(const ResourceResponseInfo& info) = 0;
318 // Called when a chunk of response data is downloaded. This method may be
319 // called multiple times or not at all if an error occurs. This method is
320 // only called if RequestInfo::download_to_file was set to true, and in
321 // that case, OnReceivedData will not be called.
322 virtual void OnDownloadedData(int len) = 0;
324 // Called when a chunk of response data is available. This method may
325 // be called multiple times or not at all if an error occurs.
326 // The encoded_data_length is the length of the encoded data transferred
327 // over the network, which could be different from data length (e.g. for
328 // gzipped content), or -1 if if unknown.
329 virtual void OnReceivedData(const char* data,
330 int data_length,
331 int encoded_data_length) = 0;
333 // Called when metadata generated by the renderer is retrieved from the
334 // cache. This method may be called zero or one times.
335 virtual void OnReceivedCachedMetadata(const char* data, int len) { }
337 // Called when the response is complete. This method signals completion of
338 // the resource load.
339 virtual void OnCompletedRequest(
340 int error_code,
341 bool was_ignored_by_handler,
342 const std::string& security_info,
343 const base::TimeTicks& completion_time) = 0;
345 protected:
346 virtual ~Peer() {}
349 // use WebKitPlatformSupportImpl::CreateResourceLoader() for construction, but
350 // anybody can delete at any time, INCLUDING during processing of callbacks.
351 WEBKIT_GLUE_EXPORT virtual ~ResourceLoaderBridge();
353 // Call this method before calling Start() to set the request body.
354 // May only be used with HTTP(S) POST requests.
355 virtual void SetRequestBody(ResourceRequestBody* request_body) = 0;
357 // Call this method to initiate the request. If this method succeeds, then
358 // the peer's methods will be called asynchronously to report various events.
359 virtual bool Start(Peer* peer) = 0;
361 // Call this method to cancel a request that is in progress. This method
362 // causes the request to immediately transition into the 'done' state. The
363 // OnCompletedRequest method will be called asynchronously; this assumes
364 // the peer is still valid.
365 virtual void Cancel() = 0;
367 // Call this method to suspend or resume a load that is in progress. This
368 // method may only be called after a successful call to the Start method.
369 virtual void SetDefersLoading(bool value) = 0;
371 // Call this method to load the resource synchronously (i.e., in one shot).
372 // This is an alternative to the Start method. Be warned that this method
373 // will block the calling thread until the resource is fully downloaded or an
374 // error occurs. It could block the calling thread for a long time, so only
375 // use this if you really need it! There is also no way for the caller to
376 // interrupt this method. Errors are reported via the status field of the
377 // response parameter.
378 virtual void SyncLoad(SyncLoadResponse* response) = 0;
380 protected:
381 // Construction must go through
382 // WebKitPlatformSupportImpl::CreateResourceLoader()
383 // For HTTP(S) POST requests, the AppendDataToUpload and AppendFileToUpload
384 // methods may be called to construct the body of the request.
385 WEBKIT_GLUE_EXPORT ResourceLoaderBridge();
387 private:
388 DISALLOW_COPY_AND_ASSIGN(ResourceLoaderBridge);
391 } // namespace webkit_glue
393 #endif // WEBKIT_GLUE_RESOURCE_LOADER_BRIDGE_H_