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 NET_CRONET_ANDROID_URL_REQUEST_PEER_H_ | |
6 #define NET_CRONET_ANDROID_URL_REQUEST_PEER_H_ | |
7 | |
8 #include "base/compiler_specific.h" | |
9 #include "base/macros.h" | |
10 #include "base/memory/ref_counted.h" | |
11 #include "net/base/request_priority.h" | |
12 #include "net/base/upload_data_stream.h" | |
13 #include "net/cronet/android/url_request_context_peer.h" | |
14 #include "net/http/http_request_headers.h" | |
15 #include "net/url_request/url_request.h" | |
16 | |
17 // An adapter from the JNI |UrlRequest| object and the Chromium |URLRequest| | |
18 // object. | |
19 class URLRequestPeer : public net::URLRequest::Delegate { | |
20 public: | |
21 // The delegate which is called when the request finishes. | |
22 class URLRequestPeerDelegate | |
23 : public base::RefCountedThreadSafe<URLRequestPeerDelegate> { | |
24 public: | |
25 virtual void OnAppendChunkCompleted(URLRequestPeer* request) = 0; | |
26 virtual void OnResponseStarted(URLRequestPeer* request) = 0; | |
27 virtual void OnBytesRead(URLRequestPeer* request) = 0; | |
28 virtual void OnRequestFinished(URLRequestPeer* request) = 0; | |
29 | |
30 protected: | |
31 friend class base::RefCountedThreadSafe<URLRequestPeerDelegate>; | |
32 virtual ~URLRequestPeerDelegate() {} | |
33 }; | |
34 | |
35 URLRequestPeer(URLRequestContextPeer* context, | |
36 URLRequestPeerDelegate* delegate, | |
37 GURL url, | |
38 net::RequestPriority priority); | |
39 virtual ~URLRequestPeer(); | |
40 | |
41 // Sets the request method GET, POST etc | |
42 void SetMethod(const std::string& method); | |
43 | |
44 // Adds a header to the request | |
45 void AddHeader(const std::string& name, const std::string& value); | |
46 | |
47 // Sets the contents of the POST request | |
48 void SetPostContent(const char* bytes, int bytes_len); | |
49 | |
50 // Indicates that the request body will be streamed by calling AppendChunk() | |
51 // repeatedly. This must be called before Start(). | |
52 void EnableStreamingUpload(); | |
53 | |
54 // Appends a chunk to the POST body | |
55 // This must be called after EnableChunkedUpload() and Start(). | |
56 void AppendChunk(const char* bytes, int bytes_len, bool is_last_chunk); | |
57 | |
58 // Starts the request. | |
59 void Start(); | |
60 | |
61 // Cancels the request. | |
62 void Cancel(); | |
63 | |
64 // Releases all resources for the request and deletes the object itself. | |
65 void Destroy(); | |
66 | |
67 // Returns the URL of the request. | |
68 GURL url() const { return url_; } | |
69 | |
70 // Returns the error code after the request is complete. | |
71 // Negative codes indicate system errors. | |
72 int error_code() const { return error_code_; } | |
73 | |
74 // Returns the HTTP status code. | |
75 int http_status_code() const { | |
76 return http_status_code_; | |
77 }; | |
78 | |
79 // Returns the value of the content-length response header. | |
80 int64 content_length() const { return expected_size_; } | |
81 | |
82 // Returns the value of the content-type response header. | |
83 std::string content_type() const { return content_type_; } | |
84 | |
85 // Returns the overall number of bytes read. | |
86 size_t bytes_read() const { return bytes_read_; } | |
87 | |
88 // Returns a pointer to the downloaded data. | |
89 unsigned char* Data() const; | |
90 | |
91 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE; | |
92 | |
93 virtual void OnReadCompleted(net::URLRequest* request, | |
94 int bytes_read) OVERRIDE; | |
95 | |
96 private: | |
97 // typedef Delegate inherited; | |
mmenke
2014/03/04 19:36:21
Remove this (And the following blank line)
mef
2014/03/05 20:54:13
Done.
| |
98 | |
99 URLRequestContextPeer* context_; | |
100 scoped_refptr<URLRequestPeerDelegate> delegate_; | |
101 GURL url_; | |
102 net::RequestPriority priority_; | |
103 std::string method_; | |
104 net::HttpRequestHeaders headers_; | |
105 net::URLRequest* url_request_; | |
106 scoped_ptr<net::UploadDataStream> upload_data_stream_; | |
107 scoped_refptr<net::GrowableIOBuffer> read_buffer_; | |
108 int bytes_read_; | |
109 int total_bytes_read_; | |
110 int error_code_; | |
111 int http_status_code_; | |
112 std::string content_type_; | |
113 bool canceled_; | |
114 int64 expected_size_; | |
115 bool streaming_upload_; | |
116 | |
117 static void OnInitiateConnectionWrapper(URLRequestPeer* self); | |
118 static void OnCancelRequestWrapper(URLRequestPeer* self); | |
119 static void OnDestroyRequest(URLRequestPeer* self); | |
120 static void OnAppendChunkWrapper(URLRequestPeer* self, | |
121 const char* bytes, | |
122 int bytes_len, | |
123 bool is_last_chunk); | |
124 | |
125 void OnInitiateConnection(); | |
126 void OnCancelRequest(); | |
127 void OnRequestSucceeded(); | |
128 void OnRequestFailed(); | |
129 void OnRequestCompleted(); | |
130 void OnRequestCanceled(); | |
131 void OnBytesRead(int bytes_read); | |
132 void OnAppendChunk(const char* bytes, int bytes_len, bool is_last_chunk); | |
133 | |
134 void Read(); | |
135 | |
136 DISALLOW_COPY_AND_ASSIGN(URLRequestPeer); | |
137 }; | |
138 | |
139 #endif // NET_CRONET_ANDROID_URL_REQUEST_PEER_H_ | |
OLD | NEW |