| 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 "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 virtual ~URLRequestChromeJob(); | 43 virtual ~URLRequestChromeJob(); |
| 44 | 44 |
| 45 // URLRequestJob implementation. | 45 // URLRequestJob implementation. |
| 46 virtual void Start(); | 46 virtual void Start(); |
| 47 virtual void Kill(); | 47 virtual void Kill(); |
| 48 virtual bool ReadRawData(net::IOBuffer* buf, int buf_size, int *bytes_read); | 48 virtual bool ReadRawData(net::IOBuffer* buf, int buf_size, int *bytes_read); |
| 49 virtual bool GetMimeType(std::string* mime_type) const; | 49 virtual bool GetMimeType(std::string* mime_type) const; |
| 50 | 50 |
| 51 // Called by ChromeURLDataManager to notify us that the data blob is ready | 51 // Called by ChromeURLDataManager to notify us that the data blob is ready |
| 52 // for us. | 52 // for us. |
| 53 void DataAvailable(RefCountedBytes* bytes); | 53 void DataAvailable(RefCountedMemory* bytes); |
| 54 | 54 |
| 55 void SetMimeType(const std::string& mime_type) { | 55 void SetMimeType(const std::string& mime_type) { |
| 56 mime_type_ = mime_type; | 56 mime_type_ = mime_type; |
| 57 } | 57 } |
| 58 | 58 |
| 59 private: | 59 private: |
| 60 // Helper for Start(), to let us start asynchronously. | 60 // Helper for Start(), to let us start asynchronously. |
| 61 // (This pattern is shared by most URLRequestJob implementations.) | 61 // (This pattern is shared by most URLRequestJob implementations.) |
| 62 void StartAsync(); | 62 void StartAsync(); |
| 63 | 63 |
| 64 // Do the actual copy from data_ (the data we're serving) into |buf|. | 64 // Do the actual copy from data_ (the data we're serving) into |buf|. |
| 65 // Separate from ReadRawData so we can handle async I/O. | 65 // Separate from ReadRawData so we can handle async I/O. |
| 66 void CompleteRead(net::IOBuffer* buf, int buf_size, int* bytes_read); | 66 void CompleteRead(net::IOBuffer* buf, int buf_size, int* bytes_read); |
| 67 | 67 |
| 68 // The actual data we're serving. NULL until it's been fetched. | 68 // The actual data we're serving. NULL until it's been fetched. |
| 69 scoped_refptr<RefCountedBytes> data_; | 69 scoped_refptr<RefCountedMemory> data_; |
| 70 // The current offset into the data that we're handing off to our | 70 // The current offset into the data that we're handing off to our |
| 71 // callers via the Read interfaces. | 71 // callers via the Read interfaces. |
| 72 int data_offset_; | 72 int data_offset_; |
| 73 | 73 |
| 74 // For async reads, we keep around a pointer to the buffer that | 74 // For async reads, we keep around a pointer to the buffer that |
| 75 // we're reading into. | 75 // we're reading into. |
| 76 scoped_refptr<net::IOBuffer> pending_buf_; | 76 scoped_refptr<net::IOBuffer> pending_buf_; |
| 77 int pending_buf_size_; | 77 int pending_buf_size_; |
| 78 std::string mime_type_; | 78 std::string mime_type_; |
| 79 | 79 |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 i != pending_requests_.end(); ++i) { | 251 i != pending_requests_.end(); ++i) { |
| 252 if (i->second == job) { | 252 if (i->second == job) { |
| 253 pending_requests_.erase(i); | 253 pending_requests_.erase(i); |
| 254 return; | 254 return; |
| 255 } | 255 } |
| 256 } | 256 } |
| 257 } | 257 } |
| 258 | 258 |
| 259 void ChromeURLDataManager::DataAvailable( | 259 void ChromeURLDataManager::DataAvailable( |
| 260 RequestID request_id, | 260 RequestID request_id, |
| 261 scoped_refptr<RefCountedBytes> bytes) { | 261 scoped_refptr<RefCountedMemory> bytes) { |
| 262 // Forward this data on to the pending URLRequest, if it exists. | 262 // Forward this data on to the pending URLRequest, if it exists. |
| 263 PendingRequestMap::iterator i = pending_requests_.find(request_id); | 263 PendingRequestMap::iterator i = pending_requests_.find(request_id); |
| 264 if (i != pending_requests_.end()) { | 264 if (i != pending_requests_.end()) { |
| 265 // We acquire a reference to the job so that it doesn't disappear under the | 265 // We acquire a reference to the job so that it doesn't disappear under the |
| 266 // feet of any method invoked here (we could trigger a callback). | 266 // feet of any method invoked here (we could trigger a callback). |
| 267 scoped_refptr<URLRequestChromeJob> job = i->second; | 267 scoped_refptr<URLRequestChromeJob> job = i->second; |
| 268 pending_requests_.erase(i); | 268 pending_requests_.erase(i); |
| 269 job->DataAvailable(bytes); | 269 job->DataAvailable(bytes); |
| 270 } | 270 } |
| 271 } | 271 } |
| 272 | 272 |
| 273 void ChromeURLDataManager::DataSource::SendResponse( | 273 void ChromeURLDataManager::DataSource::SendResponse( |
| 274 RequestID request_id, | 274 RequestID request_id, |
| 275 RefCountedBytes* bytes) { | 275 RefCountedMemory* bytes) { |
| 276 ChromeThread::GetMessageLoop(ChromeThread::IO)->PostTask(FROM_HERE, | 276 ChromeThread::GetMessageLoop(ChromeThread::IO)->PostTask(FROM_HERE, |
| 277 NewRunnableMethod(&chrome_url_data_manager, | 277 NewRunnableMethod(&chrome_url_data_manager, |
| 278 &ChromeURLDataManager::DataAvailable, | 278 &ChromeURLDataManager::DataAvailable, |
| 279 request_id, scoped_refptr<RefCountedBytes>(bytes))); | 279 request_id, scoped_refptr<RefCountedMemory>(bytes))); |
| 280 } | 280 } |
| 281 | 281 |
| 282 MessageLoop* ChromeURLDataManager::DataSource::MessageLoopForRequestPath( | 282 MessageLoop* ChromeURLDataManager::DataSource::MessageLoopForRequestPath( |
| 283 const std::string& path) const { | 283 const std::string& path) const { |
| 284 return message_loop_; | 284 return message_loop_; |
| 285 } | 285 } |
| 286 | 286 |
| 287 // static | 287 // static |
| 288 void ChromeURLDataManager::DataSource::SetFontAndTextDirection( | 288 void ChromeURLDataManager::DataSource::SetFontAndTextDirection( |
| 289 DictionaryValue* localized_strings) { | 289 DictionaryValue* localized_strings) { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 | 361 |
| 362 void URLRequestChromeJob::Kill() { | 362 void URLRequestChromeJob::Kill() { |
| 363 chrome_url_data_manager.RemoveRequest(this); | 363 chrome_url_data_manager.RemoveRequest(this); |
| 364 } | 364 } |
| 365 | 365 |
| 366 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const { | 366 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const { |
| 367 *mime_type = mime_type_; | 367 *mime_type = mime_type_; |
| 368 return !mime_type_.empty(); | 368 return !mime_type_.empty(); |
| 369 } | 369 } |
| 370 | 370 |
| 371 void URLRequestChromeJob::DataAvailable(RefCountedBytes* bytes) { | 371 void URLRequestChromeJob::DataAvailable(RefCountedMemory* bytes) { |
| 372 if (bytes) { | 372 if (bytes) { |
| 373 // The request completed, and we have all the data. | 373 // The request completed, and we have all the data. |
| 374 // Clear any IO pending status. | 374 // Clear any IO pending status. |
| 375 SetStatus(URLRequestStatus()); | 375 SetStatus(URLRequestStatus()); |
| 376 | 376 |
| 377 data_ = bytes; | 377 data_ = bytes; |
| 378 int bytes_read; | 378 int bytes_read; |
| 379 if (pending_buf_.get()) { | 379 if (pending_buf_.get()) { |
| 380 CHECK(pending_buf_->data()); | 380 CHECK(pending_buf_->data()); |
| 381 CompleteRead(pending_buf_, pending_buf_size_, &bytes_read); | 381 CompleteRead(pending_buf_, pending_buf_size_, &bytes_read); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 399 return false; // Tell the caller we're still waiting for data. | 399 return false; // Tell the caller we're still waiting for data. |
| 400 } | 400 } |
| 401 | 401 |
| 402 // Otherwise, the data is available. | 402 // Otherwise, the data is available. |
| 403 CompleteRead(buf, buf_size, bytes_read); | 403 CompleteRead(buf, buf_size, bytes_read); |
| 404 return true; | 404 return true; |
| 405 } | 405 } |
| 406 | 406 |
| 407 void URLRequestChromeJob::CompleteRead(net::IOBuffer* buf, int buf_size, | 407 void URLRequestChromeJob::CompleteRead(net::IOBuffer* buf, int buf_size, |
| 408 int* bytes_read) { | 408 int* bytes_read) { |
| 409 int remaining = static_cast<int>(data_->data.size()) - data_offset_; | 409 int remaining = static_cast<int>(data_->size()) - data_offset_; |
| 410 if (buf_size > remaining) | 410 if (buf_size > remaining) |
| 411 buf_size = remaining; | 411 buf_size = remaining; |
| 412 if (buf_size > 0) { | 412 if (buf_size > 0) { |
| 413 memcpy(buf->data(), &data_->data[0] + data_offset_, buf_size); | 413 memcpy(buf->data(), data_->front() + data_offset_, buf_size); |
| 414 data_offset_ += buf_size; | 414 data_offset_ += buf_size; |
| 415 } | 415 } |
| 416 *bytes_read = buf_size; | 416 *bytes_read = buf_size; |
| 417 } | 417 } |
| 418 | 418 |
| 419 void URLRequestChromeJob::StartAsync() { | 419 void URLRequestChromeJob::StartAsync() { |
| 420 if (!request_) | 420 if (!request_) |
| 421 return; | 421 return; |
| 422 | 422 |
| 423 if (chrome_url_data_manager.StartRequest(request_->url(), this)) { | 423 if (chrome_url_data_manager.StartRequest(request_->url(), this)) { |
| 424 NotifyHeadersComplete(); | 424 NotifyHeadersComplete(); |
| 425 } else { | 425 } else { |
| 426 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, | 426 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, |
| 427 net::ERR_INVALID_URL)); | 427 net::ERR_INVALID_URL)); |
| 428 } | 428 } |
| 429 } | 429 } |
| 430 | 430 |
| 431 URLRequestChromeFileJob::URLRequestChromeFileJob(URLRequest* request, | 431 URLRequestChromeFileJob::URLRequestChromeFileJob(URLRequest* request, |
| 432 const FilePath& path) | 432 const FilePath& path) |
| 433 : URLRequestFileJob(request, path) { | 433 : URLRequestFileJob(request, path) { |
| 434 } | 434 } |
| 435 | 435 |
| 436 URLRequestChromeFileJob::~URLRequestChromeFileJob() { } | 436 URLRequestChromeFileJob::~URLRequestChromeFileJob() { } |
| OLD | NEW |