Chromium Code Reviews| Index: chrome/browser/dom_ui/chrome_url_data_manager_backend.cc |
| =================================================================== |
| --- chrome/browser/dom_ui/chrome_url_data_manager_backend.cc (revision 73882) |
| +++ chrome/browser/dom_ui/chrome_url_data_manager_backend.cc (working copy) |
| @@ -2,21 +2,13 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "chrome/browser/dom_ui/chrome_url_data_manager.h" |
| +#include "chrome/browser/dom_ui/chrome_url_data_manager_backend.h" |
| #include "base/file_util.h" |
| -#include "base/i18n/rtl.h" |
| #include "base/message_loop.h" |
| #include "base/path_service.h" |
| #include "base/ref_counted_memory.h" |
| -#include "base/singleton.h" |
| -#include "base/stl_util-inl.h" |
| #include "base/string_util.h" |
| -#include "base/threading/thread.h" |
| -#include "base/values.h" |
| -#if defined(OS_WIN) |
| -#include "base/win/windows_version.h" |
| -#endif |
| #include "chrome/browser/appcache/view_appcache_internals_job_factory.h" |
| #include "chrome/browser/browser_thread.h" |
| #include "chrome/browser/dom_ui/shared_resources_data_source.h" |
| @@ -24,7 +16,6 @@ |
| #include "chrome/browser/net/view_blob_internals_job_factory.h" |
| #include "chrome/browser/net/view_http_cache_job_factory.h" |
| #include "chrome/common/chrome_paths.h" |
| -#include "chrome/common/ref_counted_util.h" |
| #include "chrome/common/url_constants.h" |
| #include "googleurl/src/url_util.h" |
| #include "grit/platform_locale_settings.h" |
| @@ -33,8 +24,12 @@ |
| #include "net/url_request/url_request.h" |
| #include "net/url_request/url_request_file_job.h" |
| #include "net/url_request/url_request_job.h" |
| -#include "ui/base/l10n/l10n_util.h" |
| +static ChromeURLDataManagerBackend* GetBackend(net::URLRequest* request) { |
| + return static_cast<ChromeURLRequestContext*>(request->context())-> |
| + GetChromeURLDataManagerBackend(); |
| +} |
| + |
| // URLRequestChromeJob is a net::URLRequestJob that manages running |
| // chrome-internal resource requests asynchronously. |
| // It hands off URL requests to ChromeURLDataManager, which asynchronously |
| @@ -80,6 +75,8 @@ |
| int pending_buf_size_; |
| std::string mime_type_; |
| + scoped_refptr<ChromeURLDataManagerBackend> backend_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(URLRequestChromeJob); |
| }; |
| @@ -94,32 +91,9 @@ |
| DISALLOW_COPY_AND_ASSIGN(URLRequestChromeFileJob); |
| }; |
| -void RegisterURLRequestChromeJob() { |
| - FilePath inspector_dir; |
| - if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) { |
| - ChromeURLDataManager::GetInstance()->AddFileSource( |
| - chrome::kChromeUIDevToolsHost, inspector_dir); |
| - } |
| - |
| - SharedResourcesDataSource::Register(); |
| - net::URLRequest::RegisterProtocolFactory(chrome::kChromeDevToolsScheme, |
| - &ChromeURLDataManager::Factory); |
| - net::URLRequest::RegisterProtocolFactory(chrome::kChromeUIScheme, |
| - &ChromeURLDataManager::Factory); |
| -} |
| - |
| -void UnregisterURLRequestChromeJob() { |
| - FilePath inspector_dir; |
| - if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) { |
| - ChromeURLDataManager::GetInstance()->RemoveFileSource( |
| - chrome::kChromeUIDevToolsHost); |
| - } |
| -} |
| - |
| -// static |
| -void ChromeURLDataManager::URLToRequest(const GURL& url, |
| - std::string* source_name, |
| - std::string* path) { |
| +void ChromeURLDataManagerBackend::URLToRequest(const GURL& url, |
| + std::string* source_name, |
| + std::string* path) { |
| DCHECK(url.SchemeIs(chrome::kChromeDevToolsScheme) || |
| url.SchemeIs(chrome::kChromeUIScheme)); |
| @@ -142,9 +116,8 @@ |
| path->assign(spec.substr(offset)); |
| } |
| -// static |
| -bool ChromeURLDataManager::URLToFilePath(const GURL& url, |
| - FilePath* file_path) { |
| +bool ChromeURLDataManagerBackend::URLToFilePath(const GURL& url, |
| + FilePath* file_path) { |
| // Parse the URL into a request for a source and path. |
| std::string source_name; |
| std::string relative_path; |
| @@ -158,9 +131,8 @@ |
| URLToRequest(stripped_url, &source_name, &relative_path); |
| - FileSourceMap::const_iterator i( |
| - ChromeURLDataManager::GetInstance()->file_sources_.find(source_name)); |
| - if (i == ChromeURLDataManager::GetInstance()->file_sources_.end()) |
| + FileSourceMap::const_iterator i(file_sources_.find(source_name)); |
| + if (i == file_sources_.end()) |
| return false; |
| // Check that |relative_path| is not an absolute path (otherwise AppendASCII() |
| @@ -175,88 +147,127 @@ |
| return true; |
| } |
| -ChromeURLDataManager::ChromeURLDataManager() : next_request_id_(0) { } |
| +// static |
| +base::Lock* ChromeURLDataManagerBackend::instances_lock_ = NULL; |
| -ChromeURLDataManager::~ChromeURLDataManager() { |
| - // This is used as a Singleton, so it is only called at exit cleanup time. |
| - // This means it is called on the main (UI) thread. |
| - // |
| - // It will break if it is called at shutdown time on a different thread, as |
| - // it will attempt to call the destructors for its |data_source_|s on the |
| - // UI thread, but the UI thread's message loop will be not be running |
| - // -- so the destructor calls will be dropped and we will leak the objects. |
| -} |
| +// static |
| +ChromeURLDataManagerBackend::Instances* |
| + ChromeURLDataManagerBackend::instances_ = NULL; |
| // static |
| -ChromeURLDataManager* ChromeURLDataManager::GetInstance() { |
| - return Singleton<ChromeURLDataManager>::get(); |
| +ChromeURLDataManagerBackend::LockedInstances* |
| + ChromeURLDataManagerBackend::shutdown_instances_ = NULL; |
| + |
| +ChromeURLDataManagerBackend::ChromeURLDataManagerBackend() |
| + : next_request_id_(0) { |
| + FilePath inspector_dir; |
| + if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) |
| + AddFileSource(chrome::kChromeUIDevToolsHost, inspector_dir); |
| + AddDataSource(new SharedResourcesDataSource()); |
| + { |
| + base::AutoLock lock(*instances_lock_); |
|
Evan Martin
2011/02/07 19:06:21
It seems you have a precondition that ::Register()
|
| + instances_->push_back(this); |
| + if (shutdown_instances_) |
| + shutdown_instances_->push_back(this); |
| + } |
| } |
| -void ChromeURLDataManager::AddDataSource(scoped_refptr<DataSource> source) { |
| - // Some |DataSource|-derived classes, notably |FileIconSource| and |
| - // |WebUIFavIconSource|, have members that will DCHECK if they are not |
| - // destructed in the same thread as they are constructed (the UI thread). |
| - // |
| - // If |AddDataSource| is called more than once, it will destruct the object |
| - // that it had before, as it is the only thing still holding a reference to |
| - // that object. |DataSource| uses the |DeleteOnUIThread| trait to insure |
| - // that the destructor is called on the UI thread. |
| - // |
| - // TODO(jackson): A new data source with same name should not clobber the |
| - // existing one. |
| - data_sources_[source->source_name()] = source; |
| +void ChromeURLDataManagerBackend::Register() { |
| + net::URLRequest::RegisterProtocolFactory( |
| + chrome::kChromeDevToolsScheme, |
| + &ChromeURLDataManagerBackend::Factory); |
| + net::URLRequest::RegisterProtocolFactory( |
| + chrome::kChromeUIScheme, |
| + &ChromeURLDataManagerBackend::Factory); |
| + DCHECK(!instances_lock_); |
| + instances_lock_ = new base::Lock(); |
| + DCHECK(!instances_); |
| + instances_ = new Instances(); |
| } |
| -void ChromeURLDataManager::RemoveDataSourceForTest(const char* source_name) { |
| - DataSourceMap::iterator i = data_sources_.find(source_name); |
| - if (i == data_sources_.end()) |
| - return; |
| - (*i).second = NULL; // Calls Release(). |
| - data_sources_.erase(i); |
| +// static |
| +void ChromeURLDataManagerBackend::PrepareForShutdown() { |
| + base::AutoLock lock(*instances_lock_); |
| + DCHECK(!shutdown_instances_); |
| + // Create LockedInstances and all existing instances to it so that none of the |
| + // existing instances get destroyed. We need to do this otherwise by the time |
| + // CompleteShutdown is invoked all the ChromeURLDataManagerBackends will |
| + // likely have been destroyed along with their DataSources. But because the UI |
| + // thread is no longer running, all the DataSources end up leaking. By |
| + // delaying the destruction until CompleteShutdown is invoked on the UI |
| + // thread, we know everything gets destroyed on the UI thread. |
| + shutdown_instances_ = new LockedInstances(); |
| + for (Instances::iterator i = instances_->begin(); i != instances_->end(); ++i) |
| + shutdown_instances_->push_back(make_scoped_refptr(*i)); |
| } |
| -void ChromeURLDataManager::RemoveAllDataSources() { |
| - for (DataSourceMap::iterator i = data_sources_.begin(); |
| - i != data_sources_.end(); |
| - i = data_sources_.begin()) { |
| - (*i).second = NULL; // Calls Release(). |
| - data_sources_.erase(i); |
| +// static |
| +void ChromeURLDataManagerBackend::CompleteShutdown() { |
| + LockedInstances instances; |
| + { |
| + base::AutoLock lock(*instances_lock_); |
| + DCHECK(shutdown_instances_); // PrepareForShutdown must have been invoked. |
| + instances.swap(*shutdown_instances_); |
| + delete shutdown_instances_; |
| + shutdown_instances_ = NULL; |
| } |
| + for (LockedInstances::iterator i = instances.begin(); i != instances.end(); |
| + ++i) { |
| + (*i)->ReleaseDataSources(); |
| + } |
| } |
| -void ChromeURLDataManager::AddFileSource(const std::string& source_name, |
| - const FilePath& file_path) { |
| - DCHECK(file_sources_.count(source_name) == 0); |
| - file_sources_[source_name] = file_path; |
| +ChromeURLDataManagerBackend::~ChromeURLDataManagerBackend() { |
| + { |
| + base::AutoLock lock(*instances_lock_); |
| + instances_->erase(std::find(instances_->begin(), instances_->end(), this)); |
| + } |
| + ReleaseDataSources(); |
| } |
| -void ChromeURLDataManager::RemoveFileSource(const std::string& source_name) { |
| - DCHECK(file_sources_.count(source_name) == 1); |
| - file_sources_.erase(source_name); |
| +void ChromeURLDataManagerBackend::AddDataSource( |
| + ChromeURLDataManager::DataSource* source) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + base::AutoLock lock(data_sources_lock_); |
| + DataSourceMap::iterator i = data_sources_.find(source->source_name()); |
| + if (i != data_sources_.end()) |
| + i->second->backend_ = NULL; |
| + data_sources_[source->source_name()] = source; |
| + source->backend_ = this; |
| } |
| -bool ChromeURLDataManager::HasPendingJob(URLRequestChromeJob* job) const { |
| +void ChromeURLDataManagerBackend::AddFileSource(const std::string& source_name, |
| + const FilePath& file_path) { |
| + DCHECK(file_sources_.count(source_name) == 0); |
| + file_sources_[source_name] = file_path; |
| +} |
| + |
| +bool ChromeURLDataManagerBackend::HasPendingJob( |
| + URLRequestChromeJob* job) const { |
| for (PendingRequestMap::const_iterator i = pending_requests_.begin(); |
| i != pending_requests_.end(); ++i) { |
| if (i->second == job) |
| return true; |
| } |
| - |
| return false; |
| } |
| -bool ChromeURLDataManager::StartRequest(const GURL& url, |
| - URLRequestChromeJob* job) { |
| +bool ChromeURLDataManagerBackend::StartRequest(const GURL& url, |
| + URLRequestChromeJob* job) { |
| // Parse the URL into a request for a source and path. |
| std::string source_name; |
| std::string path; |
| URLToRequest(url, &source_name, &path); |
| // Look up the data source for the request. |
| - DataSourceMap::iterator i = data_sources_.find(source_name); |
| - if (i == data_sources_.end()) |
| - return false; |
| - DataSource* source = i->second; |
| + scoped_refptr<ChromeURLDataManager::DataSource> source; |
| + { |
| + base::AutoLock lock(data_sources_lock_); |
| + DataSourceMap::iterator i = data_sources_.find(source_name); |
| + if (i == data_sources_.end()) |
| + return false; |
| + source = i->second; |
| + } |
| // Save this request so we know where to send the data. |
| RequestID request_id = next_request_id_++; |
| @@ -280,14 +291,16 @@ |
| } else { |
| // The DataSource wants StartDataRequest to be called on a specific thread, |
| // usually the UI thread, for this path. |
| - target_message_loop->PostTask(FROM_HERE, |
| - NewRunnableMethod(source, &DataSource::StartDataRequest, |
| + target_message_loop->PostTask( |
| + FROM_HERE, |
| + NewRunnableMethod(source.get(), |
| + &ChromeURLDataManager::DataSource::StartDataRequest, |
| path, context->is_off_the_record(), request_id)); |
| } |
| return true; |
| } |
| -void ChromeURLDataManager::RemoveRequest(URLRequestChromeJob* job) { |
| +void ChromeURLDataManagerBackend::RemoveRequest(URLRequestChromeJob* job) { |
| // Remove the request from our list of pending requests. |
| // If/when the source sends the data that was requested, the data will just |
| // be thrown away. |
| @@ -300,9 +313,8 @@ |
| } |
| } |
| -void ChromeURLDataManager::DataAvailable( |
| - RequestID request_id, |
| - scoped_refptr<RefCountedMemory> bytes) { |
| +void ChromeURLDataManagerBackend::DataAvailable(RequestID request_id, |
| + RefCountedMemory* bytes) { |
| // Forward this data on to the pending net::URLRequest, if it exists. |
| PendingRequestMap::iterator i = pending_requests_.find(request_id); |
| if (i != pending_requests_.end()) { |
| @@ -314,54 +326,26 @@ |
| } |
| } |
| -ChromeURLDataManager::DataSource::DataSource(const std::string& source_name, |
| - MessageLoop* message_loop) |
| - : source_name_(source_name), message_loop_(message_loop) { |
| +void ChromeURLDataManagerBackend::ReleaseDataSources() { |
| + DataSourceMap data_sources; |
| + { |
| + base::AutoLock lock(data_sources_lock_); |
| + data_sources.swap(data_sources_); |
| + } |
| + for (DataSourceMap::iterator i = data_sources.begin(); |
| + i != data_sources.end(); ++i) { |
| + i->second->backend_ = NULL; |
| + } |
| } |
| -ChromeURLDataManager::DataSource::~DataSource() { |
| -} |
| - |
| -void ChromeURLDataManager::DataSource::SendResponse( |
| - RequestID request_id, |
| - RefCountedMemory* bytes) { |
| - BrowserThread::PostTask( |
| - BrowserThread::IO, FROM_HERE, |
| - NewRunnableMethod(ChromeURLDataManager::GetInstance(), |
| - &ChromeURLDataManager::DataAvailable, |
| - request_id, scoped_refptr<RefCountedMemory>(bytes))); |
| -} |
| - |
| -MessageLoop* ChromeURLDataManager::DataSource::MessageLoopForRequestPath( |
| - const std::string& path) const { |
| - return message_loop_; |
| -} |
| - |
| // static |
| -void ChromeURLDataManager::DataSource::SetFontAndTextDirection( |
| - DictionaryValue* localized_strings) { |
| - localized_strings->SetString("fontfamily", |
| - l10n_util::GetStringUTF16(IDS_WEB_FONT_FAMILY)); |
| - |
| - int web_font_size_id = IDS_WEB_FONT_SIZE; |
| -#if defined(OS_WIN) |
| - // Some fonts used for some languages changed a lot in terms of the font |
| - // metric in Vista. So, we need to use different size before Vista. |
| - if (base::win::GetVersion() < base::win::VERSION_VISTA) |
| - web_font_size_id = IDS_WEB_FONT_SIZE_XP; |
| -#endif |
| - localized_strings->SetString("fontsize", |
| - l10n_util::GetStringUTF16(web_font_size_id)); |
| - |
| - localized_strings->SetString("textdirection", |
| - base::i18n::IsRTL() ? "rtl" : "ltr"); |
| -} |
| - |
| -net::URLRequestJob* ChromeURLDataManager::Factory(net::URLRequest* request, |
| - const std::string& scheme) { |
| +net::URLRequestJob* ChromeURLDataManagerBackend::Factory( |
| + net::URLRequest* request, |
| + const std::string& scheme) { |
| // Try first with a file handler |
| FilePath path; |
| - if (ChromeURLDataManager::URLToFilePath(request->url(), &path)) |
| + ChromeURLDataManagerBackend* backend = GetBackend(request); |
| + if (backend->URLToFilePath(request->url(), &path)) |
| return new URLRequestChromeFileJob(request, path); |
| // Next check for chrome://view-http-cache/*, which uses its own job type. |
| @@ -383,11 +367,12 @@ |
| URLRequestChromeJob::URLRequestChromeJob(net::URLRequest* request) |
| : net::URLRequestJob(request), |
| data_offset_(0), |
| - pending_buf_size_(0) { |
| + pending_buf_size_(0), |
| + backend_(GetBackend(request)) { |
| } |
| URLRequestChromeJob::~URLRequestChromeJob() { |
| - CHECK(!ChromeURLDataManager::GetInstance()->HasPendingJob(this)); |
| + CHECK(!backend_->HasPendingJob(this)); |
| } |
| void URLRequestChromeJob::Start() { |
| @@ -398,7 +383,7 @@ |
| } |
| void URLRequestChromeJob::Kill() { |
| - ChromeURLDataManager::GetInstance()->RemoveRequest(this); |
| + backend_->RemoveRequest(this); |
| } |
| bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const { |
| @@ -459,8 +444,7 @@ |
| if (!request_) |
| return; |
| - if (ChromeURLDataManager::GetInstance()->StartRequest(request_->url(), |
| - this)) { |
| + if (backend_->StartRequest(request_->url(), this)) { |
| NotifyHeadersComplete(); |
| } else { |
| NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| @@ -473,4 +457,4 @@ |
| : net::URLRequestFileJob(request, path) { |
| } |
| -URLRequestChromeFileJob::~URLRequestChromeFileJob() { } |
| +URLRequestChromeFileJob::~URLRequestChromeFileJob() {} |