| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 | 89 |
| 90 private: | 90 private: |
| 91 virtual ~URLRequestChromeFileJob(); | 91 virtual ~URLRequestChromeFileJob(); |
| 92 | 92 |
| 93 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeFileJob); | 93 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeFileJob); |
| 94 }; | 94 }; |
| 95 | 95 |
| 96 void RegisterURLRequestChromeJob() { | 96 void RegisterURLRequestChromeJob() { |
| 97 FilePath inspector_dir; | 97 FilePath inspector_dir; |
| 98 if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) { | 98 if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) { |
| 99 Singleton<ChromeURLDataManager>()->AddFileSource( | 99 ChromeURLDataManager::GetInstance()->AddFileSource( |
| 100 chrome::kChromeUIDevToolsHost, inspector_dir); | 100 chrome::kChromeUIDevToolsHost, inspector_dir); |
| 101 } | 101 } |
| 102 | 102 |
| 103 SharedResourcesDataSource::Register(); | 103 SharedResourcesDataSource::Register(); |
| 104 net::URLRequest::RegisterProtocolFactory(chrome::kChromeDevToolsScheme, | 104 net::URLRequest::RegisterProtocolFactory(chrome::kChromeDevToolsScheme, |
| 105 &ChromeURLDataManager::Factory); | 105 &ChromeURLDataManager::Factory); |
| 106 net::URLRequest::RegisterProtocolFactory(chrome::kChromeUIScheme, | 106 net::URLRequest::RegisterProtocolFactory(chrome::kChromeUIScheme, |
| 107 &ChromeURLDataManager::Factory); | 107 &ChromeURLDataManager::Factory); |
| 108 } | 108 } |
| 109 | 109 |
| 110 void UnregisterURLRequestChromeJob() { | 110 void UnregisterURLRequestChromeJob() { |
| 111 FilePath inspector_dir; | 111 FilePath inspector_dir; |
| 112 if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) { | 112 if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) { |
| 113 Singleton<ChromeURLDataManager>()->RemoveFileSource( | 113 ChromeURLDataManager::GetInstance()->RemoveFileSource( |
| 114 chrome::kChromeUIDevToolsHost); | 114 chrome::kChromeUIDevToolsHost); |
| 115 } | 115 } |
| 116 } | 116 } |
| 117 | 117 |
| 118 // static | 118 // static |
| 119 void ChromeURLDataManager::URLToRequest(const GURL& url, | 119 void ChromeURLDataManager::URLToRequest(const GURL& url, |
| 120 std::string* source_name, | 120 std::string* source_name, |
| 121 std::string* path) { | 121 std::string* path) { |
| 122 DCHECK(url.SchemeIs(chrome::kChromeDevToolsScheme) || | 122 DCHECK(url.SchemeIs(chrome::kChromeDevToolsScheme) || |
| 123 url.SchemeIs(chrome::kChromeUIScheme)); | 123 url.SchemeIs(chrome::kChromeUIScheme)); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 151 // Remove Query and Ref from URL. | 151 // Remove Query and Ref from URL. |
| 152 GURL stripped_url; | 152 GURL stripped_url; |
| 153 GURL::Replacements replacements; | 153 GURL::Replacements replacements; |
| 154 replacements.ClearQuery(); | 154 replacements.ClearQuery(); |
| 155 replacements.ClearRef(); | 155 replacements.ClearRef(); |
| 156 stripped_url = url.ReplaceComponents(replacements); | 156 stripped_url = url.ReplaceComponents(replacements); |
| 157 | 157 |
| 158 URLToRequest(stripped_url, &source_name, &relative_path); | 158 URLToRequest(stripped_url, &source_name, &relative_path); |
| 159 | 159 |
| 160 FileSourceMap::const_iterator i( | 160 FileSourceMap::const_iterator i( |
| 161 Singleton<ChromeURLDataManager>()->file_sources_.find(source_name)); | 161 ChromeURLDataManager::GetInstance()->file_sources_.find(source_name)); |
| 162 if (i == Singleton<ChromeURLDataManager>()->file_sources_.end()) | 162 if (i == ChromeURLDataManager::GetInstance()->file_sources_.end()) |
| 163 return false; | 163 return false; |
| 164 | 164 |
| 165 // Check that |relative_path| is not an absolute path (otherwise AppendASCII() | 165 // Check that |relative_path| is not an absolute path (otherwise AppendASCII() |
| 166 // will DCHECK). The awkward use of StringType is because on some systems | 166 // will DCHECK). The awkward use of StringType is because on some systems |
| 167 // FilePath expects a std::string, but on others a std::wstring. | 167 // FilePath expects a std::string, but on others a std::wstring. |
| 168 FilePath p(FilePath::StringType(relative_path.begin(), relative_path.end())); | 168 FilePath p(FilePath::StringType(relative_path.begin(), relative_path.end())); |
| 169 if (p.IsAbsolute()) | 169 if (p.IsAbsolute()) |
| 170 return false; | 170 return false; |
| 171 | 171 |
| 172 *file_path = i->second.AppendASCII(relative_path); | 172 *file_path = i->second.AppendASCII(relative_path); |
| 173 | 173 |
| 174 return true; | 174 return true; |
| 175 } | 175 } |
| 176 | 176 |
| 177 ChromeURLDataManager::ChromeURLDataManager() : next_request_id_(0) { } | 177 ChromeURLDataManager::ChromeURLDataManager() : next_request_id_(0) { } |
| 178 | 178 |
| 179 ChromeURLDataManager::~ChromeURLDataManager() { } | 179 ChromeURLDataManager::~ChromeURLDataManager() { } |
| 180 | 180 |
| 181 // static |
| 182 ChromeURLDataManager* ChromeURLDataManager::GetInstance() { |
| 183 return Singleton<ChromeURLDataManager>::get(); |
| 184 } |
| 185 |
| 181 void ChromeURLDataManager::AddDataSource(scoped_refptr<DataSource> source) { | 186 void ChromeURLDataManager::AddDataSource(scoped_refptr<DataSource> source) { |
| 182 // TODO(jackson): A new data source with same name should not clobber the | 187 // TODO(jackson): A new data source with same name should not clobber the |
| 183 // existing one. | 188 // existing one. |
| 184 data_sources_[source->source_name()] = source; | 189 data_sources_[source->source_name()] = source; |
| 185 } | 190 } |
| 186 | 191 |
| 187 void ChromeURLDataManager::AddFileSource(const std::string& source_name, | 192 void ChromeURLDataManager::AddFileSource(const std::string& source_name, |
| 188 const FilePath& file_path) { | 193 const FilePath& file_path) { |
| 189 DCHECK(file_sources_.count(source_name) == 0); | 194 DCHECK(file_sources_.count(source_name) == 0); |
| 190 file_sources_[source_name] = file_path; | 195 file_sources_[source_name] = file_path; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 } | 285 } |
| 281 | 286 |
| 282 ChromeURLDataManager::DataSource::~DataSource() { | 287 ChromeURLDataManager::DataSource::~DataSource() { |
| 283 } | 288 } |
| 284 | 289 |
| 285 void ChromeURLDataManager::DataSource::SendResponse( | 290 void ChromeURLDataManager::DataSource::SendResponse( |
| 286 RequestID request_id, | 291 RequestID request_id, |
| 287 RefCountedMemory* bytes) { | 292 RefCountedMemory* bytes) { |
| 288 BrowserThread::PostTask( | 293 BrowserThread::PostTask( |
| 289 BrowserThread::IO, FROM_HERE, | 294 BrowserThread::IO, FROM_HERE, |
| 290 NewRunnableMethod(Singleton<ChromeURLDataManager>::get(), | 295 NewRunnableMethod(ChromeURLDataManager::GetInstance(), |
| 291 &ChromeURLDataManager::DataAvailable, | 296 &ChromeURLDataManager::DataAvailable, |
| 292 request_id, scoped_refptr<RefCountedMemory>(bytes))); | 297 request_id, scoped_refptr<RefCountedMemory>(bytes))); |
| 293 } | 298 } |
| 294 | 299 |
| 295 MessageLoop* ChromeURLDataManager::DataSource::MessageLoopForRequestPath( | 300 MessageLoop* ChromeURLDataManager::DataSource::MessageLoopForRequestPath( |
| 296 const std::string& path) const { | 301 const std::string& path) const { |
| 297 return message_loop_; | 302 return message_loop_; |
| 298 } | 303 } |
| 299 | 304 |
| 300 // static | 305 // static |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 return new URLRequestChromeJob(request); | 345 return new URLRequestChromeJob(request); |
| 341 } | 346 } |
| 342 | 347 |
| 343 URLRequestChromeJob::URLRequestChromeJob(net::URLRequest* request) | 348 URLRequestChromeJob::URLRequestChromeJob(net::URLRequest* request) |
| 344 : URLRequestJob(request), | 349 : URLRequestJob(request), |
| 345 data_offset_(0), | 350 data_offset_(0), |
| 346 pending_buf_size_(0) { | 351 pending_buf_size_(0) { |
| 347 } | 352 } |
| 348 | 353 |
| 349 URLRequestChromeJob::~URLRequestChromeJob() { | 354 URLRequestChromeJob::~URLRequestChromeJob() { |
| 350 CHECK(!Singleton<ChromeURLDataManager>()->HasPendingJob(this)); | 355 CHECK(!ChromeURLDataManager::GetInstance()->HasPendingJob(this)); |
| 351 } | 356 } |
| 352 | 357 |
| 353 void URLRequestChromeJob::Start() { | 358 void URLRequestChromeJob::Start() { |
| 354 // Start reading asynchronously so that all error reporting and data | 359 // Start reading asynchronously so that all error reporting and data |
| 355 // callbacks happen as they would for network requests. | 360 // callbacks happen as they would for network requests. |
| 356 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 361 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 357 this, &URLRequestChromeJob::StartAsync)); | 362 this, &URLRequestChromeJob::StartAsync)); |
| 358 } | 363 } |
| 359 | 364 |
| 360 void URLRequestChromeJob::Kill() { | 365 void URLRequestChromeJob::Kill() { |
| 361 Singleton<ChromeURLDataManager>()->RemoveRequest(this); | 366 ChromeURLDataManager::GetInstance()->RemoveRequest(this); |
| 362 } | 367 } |
| 363 | 368 |
| 364 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const { | 369 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const { |
| 365 *mime_type = mime_type_; | 370 *mime_type = mime_type_; |
| 366 return !mime_type_.empty(); | 371 return !mime_type_.empty(); |
| 367 } | 372 } |
| 368 | 373 |
| 369 void URLRequestChromeJob::DataAvailable(RefCountedMemory* bytes) { | 374 void URLRequestChromeJob::DataAvailable(RefCountedMemory* bytes) { |
| 370 if (bytes) { | 375 if (bytes) { |
| 371 // The request completed, and we have all the data. | 376 // The request completed, and we have all the data. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 memcpy(buf->data(), data_->front() + data_offset_, buf_size); | 416 memcpy(buf->data(), data_->front() + data_offset_, buf_size); |
| 412 data_offset_ += buf_size; | 417 data_offset_ += buf_size; |
| 413 } | 418 } |
| 414 *bytes_read = buf_size; | 419 *bytes_read = buf_size; |
| 415 } | 420 } |
| 416 | 421 |
| 417 void URLRequestChromeJob::StartAsync() { | 422 void URLRequestChromeJob::StartAsync() { |
| 418 if (!request_) | 423 if (!request_) |
| 419 return; | 424 return; |
| 420 | 425 |
| 421 if (Singleton<ChromeURLDataManager>()->StartRequest(request_->url(), this)) { | 426 if (ChromeURLDataManager::GetInstance()->StartRequest(request_->url(), |
| 427 this)) { |
| 422 NotifyHeadersComplete(); | 428 NotifyHeadersComplete(); |
| 423 } else { | 429 } else { |
| 424 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, | 430 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, |
| 425 net::ERR_INVALID_URL)); | 431 net::ERR_INVALID_URL)); |
| 426 } | 432 } |
| 427 } | 433 } |
| 428 | 434 |
| 429 URLRequestChromeFileJob::URLRequestChromeFileJob(net::URLRequest* request, | 435 URLRequestChromeFileJob::URLRequestChromeFileJob(net::URLRequest* request, |
| 430 const FilePath& path) | 436 const FilePath& path) |
| 431 : URLRequestFileJob(request, path) { | 437 : URLRequestFileJob(request, path) { |
| 432 } | 438 } |
| 433 | 439 |
| 434 URLRequestChromeFileJob::~URLRequestChromeFileJob() { } | 440 URLRequestChromeFileJob::~URLRequestChromeFileJob() { } |
| OLD | NEW |