| 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 // The backend is owned by ChromeURLRequestContext and always outlives us. |
| 79 ChromeURLDataManagerBackend* backend_; |
| 80 |
| 83 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeJob); | 81 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeJob); |
| 84 }; | 82 }; |
| 85 | 83 |
| 86 // URLRequestChromeFileJob is a net::URLRequestJob that acts like a file:// URL | 84 // URLRequestChromeFileJob is a net::URLRequestJob that acts like a file:// URL |
| 87 class URLRequestChromeFileJob : public net::URLRequestFileJob { | 85 class URLRequestChromeFileJob : public net::URLRequestFileJob { |
| 88 public: | 86 public: |
| 89 URLRequestChromeFileJob(net::URLRequest* request, const FilePath& path); | 87 URLRequestChromeFileJob(net::URLRequest* request, const FilePath& path); |
| 90 | 88 |
| 91 private: | 89 private: |
| 92 virtual ~URLRequestChromeFileJob(); | 90 virtual ~URLRequestChromeFileJob(); |
| 93 | 91 |
| 94 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeFileJob); | 92 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeFileJob); |
| 95 }; | 93 }; |
| 96 | 94 |
| 97 void RegisterURLRequestChromeJob() { | 95 void ChromeURLDataManagerBackend::URLToRequest(const GURL& url, |
| 98 FilePath inspector_dir; | 96 std::string* source_name, |
| 99 if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) { | 97 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) || | 98 DCHECK(url.SchemeIs(chrome::kChromeDevToolsScheme) || |
| 124 url.SchemeIs(chrome::kChromeUIScheme)); | 99 url.SchemeIs(chrome::kChromeUIScheme)); |
| 125 | 100 |
| 126 if (!url.is_valid()) { | 101 if (!url.is_valid()) { |
| 127 NOTREACHED(); | 102 NOTREACHED(); |
| 128 return; | 103 return; |
| 129 } | 104 } |
| 130 | 105 |
| 131 // Our input looks like: chrome://source_name/extra_bits?foo . | 106 // 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 | 107 // So the url's "host" is our source, and everything after the host is |
| 133 // the path. | 108 // the path. |
| 134 source_name->assign(url.host()); | 109 source_name->assign(url.host()); |
| 135 | 110 |
| 136 const std::string& spec = url.possibly_invalid_spec(); | 111 const std::string& spec = url.possibly_invalid_spec(); |
| 137 const url_parse::Parsed& parsed = url.parsed_for_possibly_invalid_spec(); | 112 const url_parse::Parsed& parsed = url.parsed_for_possibly_invalid_spec(); |
| 138 // + 1 to skip the slash at the beginning of the path. | 113 // + 1 to skip the slash at the beginning of the path. |
| 139 int offset = parsed.CountCharactersBefore(url_parse::Parsed::PATH, false) + 1; | 114 int offset = parsed.CountCharactersBefore(url_parse::Parsed::PATH, false) + 1; |
| 140 | 115 |
| 141 if (offset < static_cast<int>(spec.size())) | 116 if (offset < static_cast<int>(spec.size())) |
| 142 path->assign(spec.substr(offset)); | 117 path->assign(spec.substr(offset)); |
| 143 } | 118 } |
| 144 | 119 |
| 145 // static | 120 bool ChromeURLDataManagerBackend::URLToFilePath(const GURL& url, |
| 146 bool ChromeURLDataManager::URLToFilePath(const GURL& url, | 121 FilePath* file_path) { |
| 147 FilePath* file_path) { | |
| 148 // Parse the URL into a request for a source and path. | 122 // Parse the URL into a request for a source and path. |
| 149 std::string source_name; | 123 std::string source_name; |
| 150 std::string relative_path; | 124 std::string relative_path; |
| 151 | 125 |
| 152 // Remove Query and Ref from URL. | 126 // Remove Query and Ref from URL. |
| 153 GURL stripped_url; | 127 GURL stripped_url; |
| 154 GURL::Replacements replacements; | 128 GURL::Replacements replacements; |
| 155 replacements.ClearQuery(); | 129 replacements.ClearQuery(); |
| 156 replacements.ClearRef(); | 130 replacements.ClearRef(); |
| 157 stripped_url = url.ReplaceComponents(replacements); | 131 stripped_url = url.ReplaceComponents(replacements); |
| 158 | 132 |
| 159 URLToRequest(stripped_url, &source_name, &relative_path); | 133 URLToRequest(stripped_url, &source_name, &relative_path); |
| 160 | 134 |
| 161 FileSourceMap::const_iterator i( | 135 FileSourceMap::const_iterator i(file_sources_.find(source_name)); |
| 162 ChromeURLDataManager::GetInstance()->file_sources_.find(source_name)); | 136 if (i == file_sources_.end()) |
| 163 if (i == ChromeURLDataManager::GetInstance()->file_sources_.end()) | |
| 164 return false; | 137 return false; |
| 165 | 138 |
| 166 // Check that |relative_path| is not an absolute path (otherwise AppendASCII() | 139 // Check that |relative_path| is not an absolute path (otherwise AppendASCII() |
| 167 // will DCHECK). The awkward use of StringType is because on some systems | 140 // will DCHECK). The awkward use of StringType is because on some systems |
| 168 // FilePath expects a std::string, but on others a std::wstring. | 141 // FilePath expects a std::string, but on others a std::wstring. |
| 169 FilePath p(FilePath::StringType(relative_path.begin(), relative_path.end())); | 142 FilePath p(FilePath::StringType(relative_path.begin(), relative_path.end())); |
| 170 if (p.IsAbsolute()) | 143 if (p.IsAbsolute()) |
| 171 return false; | 144 return false; |
| 172 | 145 |
| 173 *file_path = i->second.AppendASCII(relative_path); | 146 *file_path = i->second.AppendASCII(relative_path); |
| 174 | 147 |
| 175 return true; | 148 return true; |
| 176 } | 149 } |
| 177 | 150 |
| 178 ChromeURLDataManager::ChromeURLDataManager() : next_request_id_(0) { } | 151 ChromeURLDataManagerBackend::ChromeURLDataManagerBackend() |
| 152 : next_request_id_(0) { |
| 153 FilePath inspector_dir; |
| 154 if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) |
| 155 AddFileSource(chrome::kChromeUIDevToolsHost, inspector_dir); |
| 156 AddDataSource(new SharedResourcesDataSource()); |
| 157 } |
| 179 | 158 |
| 180 ChromeURLDataManager::~ChromeURLDataManager() { | 159 ChromeURLDataManagerBackend::~ChromeURLDataManagerBackend() { |
| 181 // This is used as a Singleton, so it is only called at exit cleanup time. | 160 for (DataSourceMap::iterator i = data_sources_.begin(); |
| 182 // This means it is called on the main (UI) thread. | 161 i != data_sources_.end(); ++i) { |
| 183 // | 162 i->second->backend_ = NULL; |
| 184 // It will break if it is called at shutdown time on a different thread, as | 163 } |
| 185 // it will attempt to call the destructors for its |data_source_|s on the | 164 data_sources_.clear(); |
| 186 // UI thread, but the UI thread's message loop will be not be running | |
| 187 // -- so the destructor calls will be dropped and we will leak the objects. | |
| 188 } | 165 } |
| 189 | 166 |
| 190 // static | 167 // static |
| 191 ChromeURLDataManager* ChromeURLDataManager::GetInstance() { | 168 void ChromeURLDataManagerBackend::Register() { |
| 192 return Singleton<ChromeURLDataManager>::get(); | 169 net::URLRequest::RegisterProtocolFactory( |
| 170 chrome::kChromeDevToolsScheme, |
| 171 &ChromeURLDataManagerBackend::Factory); |
| 172 net::URLRequest::RegisterProtocolFactory( |
| 173 chrome::kChromeUIScheme, |
| 174 &ChromeURLDataManagerBackend::Factory); |
| 193 } | 175 } |
| 194 | 176 |
| 195 void ChromeURLDataManager::AddDataSource(scoped_refptr<DataSource> source) { | 177 void ChromeURLDataManagerBackend::AddDataSource( |
| 196 // Some |DataSource|-derived classes, notably |FileIconSource| and | 178 ChromeURLDataManager::DataSource* source) { |
| 197 // |WebUIFavIconSource|, have members that will DCHECK if they are not | 179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 198 // destructed in the same thread as they are constructed (the UI thread). | 180 DataSourceMap::iterator i = data_sources_.find(source->source_name()); |
| 199 // | 181 if (i != data_sources_.end()) |
| 200 // If |AddDataSource| is called more than once, it will destruct the object | 182 i->second->backend_ = NULL; |
| 201 // that it had before, as it is the only thing still holding a reference to | |
| 202 // that object. |DataSource| uses the |DeleteOnUIThread| trait to insure | |
| 203 // that the destructor is called on the UI thread. | |
| 204 // | |
| 205 // TODO(jackson): A new data source with same name should not clobber the | |
| 206 // existing one. | |
| 207 data_sources_[source->source_name()] = source; | 183 data_sources_[source->source_name()] = source; |
| 184 source->backend_ = this; |
| 208 } | 185 } |
| 209 | 186 |
| 210 void ChromeURLDataManager::RemoveDataSourceForTest(const char* source_name) { | 187 void ChromeURLDataManagerBackend::AddFileSource(const std::string& source_name, |
| 211 DataSourceMap::iterator i = data_sources_.find(source_name); | 188 const FilePath& file_path) { |
| 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 } | |
| 225 } | |
| 226 | |
| 227 void ChromeURLDataManager::AddFileSource(const std::string& source_name, | |
| 228 const FilePath& file_path) { | |
| 229 DCHECK(file_sources_.count(source_name) == 0); | 189 DCHECK(file_sources_.count(source_name) == 0); |
| 230 file_sources_[source_name] = file_path; | 190 file_sources_[source_name] = file_path; |
| 231 } | 191 } |
| 232 | 192 |
| 233 void ChromeURLDataManager::RemoveFileSource(const std::string& source_name) { | 193 bool ChromeURLDataManagerBackend::HasPendingJob( |
| 234 DCHECK(file_sources_.count(source_name) == 1); | 194 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(); | 195 for (PendingRequestMap::const_iterator i = pending_requests_.begin(); |
| 240 i != pending_requests_.end(); ++i) { | 196 i != pending_requests_.end(); ++i) { |
| 241 if (i->second == job) | 197 if (i->second == job) |
| 242 return true; | 198 return true; |
| 243 } | 199 } |
| 244 | |
| 245 return false; | 200 return false; |
| 246 } | 201 } |
| 247 | 202 |
| 248 bool ChromeURLDataManager::StartRequest(const GURL& url, | 203 bool ChromeURLDataManagerBackend::StartRequest(const GURL& url, |
| 249 URLRequestChromeJob* job) { | 204 URLRequestChromeJob* job) { |
| 250 // Parse the URL into a request for a source and path. | 205 // Parse the URL into a request for a source and path. |
| 251 std::string source_name; | 206 std::string source_name; |
| 252 std::string path; | 207 std::string path; |
| 253 URLToRequest(url, &source_name, &path); | 208 URLToRequest(url, &source_name, &path); |
| 254 | 209 |
| 255 // Look up the data source for the request. | 210 // Look up the data source for the request. |
| 256 DataSourceMap::iterator i = data_sources_.find(source_name); | 211 DataSourceMap::iterator i = data_sources_.find(source_name); |
| 257 if (i == data_sources_.end()) | 212 if (i == data_sources_.end()) |
| 258 return false; | 213 return false; |
| 259 DataSource* source = i->second; | 214 |
| 215 ChromeURLDataManager::DataSource* source = i->second; |
| 260 | 216 |
| 261 // Save this request so we know where to send the data. | 217 // Save this request so we know where to send the data. |
| 262 RequestID request_id = next_request_id_++; | 218 RequestID request_id = next_request_id_++; |
| 263 pending_requests_.insert(std::make_pair(request_id, job)); | 219 pending_requests_.insert(std::make_pair(request_id, job)); |
| 264 | 220 |
| 265 // TODO(eroman): would be nicer if the mimetype were set at the same time | 221 // 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 | 222 // as the data blob. For now do it here, since NotifyHeadersComplete() is |
| 267 // going to get called once we return. | 223 // going to get called once we return. |
| 268 job->SetMimeType(source->GetMimeType(path)); | 224 job->SetMimeType(source->GetMimeType(path)); |
| 269 | 225 |
| 270 ChromeURLRequestContext* context = static_cast<ChromeURLRequestContext*>( | 226 ChromeURLRequestContext* context = static_cast<ChromeURLRequestContext*>( |
| 271 job->request()->context()); | 227 job->request()->context()); |
| 272 | 228 |
| 273 // Forward along the request to the data source. | 229 // Forward along the request to the data source. |
| 274 MessageLoop* target_message_loop = source->MessageLoopForRequestPath(path); | 230 MessageLoop* target_message_loop = source->MessageLoopForRequestPath(path); |
| 275 if (!target_message_loop) { | 231 if (!target_message_loop) { |
| 276 // The DataSource is agnostic to which thread StartDataRequest is called | 232 // The DataSource is agnostic to which thread StartDataRequest is called |
| 277 // on for this path. Call directly into it from this thread, the IO | 233 // on for this path. Call directly into it from this thread, the IO |
| 278 // thread. | 234 // thread. |
| 279 source->StartDataRequest(path, context->is_off_the_record(), request_id); | 235 source->StartDataRequest(path, context->is_off_the_record(), request_id); |
| 280 } else { | 236 } else { |
| 281 // The DataSource wants StartDataRequest to be called on a specific thread, | 237 // The DataSource wants StartDataRequest to be called on a specific thread, |
| 282 // usually the UI thread, for this path. | 238 // usually the UI thread, for this path. |
| 283 target_message_loop->PostTask(FROM_HERE, | 239 target_message_loop->PostTask( |
| 284 NewRunnableMethod(source, &DataSource::StartDataRequest, | 240 FROM_HERE, |
| 241 NewRunnableMethod(source, |
| 242 &ChromeURLDataManager::DataSource::StartDataRequest, |
| 285 path, context->is_off_the_record(), request_id)); | 243 path, context->is_off_the_record(), request_id)); |
| 286 } | 244 } |
| 287 return true; | 245 return true; |
| 288 } | 246 } |
| 289 | 247 |
| 290 void ChromeURLDataManager::RemoveRequest(URLRequestChromeJob* job) { | 248 void ChromeURLDataManagerBackend::RemoveRequest(URLRequestChromeJob* job) { |
| 291 // Remove the request from our list of pending requests. | 249 // Remove the request from our list of pending requests. |
| 292 // If/when the source sends the data that was requested, the data will just | 250 // If/when the source sends the data that was requested, the data will just |
| 293 // be thrown away. | 251 // be thrown away. |
| 294 for (PendingRequestMap::iterator i = pending_requests_.begin(); | 252 for (PendingRequestMap::iterator i = pending_requests_.begin(); |
| 295 i != pending_requests_.end(); ++i) { | 253 i != pending_requests_.end(); ++i) { |
| 296 if (i->second == job) { | 254 if (i->second == job) { |
| 297 pending_requests_.erase(i); | 255 pending_requests_.erase(i); |
| 298 return; | 256 return; |
| 299 } | 257 } |
| 300 } | 258 } |
| 301 } | 259 } |
| 302 | 260 |
| 303 void ChromeURLDataManager::DataAvailable( | 261 void ChromeURLDataManagerBackend::DataAvailable(RequestID request_id, |
| 304 RequestID request_id, | 262 RefCountedMemory* bytes) { |
| 305 scoped_refptr<RefCountedMemory> bytes) { | |
| 306 // Forward this data on to the pending net::URLRequest, if it exists. | 263 // Forward this data on to the pending net::URLRequest, if it exists. |
| 307 PendingRequestMap::iterator i = pending_requests_.find(request_id); | 264 PendingRequestMap::iterator i = pending_requests_.find(request_id); |
| 308 if (i != pending_requests_.end()) { | 265 if (i != pending_requests_.end()) { |
| 309 // We acquire a reference to the job so that it doesn't disappear under the | 266 // 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). | 267 // feet of any method invoked here (we could trigger a callback). |
| 311 scoped_refptr<URLRequestChromeJob> job(i->second); | 268 scoped_refptr<URLRequestChromeJob> job(i->second); |
| 312 pending_requests_.erase(i); | 269 pending_requests_.erase(i); |
| 313 job->DataAvailable(bytes); | 270 job->DataAvailable(bytes); |
| 314 } | 271 } |
| 315 } | 272 } |
| 316 | 273 |
| 317 ChromeURLDataManager::DataSource::DataSource(const std::string& source_name, | |
| 318 MessageLoop* message_loop) | |
| 319 : source_name_(source_name), message_loop_(message_loop) { | |
| 320 } | |
| 321 | |
| 322 ChromeURLDataManager::DataSource::~DataSource() { | |
| 323 } | |
| 324 | |
| 325 void ChromeURLDataManager::DataSource::SendResponse( | |
| 326 RequestID request_id, | |
| 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 | |
| 340 // static | 274 // static |
| 341 void ChromeURLDataManager::DataSource::SetFontAndTextDirection( | 275 net::URLRequestJob* ChromeURLDataManagerBackend::Factory( |
| 342 DictionaryValue* localized_strings) { | 276 net::URLRequest* request, |
| 343 localized_strings->SetString("fontfamily", | 277 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 | 278 // Try first with a file handler |
| 363 FilePath path; | 279 FilePath path; |
| 364 if (ChromeURLDataManager::URLToFilePath(request->url(), &path)) | 280 ChromeURLDataManagerBackend* backend = GetBackend(request); |
| 281 if (backend->URLToFilePath(request->url(), &path)) |
| 365 return new URLRequestChromeFileJob(request, path); | 282 return new URLRequestChromeFileJob(request, path); |
| 366 | 283 |
| 367 // Next check for chrome://view-http-cache/*, which uses its own job type. | 284 // Next check for chrome://view-http-cache/*, which uses its own job type. |
| 368 if (ViewHttpCacheJobFactory::IsSupportedURL(request->url())) | 285 if (ViewHttpCacheJobFactory::IsSupportedURL(request->url())) |
| 369 return ViewHttpCacheJobFactory::CreateJobForRequest(request); | 286 return ViewHttpCacheJobFactory::CreateJobForRequest(request); |
| 370 | 287 |
| 371 // Next check for chrome://appcache-internals/, which uses its own job type. | 288 // Next check for chrome://appcache-internals/, which uses its own job type. |
| 372 if (ViewAppCacheInternalsJobFactory::IsSupportedURL(request->url())) | 289 if (ViewAppCacheInternalsJobFactory::IsSupportedURL(request->url())) |
| 373 return ViewAppCacheInternalsJobFactory::CreateJobForRequest(request); | 290 return ViewAppCacheInternalsJobFactory::CreateJobForRequest(request); |
| 374 | 291 |
| 375 // Next check for chrome://blob-internals/, which uses its own job type. | 292 // Next check for chrome://blob-internals/, which uses its own job type. |
| 376 if (ViewBlobInternalsJobFactory::IsSupportedURL(request->url())) | 293 if (ViewBlobInternalsJobFactory::IsSupportedURL(request->url())) |
| 377 return ViewBlobInternalsJobFactory::CreateJobForRequest(request); | 294 return ViewBlobInternalsJobFactory::CreateJobForRequest(request); |
| 378 | 295 |
| 379 // Fall back to using a custom handler | 296 // Fall back to using a custom handler |
| 380 return new URLRequestChromeJob(request); | 297 return new URLRequestChromeJob(request); |
| 381 } | 298 } |
| 382 | 299 |
| 383 URLRequestChromeJob::URLRequestChromeJob(net::URLRequest* request) | 300 URLRequestChromeJob::URLRequestChromeJob(net::URLRequest* request) |
| 384 : net::URLRequestJob(request), | 301 : net::URLRequestJob(request), |
| 385 data_offset_(0), | 302 data_offset_(0), |
| 386 pending_buf_size_(0) { | 303 pending_buf_size_(0), |
| 304 backend_(GetBackend(request)) { |
| 387 } | 305 } |
| 388 | 306 |
| 389 URLRequestChromeJob::~URLRequestChromeJob() { | 307 URLRequestChromeJob::~URLRequestChromeJob() { |
| 390 CHECK(!ChromeURLDataManager::GetInstance()->HasPendingJob(this)); | 308 CHECK(!backend_->HasPendingJob(this)); |
| 391 } | 309 } |
| 392 | 310 |
| 393 void URLRequestChromeJob::Start() { | 311 void URLRequestChromeJob::Start() { |
| 394 // Start reading asynchronously so that all error reporting and data | 312 // Start reading asynchronously so that all error reporting and data |
| 395 // callbacks happen as they would for network requests. | 313 // callbacks happen as they would for network requests. |
| 396 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 314 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 397 this, &URLRequestChromeJob::StartAsync)); | 315 this, &URLRequestChromeJob::StartAsync)); |
| 398 } | 316 } |
| 399 | 317 |
| 400 void URLRequestChromeJob::Kill() { | 318 void URLRequestChromeJob::Kill() { |
| 401 ChromeURLDataManager::GetInstance()->RemoveRequest(this); | 319 backend_->RemoveRequest(this); |
| 402 } | 320 } |
| 403 | 321 |
| 404 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const { | 322 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const { |
| 405 *mime_type = mime_type_; | 323 *mime_type = mime_type_; |
| 406 return !mime_type_.empty(); | 324 return !mime_type_.empty(); |
| 407 } | 325 } |
| 408 | 326 |
| 409 void URLRequestChromeJob::DataAvailable(RefCountedMemory* bytes) { | 327 void URLRequestChromeJob::DataAvailable(RefCountedMemory* bytes) { |
| 410 if (bytes) { | 328 if (bytes) { |
| 411 // The request completed, and we have all the data. | 329 // 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); | 370 memcpy(buf->data(), data_->front() + data_offset_, buf_size); |
| 453 data_offset_ += buf_size; | 371 data_offset_ += buf_size; |
| 454 } | 372 } |
| 455 *bytes_read = buf_size; | 373 *bytes_read = buf_size; |
| 456 } | 374 } |
| 457 | 375 |
| 458 void URLRequestChromeJob::StartAsync() { | 376 void URLRequestChromeJob::StartAsync() { |
| 459 if (!request_) | 377 if (!request_) |
| 460 return; | 378 return; |
| 461 | 379 |
| 462 if (ChromeURLDataManager::GetInstance()->StartRequest(request_->url(), | 380 if (backend_->StartRequest(request_->url(), this)) { |
| 463 this)) { | |
| 464 NotifyHeadersComplete(); | 381 NotifyHeadersComplete(); |
| 465 } else { | 382 } else { |
| 466 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 383 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 467 net::ERR_INVALID_URL)); | 384 net::ERR_INVALID_URL)); |
| 468 } | 385 } |
| 469 } | 386 } |
| 470 | 387 |
| 471 URLRequestChromeFileJob::URLRequestChromeFileJob(net::URLRequest* request, | 388 URLRequestChromeFileJob::URLRequestChromeFileJob(net::URLRequest* request, |
| 472 const FilePath& path) | 389 const FilePath& path) |
| 473 : net::URLRequestFileJob(request, path) { | 390 : net::URLRequestFileJob(request, path) { |
| 474 } | 391 } |
| 475 | 392 |
| 476 URLRequestChromeFileJob::~URLRequestChromeFileJob() { } | 393 URLRequestChromeFileJob::~URLRequestChromeFileJob() {} |
| OLD | NEW |