Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_backend.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/i18n/rtl.h" | |
| 9 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 10 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 11 #include "base/ref_counted_memory.h" | 10 #include "base/ref_counted_memory.h" |
| 12 #include "base/singleton.h" | |
| 13 #include "base/stl_util-inl.h" | |
| 14 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 15 #include "base/threading/thread.h" | |
| 16 #include "base/values.h" | |
| 17 #if defined(OS_WIN) | |
| 18 #include "base/win/windows_version.h" | |
| 19 #endif | |
| 20 #include "chrome/browser/appcache/view_appcache_internals_job_factory.h" | 12 #include "chrome/browser/appcache/view_appcache_internals_job_factory.h" |
| 21 #include "chrome/browser/browser_thread.h" | 13 #include "chrome/browser/browser_thread.h" |
| 22 #include "chrome/browser/dom_ui/shared_resources_data_source.h" | 14 #include "chrome/browser/dom_ui/shared_resources_data_source.h" |
| 23 #include "chrome/browser/net/chrome_url_request_context.h" | 15 #include "chrome/browser/net/chrome_url_request_context.h" |
| 24 #include "chrome/browser/net/view_blob_internals_job_factory.h" | 16 #include "chrome/browser/net/view_blob_internals_job_factory.h" |
| 25 #include "chrome/browser/net/view_http_cache_job_factory.h" | 17 #include "chrome/browser/net/view_http_cache_job_factory.h" |
| 26 #include "chrome/common/chrome_paths.h" | 18 #include "chrome/common/chrome_paths.h" |
| 27 #include "chrome/common/ref_counted_util.h" | |
| 28 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
| 29 #include "googleurl/src/url_util.h" | 20 #include "googleurl/src/url_util.h" |
| 30 #include "grit/platform_locale_settings.h" | 21 #include "grit/platform_locale_settings.h" |
| 31 #include "net/base/io_buffer.h" | 22 #include "net/base/io_buffer.h" |
| 32 #include "net/base/net_errors.h" | 23 #include "net/base/net_errors.h" |
| 33 #include "net/url_request/url_request.h" | 24 #include "net/url_request/url_request.h" |
| 34 #include "net/url_request/url_request_file_job.h" | 25 #include "net/url_request/url_request_file_job.h" |
| 35 #include "net/url_request/url_request_job.h" | 26 #include "net/url_request/url_request_job.h" |
| 36 #include "ui/base/l10n/l10n_util.h" | 27 |
| 28 static ChromeURLDataManagerBackend* GetBackend(net::URLRequest* request) { | |
| 29 return static_cast<ChromeURLRequestContext*>(request->context())-> | |
| 30 GetChromeURLDataManagerBackend(); | |
| 31 } | |
| 37 | 32 |
| 38 // URLRequestChromeJob is a net::URLRequestJob that manages running | 33 // URLRequestChromeJob is a net::URLRequestJob that manages running |
| 39 // chrome-internal resource requests asynchronously. | 34 // chrome-internal resource requests asynchronously. |
| 40 // It hands off URL requests to ChromeURLDataManager, which asynchronously | 35 // It hands off URL requests to ChromeURLDataManager, which asynchronously |
| 41 // calls back once the data is available. | 36 // calls back once the data is available. |
| 42 class URLRequestChromeJob : public net::URLRequestJob { | 37 class URLRequestChromeJob : public net::URLRequestJob { |
| 43 public: | 38 public: |
| 44 explicit URLRequestChromeJob(net::URLRequest* request); | 39 explicit URLRequestChromeJob(net::URLRequest* request); |
| 45 | 40 |
| 46 // net::URLRequestJob implementation. | 41 // net::URLRequestJob implementation. |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 73 // The current offset into the data that we're handing off to our | 68 // The current offset into the data that we're handing off to our |
| 74 // callers via the Read interfaces. | 69 // callers via the Read interfaces. |
| 75 int data_offset_; | 70 int data_offset_; |
| 76 | 71 |
| 77 // For async reads, we keep around a pointer to the buffer that | 72 // For async reads, we keep around a pointer to the buffer that |
| 78 // we're reading into. | 73 // we're reading into. |
| 79 scoped_refptr<net::IOBuffer> pending_buf_; | 74 scoped_refptr<net::IOBuffer> pending_buf_; |
| 80 int pending_buf_size_; | 75 int pending_buf_size_; |
| 81 std::string mime_type_; | 76 std::string mime_type_; |
| 82 | 77 |
| 78 scoped_refptr<ChromeURLDataManagerBackend> backend_; | |
| 79 | |
| 83 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeJob); | 80 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeJob); |
| 84 }; | 81 }; |
| 85 | 82 |
| 86 // URLRequestChromeFileJob is a net::URLRequestJob that acts like a file:// URL | 83 // URLRequestChromeFileJob is a net::URLRequestJob that acts like a file:// URL |
| 87 class URLRequestChromeFileJob : public net::URLRequestFileJob { | 84 class URLRequestChromeFileJob : public net::URLRequestFileJob { |
| 88 public: | 85 public: |
| 89 URLRequestChromeFileJob(net::URLRequest* request, const FilePath& path); | 86 URLRequestChromeFileJob(net::URLRequest* request, const FilePath& path); |
| 90 | 87 |
| 91 private: | 88 private: |
| 92 virtual ~URLRequestChromeFileJob(); | 89 virtual ~URLRequestChromeFileJob(); |
| 93 | 90 |
| 94 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeFileJob); | 91 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeFileJob); |
| 95 }; | 92 }; |
| 96 | 93 |
| 97 void RegisterURLRequestChromeJob() { | 94 void ChromeURLDataManagerBackend::URLToRequest(const GURL& url, |
| 98 FilePath inspector_dir; | 95 std::string* source_name, |
| 99 if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) { | 96 std::string* path) { |
| 100 ChromeURLDataManager::GetInstance()->AddFileSource( | |
| 101 chrome::kChromeUIDevToolsHost, inspector_dir); | |
| 102 } | |
| 103 | |
| 104 SharedResourcesDataSource::Register(); | |
| 105 net::URLRequest::RegisterProtocolFactory(chrome::kChromeDevToolsScheme, | |
| 106 &ChromeURLDataManager::Factory); | |
| 107 net::URLRequest::RegisterProtocolFactory(chrome::kChromeUIScheme, | |
| 108 &ChromeURLDataManager::Factory); | |
| 109 } | |
| 110 | |
| 111 void UnregisterURLRequestChromeJob() { | |
| 112 FilePath inspector_dir; | |
| 113 if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) { | |
| 114 ChromeURLDataManager::GetInstance()->RemoveFileSource( | |
| 115 chrome::kChromeUIDevToolsHost); | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 // static | |
| 120 void ChromeURLDataManager::URLToRequest(const GURL& url, | |
| 121 std::string* source_name, | |
| 122 std::string* path) { | |
| 123 DCHECK(url.SchemeIs(chrome::kChromeDevToolsScheme) || | 97 DCHECK(url.SchemeIs(chrome::kChromeDevToolsScheme) || |
| 124 url.SchemeIs(chrome::kChromeUIScheme)); | 98 url.SchemeIs(chrome::kChromeUIScheme)); |
| 125 | 99 |
| 126 if (!url.is_valid()) { | 100 if (!url.is_valid()) { |
| 127 NOTREACHED(); | 101 NOTREACHED(); |
| 128 return; | 102 return; |
| 129 } | 103 } |
| 130 | 104 |
| 131 // Our input looks like: chrome://source_name/extra_bits?foo . | 105 // Our input looks like: chrome://source_name/extra_bits?foo . |
| 132 // So the url's "host" is our source, and everything after the host is | 106 // So the url's "host" is our source, and everything after the host is |
| 133 // the path. | 107 // the path. |
| 134 source_name->assign(url.host()); | 108 source_name->assign(url.host()); |
| 135 | 109 |
| 136 const std::string& spec = url.possibly_invalid_spec(); | 110 const std::string& spec = url.possibly_invalid_spec(); |
| 137 const url_parse::Parsed& parsed = url.parsed_for_possibly_invalid_spec(); | 111 const url_parse::Parsed& parsed = url.parsed_for_possibly_invalid_spec(); |
| 138 // + 1 to skip the slash at the beginning of the path. | 112 // + 1 to skip the slash at the beginning of the path. |
| 139 int offset = parsed.CountCharactersBefore(url_parse::Parsed::PATH, false) + 1; | 113 int offset = parsed.CountCharactersBefore(url_parse::Parsed::PATH, false) + 1; |
| 140 | 114 |
| 141 if (offset < static_cast<int>(spec.size())) | 115 if (offset < static_cast<int>(spec.size())) |
| 142 path->assign(spec.substr(offset)); | 116 path->assign(spec.substr(offset)); |
| 143 } | 117 } |
| 144 | 118 |
| 145 // static | 119 bool ChromeURLDataManagerBackend::URLToFilePath(const GURL& url, |
| 146 bool ChromeURLDataManager::URLToFilePath(const GURL& url, | 120 FilePath* file_path) { |
| 147 FilePath* file_path) { | |
| 148 // Parse the URL into a request for a source and path. | 121 // Parse the URL into a request for a source and path. |
| 149 std::string source_name; | 122 std::string source_name; |
| 150 std::string relative_path; | 123 std::string relative_path; |
| 151 | 124 |
| 152 // Remove Query and Ref from URL. | 125 // Remove Query and Ref from URL. |
| 153 GURL stripped_url; | 126 GURL stripped_url; |
| 154 GURL::Replacements replacements; | 127 GURL::Replacements replacements; |
| 155 replacements.ClearQuery(); | 128 replacements.ClearQuery(); |
| 156 replacements.ClearRef(); | 129 replacements.ClearRef(); |
| 157 stripped_url = url.ReplaceComponents(replacements); | 130 stripped_url = url.ReplaceComponents(replacements); |
| 158 | 131 |
| 159 URLToRequest(stripped_url, &source_name, &relative_path); | 132 URLToRequest(stripped_url, &source_name, &relative_path); |
| 160 | 133 |
| 161 FileSourceMap::const_iterator i( | 134 FileSourceMap::const_iterator i(file_sources_.find(source_name)); |
| 162 ChromeURLDataManager::GetInstance()->file_sources_.find(source_name)); | 135 if (i == file_sources_.end()) |
| 163 if (i == ChromeURLDataManager::GetInstance()->file_sources_.end()) | |
| 164 return false; | 136 return false; |
| 165 | 137 |
| 166 // Check that |relative_path| is not an absolute path (otherwise AppendASCII() | 138 // Check that |relative_path| is not an absolute path (otherwise AppendASCII() |
| 167 // will DCHECK). The awkward use of StringType is because on some systems | 139 // will DCHECK). The awkward use of StringType is because on some systems |
| 168 // FilePath expects a std::string, but on others a std::wstring. | 140 // FilePath expects a std::string, but on others a std::wstring. |
| 169 FilePath p(FilePath::StringType(relative_path.begin(), relative_path.end())); | 141 FilePath p(FilePath::StringType(relative_path.begin(), relative_path.end())); |
| 170 if (p.IsAbsolute()) | 142 if (p.IsAbsolute()) |
| 171 return false; | 143 return false; |
| 172 | 144 |
| 173 *file_path = i->second.AppendASCII(relative_path); | 145 *file_path = i->second.AppendASCII(relative_path); |
| 174 | 146 |
| 175 return true; | 147 return true; |
| 176 } | 148 } |
| 177 | 149 |
| 178 ChromeURLDataManager::ChromeURLDataManager() : next_request_id_(0) { } | 150 // static |
| 151 base::Lock* ChromeURLDataManagerBackend::instances_lock_ = NULL; | |
| 179 | 152 |
| 180 ChromeURLDataManager::~ChromeURLDataManager() { | 153 // static |
| 181 // This is used as a Singleton, so it is only called at exit cleanup time. | 154 ChromeURLDataManagerBackend::Instances* |
| 182 // This means it is called on the main (UI) thread. | 155 ChromeURLDataManagerBackend::instances_ = NULL; |
| 183 // | 156 |
| 184 // It will break if it is called at shutdown time on a different thread, as | 157 // static |
| 185 // it will attempt to call the destructors for its |data_source_|s on the | 158 ChromeURLDataManagerBackend::LockedInstances* |
| 186 // UI thread, but the UI thread's message loop will be not be running | 159 ChromeURLDataManagerBackend::shutdown_instances_ = NULL; |
| 187 // -- so the destructor calls will be dropped and we will leak the objects. | 160 |
| 161 ChromeURLDataManagerBackend::ChromeURLDataManagerBackend() | |
| 162 : next_request_id_(0) { | |
| 163 FilePath inspector_dir; | |
| 164 if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) | |
| 165 AddFileSource(chrome::kChromeUIDevToolsHost, inspector_dir); | |
| 166 AddDataSource(new SharedResourcesDataSource()); | |
| 167 { | |
| 168 base::AutoLock lock(*instances_lock_); | |
|
Evan Martin
2011/02/07 19:06:21
It seems you have a precondition that ::Register()
| |
| 169 instances_->push_back(this); | |
| 170 if (shutdown_instances_) | |
| 171 shutdown_instances_->push_back(this); | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 void ChromeURLDataManagerBackend::Register() { | |
| 176 net::URLRequest::RegisterProtocolFactory( | |
| 177 chrome::kChromeDevToolsScheme, | |
| 178 &ChromeURLDataManagerBackend::Factory); | |
| 179 net::URLRequest::RegisterProtocolFactory( | |
| 180 chrome::kChromeUIScheme, | |
| 181 &ChromeURLDataManagerBackend::Factory); | |
| 182 DCHECK(!instances_lock_); | |
| 183 instances_lock_ = new base::Lock(); | |
| 184 DCHECK(!instances_); | |
| 185 instances_ = new Instances(); | |
| 188 } | 186 } |
| 189 | 187 |
| 190 // static | 188 // static |
| 191 ChromeURLDataManager* ChromeURLDataManager::GetInstance() { | 189 void ChromeURLDataManagerBackend::PrepareForShutdown() { |
| 192 return Singleton<ChromeURLDataManager>::get(); | 190 base::AutoLock lock(*instances_lock_); |
| 191 DCHECK(!shutdown_instances_); | |
| 192 // Create LockedInstances and all existing instances to it so that none of the | |
| 193 // existing instances get destroyed. We need to do this otherwise by the time | |
| 194 // CompleteShutdown is invoked all the ChromeURLDataManagerBackends will | |
| 195 // likely have been destroyed along with their DataSources. But because the UI | |
| 196 // thread is no longer running, all the DataSources end up leaking. By | |
| 197 // delaying the destruction until CompleteShutdown is invoked on the UI | |
| 198 // thread, we know everything gets destroyed on the UI thread. | |
| 199 shutdown_instances_ = new LockedInstances(); | |
| 200 for (Instances::iterator i = instances_->begin(); i != instances_->end(); ++i) | |
| 201 shutdown_instances_->push_back(make_scoped_refptr(*i)); | |
| 193 } | 202 } |
| 194 | 203 |
| 195 void ChromeURLDataManager::AddDataSource(scoped_refptr<DataSource> source) { | 204 // static |
| 196 // Some |DataSource|-derived classes, notably |FileIconSource| and | 205 void ChromeURLDataManagerBackend::CompleteShutdown() { |
| 197 // |WebUIFavIconSource|, have members that will DCHECK if they are not | 206 LockedInstances instances; |
| 198 // destructed in the same thread as they are constructed (the UI thread). | 207 { |
| 199 // | 208 base::AutoLock lock(*instances_lock_); |
| 200 // If |AddDataSource| is called more than once, it will destruct the object | 209 DCHECK(shutdown_instances_); // PrepareForShutdown must have been invoked. |
| 201 // that it had before, as it is the only thing still holding a reference to | 210 instances.swap(*shutdown_instances_); |
| 202 // that object. |DataSource| uses the |DeleteOnUIThread| trait to insure | 211 delete shutdown_instances_; |
| 203 // that the destructor is called on the UI thread. | 212 shutdown_instances_ = NULL; |
| 204 // | 213 } |
| 205 // TODO(jackson): A new data source with same name should not clobber the | 214 for (LockedInstances::iterator i = instances.begin(); i != instances.end(); |
| 206 // existing one. | 215 ++i) { |
| 207 data_sources_[source->source_name()] = source; | 216 (*i)->ReleaseDataSources(); |
| 208 } | |
| 209 | |
| 210 void ChromeURLDataManager::RemoveDataSourceForTest(const char* source_name) { | |
| 211 DataSourceMap::iterator i = data_sources_.find(source_name); | |
| 212 if (i == data_sources_.end()) | |
| 213 return; | |
| 214 (*i).second = NULL; // Calls Release(). | |
| 215 data_sources_.erase(i); | |
| 216 } | |
| 217 | |
| 218 void ChromeURLDataManager::RemoveAllDataSources() { | |
| 219 for (DataSourceMap::iterator i = data_sources_.begin(); | |
| 220 i != data_sources_.end(); | |
| 221 i = data_sources_.begin()) { | |
| 222 (*i).second = NULL; // Calls Release(). | |
| 223 data_sources_.erase(i); | |
| 224 } | 217 } |
| 225 } | 218 } |
| 226 | 219 |
| 227 void ChromeURLDataManager::AddFileSource(const std::string& source_name, | 220 ChromeURLDataManagerBackend::~ChromeURLDataManagerBackend() { |
| 228 const FilePath& file_path) { | 221 { |
| 222 base::AutoLock lock(*instances_lock_); | |
| 223 instances_->erase(std::find(instances_->begin(), instances_->end(), this)); | |
| 224 } | |
| 225 ReleaseDataSources(); | |
| 226 } | |
| 227 | |
| 228 void ChromeURLDataManagerBackend::AddDataSource( | |
| 229 ChromeURLDataManager::DataSource* source) { | |
| 230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 231 base::AutoLock lock(data_sources_lock_); | |
| 232 DataSourceMap::iterator i = data_sources_.find(source->source_name()); | |
| 233 if (i != data_sources_.end()) | |
| 234 i->second->backend_ = NULL; | |
| 235 data_sources_[source->source_name()] = source; | |
| 236 source->backend_ = this; | |
| 237 } | |
| 238 | |
| 239 void ChromeURLDataManagerBackend::AddFileSource(const std::string& source_name, | |
| 240 const FilePath& file_path) { | |
| 229 DCHECK(file_sources_.count(source_name) == 0); | 241 DCHECK(file_sources_.count(source_name) == 0); |
| 230 file_sources_[source_name] = file_path; | 242 file_sources_[source_name] = file_path; |
| 231 } | 243 } |
| 232 | 244 |
| 233 void ChromeURLDataManager::RemoveFileSource(const std::string& source_name) { | 245 bool ChromeURLDataManagerBackend::HasPendingJob( |
| 234 DCHECK(file_sources_.count(source_name) == 1); | 246 URLRequestChromeJob* job) const { |
| 235 file_sources_.erase(source_name); | |
| 236 } | |
| 237 | |
| 238 bool ChromeURLDataManager::HasPendingJob(URLRequestChromeJob* job) const { | |
| 239 for (PendingRequestMap::const_iterator i = pending_requests_.begin(); | 247 for (PendingRequestMap::const_iterator i = pending_requests_.begin(); |
| 240 i != pending_requests_.end(); ++i) { | 248 i != pending_requests_.end(); ++i) { |
| 241 if (i->second == job) | 249 if (i->second == job) |
| 242 return true; | 250 return true; |
| 243 } | 251 } |
| 244 | |
| 245 return false; | 252 return false; |
| 246 } | 253 } |
| 247 | 254 |
| 248 bool ChromeURLDataManager::StartRequest(const GURL& url, | 255 bool ChromeURLDataManagerBackend::StartRequest(const GURL& url, |
| 249 URLRequestChromeJob* job) { | 256 URLRequestChromeJob* job) { |
| 250 // Parse the URL into a request for a source and path. | 257 // Parse the URL into a request for a source and path. |
| 251 std::string source_name; | 258 std::string source_name; |
| 252 std::string path; | 259 std::string path; |
| 253 URLToRequest(url, &source_name, &path); | 260 URLToRequest(url, &source_name, &path); |
| 254 | 261 |
| 255 // Look up the data source for the request. | 262 // Look up the data source for the request. |
| 256 DataSourceMap::iterator i = data_sources_.find(source_name); | 263 scoped_refptr<ChromeURLDataManager::DataSource> source; |
| 257 if (i == data_sources_.end()) | 264 { |
| 258 return false; | 265 base::AutoLock lock(data_sources_lock_); |
| 259 DataSource* source = i->second; | 266 DataSourceMap::iterator i = data_sources_.find(source_name); |
| 267 if (i == data_sources_.end()) | |
| 268 return false; | |
| 269 source = i->second; | |
| 270 } | |
| 260 | 271 |
| 261 // Save this request so we know where to send the data. | 272 // Save this request so we know where to send the data. |
| 262 RequestID request_id = next_request_id_++; | 273 RequestID request_id = next_request_id_++; |
| 263 pending_requests_.insert(std::make_pair(request_id, job)); | 274 pending_requests_.insert(std::make_pair(request_id, job)); |
| 264 | 275 |
| 265 // TODO(eroman): would be nicer if the mimetype were set at the same time | 276 // TODO(eroman): would be nicer if the mimetype were set at the same time |
| 266 // as the data blob. For now do it here, since NotifyHeadersComplete() is | 277 // as the data blob. For now do it here, since NotifyHeadersComplete() is |
| 267 // going to get called once we return. | 278 // going to get called once we return. |
| 268 job->SetMimeType(source->GetMimeType(path)); | 279 job->SetMimeType(source->GetMimeType(path)); |
| 269 | 280 |
| 270 ChromeURLRequestContext* context = static_cast<ChromeURLRequestContext*>( | 281 ChromeURLRequestContext* context = static_cast<ChromeURLRequestContext*>( |
| 271 job->request()->context()); | 282 job->request()->context()); |
| 272 | 283 |
| 273 // Forward along the request to the data source. | 284 // Forward along the request to the data source. |
| 274 MessageLoop* target_message_loop = source->MessageLoopForRequestPath(path); | 285 MessageLoop* target_message_loop = source->MessageLoopForRequestPath(path); |
| 275 if (!target_message_loop) { | 286 if (!target_message_loop) { |
| 276 // The DataSource is agnostic to which thread StartDataRequest is called | 287 // The DataSource is agnostic to which thread StartDataRequest is called |
| 277 // on for this path. Call directly into it from this thread, the IO | 288 // on for this path. Call directly into it from this thread, the IO |
| 278 // thread. | 289 // thread. |
| 279 source->StartDataRequest(path, context->is_off_the_record(), request_id); | 290 source->StartDataRequest(path, context->is_off_the_record(), request_id); |
| 280 } else { | 291 } else { |
| 281 // The DataSource wants StartDataRequest to be called on a specific thread, | 292 // The DataSource wants StartDataRequest to be called on a specific thread, |
| 282 // usually the UI thread, for this path. | 293 // usually the UI thread, for this path. |
| 283 target_message_loop->PostTask(FROM_HERE, | 294 target_message_loop->PostTask( |
| 284 NewRunnableMethod(source, &DataSource::StartDataRequest, | 295 FROM_HERE, |
| 296 NewRunnableMethod(source.get(), | |
| 297 &ChromeURLDataManager::DataSource::StartDataRequest, | |
| 285 path, context->is_off_the_record(), request_id)); | 298 path, context->is_off_the_record(), request_id)); |
| 286 } | 299 } |
| 287 return true; | 300 return true; |
| 288 } | 301 } |
| 289 | 302 |
| 290 void ChromeURLDataManager::RemoveRequest(URLRequestChromeJob* job) { | 303 void ChromeURLDataManagerBackend::RemoveRequest(URLRequestChromeJob* job) { |
| 291 // Remove the request from our list of pending requests. | 304 // Remove the request from our list of pending requests. |
| 292 // If/when the source sends the data that was requested, the data will just | 305 // If/when the source sends the data that was requested, the data will just |
| 293 // be thrown away. | 306 // be thrown away. |
| 294 for (PendingRequestMap::iterator i = pending_requests_.begin(); | 307 for (PendingRequestMap::iterator i = pending_requests_.begin(); |
| 295 i != pending_requests_.end(); ++i) { | 308 i != pending_requests_.end(); ++i) { |
| 296 if (i->second == job) { | 309 if (i->second == job) { |
| 297 pending_requests_.erase(i); | 310 pending_requests_.erase(i); |
| 298 return; | 311 return; |
| 299 } | 312 } |
| 300 } | 313 } |
| 301 } | 314 } |
| 302 | 315 |
| 303 void ChromeURLDataManager::DataAvailable( | 316 void ChromeURLDataManagerBackend::DataAvailable(RequestID request_id, |
| 304 RequestID request_id, | 317 RefCountedMemory* bytes) { |
| 305 scoped_refptr<RefCountedMemory> bytes) { | |
| 306 // Forward this data on to the pending net::URLRequest, if it exists. | 318 // Forward this data on to the pending net::URLRequest, if it exists. |
| 307 PendingRequestMap::iterator i = pending_requests_.find(request_id); | 319 PendingRequestMap::iterator i = pending_requests_.find(request_id); |
| 308 if (i != pending_requests_.end()) { | 320 if (i != pending_requests_.end()) { |
| 309 // We acquire a reference to the job so that it doesn't disappear under the | 321 // We acquire a reference to the job so that it doesn't disappear under the |
| 310 // feet of any method invoked here (we could trigger a callback). | 322 // feet of any method invoked here (we could trigger a callback). |
| 311 scoped_refptr<URLRequestChromeJob> job(i->second); | 323 scoped_refptr<URLRequestChromeJob> job(i->second); |
| 312 pending_requests_.erase(i); | 324 pending_requests_.erase(i); |
| 313 job->DataAvailable(bytes); | 325 job->DataAvailable(bytes); |
| 314 } | 326 } |
| 315 } | 327 } |
| 316 | 328 |
| 317 ChromeURLDataManager::DataSource::DataSource(const std::string& source_name, | 329 void ChromeURLDataManagerBackend::ReleaseDataSources() { |
| 318 MessageLoop* message_loop) | 330 DataSourceMap data_sources; |
| 319 : source_name_(source_name), message_loop_(message_loop) { | 331 { |
| 320 } | 332 base::AutoLock lock(data_sources_lock_); |
| 321 | 333 data_sources.swap(data_sources_); |
| 322 ChromeURLDataManager::DataSource::~DataSource() { | 334 } |
| 323 } | 335 for (DataSourceMap::iterator i = data_sources.begin(); |
| 324 | 336 i != data_sources.end(); ++i) { |
| 325 void ChromeURLDataManager::DataSource::SendResponse( | 337 i->second->backend_ = NULL; |
| 326 RequestID request_id, | 338 } |
| 327 RefCountedMemory* bytes) { | |
| 328 BrowserThread::PostTask( | |
| 329 BrowserThread::IO, FROM_HERE, | |
| 330 NewRunnableMethod(ChromeURLDataManager::GetInstance(), | |
| 331 &ChromeURLDataManager::DataAvailable, | |
| 332 request_id, scoped_refptr<RefCountedMemory>(bytes))); | |
| 333 } | |
| 334 | |
| 335 MessageLoop* ChromeURLDataManager::DataSource::MessageLoopForRequestPath( | |
| 336 const std::string& path) const { | |
| 337 return message_loop_; | |
| 338 } | 339 } |
| 339 | 340 |
| 340 // static | 341 // static |
| 341 void ChromeURLDataManager::DataSource::SetFontAndTextDirection( | 342 net::URLRequestJob* ChromeURLDataManagerBackend::Factory( |
| 342 DictionaryValue* localized_strings) { | 343 net::URLRequest* request, |
| 343 localized_strings->SetString("fontfamily", | 344 const std::string& scheme) { |
| 344 l10n_util::GetStringUTF16(IDS_WEB_FONT_FAMILY)); | |
| 345 | |
| 346 int web_font_size_id = IDS_WEB_FONT_SIZE; | |
| 347 #if defined(OS_WIN) | |
| 348 // Some fonts used for some languages changed a lot in terms of the font | |
| 349 // metric in Vista. So, we need to use different size before Vista. | |
| 350 if (base::win::GetVersion() < base::win::VERSION_VISTA) | |
| 351 web_font_size_id = IDS_WEB_FONT_SIZE_XP; | |
| 352 #endif | |
| 353 localized_strings->SetString("fontsize", | |
| 354 l10n_util::GetStringUTF16(web_font_size_id)); | |
| 355 | |
| 356 localized_strings->SetString("textdirection", | |
| 357 base::i18n::IsRTL() ? "rtl" : "ltr"); | |
| 358 } | |
| 359 | |
| 360 net::URLRequestJob* ChromeURLDataManager::Factory(net::URLRequest* request, | |
| 361 const std::string& scheme) { | |
| 362 // Try first with a file handler | 345 // Try first with a file handler |
| 363 FilePath path; | 346 FilePath path; |
| 364 if (ChromeURLDataManager::URLToFilePath(request->url(), &path)) | 347 ChromeURLDataManagerBackend* backend = GetBackend(request); |
| 348 if (backend->URLToFilePath(request->url(), &path)) | |
| 365 return new URLRequestChromeFileJob(request, path); | 349 return new URLRequestChromeFileJob(request, path); |
| 366 | 350 |
| 367 // Next check for chrome://view-http-cache/*, which uses its own job type. | 351 // Next check for chrome://view-http-cache/*, which uses its own job type. |
| 368 if (ViewHttpCacheJobFactory::IsSupportedURL(request->url())) | 352 if (ViewHttpCacheJobFactory::IsSupportedURL(request->url())) |
| 369 return ViewHttpCacheJobFactory::CreateJobForRequest(request); | 353 return ViewHttpCacheJobFactory::CreateJobForRequest(request); |
| 370 | 354 |
| 371 // Next check for chrome://appcache-internals/, which uses its own job type. | 355 // Next check for chrome://appcache-internals/, which uses its own job type. |
| 372 if (ViewAppCacheInternalsJobFactory::IsSupportedURL(request->url())) | 356 if (ViewAppCacheInternalsJobFactory::IsSupportedURL(request->url())) |
| 373 return ViewAppCacheInternalsJobFactory::CreateJobForRequest(request); | 357 return ViewAppCacheInternalsJobFactory::CreateJobForRequest(request); |
| 374 | 358 |
| 375 // Next check for chrome://blob-internals/, which uses its own job type. | 359 // Next check for chrome://blob-internals/, which uses its own job type. |
| 376 if (ViewBlobInternalsJobFactory::IsSupportedURL(request->url())) | 360 if (ViewBlobInternalsJobFactory::IsSupportedURL(request->url())) |
| 377 return ViewBlobInternalsJobFactory::CreateJobForRequest(request); | 361 return ViewBlobInternalsJobFactory::CreateJobForRequest(request); |
| 378 | 362 |
| 379 // Fall back to using a custom handler | 363 // Fall back to using a custom handler |
| 380 return new URLRequestChromeJob(request); | 364 return new URLRequestChromeJob(request); |
| 381 } | 365 } |
| 382 | 366 |
| 383 URLRequestChromeJob::URLRequestChromeJob(net::URLRequest* request) | 367 URLRequestChromeJob::URLRequestChromeJob(net::URLRequest* request) |
| 384 : net::URLRequestJob(request), | 368 : net::URLRequestJob(request), |
| 385 data_offset_(0), | 369 data_offset_(0), |
| 386 pending_buf_size_(0) { | 370 pending_buf_size_(0), |
| 371 backend_(GetBackend(request)) { | |
| 387 } | 372 } |
| 388 | 373 |
| 389 URLRequestChromeJob::~URLRequestChromeJob() { | 374 URLRequestChromeJob::~URLRequestChromeJob() { |
| 390 CHECK(!ChromeURLDataManager::GetInstance()->HasPendingJob(this)); | 375 CHECK(!backend_->HasPendingJob(this)); |
| 391 } | 376 } |
| 392 | 377 |
| 393 void URLRequestChromeJob::Start() { | 378 void URLRequestChromeJob::Start() { |
| 394 // Start reading asynchronously so that all error reporting and data | 379 // Start reading asynchronously so that all error reporting and data |
| 395 // callbacks happen as they would for network requests. | 380 // callbacks happen as they would for network requests. |
| 396 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 381 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 397 this, &URLRequestChromeJob::StartAsync)); | 382 this, &URLRequestChromeJob::StartAsync)); |
| 398 } | 383 } |
| 399 | 384 |
| 400 void URLRequestChromeJob::Kill() { | 385 void URLRequestChromeJob::Kill() { |
| 401 ChromeURLDataManager::GetInstance()->RemoveRequest(this); | 386 backend_->RemoveRequest(this); |
| 402 } | 387 } |
| 403 | 388 |
| 404 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const { | 389 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const { |
| 405 *mime_type = mime_type_; | 390 *mime_type = mime_type_; |
| 406 return !mime_type_.empty(); | 391 return !mime_type_.empty(); |
| 407 } | 392 } |
| 408 | 393 |
| 409 void URLRequestChromeJob::DataAvailable(RefCountedMemory* bytes) { | 394 void URLRequestChromeJob::DataAvailable(RefCountedMemory* bytes) { |
| 410 if (bytes) { | 395 if (bytes) { |
| 411 // The request completed, and we have all the data. | 396 // The request completed, and we have all the data. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 452 memcpy(buf->data(), data_->front() + data_offset_, buf_size); | 437 memcpy(buf->data(), data_->front() + data_offset_, buf_size); |
| 453 data_offset_ += buf_size; | 438 data_offset_ += buf_size; |
| 454 } | 439 } |
| 455 *bytes_read = buf_size; | 440 *bytes_read = buf_size; |
| 456 } | 441 } |
| 457 | 442 |
| 458 void URLRequestChromeJob::StartAsync() { | 443 void URLRequestChromeJob::StartAsync() { |
| 459 if (!request_) | 444 if (!request_) |
| 460 return; | 445 return; |
| 461 | 446 |
| 462 if (ChromeURLDataManager::GetInstance()->StartRequest(request_->url(), | 447 if (backend_->StartRequest(request_->url(), this)) { |
| 463 this)) { | |
| 464 NotifyHeadersComplete(); | 448 NotifyHeadersComplete(); |
| 465 } else { | 449 } else { |
| 466 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 450 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 467 net::ERR_INVALID_URL)); | 451 net::ERR_INVALID_URL)); |
| 468 } | 452 } |
| 469 } | 453 } |
| 470 | 454 |
| 471 URLRequestChromeFileJob::URLRequestChromeFileJob(net::URLRequest* request, | 455 URLRequestChromeFileJob::URLRequestChromeFileJob(net::URLRequest* request, |
| 472 const FilePath& path) | 456 const FilePath& path) |
| 473 : net::URLRequestFileJob(request, path) { | 457 : net::URLRequestFileJob(request, path) { |
| 474 } | 458 } |
| 475 | 459 |
| 476 URLRequestChromeFileJob::~URLRequestChromeFileJob() { } | 460 URLRequestChromeFileJob::~URLRequestChromeFileJob() {} |
| OLD | NEW |