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_URL_REQUEST_PEER_H_ | |
6 #define COMPONENTS_CRONET_ANDROID_URL_REQUEST_PEER_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 net { | |
20 class GrowableIOBuffer; | |
21 class HttpResponseHeaders; | |
22 class UploadDataStream; | |
23 } // namespace net | |
24 | |
25 namespace cronet { | |
26 | |
27 class URLRequestContextPeer; | |
28 | |
29 // An adapter from the JNI |UrlRequest| object and the Chromium |URLRequest| | |
30 // object. | |
31 class URLRequestPeer : public net::URLRequest::Delegate { | |
32 public: | |
33 // The delegate which is called when the request finishes. | |
34 class URLRequestPeerDelegate | |
35 : public base::RefCountedThreadSafe<URLRequestPeerDelegate> { | |
36 public: | |
37 virtual void OnResponseStarted(URLRequestPeer* request) = 0; | |
38 virtual void OnBytesRead(URLRequestPeer* request) = 0; | |
39 virtual void OnRequestFinished(URLRequestPeer* request) = 0; | |
40 virtual int ReadFromUploadChannel(net::IOBuffer* buf, int buf_length) = 0; | |
41 | |
42 protected: | |
43 friend class base::RefCountedThreadSafe<URLRequestPeerDelegate>; | |
44 virtual ~URLRequestPeerDelegate() {} | |
45 }; | |
46 | |
47 URLRequestPeer(URLRequestContextPeer* context, | |
48 URLRequestPeerDelegate* delegate, | |
49 GURL url, | |
50 net::RequestPriority priority); | |
51 virtual ~URLRequestPeer(); | |
52 | |
53 // Sets the request method GET, POST etc | |
54 void SetMethod(const std::string& method); | |
55 | |
56 // Adds a header to the request | |
57 void AddHeader(const std::string& name, const std::string& value); | |
58 | |
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 // Starts the request. | |
66 void Start(); | |
67 | |
68 // Cancels the request. | |
69 void Cancel(); | |
70 | |
71 // Releases all resources for the request and deletes the object itself. | |
72 void Destroy(); | |
73 | |
74 // Returns the URL of the request. | |
75 GURL url() const { return url_; } | |
76 | |
77 // Returns the error code after the request is complete. | |
78 // Negative codes indicate system errors. | |
79 int error_code() const { return error_code_; } | |
80 | |
81 // Returns the HTTP status code. | |
82 int http_status_code() const { | |
83 return http_status_code_; | |
84 }; | |
85 | |
86 // Returns the value of the content-length response header. | |
87 int64 content_length() const { return expected_size_; } | |
88 | |
89 // Returns the value of the content-type response header. | |
90 std::string content_type() const { return content_type_; } | |
91 | |
92 // Returns the value of the specified response header. | |
93 std::string GetHeader(const std::string& name) const; | |
94 | |
95 // Get all response headers, as a HttpResponseHeaders object. | |
96 net::HttpResponseHeaders* GetResponseHeaders() const; | |
97 | |
98 // Returns the overall number of bytes read. | |
99 size_t bytes_read() const { return bytes_read_; } | |
100 | |
101 // Returns a pointer to the downloaded data. | |
102 unsigned char* Data() const; | |
103 | |
104 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE; | |
105 | |
106 virtual void OnReadCompleted(net::URLRequest* request, | |
107 int bytes_read) OVERRIDE; | |
108 | |
109 private: | |
110 static void OnDestroyRequest(URLRequestPeer* self); | |
111 | |
112 void OnInitiateConnection(); | |
113 void OnCancelRequest(); | |
114 void OnRequestSucceeded(); | |
115 void OnRequestFailed(); | |
116 void OnRequestCompleted(); | |
117 void OnRequestCanceled(); | |
118 void OnBytesRead(int bytes_read); | |
119 void OnAppendChunk(const char* bytes, int bytes_len, bool is_last_chunk); | |
120 | |
121 void Read(); | |
122 | |
123 URLRequestContextPeer* context_; | |
124 scoped_refptr<URLRequestPeerDelegate> delegate_; | |
125 GURL url_; | |
126 net::RequestPriority priority_; | |
127 std::string method_; | |
128 net::HttpRequestHeaders headers_; | |
129 net::URLRequest* url_request_; | |
130 scoped_ptr<net::UploadDataStream> upload_data_stream_; | |
131 scoped_refptr<net::GrowableIOBuffer> read_buffer_; | |
132 int bytes_read_; | |
133 int total_bytes_read_; | |
134 int error_code_; | |
135 int http_status_code_; | |
136 std::string content_type_; | |
137 bool canceled_; | |
138 int64 expected_size_; | |
139 | |
140 DISALLOW_COPY_AND_ASSIGN(URLRequestPeer); | |
141 }; | |
142 | |
143 } // namespace cronet | |
144 | |
145 #endif // COMPONENTS_CRONET_ANDROID_URL_REQUEST_PEER_H_ | |
OLD | NEW |