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.
5 #ifndef NET_CRONET_ANDROID_URL_REQUEST_PEER_H_
6 #define NET_CRONET_ANDROID_URL_REQUEST_PEER_H_
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"
17 // An adapter from the JNI |UrlRequest| object and the Chromium |URLRequest|
19 class URLRequestPeer : public net::URLRequest::Delegate {
21 // The delegate which is called when the request finishes.
22 class URLRequestPeerDelegate
23 : public base::RefCountedThreadSafe<URLRequestPeerDelegate> {
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;
31 friend class base::RefCountedThreadSafe<URLRequestPeerDelegate>;
32 virtual ~URLRequestPeerDelegate() {}
35 URLRequestPeer(URLRequestContextPeer* context,
36 URLRequestPeerDelegate* delegate,
38 net::RequestPriority priority);
39 virtual ~URLRequestPeer();
41 // Sets the request method GET, POST etc
42 void SetMethod(const std::string& method);
44 // Adds a header to the request
45 void AddHeader(const std::string& name, const std::string& value);
47 // Sets the contents of the POST request
48 void SetPostContent(const char* bytes, int bytes_len);
50 // Indicates that the request body will be streamed by calling AppendChunk()
51 // repeatedly. This must be called before Start().
52 void EnableStreamingUpload();
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);
58 // Starts the request.
61 // Cancels the request.
64 // Releases all resources for the request and deletes the object itself.
67 // Returns the URL of the request.
68 GURL url() const { return url_; }
70 // Returns the error code after the request is complete.
71 // Negative codes indicate system errors.
72 int error_code() const { return error_code_; }
74 // Returns the HTTP status code.
75 int http_status_code() const {
76 return http_status_code_;
79 // Returns the value of the content-length response header.
80 int64 content_length() const { return expected_size_; }
82 // Returns the value of the content-type response header.
83 std::string content_type() const { return content_type_; }
85 // Returns the overall number of bytes read.
86 size_t bytes_read() const { return bytes_read_; }
88 // Returns a pointer to the downloaded data.
89 unsigned char* Data() const;
91 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE;
93 virtual void OnReadCompleted(net::URLRequest* request,
94 int bytes_read) OVERRIDE;
97 URLRequestContextPeer* context_;
98 scoped_refptr<URLRequestPeerDelegate> delegate_;
100 net::RequestPriority priority_;
102 net::HttpRequestHeaders headers_;
103 net::URLRequest* url_request_;
104 scoped_ptr<net::UploadDataStream> upload_data_stream_;
105 scoped_refptr<net::GrowableIOBuffer> read_buffer_;
107 int total_bytes_read_;
109 int http_status_code_;
110 std::string content_type_;
112 int64 expected_size_;
113 bool streaming_upload_;
115 static void OnInitiateConnectionWrapper(URLRequestPeer* self);
116 static void OnCancelRequestWrapper(URLRequestPeer* self);
117 static void OnDestroyRequest(URLRequestPeer* self);
118 static void OnAppendChunkWrapper(URLRequestPeer* self,
123 void OnInitiateConnection();
124 void OnCancelRequest();
125 void OnRequestSucceeded();
126 void OnRequestFailed();
127 void OnRequestCompleted();
128 void OnRequestCanceled();
129 void OnBytesRead(int bytes_read);
130 void OnAppendChunk(const char* bytes, int bytes_len, bool is_last_chunk);
134 DISALLOW_COPY_AND_ASSIGN(URLRequestPeer);
137 #endif // NET_CRONET_ANDROID_URL_REQUEST_PEER_H_