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
;
26 class UploadDataStream
;
31 class CronetURLRequestContextAdapter
;
33 // An adapter from the JNI CronetUrlRequest object to the Chromium URLRequest.
34 // Created and configured from random thread. Start is posted to network
35 // thread and all callbacks into CronetURLRequestAdapterDelegate are done on
36 // network thread. Each delegate callback is expected to initiate next step like
37 // FollowDeferredRedirect, ReadData or Destroy. All methods except those needed
38 // to set up a request must be called exclusively on the network thread.
39 class CronetURLRequestAdapter
: public net::URLRequest::Delegate
{
41 // The delegate which is called when the request adapter completes a step.
42 // Always called on network thread.
43 class CronetURLRequestAdapterDelegate
{
45 // Called on redirect. Consumer should either destroy the request, or
46 // call FollowDeferredRedirect.
47 virtual void OnRedirect(const GURL
& newLocation
, int http_status_code
) = 0;
48 // Called when response has started and headers have been received. Consumer
49 // should either destroy the request, or call ReadData.
50 virtual void OnResponseStarted(int http_status_code
) = 0;
51 // Called when response bytes were read. Consumer should consume data and
52 // either destroy the request, or call ReadData. The request may only be
53 // destroyed after the embedder is done with |bytes|, as deleting the
54 // request frees the buffer.
55 virtual void OnBytesRead(unsigned char* bytes
,
57 // Called when response has finished successfully. Consumer should destroy
59 virtual void OnRequestFinished() = 0;
60 // Called when response has finished with error. Consumer should destroy
62 virtual void OnError(int net_error
) = 0;
64 virtual ~CronetURLRequestAdapterDelegate() {}
67 CronetURLRequestAdapter(
68 CronetURLRequestContextAdapter
* context
,
69 scoped_ptr
<CronetURLRequestAdapterDelegate
> delegate
,
71 net::RequestPriority priority
);
72 ~CronetURLRequestAdapter() override
;
74 // Methods called prior to Start are never called on network thread.
76 // Sets the request method GET, POST etc.
77 void set_method(const std::string
& method
) {
78 initial_method_
= method
;
81 // Adds a header to the request before it starts.
82 void AddRequestHeader(const std::string
& name
, const std::string
& value
);
84 // Adds a request body to the request before it starts.
85 void SetUpload(scoped_ptr
<net::UploadDataStream
> upload
);
87 // Methods called on any thread.
89 // Posts tasks to network thread.
90 void PostTaskToNetworkThread(const tracked_objects::Location
& from_here
,
91 const base::Closure
& task
);
93 // Returns true if called on network thread.
94 bool IsOnNetworkThread() const;
96 // Methods called only on network thread.
98 // Starts the request.
102 void FollowDeferredRedirect();
107 // Releases all resources for the request and deletes the object itself.
110 // When called during a OnRedirect or OnResponseStarted callback, these
111 // methods return the corresponding response information.
113 // Gets all response headers, as a HttpResponseHeaders object. Returns NULL
114 // if the last attempted request received no headers.
115 const net::HttpResponseHeaders
* GetResponseHeaders() const;
117 // Gets NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo.
118 const std::string
& GetNegotiatedProtocol() const;
120 // Returns true if response is coming from the cache.
121 bool GetWasCached() const;
123 // Gets the total amount of body data received from network after
124 // SSL/SPDY/QUIC decoding and proxy handling. Basically the size of the body
125 // prior to decompression.
126 int64
GetTotalReceivedBytes() const;
128 // Bypasses cache. If context is not set up to use cache, this call has no
132 // net::URLRequest::Delegate overrides.
133 void OnReceivedRedirect(net::URLRequest
* request
,
134 const net::RedirectInfo
& redirect_info
,
135 bool* defer_redirect
) override
;
136 void OnResponseStarted(net::URLRequest
* request
) override
;
137 void OnReadCompleted(net::URLRequest
* request
, int bytes_read
) override
;
140 // Checks status of the request_adapter, return false if |is_success()| is
141 // true, otherwise report error and cancel request_adapter.
142 bool MaybeReportError(net::URLRequest
* request
) const;
144 CronetURLRequestContextAdapter
* context_
;
145 scoped_ptr
<CronetURLRequestAdapterDelegate
> delegate_
;
147 const GURL initial_url_
;
148 const net::RequestPriority initial_priority_
;
149 std::string initial_method_
;
151 net::HttpRequestHeaders initial_request_headers_
;
152 scoped_ptr
<net::UploadDataStream
> upload_
;
154 scoped_refptr
<net::IOBufferWithSize
> read_buffer_
;
155 scoped_ptr
<net::URLRequest
> url_request_
;
157 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestAdapter
);
160 } // namespace cronet
162 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_