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 COMPONENTS_CRONET_ANDROID_URL_REQUEST_PEER_H_
6 #define COMPONENTS_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 "components/cronet/android/url_request_context_peer.h"
12 #include "net/base/request_priority.h"
13 #include "net/base/upload_data_stream.h"
14 #include "net/http/http_request_headers.h"
15 #include "net/url_request/url_request.h"
19 // An adapter from the JNI |UrlRequest| object and the Chromium |URLRequest|
21 class URLRequestPeer
: public net::URLRequest::Delegate
{
23 // The delegate which is called when the request finishes.
24 class URLRequestPeerDelegate
25 : public base::RefCountedThreadSafe
<URLRequestPeerDelegate
> {
27 virtual void OnAppendChunkCompleted(URLRequestPeer
* request
) = 0;
28 virtual void OnResponseStarted(URLRequestPeer
* request
) = 0;
29 virtual void OnBytesRead(URLRequestPeer
* request
) = 0;
30 virtual void OnRequestFinished(URLRequestPeer
* request
) = 0;
33 friend class base::RefCountedThreadSafe
<URLRequestPeerDelegate
>;
34 virtual ~URLRequestPeerDelegate() {}
37 URLRequestPeer(URLRequestContextPeer
* context
,
38 URLRequestPeerDelegate
* delegate
,
40 net::RequestPriority priority
);
41 virtual ~URLRequestPeer();
43 // Sets the request method GET, POST etc
44 void SetMethod(const std::string
& method
);
46 // Adds a header to the request
47 void AddHeader(const std::string
& name
, const std::string
& value
);
49 // Sets the contents of the POST request
50 void SetPostContent(const char* bytes
, int bytes_len
);
52 // Indicates that the request body will be streamed by calling AppendChunk()
53 // repeatedly. This must be called before Start().
54 void EnableStreamingUpload();
56 // Appends a chunk to the POST body
57 // This must be called after EnableChunkedUpload() and Start().
58 void AppendChunk(const char* bytes
, int bytes_len
, bool is_last_chunk
);
60 // Starts the request.
63 // Cancels the request.
66 // Releases all resources for the request and deletes the object itself.
69 // Returns the URL of the request.
70 GURL
url() const { return url_
; }
72 // Returns the error code after the request is complete.
73 // Negative codes indicate system errors.
74 int error_code() const { return error_code_
; }
76 // Returns the HTTP status code.
77 int http_status_code() const {
78 return http_status_code_
;
81 // Returns the value of the content-length response header.
82 int64
content_length() const { return expected_size_
; }
84 // Returns the value of the content-type response header.
85 std::string
content_type() const { return content_type_
; }
87 // Returns the overall number of bytes read.
88 size_t bytes_read() const { return bytes_read_
; }
90 // Returns a pointer to the downloaded data.
91 unsigned char* Data() const;
93 virtual void OnResponseStarted(net::URLRequest
* request
) OVERRIDE
;
95 virtual void OnReadCompleted(net::URLRequest
* request
,
96 int bytes_read
) OVERRIDE
;
99 URLRequestContextPeer
* context_
;
100 scoped_refptr
<URLRequestPeerDelegate
> delegate_
;
102 net::RequestPriority priority_
;
104 net::HttpRequestHeaders headers_
;
105 net::URLRequest
* url_request_
;
106 scoped_ptr
<net::UploadDataStream
> upload_data_stream_
;
107 scoped_refptr
<net::GrowableIOBuffer
> read_buffer_
;
109 int total_bytes_read_
;
111 int http_status_code_
;
112 std::string content_type_
;
114 int64 expected_size_
;
115 bool streaming_upload_
;
117 static void OnDestroyRequest(URLRequestPeer
* self
);
119 void OnInitiateConnection();
120 void OnCancelRequest();
121 void OnRequestSucceeded();
122 void OnRequestFailed();
123 void OnRequestCompleted();
124 void OnRequestCanceled();
125 void OnBytesRead(int bytes_read
);
126 void OnAppendChunk(const char* bytes
, int bytes_len
, bool is_last_chunk
);
130 DISALLOW_COPY_AND_ASSIGN(URLRequestPeer
);
133 } // namespace cronet
135 #endif // COMPONENTS_CRONET_ANDROID_URL_REQUEST_PEER_H_