ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / components / cronet / android / url_request_adapter.h
blob8704a007bf3937fcc7c6caa73636845e8ea2f9a4
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 IOBufferWithSize;
21 class HttpResponseHeaders;
22 class UploadDataStream;
23 struct RedirectInfo;
24 } // namespace net
26 namespace cronet {
28 class URLRequestContextAdapter;
30 // An adapter from the JNI |UrlRequest| object and the Chromium |URLRequest|
31 // object.
32 class URLRequestAdapter : public net::URLRequest::Delegate {
33 public:
34 // The delegate which is called when the request finishes.
35 class URLRequestAdapterDelegate
36 : public base::RefCountedThreadSafe<URLRequestAdapterDelegate> {
37 public:
38 virtual void OnResponseStarted(URLRequestAdapter* request) = 0;
39 virtual void OnBytesRead(URLRequestAdapter* request, int bytes_read) = 0;
40 virtual void OnRequestFinished(URLRequestAdapter* request) = 0;
41 virtual int ReadFromUploadChannel(net::IOBuffer* buf, int buf_length) = 0;
43 protected:
44 friend class base::RefCountedThreadSafe<URLRequestAdapterDelegate>;
45 virtual ~URLRequestAdapterDelegate() {}
48 URLRequestAdapter(URLRequestContextAdapter* context,
49 URLRequestAdapterDelegate* delegate,
50 GURL url,
51 net::RequestPriority priority);
52 ~URLRequestAdapter() override;
54 // Sets the request method GET, POST etc
55 void SetMethod(const std::string& method);
57 // Adds a header to the request
58 void AddHeader(const std::string& name, const std::string& value);
60 // Sets the contents of the POST or PUT request
61 void SetUploadContent(const char* bytes, int bytes_len);
63 // Sets the request to streaming upload.
64 void SetUploadChannel(JNIEnv* env, int64 content_length);
66 // Disables redirect. Note that redirect is enabled by default.
67 void DisableRedirects();
69 // Indicates that the request body will be streamed by calling AppendChunk()
70 // repeatedly. This must be called before Start().
71 void EnableChunkedUpload();
73 // Appends a chunk to the POST body.
74 // This must be called after EnableChunkedUpload() and Start().
75 void AppendChunk(const char* bytes, int bytes_len, bool is_last_chunk);
77 // Starts the request.
78 void Start();
80 // Cancels the request.
81 void Cancel();
83 // Releases all resources for the request and deletes the object itself.
84 void Destroy();
86 // Returns the URL of the request.
87 GURL url() const { return url_; }
89 // Returns the error code after the request is complete.
90 // Negative codes indicate system errors.
91 int error_code() const { return error_code_; }
93 // Returns the HTTP status code.
94 int http_status_code() const {
95 return http_status_code_;
98 // Returns the HTTP status text of the normalized status line.
99 const std::string& http_status_text() const {
100 return http_status_text_;
103 // Returns the value of the content-length response header.
104 int64 content_length() const { return expected_size_; }
106 // Returns the value of the content-type response header.
107 std::string content_type() const { return content_type_; }
109 // Returns the value of the specified response header.
110 std::string GetHeader(const std::string& name) const;
112 // Get all response headers, as a HttpResponseHeaders object.
113 net::HttpResponseHeaders* GetResponseHeaders() const;
115 // Returns a pointer to the downloaded data.
116 unsigned char* Data() const;
118 // Get NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo.
119 std::string GetNegotiatedProtocol() const;
121 // net::URLRequest::Delegate implementation:
122 void OnResponseStarted(net::URLRequest* request) override;
123 void OnReadCompleted(net::URLRequest* request, int bytes_read) override;
124 void OnReceivedRedirect(net::URLRequest* request,
125 const net::RedirectInfo& redirect_info,
126 bool* defer_redirect) override;
128 bool OnNetworkThread() const;
130 private:
131 static void OnDestroyRequest(URLRequestAdapter* self);
133 void OnInitiateConnection();
134 void OnCancelRequest();
135 void OnRequestSucceeded();
136 void OnRequestFailed();
137 void OnRequestCompleted();
138 void OnAppendChunk(const scoped_ptr<char[]> bytes, int bytes_len,
139 bool is_last_chunk);
141 void Read();
143 // Handles synchronous or asynchronous read result, calls |delegate_| with
144 // bytes read and returns true unless request has succeeded or failed.
145 bool HandleReadResult(int bytes_read);
147 URLRequestContextAdapter* context_;
148 scoped_refptr<URLRequestAdapterDelegate> delegate_;
149 GURL url_;
150 net::RequestPriority priority_;
151 std::string method_;
152 net::HttpRequestHeaders headers_;
153 scoped_ptr<net::URLRequest> url_request_;
154 scoped_ptr<net::UploadDataStream> upload_data_stream_;
155 scoped_refptr<net::IOBufferWithSize> read_buffer_;
156 int total_bytes_read_;
157 int error_code_;
158 int http_status_code_;
159 std::string http_status_text_;
160 std::string content_type_;
161 bool canceled_;
162 int64 expected_size_;
163 bool chunked_upload_;
164 // Indicates whether redirect has been disabled.
165 bool disable_redirect_;
167 DISALLOW_COPY_AND_ASSIGN(URLRequestAdapter);
170 } // namespace cronet
172 #endif // COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_