| 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 #include "url/gurl.h" |
| 19 |
| 20 namespace base { |
| 21 class SingleThreadTaskRunner; |
| 22 } // namespace base |
| 23 |
| 24 namespace net { |
| 25 class GrowableIOBuffer; |
| 26 class HttpResponseHeaders; |
| 27 class UploadDataStream; |
| 28 } // namespace net |
| 29 |
| 30 namespace cronet { |
| 31 |
| 32 class CronetURLRequestContextAdapter; |
| 33 |
| 34 // An adapter from the JNI CronetUrlRequest object to the Chromium URLRequest. |
| 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(const GURL& newLocation) = 0; |
| 42 virtual void OnResponseStarted() = 0; |
| 43 virtual void OnBytesRead(unsigned char* bytes_buffer, |
| 44 int bytes_read) = 0; |
| 45 virtual void OnRequestCanceled() = 0; |
| 46 virtual void OnRequestFinished() = 0; |
| 47 virtual void OnError(int error) = 0; |
| 48 |
| 49 protected: |
| 50 friend class base::RefCountedThreadSafe<CronetURLRequestAdapterDelegate>; |
| 51 virtual ~CronetURLRequestAdapterDelegate() {} |
| 52 }; |
| 53 |
| 54 CronetURLRequestAdapter(CronetURLRequestContextAdapter* context, |
| 55 CronetURLRequestAdapterDelegate* delegate, |
| 56 const GURL& url, |
| 57 net::RequestPriority priority); |
| 58 ~CronetURLRequestAdapter() override; |
| 59 |
| 60 // Sets the request method GET, POST etc. |
| 61 void SetMethod(const std::string& method); |
| 62 |
| 63 // Adds a header to the request before it starts. |
| 64 void AddRequestHeader(const std::string& name, const std::string& value); |
| 65 |
| 66 // Starts the request. |
| 67 void Start(); |
| 68 |
| 69 // Follow redirect. |
| 70 void FollowDeferredRedirect(); |
| 71 |
| 72 // Read more data. |
| 73 void ReadData(); |
| 74 |
| 75 // Cancels the request. |
| 76 void Cancel(); |
| 77 |
| 78 // Releases all resources for the request and deletes the object |
| 79 // itself. |
| 80 void Destroy(); |
| 81 |
| 82 // Returns the URL of the request. |
| 83 const GURL& url() const { return initial_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 void OnReceivedRedirect(net::URLRequest* request, |
| 106 const net::RedirectInfo& redirect_info, |
| 107 bool* defer_redirect) override; |
| 108 |
| 109 void OnResponseStarted(net::URLRequest* request) override; |
| 110 |
| 111 void OnReadCompleted(net::URLRequest* request, int bytes_read) override; |
| 112 // Return true if currently running on network thread. |
| 113 bool IsOnNetworkThread() const; |
| 114 |
| 115 private: |
| 116 void StartOnNetworkThread(); |
| 117 void FollowDeferredRedirectOnNetworkThread(); |
| 118 void ReadDataOnNetworkThread(); |
| 119 void CancelOnNetworkThread(); |
| 120 // Check whether request was cancelled while |called_to_delegate_| and if so, |
| 121 // then perform actual cancellation and return true. |
| 122 bool WasCanceledWhileCalledToDelegate(); |
| 123 // Check status of the request_adapter, return true if |is_success()| is true, |
| 124 // otherwise report error and cancel request_adapter. |
| 125 bool CheckStatus(net::URLRequest* request); |
| 126 |
| 127 CronetURLRequestContextAdapter* context_; |
| 128 scoped_refptr<CronetURLRequestAdapterDelegate> delegate_; |
| 129 GURL initial_url_; |
| 130 net::RequestPriority initial_priority_; |
| 131 std::string initial_method_; |
| 132 net::HttpRequestHeaders initial_request_headers_; |
| 133 scoped_ptr<net::URLRequest> url_request_; |
| 134 scoped_refptr<net::IOBufferWithSize> read_buffer_; |
| 135 int http_status_code_; |
| 136 bool called_to_delegate_; |
| 137 // Set to |true| if request should be canceled upon return from the delegate. |
| 138 bool cancel_soon_; |
| 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 |