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" |
| 11 #include "base/singleton.h" |
11 #include "base/string_util.h" | 12 #include "base/string_util.h" |
12 #include "base/thread.h" | 13 #include "base/thread.h" |
13 #include "base/values.h" | 14 #include "base/values.h" |
14 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
15 #include "chrome/browser/chrome_thread.h" | 16 #include "chrome/browser/chrome_thread.h" |
16 #include "chrome/common/chrome_paths.h" | 17 #include "chrome/common/chrome_paths.h" |
17 #include "chrome/common/ref_counted_util.h" | 18 #include "chrome/common/ref_counted_util.h" |
18 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
19 #include "googleurl/src/url_util.h" | 20 #include "googleurl/src/url_util.h" |
20 #include "net/base/io_buffer.h" | 21 #include "net/base/io_buffer.h" |
21 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
22 #include "net/url_request/url_request.h" | 23 #include "net/url_request/url_request.h" |
23 #include "net/url_request/url_request_file_job.h" | 24 #include "net/url_request/url_request_file_job.h" |
24 #include "net/url_request/url_request_job.h" | 25 #include "net/url_request/url_request_job.h" |
25 #include "net/url_request/url_request_view_net_internals_job.h" | 26 #include "net/url_request/url_request_view_net_internals_job.h" |
26 | 27 |
27 #include "grit/locale_settings.h" | 28 #include "grit/locale_settings.h" |
28 | 29 |
29 // The URL scheme used for internal chrome resources. | 30 // The URL scheme used for internal chrome resources. |
30 // TODO(glen): Choose a better location for this. | 31 // TODO(glen): Choose a better location for this. |
31 static const char kChromeURLScheme[] = "chrome"; | 32 static const char kChromeURLScheme[] = "chrome"; |
32 | 33 |
33 // The single global instance of ChromeURLDataManager. | |
34 ChromeURLDataManager chrome_url_data_manager; | |
35 | |
36 // URLRequestChromeJob is a URLRequestJob that manages running chrome-internal | 34 // URLRequestChromeJob is a URLRequestJob that manages running chrome-internal |
37 // resource requests asynchronously. | 35 // resource requests asynchronously. |
38 // It hands off URL requests to ChromeURLDataManager, which asynchronously | 36 // It hands off URL requests to ChromeURLDataManager, which asynchronously |
39 // calls back once the data is available. | 37 // calls back once the data is available. |
40 class URLRequestChromeJob : public URLRequestJob { | 38 class URLRequestChromeJob : public URLRequestJob { |
41 public: | 39 public: |
42 explicit URLRequestChromeJob(URLRequest* request); | 40 explicit URLRequestChromeJob(URLRequest* request); |
43 virtual ~URLRequestChromeJob(); | 41 virtual ~URLRequestChromeJob(); |
44 | 42 |
45 // URLRequestJob implementation. | 43 // URLRequestJob implementation. |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 url_scheme_component)) { | 96 url_scheme_component)) { |
99 url_util::AddStandardScheme(kChromeURLScheme); | 97 url_util::AddStandardScheme(kChromeURLScheme); |
100 } | 98 } |
101 | 99 |
102 FilePath inspector_dir; | 100 FilePath inspector_dir; |
103 if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) { | 101 if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) { |
104 // TODO(yurys): remove "inspector" source when new developer tools support | 102 // TODO(yurys): remove "inspector" source when new developer tools support |
105 // all features of in-process Web Inspector and Console Debugger. For the | 103 // all features of in-process Web Inspector and Console Debugger. For the |
106 // time being we need to serve the same content from chrome://inspector | 104 // time being we need to serve the same content from chrome://inspector |
107 // for the Console Debugger and in-process Web Inspector. | 105 // for the Console Debugger and in-process Web Inspector. |
108 chrome_url_data_manager.AddFileSource("inspector", inspector_dir); | 106 Singleton<ChromeURLDataManager>()->AddFileSource("inspector", |
109 chrome_url_data_manager.AddFileSource(chrome::kChromeUIDevToolsHost, | 107 inspector_dir); |
110 inspector_dir); | 108 Singleton<ChromeURLDataManager>()->AddFileSource( |
| 109 chrome::kChromeUIDevToolsHost, inspector_dir); |
111 } | 110 } |
112 | 111 |
113 URLRequest::RegisterProtocolFactory(kChromeURLScheme, | 112 URLRequest::RegisterProtocolFactory(kChromeURLScheme, |
114 &ChromeURLDataManager::Factory); | 113 &ChromeURLDataManager::Factory); |
115 URLRequest::RegisterProtocolFactory(chrome::kPrintScheme, | 114 URLRequest::RegisterProtocolFactory(chrome::kPrintScheme, |
116 &ChromeURLDataManager::Factory); | 115 &ChromeURLDataManager::Factory); |
117 } | 116 } |
118 | 117 |
119 void UnregisterURLRequestChromeJob() { | 118 void UnregisterURLRequestChromeJob() { |
120 FilePath inspector_dir; | 119 FilePath inspector_dir; |
121 if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) { | 120 if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) { |
122 chrome_url_data_manager.RemoveFileSource("inspector"); | 121 Singleton<ChromeURLDataManager>()->RemoveFileSource("inspector"); |
123 chrome_url_data_manager.RemoveFileSource(chrome::kChromeUIDevToolsHost); | 122 Singleton<ChromeURLDataManager>()->RemoveFileSource( |
| 123 chrome::kChromeUIDevToolsHost); |
124 } | 124 } |
125 } | 125 } |
126 | 126 |
127 // static | 127 // static |
128 void ChromeURLDataManager::URLToRequest(const GURL& url, | 128 void ChromeURLDataManager::URLToRequest(const GURL& url, |
129 std::string* source_name, | 129 std::string* source_name, |
130 std::string* path) { | 130 std::string* path) { |
131 DCHECK(url.SchemeIs(kChromeURLScheme) || url.SchemeIs(chrome::kPrintScheme)); | 131 DCHECK(url.SchemeIs(kChromeURLScheme) || url.SchemeIs(chrome::kPrintScheme)); |
132 | 132 |
133 if (!url.is_valid()) { | 133 if (!url.is_valid()) { |
(...skipping 23 matching lines...) Expand all Loading... |
157 | 157 |
158 // static | 158 // static |
159 bool ChromeURLDataManager::URLToFilePath(const GURL& url, | 159 bool ChromeURLDataManager::URLToFilePath(const GURL& url, |
160 FilePath* file_path) { | 160 FilePath* file_path) { |
161 // Parse the URL into a request for a source and path. | 161 // Parse the URL into a request for a source and path. |
162 std::string source_name; | 162 std::string source_name; |
163 std::string relative_path; | 163 std::string relative_path; |
164 URLToRequest(url, &source_name, &relative_path); | 164 URLToRequest(url, &source_name, &relative_path); |
165 | 165 |
166 FileSourceMap::const_iterator i( | 166 FileSourceMap::const_iterator i( |
167 chrome_url_data_manager.file_sources_.find(source_name)); | 167 Singleton<ChromeURLDataManager>()->file_sources_.find(source_name)); |
168 if (i == chrome_url_data_manager.file_sources_.end()) | 168 if (i == Singleton<ChromeURLDataManager>()->file_sources_.end()) |
169 return false; | 169 return false; |
170 | 170 |
171 *file_path = i->second.AppendASCII(relative_path); | 171 *file_path = i->second.AppendASCII(relative_path); |
172 | 172 |
173 return true; | 173 return true; |
174 } | 174 } |
175 | 175 |
176 ChromeURLDataManager::ChromeURLDataManager() : next_request_id_(0) { } | 176 ChromeURLDataManager::ChromeURLDataManager() : next_request_id_(0) { } |
177 | 177 |
178 ChromeURLDataManager::~ChromeURLDataManager() { } | 178 ChromeURLDataManager::~ChromeURLDataManager() { } |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 RefCountedMemory* bytes) { | 275 RefCountedMemory* bytes) { |
276 ChromeThread::PostTask( | 276 ChromeThread::PostTask( |
277 ChromeThread::IO, FROM_HERE, | 277 ChromeThread::IO, FROM_HERE, |
278 NewRunnableMethod(&chrome_url_data_manager, | 278 NewRunnableMethod(Singleton<ChromeURLDataManager>().get(), |
279 &ChromeURLDataManager::DataAvailable, | 279 &ChromeURLDataManager::DataAvailable, |
280 request_id, scoped_refptr<RefCountedMemory>(bytes))); | 280 request_id, scoped_refptr<RefCountedMemory>(bytes))); |
281 } | 281 } |
282 | 282 |
283 MessageLoop* ChromeURLDataManager::DataSource::MessageLoopForRequestPath( | 283 MessageLoop* ChromeURLDataManager::DataSource::MessageLoopForRequestPath( |
284 const std::string& path) const { | 284 const std::string& path) const { |
285 return message_loop_; | 285 return message_loop_; |
286 } | 286 } |
287 | 287 |
288 // static | 288 // static |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 return new URLRequestChromeJob(request); | 343 return new URLRequestChromeJob(request); |
344 } | 344 } |
345 | 345 |
346 URLRequestChromeJob::URLRequestChromeJob(URLRequest* request) | 346 URLRequestChromeJob::URLRequestChromeJob(URLRequest* request) |
347 : URLRequestJob(request), | 347 : URLRequestJob(request), |
348 data_offset_(0), | 348 data_offset_(0), |
349 pending_buf_size_(0) { | 349 pending_buf_size_(0) { |
350 } | 350 } |
351 | 351 |
352 URLRequestChromeJob::~URLRequestChromeJob() { | 352 URLRequestChromeJob::~URLRequestChromeJob() { |
353 CHECK(!chrome_url_data_manager.HasPendingJob(this)); | 353 CHECK(!Singleton<ChromeURLDataManager>()->HasPendingJob(this)); |
354 } | 354 } |
355 | 355 |
356 void URLRequestChromeJob::Start() { | 356 void URLRequestChromeJob::Start() { |
357 // Start reading asynchronously so that all error reporting and data | 357 // Start reading asynchronously so that all error reporting and data |
358 // callbacks happen as they would for network requests. | 358 // callbacks happen as they would for network requests. |
359 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 359 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
360 this, &URLRequestChromeJob::StartAsync)); | 360 this, &URLRequestChromeJob::StartAsync)); |
361 } | 361 } |
362 | 362 |
363 void URLRequestChromeJob::Kill() { | 363 void URLRequestChromeJob::Kill() { |
364 chrome_url_data_manager.RemoveRequest(this); | 364 Singleton<ChromeURLDataManager>()->RemoveRequest(this); |
365 } | 365 } |
366 | 366 |
367 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const { | 367 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const { |
368 *mime_type = mime_type_; | 368 *mime_type = mime_type_; |
369 return !mime_type_.empty(); | 369 return !mime_type_.empty(); |
370 } | 370 } |
371 | 371 |
372 void URLRequestChromeJob::DataAvailable(RefCountedMemory* bytes) { | 372 void URLRequestChromeJob::DataAvailable(RefCountedMemory* bytes) { |
373 if (bytes) { | 373 if (bytes) { |
374 // The request completed, and we have all the data. | 374 // The request completed, and we have all the data. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 memcpy(buf->data(), data_->front() + data_offset_, buf_size); | 414 memcpy(buf->data(), data_->front() + data_offset_, buf_size); |
415 data_offset_ += buf_size; | 415 data_offset_ += buf_size; |
416 } | 416 } |
417 *bytes_read = buf_size; | 417 *bytes_read = buf_size; |
418 } | 418 } |
419 | 419 |
420 void URLRequestChromeJob::StartAsync() { | 420 void URLRequestChromeJob::StartAsync() { |
421 if (!request_) | 421 if (!request_) |
422 return; | 422 return; |
423 | 423 |
424 if (chrome_url_data_manager.StartRequest(request_->url(), this)) { | 424 if (Singleton<ChromeURLDataManager>()->StartRequest(request_->url(), this)) { |
425 NotifyHeadersComplete(); | 425 NotifyHeadersComplete(); |
426 } else { | 426 } else { |
427 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, | 427 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, |
428 net::ERR_INVALID_URL)); | 428 net::ERR_INVALID_URL)); |
429 } | 429 } |
430 } | 430 } |
431 | 431 |
432 URLRequestChromeFileJob::URLRequestChromeFileJob(URLRequest* request, | 432 URLRequestChromeFileJob::URLRequestChromeFileJob(URLRequest* request, |
433 const FilePath& path) | 433 const FilePath& path) |
434 : URLRequestFileJob(request, path) { | 434 : URLRequestFileJob(request, path) { |
435 } | 435 } |
436 | 436 |
437 URLRequestChromeFileJob::~URLRequestChromeFileJob() { } | 437 URLRequestChromeFileJob::~URLRequestChromeFileJob() { } |
OLD | NEW |