| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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(char* buf, int buf_size, int *bytes_read); | 48 virtual bool ReadRawData(char* buf, int buf_size, int *bytes_read); |
| 49 virtual bool GetMimeType(std::string* mime_type); | 49 virtual bool GetMimeType(std::string* mime_type); |
| 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(RefCountedBytes* bytes); |
| 54 | 54 |
| 55 void SetMimeType(const std::string& mime_type) { |
| 56 mime_type_ = mime_type; |
| 57 } |
| 58 |
| 55 private: | 59 private: |
| 56 // Helper for Start(), to let us start asynchronously. | 60 // Helper for Start(), to let us start asynchronously. |
| 57 // (This pattern is shared by most URLRequestJob implementations.) | 61 // (This pattern is shared by most URLRequestJob implementations.) |
| 58 void StartAsync(); | 62 void StartAsync(); |
| 59 | 63 |
| 60 // 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|. |
| 61 // Separate from ReadRawData so we can handle async I/O. | 65 // Separate from ReadRawData so we can handle async I/O. |
| 62 void CompleteRead(char* buf, int buf_size, int* bytes_read); | 66 void CompleteRead(char* buf, int buf_size, int* bytes_read); |
| 63 | 67 |
| 64 // 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. |
| 65 scoped_refptr<RefCountedBytes> data_; | 69 scoped_refptr<RefCountedBytes> data_; |
| 66 // 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 |
| 67 // callers via the Read interfaces. | 71 // callers via the Read interfaces. |
| 68 int data_offset_; | 72 int data_offset_; |
| 69 | 73 |
| 70 // 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 |
| 71 // we're reading into. | 75 // we're reading into. |
| 72 char* pending_buf_; | 76 char* pending_buf_; |
| 73 int pending_buf_size_; | 77 int pending_buf_size_; |
| 78 std::string mime_type_; |
| 74 | 79 |
| 75 DISALLOW_EVIL_CONSTRUCTORS(URLRequestChromeJob); | 80 DISALLOW_EVIL_CONSTRUCTORS(URLRequestChromeJob); |
| 76 }; | 81 }; |
| 77 | 82 |
| 78 // URLRequestChromeFileJob is a URLRequestJob that acts like a file:// URL | 83 // URLRequestChromeFileJob is a URLRequestJob that acts like a file:// URL |
| 79 class URLRequestChromeFileJob : public URLRequestFileJob { | 84 class URLRequestChromeFileJob : public URLRequestFileJob { |
| 80 public: | 85 public: |
| 81 URLRequestChromeFileJob(URLRequest* request, const std::wstring& path); | 86 URLRequestChromeFileJob(URLRequest* request, const std::wstring& path); |
| 82 virtual ~URLRequestChromeFileJob(); | 87 virtual ~URLRequestChromeFileJob(); |
| 83 | 88 |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 // Look up the data source for the request. | 185 // Look up the data source for the request. |
| 181 DataSourceMap::iterator i = data_sources_.find(source_name); | 186 DataSourceMap::iterator i = data_sources_.find(source_name); |
| 182 if (i == data_sources_.end()) | 187 if (i == data_sources_.end()) |
| 183 return false; | 188 return false; |
| 184 DataSource* source = i->second; | 189 DataSource* source = i->second; |
| 185 | 190 |
| 186 // Save this request so we know where to send the data. | 191 // Save this request so we know where to send the data. |
| 187 RequestID request_id = next_request_id_++; | 192 RequestID request_id = next_request_id_++; |
| 188 pending_requests_.insert(std::make_pair(request_id, job)); | 193 pending_requests_.insert(std::make_pair(request_id, job)); |
| 189 | 194 |
| 195 // TODO(eroman): would be nicer if the mimetype were set at the same time |
| 196 // as the data blob. For now do it here, since NotifyHeadersComplete() is |
| 197 // going to get called once we return. |
| 198 job->SetMimeType(source->GetMimeType(path)); |
| 199 |
| 190 // Forward along the request to the data source. | 200 // Forward along the request to the data source. |
| 191 source->message_loop()->PostTask(FROM_HERE, | 201 source->message_loop()->PostTask(FROM_HERE, |
| 192 NewRunnableMethod(source, &DataSource::StartDataRequest, | 202 NewRunnableMethod(source, &DataSource::StartDataRequest, |
| 193 path, request_id)); | 203 path, request_id)); |
| 194 return true; | 204 return true; |
| 195 } | 205 } |
| 196 | 206 |
| 197 void ChromeURLDataManager::RemoveRequest(URLRequestChromeJob* job) { | 207 void ChromeURLDataManager::RemoveRequest(URLRequestChromeJob* job) { |
| 198 // Remove the request from our list of pending requests. | 208 // Remove the request from our list of pending requests. |
| 199 // If/when the source sends the data that was requested, the data will just | 209 // If/when the source sends the data that was requested, the data will just |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 // callbacks happen as they would for network requests. | 261 // callbacks happen as they would for network requests. |
| 252 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 262 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 253 this, &URLRequestChromeJob::StartAsync)); | 263 this, &URLRequestChromeJob::StartAsync)); |
| 254 } | 264 } |
| 255 | 265 |
| 256 void URLRequestChromeJob::Kill() { | 266 void URLRequestChromeJob::Kill() { |
| 257 chrome_url_data_manager.RemoveRequest(this); | 267 chrome_url_data_manager.RemoveRequest(this); |
| 258 } | 268 } |
| 259 | 269 |
| 260 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) { | 270 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) { |
| 261 // Rely on MIME sniffing to simplify the logic here. | 271 *mime_type = mime_type_; |
| 262 *mime_type = "text/html"; | 272 return !mime_type_.empty(); |
| 263 return true; | |
| 264 } | 273 } |
| 265 | 274 |
| 266 void URLRequestChromeJob::DataAvailable(RefCountedBytes* bytes) { | 275 void URLRequestChromeJob::DataAvailable(RefCountedBytes* bytes) { |
| 267 if (bytes) { | 276 if (bytes) { |
| 268 // The request completed, and we have all the data. | 277 // The request completed, and we have all the data. |
| 269 // Clear any IO pending status. | 278 // Clear any IO pending status. |
| 270 SetStatus(URLRequestStatus()); | 279 SetStatus(URLRequestStatus()); |
| 271 | 280 |
| 272 data_ = bytes; | 281 data_ = bytes; |
| 273 int bytes_read; | 282 int bytes_read; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 } | 329 } |
| 321 | 330 |
| 322 URLRequestChromeFileJob::URLRequestChromeFileJob(URLRequest* request, | 331 URLRequestChromeFileJob::URLRequestChromeFileJob(URLRequest* request, |
| 323 const std::wstring& path) | 332 const std::wstring& path) |
| 324 : URLRequestFileJob(request) { | 333 : URLRequestFileJob(request) { |
| 325 this->file_path_ = path; // set URLRequestFileJob::file_path_ | 334 this->file_path_ = path; // set URLRequestFileJob::file_path_ |
| 326 } | 335 } |
| 327 | 336 |
| 328 URLRequestChromeFileJob::~URLRequestChromeFileJob() { } | 337 URLRequestChromeFileJob::~URLRequestChromeFileJob() { } |
| 329 | 338 |
| OLD | NEW |