Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(197)

Side by Side Diff: components/cronet/android/cronet_url_request_adapter.h

Issue 586143002: Initial implementation of Cronet Async API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move userAgent into config, introduce ExtendedResponseInfo. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_ 5 #ifndef COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_
6 #define COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_ 6 #define COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_
7 7
8 #include <jni.h> 8 #include <jni.h>
9 9
10 #include <string> 10 #include <string>
11 11
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "net/base/request_priority.h" 15 #include "net/base/request_priority.h"
16 #include "net/http/http_request_headers.h" 16 #include "net/http/http_request_headers.h"
17 #include "net/url_request/url_request.h" 17 #include "net/url_request/url_request.h"
18 18
19 namespace net { 19 namespace net {
20 class GrowableIOBuffer; 20 class GrowableIOBuffer;
21 class HttpResponseHeaders; 21 class HttpResponseHeaders;
22 class UploadDataStream; 22 class UploadDataStream;
23 } // namespace net 23 } // namespace net
24 24
25 namespace cronet { 25 namespace cronet {
26 26
27 class URLRequestContextAdapter; 27 class CronetURLRequestContextAdapter;
28 28
29 // An adapter from the JNI |UrlRequest| object and the Chromium |URLRequest| 29 // An adapter from the JNI |UrlRequest| object and the Chromium |URLRequest|
30 // object. 30 // object.
31 class URLRequestAdapter : public net::URLRequest::Delegate { 31 class CronetURLRequestAdapter : public net::URLRequest::Delegate {
mmenke 2014/10/02 15:24:09 I think we should make the JniCronetURLRequestAdap
mef 2014/10/02 22:07:43 Acknowledged. Per conversation we've agreed to hav
32 public: 32 public:
33 // The delegate which is called when the request finishes. 33 // The delegate which is called when the request finishes.
34 class URLRequestAdapterDelegate 34 class CronetURLRequestAdapterDelegate
35 : public base::RefCountedThreadSafe<URLRequestAdapterDelegate> { 35 : public base::RefCountedThreadSafe<CronetURLRequestAdapterDelegate> {
36 public: 36 public:
37 virtual void OnResponseStarted(URLRequestAdapter* request) = 0; 37 virtual void OnRedirect(CronetURLRequestAdapter* request,
38 virtual void OnBytesRead(URLRequestAdapter* request) = 0; 38 const GURL& newLocation) = 0;
39 virtual void OnRequestFinished(URLRequestAdapter* request) = 0; 39 virtual void OnResponseStarted(CronetURLRequestAdapter* request) = 0;
40 virtual int ReadFromUploadChannel(net::IOBuffer* buf, int buf_length) = 0; 40 virtual void OnBytesRead(CronetURLRequestAdapter* request,
41 int bytes_read) = 0;
42 virtual void OnRequestFinished(CronetURLRequestAdapter* request,
43 bool cancelled) = 0;
41 44
42 protected: 45 protected:
43 friend class base::RefCountedThreadSafe<URLRequestAdapterDelegate>; 46 friend class base::RefCountedThreadSafe<CronetURLRequestAdapterDelegate>;
44 virtual ~URLRequestAdapterDelegate() {} 47 virtual ~CronetURLRequestAdapterDelegate() {}
45 }; 48 };
46 49
47 URLRequestAdapter(URLRequestContextAdapter* context, 50 CronetURLRequestAdapter(CronetURLRequestContextAdapter* context,
48 URLRequestAdapterDelegate* delegate, 51 CronetURLRequestAdapterDelegate* delegate,
49 GURL url, 52 GURL url,
50 net::RequestPriority priority); 53 net::RequestPriority priority);
51 virtual ~URLRequestAdapter(); 54 virtual ~CronetURLRequestAdapter();
52 55
53 // Sets the request method GET, POST etc 56 // Sets the request method GET, POST etc
54 void SetMethod(const std::string& method); 57 void SetMethod(const std::string& method);
55 58
56 // Adds a header to the request 59 // Adds a header to the request
57 void AddHeader(const std::string& name, const std::string& value); 60 void AddHeader(const std::string& name, const std::string& value);
58 61
59 // Sets the contents of the POST or PUT request
60 void SetUploadContent(const char* bytes, int bytes_len);
61
62 // Sets the request to streaming upload.
63 void SetUploadChannel(JNIEnv* env, int64 content_length);
64
65 // Indicates that the request body will be streamed by calling AppendChunk()
66 // repeatedly. This must be called before Start().
67 void EnableChunkedUpload();
68
69 // Appends a chunk to the POST body.
70 // This must be called after EnableChunkedUpload() and Start().
71 void AppendChunk(const char* bytes, int bytes_len, bool is_last_chunk);
72
73 // Starts the request. 62 // Starts the request.
74 void Start(); 63 void Start();
75 64
65 // Follow redirect.
66 void FollowDeferredRedirect();
67
68 // Read more data.
69 void ReadData();
70
76 // Cancels the request. 71 // Cancels the request.
77 void Cancel(); 72 void Cancel();
78 73
79 // Releases all resources for the request and deletes the object itself. 74 // Releases all resources for the request and deletes the object itself.
80 void Destroy(); 75 void Destroy();
81 76
82 // Returns the URL of the request. 77 // Returns the URL of the request.
83 GURL url() const { return url_; } 78 GURL url() const { return url_; }
84 79
85 // Returns the error code after the request is complete. 80 // Returns the error code after the request is complete.
(...skipping 19 matching lines...) Expand all
105 100
106 // Returns the overall number of bytes read. 101 // Returns the overall number of bytes read.
107 size_t bytes_read() const { return bytes_read_; } 102 size_t bytes_read() const { return bytes_read_; }
108 103
109 // Returns a pointer to the downloaded data. 104 // Returns a pointer to the downloaded data.
110 unsigned char* Data() const; 105 unsigned char* Data() const;
111 106
112 // Get NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo. 107 // Get NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo.
113 std::string GetNegotiatedProtocol() const; 108 std::string GetNegotiatedProtocol() const;
114 109
110 // Returns true if response is coming from the cache.
111 bool GetWasCached() const;
112
113 // Gets the total amount of data received from network after SSL decoding and
114 // proxy handling.
115 int64 GetTotalReceivedBytes() const;
116
117 // net::URLRequest::Delegate overrides
118 virtual void OnReceivedRedirect(net::URLRequest* request,
119 const net::RedirectInfo& redirect_info,
120 bool* defer_redirect) OVERRIDE;
121
115 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE; 122 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE;
116 123
117 virtual void OnReadCompleted(net::URLRequest* request, 124 virtual void OnReadCompleted(net::URLRequest* request,
118 int bytes_read) OVERRIDE; 125 int bytes_read) OVERRIDE;
119 126
120 private: 127 private:
121 static void OnDestroyRequest(URLRequestAdapter* self); 128 static void DestroyOnNetworkThread(CronetURLRequestAdapter* self);
129 void StartOnNetworkThread();
130 void FollowDeferredRedirectOnNetworkThread();
131 void ReadDataOnNetworkThread();
132 void CancelOnNetworkThread();
122 133
123 void OnInitiateConnection(); 134 CronetURLRequestContextAdapter* context_;
124 void OnCancelRequest(); 135 scoped_refptr<CronetURLRequestAdapterDelegate> delegate_;
125 void OnRequestSucceeded();
126 void OnRequestFailed();
127 void OnRequestCompleted();
128 void OnRequestCanceled();
129 void OnBytesRead(int bytes_read);
130 void OnAppendChunk(const scoped_ptr<char[]> bytes, int bytes_len,
131 bool is_last_chunk);
132
133 void Read();
134
135 URLRequestContextAdapter* context_;
136 scoped_refptr<URLRequestAdapterDelegate> delegate_;
137 GURL url_; 136 GURL url_;
138 net::RequestPriority priority_; 137 net::RequestPriority priority_;
139 std::string method_; 138 std::string method_;
140 net::HttpRequestHeaders headers_; 139 net::HttpRequestHeaders headers_;
141 scoped_ptr<net::URLRequest> url_request_; 140 scoped_ptr<net::URLRequest> url_request_;
142 scoped_ptr<net::UploadDataStream> upload_data_stream_; 141 scoped_refptr<net::IOBufferWithSize> read_buffer_;
143 scoped_refptr<net::GrowableIOBuffer> read_buffer_;
144 int bytes_read_; 142 int bytes_read_;
145 int total_bytes_read_; 143 int total_bytes_read_;
146 int error_code_; 144 int error_code_;
147 int http_status_code_; 145 int http_status_code_;
148 std::string content_type_; 146 std::string content_type_;
149 bool canceled_; 147 bool canceled_;
150 int64 expected_size_; 148 int64 expected_size_;
151 bool chunked_upload_; 149 bool chunked_upload_;
152 150
153 DISALLOW_COPY_AND_ASSIGN(URLRequestAdapter); 151 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestAdapter);
154 }; 152 };
155 153
156 } // namespace cronet 154 } // namespace cronet
157 155
158 #endif // COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_ 156 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698