Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Side by Side Diff: components/cronet/android/cronet_url_request_adapter.h

Issue 586143002: Initial implementation of Cronet Async API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review comments, add mock errors to CronetUrlRequestTest. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_
6 #define COMPONENTS_CRONET_ANDROID_CRONET_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 base {
20 class SingleThreadTaskRunner;
xunjieli 2014/10/17 00:31:34 nit: new line before ending a namespace.
mef 2014/10/17 20:19:42 Done. Although it looks funny. Should there be a n
xunjieli 2014/10/17 20:32:22 http://google-styleguide.googlecode.com/svn/trunk/
mmenke 2014/10/17 20:38:12 This falls under the keep with prevailing style ru
21 } // namespace base
22
23 namespace net {
24 class GrowableIOBuffer;
25 class HttpResponseHeaders;
26 class UploadDataStream;
27 } // namespace net
28
29 namespace cronet {
30
31 class CronetURLRequestContextAdapter;
32
33 // An adapter from the JNI |UrlRequest| object and the Chromium |URLRequest|
xunjieli 2014/10/17 00:31:34 nit: s/and/to ?
mef 2014/10/17 20:19:42 Done.
34 // object.
35 class CronetURLRequestAdapter : public net::URLRequest::Delegate {
36 public:
37 // The delegate which is called when the request finishes.
38 class CronetURLRequestAdapterDelegate
39 : public base::RefCountedThreadSafe<CronetURLRequestAdapterDelegate> {
40 public:
41 virtual void OnRedirect(CronetURLRequestAdapter* request,
xunjieli 2014/10/17 00:31:34 nit: I find using a longer name might improve the
mef 2014/10/17 20:19:42 Done.
42 const GURL& newLocation) = 0;
43 virtual void OnResponseStarted(CronetURLRequestAdapter* request) = 0;
44 virtual void OnBytesRead(CronetURLRequestAdapter* request,
45 int bytes_read) = 0;
46 virtual void OnRequestFinished(CronetURLRequestAdapter* request,
47 bool cancelled) = 0;
48 virtual void OnError(CronetURLRequestAdapter* request, int error) = 0;
49
50 protected:
51 friend class base::RefCountedThreadSafe<CronetURLRequestAdapterDelegate>;
52 virtual ~CronetURLRequestAdapterDelegate() {}
53 };
54
55 CronetURLRequestAdapter(CronetURLRequestContextAdapter* context,
56 CronetURLRequestAdapterDelegate* delegate,
57 GURL url,
58 net::RequestPriority priority);
59 virtual ~CronetURLRequestAdapter();
60
61 // Sets the request method GET, POST etc
62 void SetMethod(const std::string& method);
63
64 // Adds a header to the request
65 void AddRequestHeader(const std::string& name, const std::string& value);
66
67 // Starts the request.
68 void Start();
69
70 // Follow redirect.
71 void FollowDeferredRedirect();
72
73 // Read more data.
74 void ReadData();
75
76 // Cancels the request.
77 void Cancel();
78
79 // Releases all resources for the request and deletes the object itself.
80 void Destroy();
81
82 // Returns the URL of the request.
83 GURL url() const { return url_; }
84
85 // Returns the HTTP status code.
86 int http_status_code() const { return http_status_code_; };
87
88 // Get all response headers, as a HttpResponseHeaders object.
89 net::HttpResponseHeaders* GetResponseHeaders() const;
90
91 // Returns a pointer to the downloaded data.
92 unsigned char* Data() const;
93
94 // Get NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo.
95 std::string GetNegotiatedProtocol() const;
96
97 // Returns true if response is coming from the cache.
98 bool GetWasCached() const;
99
100 // Gets the total amount of data received from network after SSL decoding and
101 // proxy handling.
102 int64 GetTotalReceivedBytes() const;
103
104 // net::URLRequest::Delegate overrides
105 void OnReceivedRedirect(net::URLRequest* request,
106 const net::RedirectInfo& redirect_info,
107 bool* defer_redirect) override;
108
109 void OnResponseStarted(net::URLRequest* request) override;
110
111 void OnReadCompleted(net::URLRequest* request, int bytes_read) override;
112 // Return true if currently running on network thread.
113 bool IsOnNetworkThread() const;
114
115 private:
116 void StartOnNetworkThread();
117 void FollowDeferredRedirectOnNetworkThread();
118 void ReadDataOnNetworkThread();
119 void CancelOnNetworkThread();
120 // Check status of the request, return true if |is_success()| is true,
121 // otherwise report error and cancel request.
122 bool CheckStatus(net::URLRequest* request);
123
124 CronetURLRequestContextAdapter* context_;
125 scoped_refptr<CronetURLRequestAdapterDelegate> delegate_;
126 GURL url_;
127 net::RequestPriority priority_;
128 std::string method_;
129 net::HttpRequestHeaders request_headers_;
130 scoped_ptr<net::URLRequest> url_request_;
131 scoped_refptr<net::IOBufferWithSize> read_buffer_;
132 int http_status_code_;
133 bool canceled_;
134
135 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestAdapter);
136 };
137
138 } // namespace cronet
139
140 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698