OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ |
| 6 #define COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ |
| 7 |
| 8 #include <jni.h> |
| 9 |
| 10 #include <string> |
| 11 |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "net/base/request_priority.h" |
| 16 #include "net/http/http_request_headers.h" |
| 17 #include "net/url_request/url_request.h" |
| 18 |
| 19 namespace base { |
| 20 class SingleThreadTaskRunner; |
| 21 } // namespace base |
| 22 |
| 23 namespace net { |
| 24 class GrowableIOBuffer; |
| 25 class HttpResponseHeaders; |
| 26 class UploadDataStream; |
| 27 } // namespace net |
| 28 |
| 29 namespace cronet { |
| 30 |
| 31 class CronetURLRequestContextAdapter; |
| 32 |
| 33 // An adapter from the JNI |UrlRequest| object to the Chromium |URLRequest| |
| 34 // object. |
| 35 class CronetURLRequestAdapter : public net::URLRequest::Delegate { |
| 36 public: |
| 37 // The delegate which is called when the request_adapter finishes. |
| 38 class CronetURLRequestAdapterDelegate |
| 39 : public base::RefCountedThreadSafe<CronetURLRequestAdapterDelegate> { |
| 40 public: |
| 41 virtual void OnRedirect(CronetURLRequestAdapter* request_adapter, |
| 42 const GURL& newLocation) = 0; |
| 43 virtual void OnResponseStarted( |
| 44 CronetURLRequestAdapter* request_adapter) = 0; |
| 45 virtual void OnBytesRead(CronetURLRequestAdapter* request_adapter, |
| 46 int bytes_read) = 0; |
| 47 virtual void OnRequestCanceled( |
| 48 CronetURLRequestAdapter* request_adapter) = 0; |
| 49 virtual void OnRequestFinished( |
| 50 CronetURLRequestAdapter* request_adapter) = 0; |
| 51 virtual void OnError(CronetURLRequestAdapter* request_adapter, |
| 52 int error) = 0; |
| 53 |
| 54 protected: |
| 55 friend class base::RefCountedThreadSafe<CronetURLRequestAdapterDelegate>; |
| 56 virtual ~CronetURLRequestAdapterDelegate() {} |
| 57 }; |
| 58 |
| 59 CronetURLRequestAdapter(CronetURLRequestContextAdapter* context, |
| 60 CronetURLRequestAdapterDelegate* delegate, |
| 61 GURL url, |
| 62 net::RequestPriority priority); |
| 63 virtual ~CronetURLRequestAdapter(); |
| 64 |
| 65 // Sets the request_adapter method GET, POST etc |
| 66 void SetMethod(const std::string& method); |
| 67 |
| 68 // Adds a header to the request_adapter |
| 69 void AddRequestHeader(const std::string& name, const std::string& value); |
| 70 |
| 71 // Starts the request_adapter. |
| 72 void Start(); |
| 73 |
| 74 // Follow redirect. |
| 75 void FollowDeferredRedirect(); |
| 76 |
| 77 // Read more data. |
| 78 void ReadData(); |
| 79 |
| 80 // Cancels the request_adapter. |
| 81 void Cancel(); |
| 82 |
| 83 // Releases all resources for the request_adapter and deletes the object |
| 84 // itself. |
| 85 void Destroy(); |
| 86 |
| 87 // Returns the URL of the request_adapter. |
| 88 GURL url() const { return url_; } |
| 89 |
| 90 // Returns the HTTP status code. |
| 91 int http_status_code() const { return http_status_code_; }; |
| 92 |
| 93 // Get all response headers, as a HttpResponseHeaders object. |
| 94 net::HttpResponseHeaders* GetResponseHeaders() const; |
| 95 |
| 96 // Returns a pointer to the downloaded data. |
| 97 unsigned char* Data() const; |
| 98 |
| 99 // Get NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo. |
| 100 std::string GetNegotiatedProtocol() const; |
| 101 |
| 102 // Returns true if response is coming from the cache. |
| 103 bool GetWasCached() const; |
| 104 |
| 105 // Gets the total amount of data received from network after SSL decoding and |
| 106 // proxy handling. |
| 107 int64 GetTotalReceivedBytes() const; |
| 108 |
| 109 // net::URLRequest::Delegate overrides |
| 110 void OnReceivedRedirect(net::URLRequest* request, |
| 111 const net::RedirectInfo& redirect_info, |
| 112 bool* defer_redirect) override; |
| 113 |
| 114 void OnResponseStarted(net::URLRequest* request) override; |
| 115 |
| 116 void OnReadCompleted(net::URLRequest* request, int bytes_read) override; |
| 117 // Return true if currently running on network thread. |
| 118 bool IsOnNetworkThread() const; |
| 119 |
| 120 private: |
| 121 void StartOnNetworkThread(); |
| 122 void FollowDeferredRedirectOnNetworkThread(); |
| 123 void ReadDataOnNetworkThread(); |
| 124 void CancelOnNetworkThread(); |
| 125 // Check status of the request_adapter, return true if |is_success()| is true, |
| 126 // otherwise report error and cancel request_adapter. |
| 127 bool CheckStatus(net::URLRequest* request); |
| 128 |
| 129 CronetURLRequestContextAdapter* context_; |
| 130 scoped_refptr<CronetURLRequestAdapterDelegate> delegate_; |
| 131 GURL url_; |
| 132 net::RequestPriority priority_; |
| 133 std::string method_; |
| 134 net::HttpRequestHeaders request_headers_; |
| 135 scoped_ptr<net::URLRequest> url_request_; |
| 136 scoped_refptr<net::IOBufferWithSize> read_buffer_; |
| 137 int http_status_code_; |
| 138 bool canceled_; |
| 139 |
| 140 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestAdapter); |
| 141 }; |
| 142 |
| 143 } // namespace cronet |
| 144 |
| 145 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ |
OLD | NEW |