| Index: chrome/browser/dom_ui/chrome_url_data_manager_backend.cc
|
| ===================================================================
|
| --- chrome/browser/dom_ui/chrome_url_data_manager_backend.cc (revision 74394)
|
| +++ 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,9 @@
|
| int pending_buf_size_;
|
| std::string mime_type_;
|
|
|
| + // The backend is owned by ChromeURLRequestContext and always outlives us.
|
| + ChromeURLDataManagerBackend* backend_;
|
| +
|
| DISALLOW_COPY_AND_ASSIGN(URLRequestChromeJob);
|
| };
|
|
|
| @@ -94,32 +92,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 +117,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 +132,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,78 +148,60 @@
|
| return true;
|
| }
|
|
|
| -ChromeURLDataManager::ChromeURLDataManager() : next_request_id_(0) { }
|
| +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());
|
| +}
|
|
|
| -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.
|
| +ChromeURLDataManagerBackend::~ChromeURLDataManagerBackend() {
|
| + for (DataSourceMap::iterator i = data_sources_.begin();
|
| + i != data_sources_.end(); ++i) {
|
| + i->second->backend_ = NULL;
|
| + }
|
| + data_sources_.clear();
|
| }
|
|
|
| // static
|
| -ChromeURLDataManager* ChromeURLDataManager::GetInstance() {
|
| - return Singleton<ChromeURLDataManager>::get();
|
| +void ChromeURLDataManagerBackend::Register() {
|
| + net::URLRequest::RegisterProtocolFactory(
|
| + chrome::kChromeDevToolsScheme,
|
| + &ChromeURLDataManagerBackend::Factory);
|
| + net::URLRequest::RegisterProtocolFactory(
|
| + chrome::kChromeUIScheme,
|
| + &ChromeURLDataManagerBackend::Factory);
|
| }
|
|
|
| -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.
|
| +void ChromeURLDataManagerBackend::AddDataSource(
|
| + ChromeURLDataManager::DataSource* source) {
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
| + 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;
|
| }
|
|
|
| -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);
|
| -}
|
| -
|
| -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);
|
| - }
|
| -}
|
| -
|
| -void ChromeURLDataManager::AddFileSource(const std::string& source_name,
|
| - const FilePath& file_path) {
|
| +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;
|
| }
|
|
|
| -void ChromeURLDataManager::RemoveFileSource(const std::string& source_name) {
|
| - DCHECK(file_sources_.count(source_name) == 1);
|
| - file_sources_.erase(source_name);
|
| -}
|
| -
|
| -bool ChromeURLDataManager::HasPendingJob(URLRequestChromeJob* job) const {
|
| +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;
|
| @@ -256,8 +211,9 @@
|
| DataSourceMap::iterator i = data_sources_.find(source_name);
|
| if (i == data_sources_.end())
|
| return false;
|
| - DataSource* source = i->second;
|
|
|
| + ChromeURLDataManager::DataSource* source = i->second;
|
| +
|
| // Save this request so we know where to send the data.
|
| RequestID request_id = next_request_id_++;
|
| pending_requests_.insert(std::make_pair(request_id, job));
|
| @@ -280,14 +236,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,
|
| + &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 +258,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 +271,14 @@
|
| }
|
| }
|
|
|
| -ChromeURLDataManager::DataSource::DataSource(const std::string& source_name,
|
| - MessageLoop* message_loop)
|
| - : source_name_(source_name), message_loop_(message_loop) {
|
| -}
|
| -
|
| -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 +300,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 +316,7 @@
|
| }
|
|
|
| void URLRequestChromeJob::Kill() {
|
| - ChromeURLDataManager::GetInstance()->RemoveRequest(this);
|
| + backend_->RemoveRequest(this);
|
| }
|
|
|
| bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const {
|
| @@ -459,8 +377,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 +390,4 @@
|
| : net::URLRequestFileJob(request, path) {
|
| }
|
|
|
| -URLRequestChromeFileJob::~URLRequestChromeFileJob() { }
|
| +URLRequestChromeFileJob::~URLRequestChromeFileJob() {}
|
|
|