1 // Copyright 2014 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 #ifndef COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_
6 #define COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_
10 #include "base/callback.h"
11 #include "base/location.h"
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "net/base/request_priority.h"
15 #include "net/http/http_request_headers.h"
16 #include "net/url_request/url_request.h"
20 class SingleThreadTaskRunner
;
24 class GrowableIOBuffer
;
25 class HttpResponseHeaders
;
30 class CronetURLRequestContextAdapter
;
32 // An adapter from the JNI CronetUrlRequest object to the Chromium URLRequest.
33 // Created and configured from random thread. Start is posted to network
34 // thread and all callbacks into CronetURLRequestAdapterDelegate are done on
35 // network thread. Each delegate callback is expected to initiate next step like
36 // FollowDeferredRedirect, ReadData or Destroy. All methods except those needed
37 // to set up a request must be called exclusively on the network thread.
38 class CronetURLRequestAdapter
: public net::URLRequest::Delegate
{
40 // The delegate which is called when the request adapter completes a step.
41 // Always called on network thread.
42 class CronetURLRequestAdapterDelegate
{
44 // Called on redirect. Consumer should either destroy the request, or
45 // call FollowDeferredRedirect.
46 virtual void OnRedirect(const GURL
& newLocation
, int http_status_code
) = 0;
47 // Called when response has started and headers have been received. Consumer
48 // should either destroy the request, or call ReadData.
49 virtual void OnResponseStarted(int http_status_code
) = 0;
50 // Called when response bytes were read. Consumer should consume data and
51 // either destroy the request, or call ReadData. The request may only be
52 // destroyed after the embedder is done with |bytes|, as deleting the
53 // request frees the buffer.
54 virtual void OnBytesRead(unsigned char* bytes
,
56 // Called when response has finished successfully. Consumer should destroy
58 virtual void OnRequestFinished() = 0;
59 // Called when response has finished with error. Consumer should destroy
61 virtual void OnError(int net_error
) = 0;
63 virtual ~CronetURLRequestAdapterDelegate() {}
66 CronetURLRequestAdapter(
67 CronetURLRequestContextAdapter
* context
,
68 scoped_ptr
<CronetURLRequestAdapterDelegate
> delegate
,
70 net::RequestPriority priority
);
71 ~CronetURLRequestAdapter() override
;
73 // Methods called prior to Start are never called on network thread.
75 // Sets the request method GET, POST etc.
76 void set_method(const std::string
& method
) {
77 initial_method_
= method
;
80 // Adds a header to the request before it starts.
81 void AddRequestHeader(const std::string
& name
, const std::string
& value
);
83 // Methods called on any thread.
85 // Posts tasks to network thread.
86 bool PostTaskToNetworkThread(const tracked_objects::Location
& from_here
,
87 const base::Closure
& task
);
89 // Returns true if called on network thread.
90 bool IsOnNetworkThread() const;
92 // Methods called only on network thread.
94 // Starts the request.
98 void FollowDeferredRedirect();
103 // Releases all resources for the request and deletes the object itself.
106 // When called during a OnRedirect or OnResponseStarted callback, these
107 // methods return the corresponding response information.
109 // Gets all response headers, as a HttpResponseHeaders object. Returns NULL
110 // if the last attempted request received no headers.
111 const net::HttpResponseHeaders
* GetResponseHeaders() const;
113 // Gets NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo.
114 const std::string
& GetNegotiatedProtocol() const;
116 // Returns true if response is coming from the cache.
117 bool GetWasCached() const;
119 // Gets the total amount of body data received from network after
120 // SSL/SPDY/QUIC decoding and proxy handling. Basically the size of the body
121 // prior to decompression.
122 int64
GetTotalReceivedBytes() const;
124 // net::URLRequest::Delegate overrides.
125 void OnReceivedRedirect(net::URLRequest
* request
,
126 const net::RedirectInfo
& redirect_info
,
127 bool* defer_redirect
) override
;
128 void OnResponseStarted(net::URLRequest
* request
) override
;
129 void OnReadCompleted(net::URLRequest
* request
, int bytes_read
) override
;
132 // Checks status of the request_adapter, return false if |is_success()| is
133 // true, otherwise report error and cancel request_adapter.
134 bool MaybeReportError(net::URLRequest
* request
) const;
136 CronetURLRequestContextAdapter
* context_
;
137 scoped_ptr
<CronetURLRequestAdapterDelegate
> delegate_
;
139 const GURL initial_url_
;
140 const net::RequestPriority initial_priority_
;
141 std::string initial_method_
;
142 net::HttpRequestHeaders initial_request_headers_
;
144 scoped_refptr
<net::IOBufferWithSize
> read_buffer_
;
145 scoped_ptr
<net::URLRequest
> url_request_
;
147 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestAdapter
);
150 } // namespace cronet
152 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_