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