Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / cronet / android / url_request_adapter.h
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.
4
5 #ifndef COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_
6 #define COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_
7
8 #include <jni.h>
9
10 #include <string>
11
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"
18
19 namespace net {
20 class GrowableIOBuffer;
21 class HttpResponseHeaders;
22 class UploadDataStream;
23 class RedirectInfo;
24 }  // namespace net
25
26 namespace cronet {
27
28 class URLRequestContextAdapter;
29
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) = 0;
40     virtual void OnRequestFinished(URLRequestAdapter* request) = 0;
41     virtual int ReadFromUploadChannel(net::IOBuffer* buf, int buf_length) = 0;
42
43    protected:
44     friend class base::RefCountedThreadSafe<URLRequestAdapterDelegate>;
45     virtual ~URLRequestAdapterDelegate() {}
46   };
47
48   URLRequestAdapter(URLRequestContextAdapter* context,
49                     URLRequestAdapterDelegate* delegate,
50                     GURL url,
51                     net::RequestPriority priority);
52   virtual ~URLRequestAdapter();
53
54   // Sets the request method GET, POST etc
55   void SetMethod(const std::string& method);
56
57   // Adds a header to the request
58   void AddHeader(const std::string& name, const std::string& value);
59
60   // Sets the contents of the POST or PUT request
61   void SetUploadContent(const char* bytes, int bytes_len);
62
63   // Sets the request to streaming upload.
64   void SetUploadChannel(JNIEnv* env, int64 content_length);
65
66   // Disables redirect. Note that redirect is enabled by default.
67   void DisableRedirects();
68
69   // Indicates that the request body will be streamed by calling AppendChunk()
70   // repeatedly. This must be called before Start().
71   void EnableChunkedUpload();
72
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);
76
77   // Starts the request.
78   void Start();
79
80   // Cancels the request.
81   void Cancel();
82
83   // Releases all resources for the request and deletes the object itself.
84   void Destroy();
85
86   // Returns the URL of the request.
87   GURL url() const { return url_; }
88
89   // Returns the error code after the request is complete.
90   // Negative codes indicate system errors.
91   int error_code() const { return error_code_; }
92
93   // Returns the HTTP status code.
94   int http_status_code() const {
95     return http_status_code_;
96   };
97
98   // Returns the HTTP status text of the normalized status line.
99   const std::string& http_status_text() const {
100     return http_status_text_;
101   }
102
103   // Returns the value of the content-length response header.
104   int64 content_length() const { return expected_size_; }
105
106   // Returns the value of the content-type response header.
107   std::string content_type() const { return content_type_; }
108
109   // Returns the value of the specified response header.
110   std::string GetHeader(const std::string& name) const;
111
112   // Get all response headers, as a HttpResponseHeaders object.
113   net::HttpResponseHeaders* GetResponseHeaders() const;
114
115   // Returns the overall number of bytes read.
116   size_t bytes_read() const { return bytes_read_; }
117
118   // Returns a pointer to the downloaded data.
119   unsigned char* Data() const;
120
121   // Get NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo.
122   std::string GetNegotiatedProtocol() const;
123
124   // net::URLRequest::Delegate implementation:
125   void OnResponseStarted(net::URLRequest* request) override;
126   void OnReadCompleted(net::URLRequest* request, int bytes_read) override;
127   void OnReceivedRedirect(net::URLRequest* request,
128                           const net::RedirectInfo& redirect_info,
129                           bool* defer_redirect) override;
130
131   bool OnNetworkThread() const;
132
133  private:
134   static void OnDestroyRequest(URLRequestAdapter* self);
135
136   void OnInitiateConnection();
137   void OnCancelRequest();
138   void OnRequestSucceeded();
139   void OnRequestFailed();
140   void OnRequestCompleted();
141   void OnRequestCanceled();
142   void OnBytesRead(int bytes_read);
143   void OnAppendChunk(const scoped_ptr<char[]> bytes, int bytes_len,
144                      bool is_last_chunk);
145
146   void Read();
147
148   URLRequestContextAdapter* context_;
149   scoped_refptr<URLRequestAdapterDelegate> delegate_;
150   GURL url_;
151   net::RequestPriority priority_;
152   std::string method_;
153   net::HttpRequestHeaders headers_;
154   scoped_ptr<net::URLRequest> url_request_;
155   scoped_ptr<net::UploadDataStream> upload_data_stream_;
156   scoped_refptr<net::GrowableIOBuffer> read_buffer_;
157   int bytes_read_;
158   int total_bytes_read_;
159   int error_code_;
160   int http_status_code_;
161   std::string http_status_text_;
162   std::string content_type_;
163   bool canceled_;
164   int64 expected_size_;
165   bool chunked_upload_;
166   // Indicates whether redirect has been disabled.
167   bool disable_redirect_;
168
169   DISALLOW_COPY_AND_ASSIGN(URLRequestAdapter);
170 };
171
172 }  // namespace cronet
173
174 #endif  // COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_