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; | |
|
willchan no longer on Chromium
2011/02/08 18:24:45
I think all this complexity with the instances_loc
| |
| 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_); | |
| 169 instances_->push_back(this); | |
| 170 if (shutdown_instances_) | |
| 171 shutdown_instances_->push_back(this); | |
| 172 } | |
| 188 } | 173 } |
| 189 | 174 |
| 190 // static | 175 // static |
| 191 ChromeURLDataManager* ChromeURLDataManager::GetInstance() { | 176 void ChromeURLDataManagerBackend::Register() { |
| 192 return Singleton<ChromeURLDataManager>::get(); | 177 net::URLRequest::RegisterProtocolFactory( |
| 178 chrome::kChromeDevToolsScheme, | |
| 179 &ChromeURLDataManagerBackend::Factory); | |
| 180 net::URLRequest::RegisterProtocolFactory( | |
| 181 chrome::kChromeUIScheme, | |
| 182 &ChromeURLDataManagerBackend::Factory); | |
| 183 DCHECK(!instances_lock_); | |
| 184 instances_lock_ = new base::Lock(); | |
| 185 DCHECK(!instances_); | |
| 186 instances_ = new Instances(); | |
| 193 } | 187 } |
| 194 | 188 |
| 195 void ChromeURLDataManager::AddDataSource(scoped_refptr<DataSource> source) { | 189 // static |
| 196 // Some |DataSource|-derived classes, notably |FileIconSource| and | 190 void ChromeURLDataManagerBackend::PrepareForShutdown() { |
| 197 // |WebUIFavIconSource|, have members that will DCHECK if they are not | 191 base::AutoLock lock(*instances_lock_); |
| 198 // destructed in the same thread as they are constructed (the UI thread). | 192 DCHECK(!shutdown_instances_); |
| 199 // | 193 // Create LockedInstances and all existing instances to it so that none of the |
| 200 // If |AddDataSource| is called more than once, it will destruct the object | 194 // existing instances get destroyed. We need to do this otherwise by the time |
| 201 // that it had before, as it is the only thing still holding a reference to | 195 // CompleteShutdown is invoked all the ChromeURLDataManagerBackends will |
| 202 // that object. |DataSource| uses the |DeleteOnUIThread| trait to insure | 196 // likely have been destroyed along with their DataSources. But because the UI |
| 203 // that the destructor is called on the UI thread. | 197 // thread is no longer running, all the DataSources end up leaking. By |
| 204 // | 198 // delaying the destruction until CompleteShutdown is invoked on the UI |
| 205 // TODO(jackson): A new data source with same name should not clobber the | 199 // thread, we know everything gets destroyed on the UI thread. |
| 206 // existing one. | 200 shutdown_instances_ = new LockedInstances(); |
| 207 data_sources_[source->source_name()] = source; | 201 for (Instances::iterator i = instances_->begin(); i != instances_->end(); ++i) |
| 202 shutdown_instances_->push_back(make_scoped_refptr(*i)); | |
| 208 } | 203 } |
| 209 | 204 |
| 210 void ChromeURLDataManager::RemoveDataSourceForTest(const char* source_name) { | 205 // static |
| 211 DataSourceMap::iterator i = data_sources_.find(source_name); | 206 void ChromeURLDataManagerBackend::CompleteShutdown() { |
| 212 if (i == data_sources_.end()) | 207 LockedInstances instances; |
| 213 return; | 208 { |
| 214 (*i).second = NULL; // Calls Release(). | 209 base::AutoLock lock(*instances_lock_); |
| 215 data_sources_.erase(i); | 210 DCHECK(shutdown_instances_); // PrepareForShutdown must have been invoked. |
| 216 } | 211 instances.swap(*shutdown_instances_); |
| 217 | 212 delete shutdown_instances_; |
| 218 void ChromeURLDataManager::RemoveAllDataSources() { | 213 shutdown_instances_ = NULL; |
| 219 for (DataSourceMap::iterator i = data_sources_.begin(); | 214 } |
| 220 i != data_sources_.end(); | 215 for (LockedInstances::iterator i = instances.begin(); i != instances.end(); |
| 221 i = data_sources_.begin()) { | 216 ++i) { |
| 222 (*i).second = NULL; // Calls Release(). | 217 (*i)->ReleaseDataSources(); |
| 223 data_sources_.erase(i); | |
| 224 } | 218 } |
| 225 } | 219 } |
| 226 | 220 |
| 227 void ChromeURLDataManager::AddFileSource(const std::string& source_name, | 221 ChromeURLDataManagerBackend::~ChromeURLDataManagerBackend() { |
| 228 const FilePath& file_path) { | 222 { |
| 223 base::AutoLock lock(*instances_lock_); | |
| 224 instances_->erase(std::find(instances_->begin(), instances_->end(), this)); | |
| 225 } | |
| 226 ReleaseDataSources(); | |
| 227 } | |
| 228 | |
| 229 void ChromeURLDataManagerBackend::AddDataSource( | |
| 230 ChromeURLDataManager::DataSource* source) { | |
| 231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 232 base::AutoLock lock(data_sources_lock_); | |
| 233 DataSourceMap::iterator i = data_sources_.find(source->source_name()); | |
| 234 if (i != data_sources_.end()) | |
| 235 i->second->backend_ = NULL; | |
| 236 data_sources_[source->source_name()] = source; | |
| 237 source->backend_ = this; | |
| 238 } | |
| 239 | |
| 240 void ChromeURLDataManagerBackend::AddFileSource(const std::string& source_name, | |
| 241 const FilePath& file_path) { | |
| 229 DCHECK(file_sources_.count(source_name) == 0); | 242 DCHECK(file_sources_.count(source_name) == 0); |
| 230 file_sources_[source_name] = file_path; | 243 file_sources_[source_name] = file_path; |
| 231 } | 244 } |
| 232 | 245 |
| 233 void ChromeURLDataManager::RemoveFileSource(const std::string& source_name) { | 246 bool ChromeURLDataManagerBackend::HasPendingJob( |
| 234 DCHECK(file_sources_.count(source_name) == 1); | 247 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(); | 248 for (PendingRequestMap::const_iterator i = pending_requests_.begin(); |
| 240 i != pending_requests_.end(); ++i) { | 249 i != pending_requests_.end(); ++i) { |
| 241 if (i->second == job) | 250 if (i->second == job) |
| 242 return true; | 251 return true; |
| 243 } | 252 } |
| 244 | |
| 245 return false; | 253 return false; |
| 246 } | 254 } |
| 247 | 255 |
| 248 bool ChromeURLDataManager::StartRequest(const GURL& url, | 256 bool ChromeURLDataManagerBackend::StartRequest(const GURL& url, |
| 249 URLRequestChromeJob* job) { | 257 URLRequestChromeJob* job) { |
| 250 // Parse the URL into a request for a source and path. | 258 // Parse the URL into a request for a source and path. |
| 251 std::string source_name; | 259 std::string source_name; |
| 252 std::string path; | 260 std::string path; |
| 253 URLToRequest(url, &source_name, &path); | 261 URLToRequest(url, &source_name, &path); |
| 254 | 262 |
| 255 // Look up the data source for the request. | 263 // Look up the data source for the request. |
| 256 DataSourceMap::iterator i = data_sources_.find(source_name); | 264 scoped_refptr<ChromeURLDataManager::DataSource> source; |
| 257 if (i == data_sources_.end()) | 265 { |
| 258 return false; | 266 base::AutoLock lock(data_sources_lock_); |
| 259 DataSource* source = i->second; | 267 DataSourceMap::iterator i = data_sources_.find(source_name); |
| 268 if (i == data_sources_.end()) | |
| 269 return false; | |
| 270 source = i->second; | |
| 271 } | |
| 260 | 272 |
| 261 // Save this request so we know where to send the data. | 273 // Save this request so we know where to send the data. |
| 262 RequestID request_id = next_request_id_++; | 274 RequestID request_id = next_request_id_++; |
| 263 pending_requests_.insert(std::make_pair(request_id, job)); | 275 pending_requests_.insert(std::make_pair(request_id, job)); |
| 264 | 276 |
| 265 // TODO(eroman): would be nicer if the mimetype were set at the same time | 277 // 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 | 278 // as the data blob. For now do it here, since NotifyHeadersComplete() is |
| 267 // going to get called once we return. | 279 // going to get called once we return. |
| 268 job->SetMimeType(source->GetMimeType(path)); | 280 job->SetMimeType(source->GetMimeType(path)); |
| 269 | 281 |
| 270 ChromeURLRequestContext* context = static_cast<ChromeURLRequestContext*>( | 282 ChromeURLRequestContext* context = static_cast<ChromeURLRequestContext*>( |
| 271 job->request()->context()); | 283 job->request()->context()); |
| 272 | 284 |
| 273 // Forward along the request to the data source. | 285 // Forward along the request to the data source. |
| 274 MessageLoop* target_message_loop = source->MessageLoopForRequestPath(path); | 286 MessageLoop* target_message_loop = source->MessageLoopForRequestPath(path); |
| 275 if (!target_message_loop) { | 287 if (!target_message_loop) { |
| 276 // The DataSource is agnostic to which thread StartDataRequest is called | 288 // The DataSource is agnostic to which thread StartDataRequest is called |
| 277 // on for this path. Call directly into it from this thread, the IO | 289 // on for this path. Call directly into it from this thread, the IO |
| 278 // thread. | 290 // thread. |
| 279 source->StartDataRequest(path, context->is_off_the_record(), request_id); | 291 source->StartDataRequest(path, context->is_off_the_record(), request_id); |
| 280 } else { | 292 } else { |
| 281 // The DataSource wants StartDataRequest to be called on a specific thread, | 293 // The DataSource wants StartDataRequest to be called on a specific thread, |
| 282 // usually the UI thread, for this path. | 294 // usually the UI thread, for this path. |
| 283 target_message_loop->PostTask(FROM_HERE, | 295 target_message_loop->PostTask( |
| 284 NewRunnableMethod(source, &DataSource::StartDataRequest, | 296 FROM_HERE, |
| 297 NewRunnableMethod(source.get(), | |
| 298 &ChromeURLDataManager::DataSource::StartDataRequest, | |
| 285 path, context->is_off_the_record(), request_id)); | 299 path, context->is_off_the_record(), request_id)); |
| 286 } | 300 } |
| 287 return true; | 301 return true; |
| 288 } | 302 } |
| 289 | 303 |
| 290 void ChromeURLDataManager::RemoveRequest(URLRequestChromeJob* job) { | 304 void ChromeURLDataManagerBackend::RemoveRequest(URLRequestChromeJob* job) { |
| 291 // Remove the request from our list of pending requests. | 305 // Remove the request from our list of pending requests. |
| 292 // If/when the source sends the data that was requested, the data will just | 306 // If/when the source sends the data that was requested, the data will just |
| 293 // be thrown away. | 307 // be thrown away. |
| 294 for (PendingRequestMap::iterator i = pending_requests_.begin(); | 308 for (PendingRequestMap::iterator i = pending_requests_.begin(); |
| 295 i != pending_requests_.end(); ++i) { | 309 i != pending_requests_.end(); ++i) { |
| 296 if (i->second == job) { | 310 if (i->second == job) { |
| 297 pending_requests_.erase(i); | 311 pending_requests_.erase(i); |
| 298 return; | 312 return; |
| 299 } | 313 } |
| 300 } | 314 } |
| 301 } | 315 } |
| 302 | 316 |
| 303 void ChromeURLDataManager::DataAvailable( | 317 void ChromeURLDataManagerBackend::DataAvailable(RequestID request_id, |
| 304 RequestID request_id, | 318 RefCountedMemory* bytes) { |
| 305 scoped_refptr<RefCountedMemory> bytes) { | |
| 306 // Forward this data on to the pending net::URLRequest, if it exists. | 319 // Forward this data on to the pending net::URLRequest, if it exists. |
| 307 PendingRequestMap::iterator i = pending_requests_.find(request_id); | 320 PendingRequestMap::iterator i = pending_requests_.find(request_id); |
| 308 if (i != pending_requests_.end()) { | 321 if (i != pending_requests_.end()) { |
| 309 // We acquire a reference to the job so that it doesn't disappear under the | 322 // 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). | 323 // feet of any method invoked here (we could trigger a callback). |
| 311 scoped_refptr<URLRequestChromeJob> job(i->second); | 324 scoped_refptr<URLRequestChromeJob> job(i->second); |
| 312 pending_requests_.erase(i); | 325 pending_requests_.erase(i); |
| 313 job->DataAvailable(bytes); | 326 job->DataAvailable(bytes); |
| 314 } | 327 } |
| 315 } | 328 } |
| 316 | 329 |
| 317 ChromeURLDataManager::DataSource::DataSource(const std::string& source_name, | 330 void ChromeURLDataManagerBackend::ReleaseDataSources() { |
| 318 MessageLoop* message_loop) | 331 DataSourceMap data_sources; |
| 319 : source_name_(source_name), message_loop_(message_loop) { | 332 { |
| 320 } | 333 base::AutoLock lock(data_sources_lock_); |
| 321 | 334 data_sources.swap(data_sources_); |
| 322 ChromeURLDataManager::DataSource::~DataSource() { | 335 } |
| 323 } | 336 for (DataSourceMap::iterator i = data_sources.begin(); |
| 324 | 337 i != data_sources.end(); ++i) { |
| 325 void ChromeURLDataManager::DataSource::SendResponse( | 338 i->second->backend_ = NULL; |
| 326 RequestID request_id, | 339 } |
| 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 } | 340 } |
| 339 | 341 |
| 340 // static | 342 // static |
| 341 void ChromeURLDataManager::DataSource::SetFontAndTextDirection( | 343 net::URLRequestJob* ChromeURLDataManagerBackend::Factory( |
| 342 DictionaryValue* localized_strings) { | 344 net::URLRequest* request, |
| 343 localized_strings->SetString("fontfamily", | 345 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 | 346 // Try first with a file handler |
| 363 FilePath path; | 347 FilePath path; |
| 364 if (ChromeURLDataManager::URLToFilePath(request->url(), &path)) | 348 ChromeURLDataManagerBackend* backend = GetBackend(request); |
| 349 if (backend->URLToFilePath(request->url(), &path)) | |
| 365 return new URLRequestChromeFileJob(request, path); | 350 return new URLRequestChromeFileJob(request, path); |
| 366 | 351 |
| 367 // Next check for chrome://view-http-cache/*, which uses its own job type. | 352 // Next check for chrome://view-http-cache/*, which uses its own job type. |
| 368 if (ViewHttpCacheJobFactory::IsSupportedURL(request->url())) | 353 if (ViewHttpCacheJobFactory::IsSupportedURL(request->url())) |
| 369 return ViewHttpCacheJobFactory::CreateJobForRequest(request); | 354 return ViewHttpCacheJobFactory::CreateJobForRequest(request); |
| 370 | 355 |
| 371 // Next check for chrome://appcache-internals/, which uses its own job type. | 356 // Next check for chrome://appcache-internals/, which uses its own job type. |
| 372 if (ViewAppCacheInternalsJobFactory::IsSupportedURL(request->url())) | 357 if (ViewAppCacheInternalsJobFactory::IsSupportedURL(request->url())) |
| 373 return ViewAppCacheInternalsJobFactory::CreateJobForRequest(request); | 358 return ViewAppCacheInternalsJobFactory::CreateJobForRequest(request); |
| 374 | 359 |
| 375 // Next check for chrome://blob-internals/, which uses its own job type. | 360 // Next check for chrome://blob-internals/, which uses its own job type. |
| 376 if (ViewBlobInternalsJobFactory::IsSupportedURL(request->url())) | 361 if (ViewBlobInternalsJobFactory::IsSupportedURL(request->url())) |
| 377 return ViewBlobInternalsJobFactory::CreateJobForRequest(request); | 362 return ViewBlobInternalsJobFactory::CreateJobForRequest(request); |
| 378 | 363 |
| 379 // Fall back to using a custom handler | 364 // Fall back to using a custom handler |
| 380 return new URLRequestChromeJob(request); | 365 return new URLRequestChromeJob(request); |
| 381 } | 366 } |
| 382 | 367 |
| 383 URLRequestChromeJob::URLRequestChromeJob(net::URLRequest* request) | 368 URLRequestChromeJob::URLRequestChromeJob(net::URLRequest* request) |
| 384 : net::URLRequestJob(request), | 369 : net::URLRequestJob(request), |
| 385 data_offset_(0), | 370 data_offset_(0), |
| 386 pending_buf_size_(0) { | 371 pending_buf_size_(0), |
| 372 backend_(GetBackend(request)) { | |
| 387 } | 373 } |
| 388 | 374 |
| 389 URLRequestChromeJob::~URLRequestChromeJob() { | 375 URLRequestChromeJob::~URLRequestChromeJob() { |
| 390 CHECK(!ChromeURLDataManager::GetInstance()->HasPendingJob(this)); | 376 CHECK(!backend_->HasPendingJob(this)); |
| 391 } | 377 } |
| 392 | 378 |
| 393 void URLRequestChromeJob::Start() { | 379 void URLRequestChromeJob::Start() { |
| 394 // Start reading asynchronously so that all error reporting and data | 380 // Start reading asynchronously so that all error reporting and data |
| 395 // callbacks happen as they would for network requests. | 381 // callbacks happen as they would for network requests. |
| 396 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 382 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 397 this, &URLRequestChromeJob::StartAsync)); | 383 this, &URLRequestChromeJob::StartAsync)); |
| 398 } | 384 } |
| 399 | 385 |
| 400 void URLRequestChromeJob::Kill() { | 386 void URLRequestChromeJob::Kill() { |
| 401 ChromeURLDataManager::GetInstance()->RemoveRequest(this); | 387 backend_->RemoveRequest(this); |
| 402 } | 388 } |
| 403 | 389 |
| 404 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const { | 390 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const { |
| 405 *mime_type = mime_type_; | 391 *mime_type = mime_type_; |
| 406 return !mime_type_.empty(); | 392 return !mime_type_.empty(); |
| 407 } | 393 } |
| 408 | 394 |
| 409 void URLRequestChromeJob::DataAvailable(RefCountedMemory* bytes) { | 395 void URLRequestChromeJob::DataAvailable(RefCountedMemory* bytes) { |
| 410 if (bytes) { | 396 if (bytes) { |
| 411 // The request completed, and we have all the data. | 397 // 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); | 438 memcpy(buf->data(), data_->front() + data_offset_, buf_size); |
| 453 data_offset_ += buf_size; | 439 data_offset_ += buf_size; |
| 454 } | 440 } |
| 455 *bytes_read = buf_size; | 441 *bytes_read = buf_size; |
| 456 } | 442 } |
| 457 | 443 |
| 458 void URLRequestChromeJob::StartAsync() { | 444 void URLRequestChromeJob::StartAsync() { |
| 459 if (!request_) | 445 if (!request_) |
| 460 return; | 446 return; |
| 461 | 447 |
| 462 if (ChromeURLDataManager::GetInstance()->StartRequest(request_->url(), | 448 if (backend_->StartRequest(request_->url(), this)) { |
| 463 this)) { | |
| 464 NotifyHeadersComplete(); | 449 NotifyHeadersComplete(); |
| 465 } else { | 450 } else { |
| 466 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 451 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 467 net::ERR_INVALID_URL)); | 452 net::ERR_INVALID_URL)); |
| 468 } | 453 } |
| 469 } | 454 } |
| 470 | 455 |
| 471 URLRequestChromeFileJob::URLRequestChromeFileJob(net::URLRequest* request, | 456 URLRequestChromeFileJob::URLRequestChromeFileJob(net::URLRequest* request, |
| 472 const FilePath& path) | 457 const FilePath& path) |
| 473 : net::URLRequestFileJob(request, path) { | 458 : net::URLRequestFileJob(request, path) { |
| 474 } | 459 } |
| 475 | 460 |
| 476 URLRequestChromeFileJob::~URLRequestChromeFileJob() { } | 461 URLRequestChromeFileJob::~URLRequestChromeFileJob() {} |
| OLD | NEW |