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 value of the specified response header.
88 std::string GetHeader(const std::string& name) const;
90 // Returns the overall number of bytes read.
91 size_t bytes_read() const { return bytes_read_; }
93 // Returns a pointer to the downloaded data.
94 unsigned char* Data() const;
96 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE;
98 virtual void OnReadCompleted(net::URLRequest* request,
99 int bytes_read) OVERRIDE;
102 URLRequestContextPeer* context_;
103 scoped_refptr<URLRequestPeerDelegate> delegate_;
105 net::RequestPriority priority_;
107 net::HttpRequestHeaders headers_;
108 net::URLRequest* url_request_;
109 scoped_ptr<net::UploadDataStream> upload_data_stream_;
110 scoped_refptr<net::GrowableIOBuffer> read_buffer_;
112 int total_bytes_read_;
114 int http_status_code_;
115 std::string content_type_;
117 int64 expected_size_;
118 bool streaming_upload_;
120 static void OnDestroyRequest(URLRequestPeer* self);
122 void OnInitiateConnection();
123 void OnCancelRequest();
124 void OnRequestSucceeded();
125 void OnRequestFailed();
126 void OnRequestCompleted();
127 void OnRequestCanceled();
128 void OnBytesRead(int bytes_read);
129 void OnAppendChunk(const char* bytes, int bytes_len, bool is_last_chunk);
133 DISALLOW_COPY_AND_ASSIGN(URLRequestPeer);
136 } // namespace cronet
138 #endif // COMPONENTS_CRONET_ANDROID_URL_REQUEST_PEER_H_