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..f2cbb6c7a971ff821048751d8a2ce91d5d367112 |
| --- /dev/null |
| +++ b/components/cronet/android/cronet_url_request_adapter.h |
| @@ -0,0 +1,148 @@ |
| +// 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 <string> |
| + |
| +#include "base/callback.h" |
| +#include "base/location.h" |
| +#include "base/macros.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; |
| +} // namespace net |
| + |
| +namespace cronet { |
| + |
| +class CronetURLRequestContextAdapter; |
| + |
| +// An adapter from the JNI CronetUrlRequest object to the Chromium URLRequest. |
| +// Created and configured from random thread. Start is posted to network |
| +// thread and all callbacks into CronetURLRequestAdapterDelegate are done on |
| +// network thread. Each delegate callback is expected to initiate next step like |
| +// FollowDeferredRedirect, ReadData or Destroy from random thread, which post |
| +// to network thread. |
|
mmenke
2014/11/07 15:19:09
Last two sentences are now incorrect.
Maybe "All
mef
2014/11/07 16:22:25
Done.
|
| +class CronetURLRequestAdapter : public net::URLRequest::Delegate { |
| + public: |
| + // The delegate which is called when the request adapter completes a step. |
| + // Always called on network thread. |
| + class CronetURLRequestAdapterDelegate { |
| + public: |
| + // Called on redirect. Embedder should either destroy the request, or |
| + // call FollowDeferredRedirect. |
| + virtual void OnRedirect(const GURL& newLocation, int http_status_code) = 0; |
| + // Called when response has started and headers have been received. Embedder |
| + // should either destroy the request, or call ReadData. |
| + virtual void OnResponseStarted(int http_status_code) = 0; |
| + // Called when response bytes were read. Embedder should consume data and |
| + // either destroy the request, or call ReadData. |
|
mmenke
2014/11/07 15:19:08
Maybe add "The request may only be destroyed after
mef
2014/11/07 16:22:25
Done.
|
| + virtual void OnBytesRead(unsigned char* bytes_buffer, |
| + int bytes_read) = 0; |
| + // Called when response has finished successfully. Embedder should destroy |
| + // the request. |
| + virtual void OnRequestFinished() = 0; |
| + // Called when response has finished with error. Embedder should destroy |
| + // the request. |
|
mmenke
2014/11/07 15:19:09
Instead of "embedder", should we use "consumer", o
mef
2014/11/07 16:22:25
Done.
|
| + virtual void OnError(int net_error) = 0; |
| + |
| + virtual ~CronetURLRequestAdapterDelegate() {} |
| + }; |
| + |
| + CronetURLRequestAdapter( |
| + CronetURLRequestContextAdapter* context, |
| + scoped_ptr<CronetURLRequestAdapterDelegate> delegate, |
| + const GURL& url, |
| + net::RequestPriority priority); |
| + ~CronetURLRequestAdapter() override; |
| + |
| + // Methods called prior to Start are never called on network thread. |
| + |
| + // Sets the request method GET, POST etc. |
| + void set_method(const std::string& method) { |
| + initial_method_ = method; |
| + } |
| + |
| + // Adds a header to the request before it starts. |
| + void AddRequestHeader(const std::string& name, const std::string& value); |
| + |
| + // Methods called only on network thread. |
| + |
| + // 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(); |
| + |
| + // When called during a OnRedirect or OnResponseStarted callback, these |
| + // methods return the corresponding response information. |
| + |
| + // Gets all response headers, as a HttpResponseHeaders object. Returns NULL |
| + // if the last attempted request received no headers. |
| + const net::HttpResponseHeaders* GetResponseHeaders() const; |
| + |
| + // Gets NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo. |
| + const std::string& GetNegotiatedProtocol() const; |
| + |
| + // Returns true if response is coming from the cache. |
| + bool GetWasCached() const; |
| + |
| + // Gets the total amount of body data received from network after |
| + // SSL/SPDY/QUIC decoding and proxy handling. Basically the size of the body |
| + // prior to decompression. |
| + 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; |
| + void OnReadCompleted(net::URLRequest* request, int bytes_read) override; |
| + |
| + // Posts tasks to network thread. |
| + bool PostTaskToNetworkThread(const tracked_objects::Location& from_here, |
| + const base::Closure& task); |
| + |
| + // Returns true if currently running on network thread. |
|
mmenke
2014/11/07 15:19:09
nit: Maybe "currently running" -> run / called /
mef
2014/11/07 16:22:26
Done.
|
| + bool IsOnNetworkThread() const; |
|
mmenke
2014/11/07 15:19:08
Suggest moving these two methods up above the "Met
mef
2014/11/07 16:22:25
Done.
|
| + |
| + private: |
| + // Checks status of the request_adapter, return true if |is_success()| is |
| + // true, otherwise report error and cancel request_adapter. |
| + bool MaybeReportError(net::URLRequest* request) const; |
| + |
| + CronetURLRequestContextAdapter* context_; |
| + scoped_ptr<CronetURLRequestAdapterDelegate> delegate_; |
| + |
| + const GURL initial_url_; |
| + const net::RequestPriority initial_priority_; |
| + std::string initial_method_; |
| + net::HttpRequestHeaders initial_request_headers_; |
| + |
| + scoped_refptr<net::IOBufferWithSize> read_buffer_; |
| + scoped_ptr<net::URLRequest> url_request_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(CronetURLRequestAdapter); |
| +}; |
| + |
| +} // namespace cronet |
| + |
| +#endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ |