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 bool ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read) override; | 99 int ReadRawData(net::IOBuffer* buf, int buf_size) 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 void CompleteRead(net::IOBuffer* buf, int buf_size, int* bytes_read); | 145 int CompleteRead(net::IOBuffer* buf, int buf_size); |
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. | 294 // The request completed, and we have all the data. |
Eugene But (OOO till 7-30)
2015/10/31 03:14:35
This comment belonged to removed code. Please remo
xunjieli
2015/11/02 15:40:12
Done.
| |
295 // Clear any IO pending status. | 295 // Clear any IO pending status. |
296 SetStatus(net::URLRequestStatus()); | |
297 | |
298 data_ = bytes; | 296 data_ = bytes; |
299 int bytes_read; | |
300 if (pending_buf_.get()) { | 297 if (pending_buf_.get()) { |
301 CHECK(pending_buf_->data()); | 298 CHECK(pending_buf_->data()); |
302 CompleteRead(pending_buf_.get(), pending_buf_size_, &bytes_read); | 299 int rv = CompleteRead(pending_buf_.get(), pending_buf_size_); |
303 pending_buf_ = NULL; | 300 pending_buf_ = NULL; |
304 NotifyReadComplete(bytes_read); | 301 ReadRawDataComplete(rv); |
305 } | 302 } |
306 } else { | 303 } else { |
307 // The request failed. | 304 ReadRawDataComplete(net::ERR_FAILED); |
308 NotifyDone( | |
309 net::URLRequestStatus(net::URLRequestStatus::FAILED, net::ERR_FAILED)); | |
310 } | 305 } |
311 } | 306 } |
312 | 307 |
313 bool URLRequestChromeJob::ReadRawData(net::IOBuffer* buf, | 308 int URLRequestChromeJob::ReadRawData(net::IOBuffer* buf, int buf_size) { |
314 int buf_size, | |
315 int* bytes_read) { | |
316 if (!data_.get()) { | 309 if (!data_.get()) { |
317 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); | |
318 DCHECK(!pending_buf_.get()); | 310 DCHECK(!pending_buf_.get()); |
319 CHECK(buf->data()); | 311 CHECK(buf->data()); |
320 pending_buf_ = buf; | 312 pending_buf_ = buf; |
321 pending_buf_size_ = buf_size; | 313 pending_buf_size_ = buf_size; |
322 return false; // Tell the caller we're still waiting for data. | 314 return net::ERR_IO_PENDING; // Tell the caller we're still waiting for |
315 // data. | |
323 } | 316 } |
324 | 317 |
325 // Otherwise, the data is available. | 318 // Otherwise, the data is available. |
326 CompleteRead(buf, buf_size, bytes_read); | 319 return CompleteRead(buf, buf_size); |
327 return true; | |
328 } | 320 } |
329 | 321 |
330 void URLRequestChromeJob::CompleteRead(net::IOBuffer* buf, | 322 int URLRequestChromeJob::CompleteRead(net::IOBuffer* buf, int buf_size) { |
331 int buf_size, | |
332 int* bytes_read) { | |
333 // http://crbug.com/373841 | 323 // http://crbug.com/373841 |
334 char url_buf[128]; | 324 char url_buf[128]; |
335 base::strlcpy(url_buf, request_->url().spec().c_str(), arraysize(url_buf)); | 325 base::strlcpy(url_buf, request_->url().spec().c_str(), arraysize(url_buf)); |
336 base::debug::Alias(url_buf); | 326 base::debug::Alias(url_buf); |
337 | 327 |
338 int remaining = static_cast<int>(data_->size()) - data_offset_; | 328 int remaining = data_->size() - data_offset_; |
339 if (buf_size > remaining) | 329 if (buf_size > remaining) |
340 buf_size = remaining; | 330 buf_size = remaining; |
341 if (buf_size > 0) { | 331 if (buf_size > 0) { |
342 memcpy(buf->data(), data_->front() + data_offset_, buf_size); | 332 memcpy(buf->data(), data_->front() + data_offset_, buf_size); |
343 data_offset_ += buf_size; | 333 data_offset_ += buf_size; |
344 } | 334 } |
345 *bytes_read = buf_size; | 335 return buf_size; |
346 } | 336 } |
347 | 337 |
348 namespace { | 338 namespace { |
349 | 339 |
350 // Gets mime type for data that is available from |source| by |path|. | 340 // Gets mime type for data that is available from |source| by |path|. |
351 // After that, notifies |job| that mime type is available. This method | 341 // After that, notifies |job| that mime type is available. This method |
352 // should be called on the UI thread, but notification is performed on | 342 // should be called on the UI thread, but notification is performed on |
353 // the IO thread. | 343 // the IO thread. |
354 void GetMimeTypeOnUI(URLDataSourceIOSImpl* source, | 344 void GetMimeTypeOnUI(URLDataSourceIOSImpl* source, |
355 const std::string& path, | 345 const std::string& path, |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
541 // Forward this data on to the pending net::URLRequest, if it exists. | 531 // Forward this data on to the pending net::URLRequest, if it exists. |
542 PendingRequestMap::iterator i = pending_requests_.find(request_id); | 532 PendingRequestMap::iterator i = pending_requests_.find(request_id); |
543 if (i != pending_requests_.end()) { | 533 if (i != pending_requests_.end()) { |
544 URLRequestChromeJob* job(i->second); | 534 URLRequestChromeJob* job(i->second); |
545 pending_requests_.erase(i); | 535 pending_requests_.erase(i); |
546 job->DataAvailable(bytes); | 536 job->DataAvailable(bytes); |
547 } | 537 } |
548 } | 538 } |
549 | 539 |
550 } // namespace web | 540 } // namespace web |
OLD | NEW |