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