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 #include "cronet_url_request_adapter.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/location.h" | |
9 #include "base/logging.h" | |
10 #include "components/cronet/android/cronet_url_request_context_adapter.h" | |
11 #include "components/cronet/android/wrapped_channel_upload_element_reader.h" | |
12 #include "net/base/io_buffer.h" | |
13 #include "net/base/load_flags.h" | |
14 #include "net/http/http_status_code.h" | |
15 #include "net/url_request/redirect_info.h" | |
16 #include "net/url_request/url_request_context.h" | |
17 | |
18 namespace cronet { | |
19 | |
20 static const int kReadBufferSize = 32768; | |
21 | |
22 CronetURLRequestAdapter::CronetURLRequestAdapter( | |
23 CronetURLRequestContextAdapter* context, | |
24 scoped_ptr<CronetURLRequestAdapterDelegate> delegate, | |
25 const GURL& url, | |
26 net::RequestPriority priority) | |
27 : context_(context), | |
28 delegate_(delegate.Pass()), | |
29 initial_url_(url), | |
30 initial_priority_(priority), | |
31 initial_method_("GET") { | |
32 } | |
33 | |
34 CronetURLRequestAdapter::~CronetURLRequestAdapter() { | |
35 DCHECK(IsOnNetworkThread()); | |
36 } | |
37 | |
38 void CronetURLRequestAdapter::AddRequestHeader(const std::string& name, | |
39 const std::string& value) { | |
40 DCHECK(!IsOnNetworkThread()); | |
41 initial_request_headers_.SetHeader(name, value); | |
42 } | |
43 | |
44 void CronetURLRequestAdapter::Start() { | |
45 DCHECK(IsOnNetworkThread()); | |
46 VLOG(1) << "Starting chromium request: " | |
47 << initial_url_.possibly_invalid_spec().c_str() | |
48 << " priority: " << RequestPriorityToString(initial_priority_); | |
49 url_request_ = context_->GetURLRequestContext()->CreateRequest( | |
50 initial_url_, net::DEFAULT_PRIORITY, this, NULL); | |
51 url_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE | | |
52 net::LOAD_DO_NOT_SAVE_COOKIES | | |
53 net::LOAD_DO_NOT_SEND_COOKIES); | |
54 url_request_->set_method(initial_method_); | |
55 url_request_->SetExtraRequestHeaders(initial_request_headers_); | |
56 url_request_->SetPriority(initial_priority_); | |
57 url_request_->Start(); | |
58 } | |
59 | |
60 void CronetURLRequestAdapter::FollowDeferredRedirect() { | |
61 DCHECK(IsOnNetworkThread()); | |
62 | |
63 url_request_->FollowDeferredRedirect(); | |
64 } | |
65 | |
66 void CronetURLRequestAdapter::ReadData() { | |
67 DCHECK(IsOnNetworkThread()); | |
68 if (!read_buffer_.get()) | |
69 read_buffer_ = new net::IOBufferWithSize(kReadBufferSize); | |
70 | |
71 int bytes_read = 0; | |
72 url_request_->Read(read_buffer_.get(), read_buffer_->size(), &bytes_read); | |
73 // If IO is pending, wait for the URLRequest to call OnReadCompleted. | |
74 if (url_request_->status().is_io_pending()) | |
75 return; | |
76 | |
77 OnReadCompleted(url_request_.get(), bytes_read); | |
78 } | |
79 | |
80 void CronetURLRequestAdapter::Destroy() { | |
81 DCHECK(IsOnNetworkThread()); | |
82 delete this; | |
83 } | |
84 | |
85 const net::HttpResponseHeaders* CronetURLRequestAdapter::GetResponseHeaders() | |
86 const { | |
mmenke
2014/11/07 15:19:08
nit: const should go on the same line as the clos
mef
2014/11/07 16:22:25
Done. It didn't look right, thanks for suggestion!
| |
87 DCHECK(IsOnNetworkThread()); | |
88 return url_request_->response_headers(); | |
89 } | |
90 | |
91 const std::string& CronetURLRequestAdapter::GetNegotiatedProtocol() const { | |
92 DCHECK(IsOnNetworkThread()); | |
93 return url_request_->response_info().npn_negotiated_protocol; | |
94 } | |
95 | |
96 bool CronetURLRequestAdapter::GetWasCached() const { | |
97 DCHECK(IsOnNetworkThread()); | |
98 return url_request_->response_info().was_cached; | |
99 } | |
100 | |
101 int64 CronetURLRequestAdapter::GetTotalReceivedBytes() const { | |
102 DCHECK(IsOnNetworkThread()); | |
103 return url_request_->GetTotalReceivedBytes(); | |
104 } | |
105 | |
106 // net::URLRequest::Delegate overrides (called on network thread). | |
107 | |
108 void CronetURLRequestAdapter::OnReceivedRedirect( | |
109 net::URLRequest* request, | |
110 const net::RedirectInfo& redirect_info, | |
111 bool* defer_redirect) { | |
112 DCHECK(IsOnNetworkThread()); | |
113 DCHECK(request->status().is_success()); | |
114 delegate_->OnRedirect(redirect_info.new_url, redirect_info.status_code); | |
115 *defer_redirect = true; | |
116 } | |
117 | |
118 void CronetURLRequestAdapter::OnResponseStarted(net::URLRequest* request) { | |
119 DCHECK(IsOnNetworkThread()); | |
120 if (!MaybeReportError(request)) | |
121 return; | |
122 delegate_->OnResponseStarted(request->GetResponseCode()); | |
123 } | |
124 | |
125 void CronetURLRequestAdapter::OnReadCompleted(net::URLRequest* request, | |
126 int bytes_read) { | |
127 DCHECK(IsOnNetworkThread()); | |
128 if (!MaybeReportError(request)) | |
129 return; | |
130 if (bytes_read != 0) { | |
131 delegate_->OnBytesRead( | |
132 reinterpret_cast<unsigned char*>(read_buffer_->data()), bytes_read); | |
133 } else { | |
134 delegate_->OnRequestFinished(); | |
135 } | |
136 } | |
137 | |
138 bool CronetURLRequestAdapter::PostTaskToNetworkThread( | |
139 const tracked_objects::Location& from_here, | |
140 const base::Closure& task) { | |
141 DCHECK(!IsOnNetworkThread()); | |
142 return context_->GetNetworkTaskRunner()->PostTask(from_here, task); | |
143 } | |
144 | |
145 bool CronetURLRequestAdapter::IsOnNetworkThread() const { | |
146 return context_->GetNetworkTaskRunner()->BelongsToCurrentThread(); | |
147 } | |
148 | |
149 bool CronetURLRequestAdapter::MaybeReportError(net::URLRequest* request) const { | |
150 DCHECK_NE(net::URLRequestStatus::IO_PENDING, url_request_->status().status()); | |
151 DCHECK_EQ(request, url_request_); | |
152 if (url_request_->status().is_success()) | |
153 return true; | |
mmenke
2014/11/07 15:19:08
Seems more logical that MaybeReportError return tr
mef
2014/11/07 16:22:25
Arghh, it made more sense when it was called 'Chec
| |
154 VLOG(1) << "Error " << url_request_->status().error() | |
155 << " on chromium request: " << initial_url_.possibly_invalid_spec(); | |
156 delegate_->OnError(url_request_->status().error()); | |
157 return false; | |
158 } | |
159 | |
160 } // namespace cronet | |
OLD | NEW |