OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ios/web/webui/url_data_manager_ios_backend.h" | 5 #include "ios/web/webui/url_data_manager_ios_backend.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 public: | 89 public: |
90 // |is_incognito| set when job is generated from an incognito profile. | 90 // |is_incognito| set when job is generated from an incognito profile. |
91 URLRequestChromeJob(net::URLRequest* request, | 91 URLRequestChromeJob(net::URLRequest* request, |
92 net::NetworkDelegate* network_delegate, | 92 net::NetworkDelegate* network_delegate, |
93 BrowserState* browser_state, | 93 BrowserState* browser_state, |
94 bool is_incognito); | 94 bool is_incognito); |
95 | 95 |
96 // net::URLRequestJob implementation. | 96 // net::URLRequestJob implementation. |
97 void Start() override; | 97 void Start() override; |
98 void Kill() override; | 98 void Kill() override; |
99 int ReadRawData(net::IOBuffer* buf, int buf_size) override; | 99 bool ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read) override; |
100 bool GetMimeType(std::string* mime_type) const override; | 100 bool GetMimeType(std::string* mime_type) const override; |
101 int GetResponseCode() const override; | 101 int GetResponseCode() const override; |
102 void GetResponseInfo(net::HttpResponseInfo* info) override; | 102 void GetResponseInfo(net::HttpResponseInfo* info) override; |
103 | 103 |
104 // Used to notify that the requested data's |mime_type| is ready. | 104 // Used to notify that the requested data's |mime_type| is ready. |
105 void MimeTypeAvailable(const std::string& mime_type); | 105 void MimeTypeAvailable(const std::string& mime_type); |
106 | 106 |
107 // Called by ChromeURLDataManagerIOS to notify us that the data blob is ready | 107 // Called by ChromeURLDataManagerIOS to notify us that the data blob is ready |
108 // for us. | 108 // for us. |
109 void DataAvailable(base::RefCountedMemory* bytes); | 109 void DataAvailable(base::RefCountedMemory* bytes); |
(...skipping 25 matching lines...) Expand all Loading... |
135 // Returns true when job was generated from an incognito profile. | 135 // Returns true when job was generated from an incognito profile. |
136 bool is_incognito() const { return is_incognito_; } | 136 bool is_incognito() const { return is_incognito_; } |
137 | 137 |
138 private: | 138 private: |
139 friend class URLDataManagerIOSBackend; | 139 friend class URLDataManagerIOSBackend; |
140 | 140 |
141 ~URLRequestChromeJob() override; | 141 ~URLRequestChromeJob() override; |
142 | 142 |
143 // Do the actual copy from data_ (the data we're serving) into |buf|. | 143 // Do the actual copy from data_ (the data we're serving) into |buf|. |
144 // Separate from ReadRawData so we can handle async I/O. | 144 // Separate from ReadRawData so we can handle async I/O. |
145 int CompleteRead(net::IOBuffer* buf, int buf_size); | 145 void CompleteRead(net::IOBuffer* buf, int buf_size, int* bytes_read); |
146 | 146 |
147 // The actual data we're serving. NULL until it's been fetched. | 147 // The actual data we're serving. NULL until it's been fetched. |
148 scoped_refptr<base::RefCountedMemory> data_; | 148 scoped_refptr<base::RefCountedMemory> data_; |
149 // The current offset into the data that we're handing off to our | 149 // The current offset into the data that we're handing off to our |
150 // callers via the Read interfaces. | 150 // callers via the Read interfaces. |
151 int data_offset_; | 151 int data_offset_; |
152 | 152 |
153 // For async reads, we keep around a pointer to the buffer that | 153 // For async reads, we keep around a pointer to the buffer that |
154 // we're reading into. | 154 // we're reading into. |
155 scoped_refptr<net::IOBuffer> pending_buf_; | 155 scoped_refptr<net::IOBuffer> pending_buf_; |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 } | 284 } |
285 | 285 |
286 void URLRequestChromeJob::MimeTypeAvailable(const std::string& mime_type) { | 286 void URLRequestChromeJob::MimeTypeAvailable(const std::string& mime_type) { |
287 set_mime_type(mime_type); | 287 set_mime_type(mime_type); |
288 NotifyHeadersComplete(); | 288 NotifyHeadersComplete(); |
289 } | 289 } |
290 | 290 |
291 void URLRequestChromeJob::DataAvailable(base::RefCountedMemory* bytes) { | 291 void URLRequestChromeJob::DataAvailable(base::RefCountedMemory* bytes) { |
292 TRACE_EVENT_ASYNC_END0("browser", "DataManager:Request", this); | 292 TRACE_EVENT_ASYNC_END0("browser", "DataManager:Request", this); |
293 if (bytes) { | 293 if (bytes) { |
| 294 // The request completed, and we have all the data. |
| 295 // Clear any IO pending status. |
| 296 SetStatus(net::URLRequestStatus()); |
| 297 |
294 data_ = bytes; | 298 data_ = bytes; |
| 299 int bytes_read; |
295 if (pending_buf_.get()) { | 300 if (pending_buf_.get()) { |
296 CHECK(pending_buf_->data()); | 301 CHECK(pending_buf_->data()); |
297 int rv = CompleteRead(pending_buf_.get(), pending_buf_size_); | 302 CompleteRead(pending_buf_.get(), pending_buf_size_, &bytes_read); |
298 pending_buf_ = NULL; | 303 pending_buf_ = NULL; |
299 ReadRawDataComplete(rv); | 304 NotifyReadComplete(bytes_read); |
300 } | 305 } |
301 } else { | 306 } else { |
302 ReadRawDataComplete(net::ERR_FAILED); | 307 // The request failed. |
| 308 NotifyDone( |
| 309 net::URLRequestStatus(net::URLRequestStatus::FAILED, net::ERR_FAILED)); |
303 } | 310 } |
304 } | 311 } |
305 | 312 |
306 int URLRequestChromeJob::ReadRawData(net::IOBuffer* buf, int buf_size) { | 313 bool URLRequestChromeJob::ReadRawData(net::IOBuffer* buf, |
| 314 int buf_size, |
| 315 int* bytes_read) { |
307 if (!data_.get()) { | 316 if (!data_.get()) { |
| 317 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); |
308 DCHECK(!pending_buf_.get()); | 318 DCHECK(!pending_buf_.get()); |
309 CHECK(buf->data()); | 319 CHECK(buf->data()); |
310 pending_buf_ = buf; | 320 pending_buf_ = buf; |
311 pending_buf_size_ = buf_size; | 321 pending_buf_size_ = buf_size; |
312 return net::ERR_IO_PENDING; // Tell the caller we're still waiting for | 322 return false; // Tell the caller we're still waiting for data. |
313 // data. | |
314 } | 323 } |
315 | 324 |
316 // Otherwise, the data is available. | 325 // Otherwise, the data is available. |
317 return CompleteRead(buf, buf_size); | 326 CompleteRead(buf, buf_size, bytes_read); |
| 327 return true; |
318 } | 328 } |
319 | 329 |
320 int URLRequestChromeJob::CompleteRead(net::IOBuffer* buf, int buf_size) { | 330 void URLRequestChromeJob::CompleteRead(net::IOBuffer* buf, |
| 331 int buf_size, |
| 332 int* bytes_read) { |
321 // http://crbug.com/373841 | 333 // http://crbug.com/373841 |
322 char url_buf[128]; | 334 char url_buf[128]; |
323 base::strlcpy(url_buf, request_->url().spec().c_str(), arraysize(url_buf)); | 335 base::strlcpy(url_buf, request_->url().spec().c_str(), arraysize(url_buf)); |
324 base::debug::Alias(url_buf); | 336 base::debug::Alias(url_buf); |
325 | 337 |
326 int remaining = data_->size() - data_offset_; | 338 int remaining = static_cast<int>(data_->size()) - data_offset_; |
327 if (buf_size > remaining) | 339 if (buf_size > remaining) |
328 buf_size = remaining; | 340 buf_size = remaining; |
329 if (buf_size > 0) { | 341 if (buf_size > 0) { |
330 memcpy(buf->data(), data_->front() + data_offset_, buf_size); | 342 memcpy(buf->data(), data_->front() + data_offset_, buf_size); |
331 data_offset_ += buf_size; | 343 data_offset_ += buf_size; |
332 } | 344 } |
333 return buf_size; | 345 *bytes_read = buf_size; |
334 } | 346 } |
335 | 347 |
336 namespace { | 348 namespace { |
337 | 349 |
338 // Gets mime type for data that is available from |source| by |path|. | 350 // Gets mime type for data that is available from |source| by |path|. |
339 // After that, notifies |job| that mime type is available. This method | 351 // After that, notifies |job| that mime type is available. This method |
340 // should be called on the UI thread, but notification is performed on | 352 // should be called on the UI thread, but notification is performed on |
341 // the IO thread. | 353 // the IO thread. |
342 void GetMimeTypeOnUI(URLDataSourceIOSImpl* source, | 354 void GetMimeTypeOnUI(URLDataSourceIOSImpl* source, |
343 const std::string& path, | 355 const std::string& path, |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
529 // Forward this data on to the pending net::URLRequest, if it exists. | 541 // Forward this data on to the pending net::URLRequest, if it exists. |
530 PendingRequestMap::iterator i = pending_requests_.find(request_id); | 542 PendingRequestMap::iterator i = pending_requests_.find(request_id); |
531 if (i != pending_requests_.end()) { | 543 if (i != pending_requests_.end()) { |
532 URLRequestChromeJob* job(i->second); | 544 URLRequestChromeJob* job(i->second); |
533 pending_requests_.erase(i); | 545 pending_requests_.erase(i); |
534 job->DataAvailable(bytes); | 546 job->DataAvailable(bytes); |
535 } | 547 } |
536 } | 548 } |
537 | 549 |
538 } // namespace web | 550 } // namespace web |
OLD | NEW |