Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / components / cronet / android / url_request_adapter.h
blob6ac62e9a84460d34abe960244d17d41c49f80b86
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_ADAPTER_H_
6 #define COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_
8 #include <jni.h>
10 #include <string>
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"
19 namespace net {
20 class GrowableIOBuffer;
21 class HttpResponseHeaders;
22 class UploadDataStream;
23 } // namespace net
25 namespace cronet {
27 class URLRequestContextAdapter;
29 // An adapter from the JNI |UrlRequest| object and the Chromium |URLRequest|
30 // object.
31 class URLRequestAdapter : public net::URLRequest::Delegate {
32 public:
33 // The delegate which is called when the request finishes.
34 class URLRequestAdapterDelegate
35 : public base::RefCountedThreadSafe<URLRequestAdapterDelegate> {
36 public:
37 virtual void OnResponseStarted(URLRequestAdapter* request) = 0;
38 virtual void OnBytesRead(URLRequestAdapter* request) = 0;
39 virtual void OnRequestFinished(URLRequestAdapter* request) = 0;
40 virtual int ReadFromUploadChannel(net::IOBuffer* buf, int buf_length) = 0;
42 protected:
43 friend class base::RefCountedThreadSafe<URLRequestAdapterDelegate>;
44 virtual ~URLRequestAdapterDelegate() {}
47 URLRequestAdapter(URLRequestContextAdapter* context,
48 URLRequestAdapterDelegate* delegate,
49 GURL url,
50 net::RequestPriority priority);
51 virtual ~URLRequestAdapter();
53 // Sets the request method GET, POST etc
54 void SetMethod(const std::string& method);
56 // Adds a header to the request
57 void AddHeader(const std::string& name, const std::string& value);
59 // Sets the contents of the POST or PUT request
60 void SetUploadContent(const char* bytes, int bytes_len);
62 // Sets the request to streaming upload.
63 void SetUploadChannel(JNIEnv* env, int64 content_length);
65 // Indicates that the request body will be streamed by calling AppendChunk()
66 // repeatedly. This must be called before Start().
67 void EnableChunkedUpload();
69 // Appends a chunk to the POST body.
70 // This must be called after EnableChunkedUpload() and Start().
71 void AppendChunk(const char* bytes, int bytes_len, bool is_last_chunk);
73 // Starts the request.
74 void Start();
76 // Cancels the request.
77 void Cancel();
79 // Releases all resources for the request and deletes the object itself.
80 void Destroy();
82 // Returns the URL of the request.
83 GURL url() const { return url_; }
85 // Returns the error code after the request is complete.
86 // Negative codes indicate system errors.
87 int error_code() const { return error_code_; }
89 // Returns the HTTP status code.
90 int http_status_code() const {
91 return http_status_code_;
94 // Returns the value of the content-length response header.
95 int64 content_length() const { return expected_size_; }
97 // Returns the value of the content-type response header.
98 std::string content_type() const { return content_type_; }
100 // Returns the value of the specified response header.
101 std::string GetHeader(const std::string& name) const;
103 // Get all response headers, as a HttpResponseHeaders object.
104 net::HttpResponseHeaders* GetResponseHeaders() const;
106 // Returns the overall number of bytes read.
107 size_t bytes_read() const { return bytes_read_; }
109 // Returns a pointer to the downloaded data.
110 unsigned char* Data() const;
112 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE;
114 virtual void OnReadCompleted(net::URLRequest* request,
115 int bytes_read) OVERRIDE;
117 private:
118 static void OnDestroyRequest(URLRequestAdapter* self);
120 void OnInitiateConnection();
121 void OnCancelRequest();
122 void OnRequestSucceeded();
123 void OnRequestFailed();
124 void OnRequestCompleted();
125 void OnRequestCanceled();
126 void OnBytesRead(int bytes_read);
127 void OnAppendChunk(const scoped_ptr<char[]> bytes, int bytes_len,
128 bool is_last_chunk);
130 void Read();
132 URLRequestContextAdapter* context_;
133 scoped_refptr<URLRequestAdapterDelegate> delegate_;
134 GURL url_;
135 net::RequestPriority priority_;
136 std::string method_;
137 net::HttpRequestHeaders headers_;
138 scoped_ptr<net::URLRequest> url_request_;
139 scoped_ptr<net::UploadDataStream> upload_data_stream_;
140 scoped_refptr<net::GrowableIOBuffer> read_buffer_;
141 int bytes_read_;
142 int total_bytes_read_;
143 int error_code_;
144 int http_status_code_;
145 std::string content_type_;
146 bool canceled_;
147 int64 expected_size_;
148 bool chunked_upload_;
150 DISALLOW_COPY_AND_ASSIGN(URLRequestAdapter);
153 } // namespace cronet
155 #endif // COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_