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

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 Helen's comments, add CronetUrlRequestContextTest. Created 6 years, 1 month 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 #include "url/gurl.h"
19
20 namespace base {
21 class SingleThreadTaskRunner;
22 } // namespace base
23
24 namespace net {
25 class GrowableIOBuffer;
26 class HttpResponseHeaders;
27 class UploadDataStream;
28 } // namespace net
29
30 namespace cronet {
31
32 class CronetURLRequestContextAdapter;
33
34 // An adapter from the JNI CronetUrlRequest object to the Chromium URLRequest.
35 class CronetURLRequestAdapter : public net::URLRequest::Delegate {
36 public:
37 // The delegate which is called when the request_adapter finishes.
38 class CronetURLRequestAdapterDelegate
39 : public base::RefCountedThreadSafe<CronetURLRequestAdapterDelegate> {
40 public:
41 virtual void OnRedirect(const GURL& newLocation, int http_status_code) = 0;
42 virtual void OnResponseStarted(int http_status_code) = 0;
43 virtual void OnBytesRead(unsigned char* bytes_buffer,
44 int bytes_read) = 0;
45 virtual void OnRequestFinished() = 0;
46 virtual void OnError(int error) = 0;
47
48 protected:
49 friend class base::RefCountedThreadSafe<CronetURLRequestAdapterDelegate>;
50 virtual ~CronetURLRequestAdapterDelegate() {}
51 };
52
53 CronetURLRequestAdapter(CronetURLRequestContextAdapter* context,
54 CronetURLRequestAdapterDelegate* delegate,
55 const GURL& url,
56 net::RequestPriority priority);
57 ~CronetURLRequestAdapter() override;
58
59 // Sets the request method GET, POST etc.
60 void SetMethod(const std::string& method);
61
62 // Adds a header to the request before it starts.
63 void AddRequestHeader(const std::string& name, const std::string& value);
64
65 // Starts the request.
66 void Start();
67
68 // Follows redirect.
69 void FollowDeferredRedirect();
70
71 // Reads more data.
72 void ReadData();
73
74 // Releases all resources for the request and deletes the object
75 // itself.
76 void Destroy();
77
78 // Returns the URL of the request.
79 const GURL& url() const { return initial_url_; }
80
81 // Gets all response headers, as a HttpResponseHeaders object.
82 net::HttpResponseHeaders* GetResponseHeaders() const;
83
84 // Returns a pointer to the downloaded data.
85 unsigned char* Data() const;
86
87 // Gets NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo.
88 std::string GetNegotiatedProtocol() const;
89
90 // Returns true if response is coming from the cache.
91 bool GetWasCached() const;
92
93 // Gets the total amount of data received from network after SSL decoding and
94 // proxy handling.
95 int64 GetTotalReceivedBytes() const;
96
97 // net::URLRequest::Delegate overrides
98 void OnReceivedRedirect(net::URLRequest* request,
99 const net::RedirectInfo& redirect_info,
100 bool* defer_redirect) override;
101
102 void OnResponseStarted(net::URLRequest* request) override;
103
104 void OnReadCompleted(net::URLRequest* request, int bytes_read) override;
105 // Returns true if currently running on network thread.
106 bool IsOnNetworkThread() const;
107
108 private:
109 void StartOnNetworkThread();
110 void FollowDeferredRedirectOnNetworkThread();
111 void ReadDataOnNetworkThread();
112 void DestroyOnNetworkThread();
113 // Checks whether request was destroyed while |called_to_delegate_| and if so,
114 // then perform actual destruction and return true.
115 bool WasDestroyedWhileCalledToDelegate();
116 // Checks status of the request_adapter, return true if |is_success()| is
117 // true, otherwise report error and cancel request_adapter.
118 bool CheckStatus(net::URLRequest* request);
119
120 CronetURLRequestContextAdapter* context_;
121 scoped_refptr<CronetURLRequestAdapterDelegate> delegate_;
122 GURL initial_url_;
123 net::RequestPriority initial_priority_;
124 std::string initial_method_;
125 net::HttpRequestHeaders initial_request_headers_;
126 scoped_ptr<net::URLRequest> url_request_;
127 scoped_refptr<net::IOBufferWithSize> read_buffer_;
128 // Set to |true| when |delegate_| is called, so |this| could be accessed from
129 // non-network threads, and therefore should not be destroyed until delegate
130 // posts another task to network thread.
131 bool called_to_delegate_;
132 // Set to |true| if |this| should be destroyed upon return from the delegate.
133 // Used to protect adapter from being destructed while it is accessed by
134 // delegate.
135 bool destroy_soon_;
136
137 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestAdapter);
138 };
139
140 } // namespace cronet
141
142 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698