Chromium Code Reviews| Index: components/cronet/android/cronet_url_request_adapter.h |
| diff --git a/components/cronet/android/cronet_url_request_adapter.h b/components/cronet/android/cronet_url_request_adapter.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..55abe8d3f747a6ffd254fa35deaa613514f1fb44 |
| --- /dev/null |
| +++ b/components/cronet/android/cronet_url_request_adapter.h |
| @@ -0,0 +1,131 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ |
| +#define COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ |
| + |
| +#include <jni.h> |
| + |
| +#include <string> |
| + |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "net/base/request_priority.h" |
| +#include "net/http/http_request_headers.h" |
| +#include "net/url_request/url_request.h" |
| +#include "url/gurl.h" |
| + |
| +namespace base { |
| +class SingleThreadTaskRunner; |
| +} // namespace base |
| + |
| +namespace net { |
| +class GrowableIOBuffer; |
| +class HttpResponseHeaders; |
| +class UploadDataStream; |
| +} // namespace net |
| + |
| +namespace cronet { |
| + |
| +class CronetURLRequestContextAdapter; |
| + |
| +// An adapter from the JNI CronetUrlRequest object to the Chromium URLRequest. |
| +class CronetURLRequestAdapter : public net::URLRequest::Delegate { |
| + public: |
| + // The delegate which is called when the request_adapter finishes. |
| + class CronetURLRequestAdapterDelegate |
| + : public base::RefCountedThreadSafe<CronetURLRequestAdapterDelegate> { |
|
mmenke
2014/10/31 19:16:23
I don't think this needs to be refcounted now, doe
mef
2014/10/31 21:43:15
Done.
|
| + public: |
| + virtual void OnRedirect(const GURL& newLocation, int http_status_code) = 0; |
| + virtual void OnResponseStarted(int http_status_code) = 0; |
| + virtual void OnBytesRead(unsigned char* bytes_buffer, |
| + int bytes_read) = 0; |
| + virtual void OnRequestFinished() = 0; |
| + virtual void OnError(int error) = 0; |
| + |
| + protected: |
| + friend class base::RefCountedThreadSafe<CronetURLRequestAdapterDelegate>; |
| + virtual ~CronetURLRequestAdapterDelegate() {} |
| + }; |
| + |
| + CronetURLRequestAdapter(CronetURLRequestContextAdapter* context, |
| + CronetURLRequestAdapterDelegate* delegate, |
|
mmenke
2014/10/31 19:16:22
The delegate should probably be passed as a const
mef
2014/10/31 21:43:15
Done.
|
| + const GURL& url, |
| + net::RequestPriority priority); |
| + ~CronetURLRequestAdapter() override; |
| + |
| + // Sets the request method GET, POST etc. |
| + void SetMethod(const std::string& method); |
|
mmenke
2014/10/31 19:16:23
nit: set_method + inline?
mef
2014/10/31 20:39:15
Done.
|
| + |
| + // Adds a header to the request before it starts. |
| + void AddRequestHeader(const std::string& name, const std::string& value); |
| + |
| + // Starts the request. |
| + void Start(); |
| + |
| + // Follows redirect. |
| + void FollowDeferredRedirect(); |
| + |
| + // Reads more data. |
| + void ReadData(); |
| + |
| + // Releases all resources for the request and deletes the object |
| + // itself. |
| + void Destroy(); |
| + |
| + // Returns the URL of the request. |
| + const GURL& url() const { return initial_url_; } |
| + |
| + // Gets all response headers, as a HttpResponseHeaders object. |
| + net::HttpResponseHeaders* GetResponseHeaders() const; |
| + |
| + // Returns a pointer to the downloaded data. |
| + unsigned char* Data() const; |
| + |
| + // Gets NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo. |
| + std::string GetNegotiatedProtocol() const; |
|
mmenke
2014/10/31 19:16:23
const std::string&?
mef
2014/10/31 20:39:15
It returns an empty string if |url_request_| is nu
mmenke
2014/10/31 21:03:24
Per earlier comment, that doesn't happen. :)
mef
2014/10/31 21:43:15
Done.
|
| + |
| + // Returns true if response is coming from the cache. |
| + bool GetWasCached() const; |
| + |
| + // Gets the total amount of data received from network after SSL decoding and |
| + // proxy handling. |
| + int64 GetTotalReceivedBytes() const; |
| + |
| + // net::URLRequest::Delegate overrides |
| + void OnReceivedRedirect(net::URLRequest* request, |
| + const net::RedirectInfo& redirect_info, |
| + bool* defer_redirect) override; |
| + |
| + void OnResponseStarted(net::URLRequest* request) override; |
| + |
|
mmenke
2014/10/31 19:16:23
nit: Remove blank line between overrides from sam
mef
2014/10/31 20:39:15
Done.
|
| + void OnReadCompleted(net::URLRequest* request, int bytes_read) override; |
| + // Returns true if currently running on network thread. |
|
mmenke
2014/10/31 19:16:23
Add blank line before comment.
mef
2014/10/31 20:39:15
Done.
|
| + bool IsOnNetworkThread() const; |
| + |
| + private: |
| + void StartOnNetworkThread(); |
| + void FollowDeferredRedirectOnNetworkThread(); |
| + void ReadDataOnNetworkThread(); |
| + void DestroyOnNetworkThread(); |
|
mmenke
2014/10/31 19:16:23
nit: Suggest blank line after DestroyOnNetworkThr
mef
2014/10/31 20:39:15
Done.
|
| + // Checks status of the request_adapter, return true if |is_success()| is |
| + // true, otherwise report error and cancel request_adapter. |
| + bool CheckStatus(net::URLRequest* request); |
|
mmenke
2014/10/31 19:16:22
nit: const
mef
2014/10/31 20:39:15
Done.
|
| + |
| + CronetURLRequestContextAdapter* context_; |
| + scoped_refptr<CronetURLRequestAdapterDelegate> delegate_; |
| + GURL initial_url_; |
| + net::RequestPriority initial_priority_; |
|
mmenke
2014/10/31 19:16:22
A couple of these can be const.
mef
2014/10/31 20:39:15
Done.
|
| + std::string initial_method_; |
| + net::HttpRequestHeaders initial_request_headers_; |
| + scoped_ptr<net::URLRequest> url_request_; |
|
mmenke
2014/10/31 19:16:22
The request should probably go last, since it depe
mmenke
2014/10/31 19:16:22
Can this be a "const scoped_ptr<net::URLRequest>"
mef
2014/10/31 20:39:15
Um, then how is it set for the first time (It is N
mmenke
2014/10/31 21:03:24
Oops. Good point.
|
| + scoped_refptr<net::IOBufferWithSize> read_buffer_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(CronetURLRequestAdapter); |
| +}; |
| + |
| +} // namespace cronet |
| + |
| +#endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ |