Home | History | Annotate | Download | only in android
      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_PEER_H_
      6 #define COMPONENTS_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 "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"
     16 
     17 namespace cronet {
     18 
     19 // An adapter from the JNI |UrlRequest| object and the Chromium |URLRequest|
     20 // object.
     21 class URLRequestPeer : public net::URLRequest::Delegate {
     22  public:
     23   // The delegate which is called when the request finishes.
     24   class URLRequestPeerDelegate
     25       : public base::RefCountedThreadSafe<URLRequestPeerDelegate> {
     26    public:
     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;
     31 
     32    protected:
     33     friend class base::RefCountedThreadSafe<URLRequestPeerDelegate>;
     34     virtual ~URLRequestPeerDelegate() {}
     35   };
     36 
     37   URLRequestPeer(URLRequestContextPeer* context,
     38                  URLRequestPeerDelegate* delegate,
     39                  GURL url,
     40                  net::RequestPriority priority);
     41   virtual ~URLRequestPeer();
     42 
     43   // Sets the request method GET, POST etc
     44   void SetMethod(const std::string& method);
     45 
     46   // Adds a header to the request
     47   void AddHeader(const std::string& name, const std::string& value);
     48 
     49   // Sets the contents of the POST request
     50   void SetPostContent(const char* bytes, int bytes_len);
     51 
     52   // Indicates that the request body will be streamed by calling AppendChunk()
     53   // repeatedly. This must be called before Start().
     54   void EnableStreamingUpload();
     55 
     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);
     59 
     60   // Starts the request.
     61   void Start();
     62 
     63   // Cancels the request.
     64   void Cancel();
     65 
     66   // Releases all resources for the request and deletes the object itself.
     67   void Destroy();
     68 
     69   // Returns the URL of the request.
     70   GURL url() const { return url_; }
     71 
     72   // Returns the error code after the request is complete.
     73   // Negative codes indicate system errors.
     74   int error_code() const { return error_code_; }
     75 
     76   // Returns the HTTP status code.
     77   int http_status_code() const {
     78     return http_status_code_;
     79   };
     80 
     81   // Returns the value of the content-length response header.
     82   int64 content_length() const { return expected_size_; }
     83 
     84   // Returns the value of the content-type response header.
     85   std::string content_type() const { return content_type_; }
     86 
     87   // Returns the value of the specified response header.
     88   std::string GetHeader(const std::string& name) const;
     89 
     90   // Returns the overall number of bytes read.
     91   size_t bytes_read() const { return bytes_read_; }
     92 
     93   // Returns a pointer to the downloaded data.
     94   unsigned char* Data() const;
     95 
     96   virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE;
     97 
     98   virtual void OnReadCompleted(net::URLRequest* request,
     99                                int bytes_read) OVERRIDE;
    100 
    101  private:
    102   URLRequestContextPeer* context_;
    103   scoped_refptr<URLRequestPeerDelegate> delegate_;
    104   GURL url_;
    105   net::RequestPriority priority_;
    106   std::string method_;
    107   net::HttpRequestHeaders headers_;
    108   net::URLRequest* url_request_;
    109   scoped_ptr<net::UploadDataStream> upload_data_stream_;
    110   scoped_refptr<net::GrowableIOBuffer> read_buffer_;
    111   int bytes_read_;
    112   int total_bytes_read_;
    113   int error_code_;
    114   int http_status_code_;
    115   std::string content_type_;
    116   bool canceled_;
    117   int64 expected_size_;
    118   bool streaming_upload_;
    119 
    120   static void OnDestroyRequest(URLRequestPeer* self);
    121 
    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);
    130 
    131   void Read();
    132 
    133   DISALLOW_COPY_AND_ASSIGN(URLRequestPeer);
    134 };
    135 
    136 }  // namespace cronet
    137 
    138 #endif  // COMPONENTS_CRONET_ANDROID_URL_REQUEST_PEER_H_
    139