File: url_request_adapter.h

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (172 lines) | stat: -rw-r--r-- 5,580 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_
#define COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_

#include <jni.h>

#include <string>

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "net/base/request_priority.h"
#include "net/http/http_request_headers.h"
#include "net/url_request/url_request.h"

namespace net {
class IOBufferWithSize;
class HttpResponseHeaders;
class UploadDataStream;
struct RedirectInfo;
}  // namespace net

namespace cronet {

class URLRequestContextAdapter;

// An adapter from the JNI |UrlRequest| object and the Chromium |URLRequest|
// object.
class URLRequestAdapter : public net::URLRequest::Delegate {
 public:
  // The delegate which is called when the request finishes.
  class URLRequestAdapterDelegate
      : public base::RefCountedThreadSafe<URLRequestAdapterDelegate> {
   public:
    virtual void OnResponseStarted(URLRequestAdapter* request) = 0;
    virtual void OnBytesRead(URLRequestAdapter* request, int bytes_read) = 0;
    virtual void OnRequestFinished(URLRequestAdapter* request) = 0;
    virtual int ReadFromUploadChannel(net::IOBuffer* buf, int buf_length) = 0;

   protected:
    friend class base::RefCountedThreadSafe<URLRequestAdapterDelegate>;
    virtual ~URLRequestAdapterDelegate() {}
  };

  URLRequestAdapter(URLRequestContextAdapter* context,
                    URLRequestAdapterDelegate* delegate,
                    GURL url,
                    net::RequestPriority priority);
  virtual ~URLRequestAdapter();

  // Sets the request method GET, POST etc
  void SetMethod(const std::string& method);

  // Adds a header to the request
  void AddHeader(const std::string& name, const std::string& value);

  // Sets the contents of the POST or PUT request
  void SetUploadContent(const char* bytes, int bytes_len);

  // Sets the request to streaming upload.
  void SetUploadChannel(JNIEnv* env, int64 content_length);

  // Disables redirect. Note that redirect is enabled by default.
  void DisableRedirects();

  // Indicates that the request body will be streamed by calling AppendChunk()
  // repeatedly. This must be called before Start().
  void EnableChunkedUpload();

  // Appends a chunk to the POST body.
  // This must be called after EnableChunkedUpload() and Start().
  void AppendChunk(const char* bytes, int bytes_len, bool is_last_chunk);

  // Starts the request.
  void Start();

  // Cancels the request.
  void Cancel();

  // Releases all resources for the request and deletes the object itself.
  void Destroy();

  // Returns the URL of the request.
  GURL url() const { return url_; }

  // Returns the error code after the request is complete.
  // Negative codes indicate system errors.
  int error_code() const { return error_code_; }

  // Returns the HTTP status code.
  int http_status_code() const {
    return http_status_code_;
  };

  // Returns the HTTP status text of the normalized status line.
  const std::string& http_status_text() const {
    return http_status_text_;
  }

  // Returns the value of the content-length response header.
  int64 content_length() const { return expected_size_; }

  // Returns the value of the content-type response header.
  std::string content_type() const { return content_type_; }

  // Returns the value of the specified response header.
  std::string GetHeader(const std::string& name) const;

  // Get all response headers, as a HttpResponseHeaders object.
  net::HttpResponseHeaders* GetResponseHeaders() const;

  // Returns a pointer to the downloaded data.
  unsigned char* Data() const;

  // Get NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo.
  std::string GetNegotiatedProtocol() const;

  // net::URLRequest::Delegate implementation:
  void OnResponseStarted(net::URLRequest* request) override;
  void OnReadCompleted(net::URLRequest* request, int bytes_read) override;
  void OnReceivedRedirect(net::URLRequest* request,
                          const net::RedirectInfo& redirect_info,
                          bool* defer_redirect) override;

  bool OnNetworkThread() const;

 private:
  static void OnDestroyRequest(URLRequestAdapter* self);

  void OnInitiateConnection();
  void OnCancelRequest();
  void OnRequestSucceeded();
  void OnRequestFailed();
  void OnRequestCompleted();
  void OnAppendChunk(const scoped_ptr<char[]> bytes, int bytes_len,
                     bool is_last_chunk);

  void Read();

  // Handles synchronous or asynchronous read result, calls |delegate_| with
  // bytes read and returns true unless request has succeeded or failed.
  bool HandleReadResult(int bytes_read);

  URLRequestContextAdapter* context_;
  scoped_refptr<URLRequestAdapterDelegate> delegate_;
  GURL url_;
  net::RequestPriority priority_;
  std::string method_;
  net::HttpRequestHeaders headers_;
  scoped_ptr<net::URLRequest> url_request_;
  scoped_ptr<net::UploadDataStream> upload_data_stream_;
  scoped_refptr<net::IOBufferWithSize> read_buffer_;
  int total_bytes_read_;
  int error_code_;
  int http_status_code_;
  std::string http_status_text_;
  std::string content_type_;
  bool canceled_;
  int64 expected_size_;
  bool chunked_upload_;
  // Indicates whether redirect has been disabled.
  bool disable_redirect_;

  DISALLOW_COPY_AND_ASSIGN(URLRequestAdapter);
};

}  // namespace cronet

#endif  // COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_