| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "chrome/browser/dom_ui/chrome_url_data_manager.h" | 5 #include "chrome/browser/dom_ui/chrome_url_data_manager.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 // It hands off URL requests to ChromeURLDataManager, which asynchronously | 35 // It hands off URL requests to ChromeURLDataManager, which asynchronously |
| 36 // calls back once the data is available. | 36 // calls back once the data is available. |
| 37 class URLRequestChromeJob : public URLRequestJob { | 37 class URLRequestChromeJob : public URLRequestJob { |
| 38 public: | 38 public: |
| 39 explicit URLRequestChromeJob(URLRequest* request); | 39 explicit URLRequestChromeJob(URLRequest* request); |
| 40 virtual ~URLRequestChromeJob(); | 40 virtual ~URLRequestChromeJob(); |
| 41 | 41 |
| 42 // URLRequestJob implementation. | 42 // URLRequestJob implementation. |
| 43 virtual void Start(); | 43 virtual void Start(); |
| 44 virtual void Kill(); | 44 virtual void Kill(); |
| 45 virtual bool ReadRawData(char* buf, int buf_size, int *bytes_read); | 45 virtual bool ReadRawData(net::IOBuffer* buf, int buf_size, int *bytes_read); |
| 46 virtual bool GetMimeType(std::string* mime_type); | 46 virtual bool GetMimeType(std::string* mime_type); |
| 47 | 47 |
| 48 // Called by ChromeURLDataManager to notify us that the data blob is ready | 48 // Called by ChromeURLDataManager to notify us that the data blob is ready |
| 49 // for us. | 49 // for us. |
| 50 void DataAvailable(RefCountedBytes* bytes); | 50 void DataAvailable(RefCountedBytes* bytes); |
| 51 | 51 |
| 52 void SetMimeType(const std::string& mime_type) { | 52 void SetMimeType(const std::string& mime_type) { |
| 53 mime_type_ = mime_type; | 53 mime_type_ = mime_type; |
| 54 } | 54 } |
| 55 | 55 |
| 56 private: | 56 private: |
| 57 // Helper for Start(), to let us start asynchronously. | 57 // Helper for Start(), to let us start asynchronously. |
| 58 // (This pattern is shared by most URLRequestJob implementations.) | 58 // (This pattern is shared by most URLRequestJob implementations.) |
| 59 void StartAsync(); | 59 void StartAsync(); |
| 60 | 60 |
| 61 // Do the actual copy from data_ (the data we're serving) into |buf|. | 61 // Do the actual copy from data_ (the data we're serving) into |buf|. |
| 62 // Separate from ReadRawData so we can handle async I/O. | 62 // Separate from ReadRawData so we can handle async I/O. |
| 63 void CompleteRead(char* buf, int buf_size, int* bytes_read); | 63 void CompleteRead(net::IOBuffer* buf, int buf_size, int* bytes_read); |
| 64 | 64 |
| 65 // The actual data we're serving. NULL until it's been fetched. | 65 // The actual data we're serving. NULL until it's been fetched. |
| 66 scoped_refptr<RefCountedBytes> data_; | 66 scoped_refptr<RefCountedBytes> data_; |
| 67 // The current offset into the data that we're handing off to our | 67 // The current offset into the data that we're handing off to our |
| 68 // callers via the Read interfaces. | 68 // callers via the Read interfaces. |
| 69 int data_offset_; | 69 int data_offset_; |
| 70 | 70 |
| 71 // For async reads, we keep around a pointer to the buffer that | 71 // For async reads, we keep around a pointer to the buffer that |
| 72 // we're reading into. | 72 // we're reading into. |
| 73 char* pending_buf_; | 73 scoped_refptr<net::IOBuffer> pending_buf_; |
| 74 int pending_buf_size_; | 74 int pending_buf_size_; |
| 75 std::string mime_type_; | 75 std::string mime_type_; |
| 76 | 76 |
| 77 DISALLOW_EVIL_CONSTRUCTORS(URLRequestChromeJob); | 77 DISALLOW_EVIL_CONSTRUCTORS(URLRequestChromeJob); |
| 78 }; | 78 }; |
| 79 | 79 |
| 80 // URLRequestChromeFileJob is a URLRequestJob that acts like a file:// URL | 80 // URLRequestChromeFileJob is a URLRequestJob that acts like a file:// URL |
| 81 class URLRequestChromeFileJob : public URLRequestFileJob { | 81 class URLRequestChromeFileJob : public URLRequestFileJob { |
| 82 public: | 82 public: |
| 83 URLRequestChromeFileJob(URLRequest* request, const FilePath& path); | 83 URLRequestChromeFileJob(URLRequest* request, const FilePath& path); |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 std::wstring path; | 242 std::wstring path; |
| 243 if (ChromeURLDataManager::URLToFilePath(request->url(), &path)) | 243 if (ChromeURLDataManager::URLToFilePath(request->url(), &path)) |
| 244 return new URLRequestChromeFileJob(request, | 244 return new URLRequestChromeFileJob(request, |
| 245 FilePath::FromWStringHack(path)); | 245 FilePath::FromWStringHack(path)); |
| 246 | 246 |
| 247 // Fall back to using a custom handler | 247 // Fall back to using a custom handler |
| 248 return new URLRequestChromeJob(request); | 248 return new URLRequestChromeJob(request); |
| 249 } | 249 } |
| 250 | 250 |
| 251 URLRequestChromeJob::URLRequestChromeJob(URLRequest* request) | 251 URLRequestChromeJob::URLRequestChromeJob(URLRequest* request) |
| 252 : URLRequestJob(request), data_offset_(0), pending_buf_(NULL) {} | 252 : URLRequestJob(request), data_offset_(0) {} |
| 253 | 253 |
| 254 URLRequestChromeJob::~URLRequestChromeJob() { | 254 URLRequestChromeJob::~URLRequestChromeJob() { |
| 255 } | 255 } |
| 256 | 256 |
| 257 void URLRequestChromeJob::Start() { | 257 void URLRequestChromeJob::Start() { |
| 258 // Start reading asynchronously so that all error reporting and data | 258 // Start reading asynchronously so that all error reporting and data |
| 259 // callbacks happen as they would for network requests. | 259 // callbacks happen as they would for network requests. |
| 260 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 260 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 261 this, &URLRequestChromeJob::StartAsync)); | 261 this, &URLRequestChromeJob::StartAsync)); |
| 262 } | 262 } |
| 263 | 263 |
| 264 void URLRequestChromeJob::Kill() { | 264 void URLRequestChromeJob::Kill() { |
| 265 chrome_url_data_manager.RemoveRequest(this); | 265 chrome_url_data_manager.RemoveRequest(this); |
| 266 } | 266 } |
| 267 | 267 |
| 268 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) { | 268 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) { |
| 269 *mime_type = mime_type_; | 269 *mime_type = mime_type_; |
| 270 return !mime_type_.empty(); | 270 return !mime_type_.empty(); |
| 271 } | 271 } |
| 272 | 272 |
| 273 void URLRequestChromeJob::DataAvailable(RefCountedBytes* bytes) { | 273 void URLRequestChromeJob::DataAvailable(RefCountedBytes* bytes) { |
| 274 if (bytes) { | 274 if (bytes) { |
| 275 // The request completed, and we have all the data. | 275 // The request completed, and we have all the data. |
| 276 // Clear any IO pending status. | 276 // Clear any IO pending status. |
| 277 SetStatus(URLRequestStatus()); | 277 SetStatus(URLRequestStatus()); |
| 278 | 278 |
| 279 data_ = bytes; | 279 data_ = bytes; |
| 280 int bytes_read; | 280 int bytes_read; |
| 281 if (pending_buf_) { | 281 if (pending_buf_.get()) { |
| 282 CompleteRead(pending_buf_, pending_buf_size_, &bytes_read); | 282 CompleteRead(pending_buf_, pending_buf_size_, &bytes_read); |
| 283 NotifyReadComplete(bytes_read); | 283 NotifyReadComplete(bytes_read); |
| 284 pending_buf_ = NULL; |
| 284 } | 285 } |
| 285 } else { | 286 } else { |
| 286 // The request failed. | 287 // The request failed. |
| 287 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, 0)); | 288 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, 0)); |
| 288 } | 289 } |
| 289 } | 290 } |
| 290 | 291 |
| 291 bool URLRequestChromeJob::ReadRawData(char* buf, int buf_size, | 292 bool URLRequestChromeJob::ReadRawData(net::IOBuffer* buf, int buf_size, |
| 292 int* bytes_read) { | 293 int* bytes_read) { |
| 293 if (!data_.get()) { | 294 if (!data_.get()) { |
| 294 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); | 295 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 296 DCHECK(!pending_buf_.get()); |
| 295 pending_buf_ = buf; | 297 pending_buf_ = buf; |
| 296 pending_buf_size_ = buf_size; | 298 pending_buf_size_ = buf_size; |
| 297 return false; // Tell the caller we're still waiting for data. | 299 return false; // Tell the caller we're still waiting for data. |
| 298 } | 300 } |
| 299 | 301 |
| 300 // Otherwise, the data is available. | 302 // Otherwise, the data is available. |
| 301 CompleteRead(buf, buf_size, bytes_read); | 303 CompleteRead(buf, buf_size, bytes_read); |
| 302 return true; | 304 return true; |
| 303 } | 305 } |
| 304 | 306 |
| 305 void URLRequestChromeJob::CompleteRead(char* buf, int buf_size, | 307 void URLRequestChromeJob::CompleteRead(net::IOBuffer* buf, int buf_size, |
| 306 int* bytes_read) { | 308 int* bytes_read) { |
| 307 int remaining = static_cast<int>(data_->data.size()) - data_offset_; | 309 int remaining = static_cast<int>(data_->data.size()) - data_offset_; |
| 308 if (buf_size > remaining) | 310 if (buf_size > remaining) |
| 309 buf_size = remaining; | 311 buf_size = remaining; |
| 310 if (buf_size > 0) { | 312 if (buf_size > 0) { |
| 311 memcpy(buf, &data_->data[0] + data_offset_, buf_size); | 313 memcpy(buf->data(), &data_->data[0] + data_offset_, buf_size); |
| 312 data_offset_ += buf_size; | 314 data_offset_ += buf_size; |
| 313 } | 315 } |
| 314 *bytes_read = buf_size; | 316 *bytes_read = buf_size; |
| 315 } | 317 } |
| 316 | 318 |
| 317 void URLRequestChromeJob::StartAsync() { | 319 void URLRequestChromeJob::StartAsync() { |
| 318 if (!request_) | 320 if (!request_) |
| 319 return; | 321 return; |
| 320 | 322 |
| 321 if (chrome_url_data_manager.StartRequest(request_->url(), this)) { | 323 if (chrome_url_data_manager.StartRequest(request_->url(), this)) { |
| 322 NotifyHeadersComplete(); | 324 NotifyHeadersComplete(); |
| 323 } else { | 325 } else { |
| 324 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, | 326 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, |
| 325 net::ERR_INVALID_URL)); | 327 net::ERR_INVALID_URL)); |
| 326 } | 328 } |
| 327 } | 329 } |
| 328 | 330 |
| 329 URLRequestChromeFileJob::URLRequestChromeFileJob(URLRequest* request, | 331 URLRequestChromeFileJob::URLRequestChromeFileJob(URLRequest* request, |
| 330 const FilePath& path) | 332 const FilePath& path) |
| 331 : URLRequestFileJob(request, path) { | 333 : URLRequestFileJob(request, path) { |
| 332 } | 334 } |
| 333 | 335 |
| 334 URLRequestChromeFileJob::~URLRequestChromeFileJob() { } | 336 URLRequestChromeFileJob::~URLRequestChromeFileJob() { } |
| 335 | 337 |
| OLD | NEW |