OLD | NEW |
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_PEER_H_ | 5 #ifndef COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_ |
6 #define COMPONENTS_CRONET_ANDROID_URL_REQUEST_PEER_H_ | 6 #define COMPONENTS_CRONET_ANDROID_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 URLRequestContextPeer; | 27 class URLRequestContextAdapter; |
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 URLRequestPeer : public net::URLRequest::Delegate { | 31 class URLRequestAdapter : public net::URLRequest::Delegate { |
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 URLRequestPeerDelegate | 34 class URLRequestAdapterDelegate |
35 : public base::RefCountedThreadSafe<URLRequestPeerDelegate> { | 35 : public base::RefCountedThreadSafe<URLRequestAdapterDelegate> { |
36 public: | 36 public: |
37 virtual void OnResponseStarted(URLRequestPeer* request) = 0; | 37 virtual void OnResponseStarted(URLRequestAdapter* request) = 0; |
38 virtual void OnBytesRead(URLRequestPeer* request) = 0; | 38 virtual void OnBytesRead(URLRequestAdapter* request) = 0; |
39 virtual void OnRequestFinished(URLRequestPeer* request) = 0; | 39 virtual void OnRequestFinished(URLRequestAdapter* request) = 0; |
40 virtual int ReadFromUploadChannel(net::IOBuffer* buf, int buf_length) = 0; | 40 virtual int ReadFromUploadChannel(net::IOBuffer* buf, int buf_length) = 0; |
41 | 41 |
42 protected: | 42 protected: |
43 friend class base::RefCountedThreadSafe<URLRequestPeerDelegate>; | 43 friend class base::RefCountedThreadSafe<URLRequestAdapterDelegate>; |
44 virtual ~URLRequestPeerDelegate() {} | 44 virtual ~URLRequestAdapterDelegate() {} |
45 }; | 45 }; |
46 | 46 |
47 URLRequestPeer(URLRequestContextPeer* context, | 47 URLRequestAdapter(URLRequestContextAdapter* context, |
48 URLRequestPeerDelegate* delegate, | 48 URLRequestAdapterDelegate* delegate, |
49 GURL url, | 49 GURL url, |
50 net::RequestPriority priority); | 50 net::RequestPriority priority); |
51 virtual ~URLRequestPeer(); | 51 virtual ~URLRequestAdapter(); |
52 | 52 |
53 // Sets the request method GET, POST etc | 53 // Sets the request method GET, POST etc |
54 void SetMethod(const std::string& method); | 54 void SetMethod(const std::string& method); |
55 | 55 |
56 // Adds a header to the request | 56 // Adds a header to the request |
57 void AddHeader(const std::string& name, const std::string& value); | 57 void AddHeader(const std::string& name, const std::string& value); |
58 | 58 |
59 // Sets the contents of the POST or PUT request | 59 // Sets the contents of the POST or PUT request |
60 void SetUploadContent(const char* bytes, int bytes_len); | 60 void SetUploadContent(const char* bytes, int bytes_len); |
61 | 61 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 | 100 |
101 // Returns a pointer to the downloaded data. | 101 // Returns a pointer to the downloaded data. |
102 unsigned char* Data() const; | 102 unsigned char* Data() const; |
103 | 103 |
104 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE; | 104 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE; |
105 | 105 |
106 virtual void OnReadCompleted(net::URLRequest* request, | 106 virtual void OnReadCompleted(net::URLRequest* request, |
107 int bytes_read) OVERRIDE; | 107 int bytes_read) OVERRIDE; |
108 | 108 |
109 private: | 109 private: |
110 static void OnDestroyRequest(URLRequestPeer* self); | 110 static void OnDestroyRequest(URLRequestAdapter* self); |
111 | 111 |
112 void OnInitiateConnection(); | 112 void OnInitiateConnection(); |
113 void OnCancelRequest(); | 113 void OnCancelRequest(); |
114 void OnRequestSucceeded(); | 114 void OnRequestSucceeded(); |
115 void OnRequestFailed(); | 115 void OnRequestFailed(); |
116 void OnRequestCompleted(); | 116 void OnRequestCompleted(); |
117 void OnRequestCanceled(); | 117 void OnRequestCanceled(); |
118 void OnBytesRead(int bytes_read); | 118 void OnBytesRead(int bytes_read); |
119 void OnAppendChunk(const char* bytes, int bytes_len, bool is_last_chunk); | 119 void OnAppendChunk(const char* bytes, int bytes_len, bool is_last_chunk); |
120 | 120 |
121 void Read(); | 121 void Read(); |
122 | 122 |
123 URLRequestContextPeer* context_; | 123 URLRequestContextAdapter* context_; |
124 scoped_refptr<URLRequestPeerDelegate> delegate_; | 124 scoped_refptr<URLRequestAdapterDelegate> delegate_; |
125 GURL url_; | 125 GURL url_; |
126 net::RequestPriority priority_; | 126 net::RequestPriority priority_; |
127 std::string method_; | 127 std::string method_; |
128 net::HttpRequestHeaders headers_; | 128 net::HttpRequestHeaders headers_; |
129 net::URLRequest* url_request_; | 129 net::URLRequest* url_request_; |
130 scoped_ptr<net::UploadDataStream> upload_data_stream_; | 130 scoped_ptr<net::UploadDataStream> upload_data_stream_; |
131 scoped_refptr<net::GrowableIOBuffer> read_buffer_; | 131 scoped_refptr<net::GrowableIOBuffer> read_buffer_; |
132 int bytes_read_; | 132 int bytes_read_; |
133 int total_bytes_read_; | 133 int total_bytes_read_; |
134 int error_code_; | 134 int error_code_; |
135 int http_status_code_; | 135 int http_status_code_; |
136 std::string content_type_; | 136 std::string content_type_; |
137 bool canceled_; | 137 bool canceled_; |
138 int64 expected_size_; | 138 int64 expected_size_; |
139 | 139 |
140 DISALLOW_COPY_AND_ASSIGN(URLRequestPeer); | 140 DISALLOW_COPY_AND_ASSIGN(URLRequestAdapter); |
141 }; | 141 }; |
142 | 142 |
143 } // namespace cronet | 143 } // namespace cronet |
144 | 144 |
145 #endif // COMPONENTS_CRONET_ANDROID_URL_REQUEST_PEER_H_ | 145 #endif // COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_ |
OLD | NEW |