| OLD | NEW |
| (Empty) |
| 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 NET_CRONET_ANDROID_URL_REQUEST_PEER_H_ | |
| 6 #define NET_CRONET_ANDROID_URL_REQUEST_PEER_H_ | |
| 7 | |
| 8 #include "base/compiler_specific.h" | |
| 9 #include "base/macros.h" | |
| 10 #include "base/memory/ref_counted.h" | |
| 11 #include "net/base/request_priority.h" | |
| 12 #include "net/base/upload_data_stream.h" | |
| 13 #include "net/cronet/android/url_request_context_peer.h" | |
| 14 #include "net/http/http_request_headers.h" | |
| 15 #include "net/url_request/url_request.h" | |
| 16 | |
| 17 // An adapter from the JNI |UrlRequest| object and the Chromium |URLRequest| | |
| 18 // object. | |
| 19 class URLRequestPeer : public net::URLRequest::Delegate { | |
| 20 public: | |
| 21 // The delegate which is called when the request finishes. | |
| 22 class URLRequestPeerDelegate | |
| 23 : public base::RefCountedThreadSafe<URLRequestPeerDelegate> { | |
| 24 public: | |
| 25 virtual void OnAppendChunkCompleted(URLRequestPeer* request) = 0; | |
| 26 virtual void OnResponseStarted(URLRequestPeer* request) = 0; | |
| 27 virtual void OnBytesRead(URLRequestPeer* request) = 0; | |
| 28 virtual void OnRequestFinished(URLRequestPeer* request) = 0; | |
| 29 | |
| 30 protected: | |
| 31 friend class base::RefCountedThreadSafe<URLRequestPeerDelegate>; | |
| 32 virtual ~URLRequestPeerDelegate() {} | |
| 33 }; | |
| 34 | |
| 35 URLRequestPeer(URLRequestContextPeer* context, | |
| 36 URLRequestPeerDelegate* delegate, | |
| 37 GURL url, | |
| 38 net::RequestPriority priority); | |
| 39 virtual ~URLRequestPeer(); | |
| 40 | |
| 41 // Sets the request method GET, POST etc | |
| 42 void SetMethod(const std::string& method); | |
| 43 | |
| 44 // Adds a header to the request | |
| 45 void AddHeader(const std::string& name, const std::string& value); | |
| 46 | |
| 47 // Sets the contents of the POST request | |
| 48 void SetPostContent(const char* bytes, int bytes_len); | |
| 49 | |
| 50 // Indicates that the request body will be streamed by calling AppendChunk() | |
| 51 // repeatedly. This must be called before Start(). | |
| 52 void EnableStreamingUpload(); | |
| 53 | |
| 54 // Appends a chunk to the POST body | |
| 55 // This must be called after EnableChunkedUpload() and Start(). | |
| 56 void AppendChunk(const char* bytes, int bytes_len, bool is_last_chunk); | |
| 57 | |
| 58 // Starts the request. | |
| 59 void Start(); | |
| 60 | |
| 61 // Cancels the request. | |
| 62 void Cancel(); | |
| 63 | |
| 64 // Releases all resources for the request and deletes the object itself. | |
| 65 void Destroy(); | |
| 66 | |
| 67 // Returns the URL of the request. | |
| 68 GURL url() const { return url_; } | |
| 69 | |
| 70 // Returns the error code after the request is complete. | |
| 71 // Negative codes indicate system errors. | |
| 72 int error_code() const { return error_code_; } | |
| 73 | |
| 74 // Returns the HTTP status code. | |
| 75 int http_status_code() const { | |
| 76 return http_status_code_; | |
| 77 }; | |
| 78 | |
| 79 // Returns the value of the content-length response header. | |
| 80 int64 content_length() const { return expected_size_; } | |
| 81 | |
| 82 // Returns the value of the content-type response header. | |
| 83 std::string content_type() const { return content_type_; } | |
| 84 | |
| 85 // Returns the overall number of bytes read. | |
| 86 size_t bytes_read() const { return bytes_read_; } | |
| 87 | |
| 88 // Returns a pointer to the downloaded data. | |
| 89 unsigned char* Data() const; | |
| 90 | |
| 91 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE; | |
| 92 | |
| 93 virtual void OnReadCompleted(net::URLRequest* request, | |
| 94 int bytes_read) OVERRIDE; | |
| 95 | |
| 96 private: | |
| 97 URLRequestContextPeer* context_; | |
| 98 scoped_refptr<URLRequestPeerDelegate> delegate_; | |
| 99 GURL url_; | |
| 100 net::RequestPriority priority_; | |
| 101 std::string method_; | |
| 102 net::HttpRequestHeaders headers_; | |
| 103 net::URLRequest* url_request_; | |
| 104 scoped_ptr<net::UploadDataStream> upload_data_stream_; | |
| 105 scoped_refptr<net::GrowableIOBuffer> read_buffer_; | |
| 106 int bytes_read_; | |
| 107 int total_bytes_read_; | |
| 108 int error_code_; | |
| 109 int http_status_code_; | |
| 110 std::string content_type_; | |
| 111 bool canceled_; | |
| 112 int64 expected_size_; | |
| 113 bool streaming_upload_; | |
| 114 | |
| 115 static void OnInitiateConnectionWrapper(URLRequestPeer* self); | |
| 116 static void OnCancelRequestWrapper(URLRequestPeer* self); | |
| 117 static void OnDestroyRequest(URLRequestPeer* self); | |
| 118 static void OnAppendChunkWrapper(URLRequestPeer* self, | |
| 119 const char* bytes, | |
| 120 int bytes_len, | |
| 121 bool is_last_chunk); | |
| 122 | |
| 123 void OnInitiateConnection(); | |
| 124 void OnCancelRequest(); | |
| 125 void OnRequestSucceeded(); | |
| 126 void OnRequestFailed(); | |
| 127 void OnRequestCompleted(); | |
| 128 void OnRequestCanceled(); | |
| 129 void OnBytesRead(int bytes_read); | |
| 130 void OnAppendChunk(const char* bytes, int bytes_len, bool is_last_chunk); | |
| 131 | |
| 132 void Read(); | |
| 133 | |
| 134 DISALLOW_COPY_AND_ASSIGN(URLRequestPeer); | |
| 135 }; | |
| 136 | |
| 137 #endif // NET_CRONET_ANDROID_URL_REQUEST_PEER_H_ | |
| OLD | NEW |