Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(173)

Side by Side Diff: chrome/browser/custom_handlers/protocol_handler_registry.cc

Issue 10546083: Convert ProtocolHandlerRegistry to be a ProfileKeyedService. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Merge in MessageLoop conflict from upstream. Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/custom_handlers/protocol_handler_registry.h" 5 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
13 #include "chrome/browser/custom_handlers/register_protocol_handler_infobar_deleg ate.h" 14 #include "chrome/browser/custom_handlers/register_protocol_handler_infobar_deleg ate.h"
14 #include "chrome/browser/net/chrome_url_request_context.h" 15 #include "chrome/browser/net/chrome_url_request_context.h"
15 #include "chrome/browser/prefs/pref_service.h" 16 #include "chrome/browser/prefs/pref_service.h"
16 #include "chrome/browser/profiles/profile_io_data.h" 17 #include "chrome/browser/profiles/profile_io_data.h"
17 #include "chrome/common/chrome_notification_types.h" 18 #include "chrome/common/chrome_notification_types.h"
18 #include "chrome/common/chrome_switches.h" 19 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/custom_handlers/protocol_handler.h" 20 #include "chrome/common/custom_handlers/protocol_handler.h"
20 #include "chrome/common/pref_names.h" 21 #include "chrome/common/pref_names.h"
21 #include "content/public/browser/browser_thread.h" 22 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/child_process_security_policy.h" 23 #include "content/public/browser/child_process_security_policy.h"
23 #include "content/public/browser/notification_service.h" 24 #include "content/public/browser/notification_service.h"
24 #include "net/base/network_delegate.h" 25 #include "net/base/network_delegate.h"
26 #include "net/url_request/url_request.h"
27 #include "net/url_request/url_request_job.h"
25 #include "net/url_request/url_request_redirect_job.h" 28 #include "net/url_request/url_request_redirect_job.h"
26 29
27 using content::BrowserThread; 30 using content::BrowserThread;
28 using content::ChildProcessSecurityPolicy; 31 using content::ChildProcessSecurityPolicy;
29 32
30 namespace { 33 namespace {
31 34
35 const ProtocolHandler& LookupHandler(
36 const ProtocolHandlerRegistry::ProtocolHandlerMap& handler_map,
37 const std::string& scheme) {
38 ProtocolHandlerRegistry::ProtocolHandlerMap::const_iterator p =
39 handler_map.find(scheme);
40 if (p != handler_map.end()) {
41 return p->second;
42 }
43 return ProtocolHandler::EmptyProtocolHandler();
44 }
45
32 // If true default protocol handlers will be removed if the OS level 46 // If true default protocol handlers will be removed if the OS level
33 // registration for a protocol is no longer Chrome. 47 // registration for a protocol is no longer Chrome.
34 bool ShouldRemoveHandlersNotInOS() { 48 bool ShouldRemoveHandlersNotInOS() {
35 #if defined(OS_LINUX) 49 #if defined(OS_LINUX)
36 // We don't do this on Linux as the OS registration there is not reliable, 50 // We don't do this on Linux as the OS registration there is not reliable,
37 // and Chrome OS doesn't have any notion of OS registration. 51 // and Chrome OS doesn't have any notion of OS registration.
38 // TODO(benwells): When Linux support is more reliable remove this 52 // TODO(benwells): When Linux support is more reliable remove this
39 // difference (http://crbug.com/88255). 53 // difference (http://crbug.com/88255).
40 return false; 54 return false;
41 #else 55 #else
42 return ShellIntegration::CanSetAsDefaultProtocolClient() != 56 return ShellIntegration::CanSetAsDefaultProtocolClient() !=
43 ShellIntegration::SET_DEFAULT_NOT_ALLOWED; 57 ShellIntegration::SET_DEFAULT_NOT_ALLOWED;
44 #endif 58 #endif
45 } 59 }
46 60
47 } // namespace 61 void InstallDefaultProtocolHandlers(ProtocolHandlerRegistry* registry) {
62 // Only chromeos has default protocol handlers at this point.
63 #if defined(OS_CHROMEOS)
64 registry->AddPredefinedHandler(
65 ProtocolHandler::CreateProtocolHandler(
66 "mailto",
67 GURL(l10n_util::GetStringUTF8(IDS_GOOGLE_MAILTO_HANDLER_URL)),
68 l10n_util::GetStringUTF16(IDS_GOOGLE_MAILTO_HANDLER_NAME)));
69 registry->AddPredefinedHandler(
70 ProtocolHandler::CreateProtocolHandler(
71 "webcal",
72 GURL(l10n_util::GetStringUTF8(IDS_GOOGLE_WEBCAL_HANDLER_URL)),
73 l10n_util::GetStringUTF16(IDS_GOOGLE_WEBCAL_HANDLER_NAME)));
74 #endif
75 }
48 76
49 static const ProtocolHandler& LookupHandler( 77 } // namespace
50 const ProtocolHandlerRegistry::ProtocolHandlerMap& handler_map, 78
51 const std::string& scheme) { 79 // Core ------------------------------------------------------------------------
52 ProtocolHandlerRegistry::ProtocolHandlerMap::const_iterator p = 80
53 handler_map.find(scheme); 81 // Core is an IO thread specific object. Access to the class should all
54 if (p != handler_map.end()) { 82 // be done via the IO thread. The registry living on the UI thread makes
55 return p->second; 83 // a best effort to update the IO object after local updates are completed.
84 class ProtocolHandlerRegistry::Core
85 : public base::RefCountedThreadSafe<ProtocolHandlerRegistry::Core> {
86 public:
87 explicit Core(bool enabled);
88
89 // Returns true if the protocol has a default protocol handler.
90 // Should be called only from the IO thread.
91 bool IsHandledProtocol(const std::string& scheme) const;
92
93 // Clears the default for the provided protocol.
94 // Should be called only from the IO thread.
95 void ClearDefault(const std::string& scheme);
96
97 // Makes this ProtocolHandler the default handler for its protocol.
98 // Should be called only from the IO thread.
99 void SetDefault(const ProtocolHandler& handler);
100
101 // Creates a URL request job for the given request if there is a matching
102 // protocol handler, returns NULL otherwise.
103 net::URLRequestJob* MaybeCreateJob(net::URLRequest* request) const;
104
105 // Indicate that the registry has been enabled in the IO thread's
106 // copy of the data.
107 void Enable() { enabled_ = true; }
108
109 // Indicate that the registry has been disabled in the IO thread's copy of
110 // the data.
111 void Disable() { enabled_ = false; }
112
113 private:
114 friend class base::RefCountedThreadSafe<Core>;
115 virtual ~Core();
116 ProtocolHandlerRegistry::ProtocolHandlerMap default_handlers_;
117 bool enabled_;
118
119 DISALLOW_COPY_AND_ASSIGN(Core);
120 };
121
122 ProtocolHandlerRegistry::Core::Core(bool) : enabled_(true) {}
123 ProtocolHandlerRegistry::Core::~Core() {}
124
125 bool ProtocolHandlerRegistry::Core::IsHandledProtocol(
126 const std::string& scheme) const {
127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
128 return enabled_ && !LookupHandler(default_handlers_, scheme).IsEmpty();
129 }
130
131 void ProtocolHandlerRegistry::Core::ClearDefault(const std::string& scheme) {
132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
133 default_handlers_.erase(scheme);
134 }
135
136 void ProtocolHandlerRegistry::Core::SetDefault(const ProtocolHandler& handler) {
137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
138 ClearDefault(handler.protocol());
139 default_handlers_.insert(std::make_pair(handler.protocol(), handler));
140 }
141
142 net::URLRequestJob* ProtocolHandlerRegistry::Core::MaybeCreateJob(
143 net::URLRequest* request) const {
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
145
146 ProtocolHandler handler = LookupHandler(default_handlers_,
147 request->url().scheme());
148 if (handler.IsEmpty()) {
149 return NULL;
56 } 150 }
57 return ProtocolHandler::EmptyProtocolHandler(); 151 GURL translated_url(handler.TranslateUrl(request->url()));
152 if (!translated_url.is_valid()) {
153 return NULL;
154 }
155 return new net::URLRequestRedirectJob(request, translated_url);
156 }
157
158 // URLInterceptor ------------------------------------------------------------
159
160 // Instances of this class are produced for ownership by the IO
161 // thread where it handler URL requests. We should never hold
162 // any pointers on this class, only produce them in response to
163 // requests via |ProtocolHandlerRegistry::CreateURLInterceptor|.
164 class ProtocolHandlerRegistry::URLInterceptor
165 : public net::URLRequestJobFactory::Interceptor {
166 public:
167 explicit URLInterceptor(Core* core);
168 virtual ~URLInterceptor();
169
170 virtual net::URLRequestJob* MaybeIntercept(
171 net::URLRequest* request) const OVERRIDE;
172
173 virtual bool WillHandleProtocol(const std::string& protocol) const OVERRIDE;
174
175 virtual net::URLRequestJob* MaybeInterceptRedirect(
176 const GURL& url, net::URLRequest* request) const OVERRIDE {
177 return NULL;
178 }
179
180 virtual net::URLRequestJob* MaybeInterceptResponse(
181 net::URLRequest* request) const OVERRIDE {
182 return NULL;
183 }
184
185 private:
186 scoped_refptr<Core> core_;
187 DISALLOW_COPY_AND_ASSIGN(URLInterceptor);
188 };
189
190 ProtocolHandlerRegistry::URLInterceptor::URLInterceptor(Core* core)
191 : core_(core) {
192 DCHECK(core_);
193 }
194
195 ProtocolHandlerRegistry::URLInterceptor::~URLInterceptor() {
196 // TODO(smckay): release what ever type of pointer we have
197 // on the C instance.
willchan no longer on Chromium 2012/06/29 21:59:50 ? If you mean the scoped_refptr<Core>, it'll get a
198 }
199
200 net::URLRequestJob* ProtocolHandlerRegistry::URLInterceptor::MaybeIntercept(
201 net::URLRequest* request) const {
202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
203
204 return core_->MaybeCreateJob(request);
205 }
206
207 bool ProtocolHandlerRegistry::URLInterceptor::WillHandleProtocol(
208 const std::string& protocol) const {
209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
210
211 return core_->IsHandledProtocol(protocol);
58 } 212 }
59 213
60 // DefaultClientObserver ------------------------------------------------------ 214 // DefaultClientObserver ------------------------------------------------------
61 215
62 ProtocolHandlerRegistry::DefaultClientObserver::DefaultClientObserver( 216 ProtocolHandlerRegistry::DefaultClientObserver::DefaultClientObserver(
63 ProtocolHandlerRegistry* registry) 217 ProtocolHandlerRegistry* registry)
64 : worker_(NULL), 218 : worker_(NULL),
65 registry_(registry) { 219 registry_(registry) {
66 DCHECK(registry_); 220 DCHECK(registry_);
67 } 221 }
68 222
69 ProtocolHandlerRegistry::DefaultClientObserver::~DefaultClientObserver() { 223 ProtocolHandlerRegistry::DefaultClientObserver::~DefaultClientObserver() {
70 if (worker_) 224 if (worker_)
71 worker_->ObserverDestroyed(); 225 worker_->ObserverDestroyed();
72 226
73 DefaultClientObserverList::iterator iter = std::find( 227 DefaultClientObserverList::iterator iter = std::find(
74 registry_->default_client_observers_.begin(), 228 registry_->default_client_observers_.begin(),
75 registry_->default_client_observers_.end(), this); 229 registry_->default_client_observers_.end(), this);
76 registry_->default_client_observers_.erase(iter); 230 registry_->default_client_observers_.erase(iter);
77 } 231 }
78 232
79 void 233 void ProtocolHandlerRegistry::DefaultClientObserver::SetDefaultWebClientUIState(
80 ProtocolHandlerRegistry::DefaultClientObserver::SetDefaultWebClientUIState(
81 ShellIntegration::DefaultWebClientUIState state) { 234 ShellIntegration::DefaultWebClientUIState state) {
82 if (worker_) { 235 if (worker_) {
83 if (ShouldRemoveHandlersNotInOS() && 236 if (ShouldRemoveHandlersNotInOS() &&
84 (state == ShellIntegration::STATE_NOT_DEFAULT)) { 237 (state == ShellIntegration::STATE_NOT_DEFAULT)) {
85 registry_->ClearDefault(worker_->protocol()); 238 registry_->ClearDefault(worker_->protocol());
86 } 239 }
87 } else { 240 } else {
88 NOTREACHED(); 241 NOTREACHED();
89 } 242 }
90 } 243 }
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 worker->StartSetAsDefault(); 297 worker->StartSetAsDefault();
145 } 298 }
146 299
147 // ProtocolHandlerRegistry ----------------------------------------------------- 300 // ProtocolHandlerRegistry -----------------------------------------------------
148 301
149 ProtocolHandlerRegistry::ProtocolHandlerRegistry(Profile* profile, 302 ProtocolHandlerRegistry::ProtocolHandlerRegistry(Profile* profile,
150 Delegate* delegate) 303 Delegate* delegate)
151 : profile_(profile), 304 : profile_(profile),
152 delegate_(delegate), 305 delegate_(delegate),
153 enabled_(true), 306 enabled_(true),
154 enabled_io_(enabled_),
155 is_loading_(false), 307 is_loading_(false),
156 is_loaded_(false) { 308 is_loaded_(false),
309 core_(new Core(enabled_)){
157 } 310 }
158 311
159 bool ProtocolHandlerRegistry::SilentlyHandleRegisterHandlerRequest( 312 bool ProtocolHandlerRegistry::SilentlyHandleRegisterHandlerRequest(
160 const ProtocolHandler& handler) { 313 const ProtocolHandler& handler) {
161 if (handler.IsEmpty() || !CanSchemeBeOverridden(handler.protocol())) 314 if (handler.IsEmpty() || !CanSchemeBeOverridden(handler.protocol()))
162 return true; 315 return true;
163 316
164 if (!enabled() || IsRegistered(handler) || HasIgnoredEquivalent(handler)) 317 if (!enabled() || IsRegistered(handler) || HasIgnoredEquivalent(handler))
165 return true; 318 return true;
166 319
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 p != handlers->end(); p++) { 379 p != handlers->end(); p++) {
227 if (handler.IsSameOrigin(*p)) { 380 if (handler.IsSameOrigin(*p)) {
228 replaced_handlers.push_back(*p); 381 replaced_handlers.push_back(*p);
229 } 382 }
230 } 383 }
231 return replaced_handlers; 384 return replaced_handlers;
232 } 385 }
233 386
234 void ProtocolHandlerRegistry::ClearDefault(const std::string& scheme) { 387 void ProtocolHandlerRegistry::ClearDefault(const std::string& scheme) {
235 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 388 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
389
236 default_handlers_.erase(scheme); 390 default_handlers_.erase(scheme);
237 BrowserThread::PostTask( 391 BrowserThread::PostTask(
238 BrowserThread::IO, 392 BrowserThread::IO,
239 FROM_HERE, 393 FROM_HERE,
240 base::Bind(&ProtocolHandlerRegistry::ClearDefaultIO, this, scheme)); 394 base::Bind(&Core::ClearDefault, core_, scheme));
241 Save(); 395 Save();
242 NotifyChanged(); 396 NotifyChanged();
243 } 397 }
244 398
245 bool ProtocolHandlerRegistry::IsDefault( 399 bool ProtocolHandlerRegistry::IsDefault(
246 const ProtocolHandler& handler) const { 400 const ProtocolHandler& handler) const {
247 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 401 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
248 return GetHandlerFor(handler.protocol()) == handler; 402 return GetHandlerFor(handler.protocol()) == handler;
249 } 403 }
250 404
251 void ProtocolHandlerRegistry::Load() { 405 void ProtocolHandlerRegistry::InitProtocolSettings() {
406 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
407
408 // Default protocol handlers must be installed first.
409 InstallDefaultProtocolHandlers(this);
410
252 // Any further default additions to the table will get rejected from now on. 411 // Any further default additions to the table will get rejected from now on.
253 is_loaded_ = true; 412 is_loaded_ = true;
254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
255 is_loading_ = true; 413 is_loading_ = true;
414
256 PrefService* prefs = profile_->GetPrefs(); 415 PrefService* prefs = profile_->GetPrefs();
257 if (prefs->HasPrefPath(prefs::kCustomHandlersEnabled)) { 416 if (prefs->HasPrefPath(prefs::kCustomHandlersEnabled)) {
258 enabled_ = prefs->GetBoolean(prefs::kCustomHandlersEnabled); 417 if (prefs->GetBoolean(prefs::kCustomHandlersEnabled)) {
259 BrowserThread::PostTask( 418 Enable();
260 BrowserThread::IO, 419 } else {
261 FROM_HERE, 420 Disable();
262 base::Bind(enabled_ ? &ProtocolHandlerRegistry::EnableIO : 421 }
263 &ProtocolHandlerRegistry::DisableIO, this));
264 } 422 }
265 std::vector<const DictionaryValue*> registered_handlers = 423 std::vector<const DictionaryValue*> registered_handlers =
266 GetHandlersFromPref(prefs::kRegisteredProtocolHandlers); 424 GetHandlersFromPref(prefs::kRegisteredProtocolHandlers);
267 for (std::vector<const DictionaryValue*>::const_iterator p = 425 for (std::vector<const DictionaryValue*>::const_iterator p =
268 registered_handlers.begin(); 426 registered_handlers.begin();
269 p != registered_handlers.end(); ++p) { 427 p != registered_handlers.end(); ++p) {
270 ProtocolHandler handler = ProtocolHandler::CreateProtocolHandler(*p); 428 ProtocolHandler handler = ProtocolHandler::CreateProtocolHandler(*p);
271 RegisterProtocolHandler(handler); 429 RegisterProtocolHandler(handler);
272 bool is_default = false; 430 bool is_default = false;
273 if ((*p)->GetBoolean("default", &is_default) && is_default) { 431 if ((*p)->GetBoolean("default", &is_default) && is_default) {
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 if (should_notify) 579 if (should_notify)
422 NotifyChanged(); 580 NotifyChanged();
423 } 581 }
424 582
425 bool ProtocolHandlerRegistry::IsHandledProtocol( 583 bool ProtocolHandlerRegistry::IsHandledProtocol(
426 const std::string& scheme) const { 584 const std::string& scheme) const {
427 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 585 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
428 return enabled_ && !GetHandlerFor(scheme).IsEmpty(); 586 return enabled_ && !GetHandlerFor(scheme).IsEmpty();
429 } 587 }
430 588
431 bool ProtocolHandlerRegistry::IsHandledProtocolIO(
432 const std::string& scheme) const {
433 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
434 return enabled_io_ && !LookupHandler(default_handlers_io_, scheme).IsEmpty();
435 }
436
437 void ProtocolHandlerRegistry::RemoveHandler( 589 void ProtocolHandlerRegistry::RemoveHandler(
438 const ProtocolHandler& handler) { 590 const ProtocolHandler& handler) {
439 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 591 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
440 ProtocolHandlerList& handlers = protocol_handlers_[handler.protocol()]; 592 ProtocolHandlerList& handlers = protocol_handlers_[handler.protocol()];
441 ProtocolHandlerList::iterator p = 593 ProtocolHandlerList::iterator p =
442 std::find(handlers.begin(), handlers.end(), handler); 594 std::find(handlers.begin(), handlers.end(), handler);
443 if (p != handlers.end()) { 595 if (p != handlers.end()) {
444 handlers.erase(p); 596 handlers.erase(p);
445 } 597 }
446 ProtocolHandlerMap::iterator q = default_handlers_.find(handler.protocol()); 598 ProtocolHandlerMap::iterator q = default_handlers_.find(handler.protocol());
447 if (q != default_handlers_.end() && q->second == handler) { 599 if (q != default_handlers_.end() && q->second == handler) {
448 // Make the new top handler in the list the default. 600 // Make the new top handler in the list the default.
449 if (!handlers.empty()) { 601 if (!handlers.empty()) {
450 // NOTE We pass a copy because SetDefault() modifies handlers. 602 // NOTE We pass a copy because SetDefault() modifies handlers.
451 SetDefault(ProtocolHandler(handlers[0])); 603 SetDefault(ProtocolHandler(handlers[0]));
452 } else { 604 } else {
453 BrowserThread::PostTask( 605 BrowserThread::PostTask(
454 BrowserThread::IO, FROM_HERE, 606 BrowserThread::IO, FROM_HERE,
455 base::Bind(&ProtocolHandlerRegistry::ClearDefaultIO, this, 607 base::Bind(&Core::ClearDefault, core_, q->second.protocol()));
456 q->second.protocol())); 608
457 default_handlers_.erase(q); 609 default_handlers_.erase(q);
458 } 610 }
459 } 611 }
460 612
461 if (!IsHandledProtocol(handler.protocol())) { 613 if (!IsHandledProtocol(handler.protocol())) {
462 delegate_->DeregisterExternalHandler(handler.protocol()); 614 delegate_->DeregisterExternalHandler(handler.protocol());
463 } 615 }
464 Save(); 616 Save();
465 NotifyChanged(); 617 NotifyChanged();
466 } 618 }
467 619
468 void ProtocolHandlerRegistry::RemoveDefaultHandler(const std::string& scheme) { 620 void ProtocolHandlerRegistry::RemoveDefaultHandler(const std::string& scheme) {
469 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 621 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
470 ProtocolHandler current_default = GetHandlerFor(scheme); 622 ProtocolHandler current_default = GetHandlerFor(scheme);
471 if (!current_default.IsEmpty()) 623 if (!current_default.IsEmpty())
472 RemoveHandler(current_default); 624 RemoveHandler(current_default);
473 } 625 }
474 626
475 const ProtocolHandler& ProtocolHandlerRegistry::GetHandlerFor( 627 const ProtocolHandler& ProtocolHandlerRegistry::GetHandlerFor(
476 const std::string& scheme) const { 628 const std::string& scheme) const {
477 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 629 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
478 return LookupHandler(default_handlers_, scheme); 630 return LookupHandler(default_handlers_, scheme);
479 } 631 }
480 632
481 net::URLRequestJob* ProtocolHandlerRegistry::MaybeCreateJob(
482 net::URLRequest* request) const {
483 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
484 ProtocolHandler handler = LookupHandler(default_handlers_io_,
485 request->url().scheme());
486 if (handler.IsEmpty()) {
487 return NULL;
488 }
489 GURL translated_url(handler.TranslateUrl(request->url()));
490 if (!translated_url.is_valid()) {
491 return NULL;
492 }
493 return new net::URLRequestRedirectJob(request, translated_url);
494 }
495
496 void ProtocolHandlerRegistry::Enable() { 633 void ProtocolHandlerRegistry::Enable() {
497 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 634 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
498 if (enabled_) { 635 if (enabled_) {
499 return; 636 return;
500 } 637 }
501 enabled_ = true; 638 enabled_ = true;
502 BrowserThread::PostTask( 639 BrowserThread::PostTask(
503 BrowserThread::IO, 640 BrowserThread::IO,
504 FROM_HERE, 641 FROM_HERE,
505 base::Bind(&ProtocolHandlerRegistry::EnableIO, this)); 642 base::Bind(&Core::Enable, core_));
643
506 ProtocolHandlerMap::const_iterator p; 644 ProtocolHandlerMap::const_iterator p;
507 for (p = default_handlers_.begin(); p != default_handlers_.end(); ++p) { 645 for (p = default_handlers_.begin(); p != default_handlers_.end(); ++p) {
508 delegate_->RegisterExternalHandler(p->first); 646 delegate_->RegisterExternalHandler(p->first);
509 } 647 }
510 Save(); 648 Save();
511 NotifyChanged(); 649 NotifyChanged();
512 } 650 }
513 651
514 void ProtocolHandlerRegistry::Disable() { 652 void ProtocolHandlerRegistry::Disable() {
515 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 653 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
516 if (!enabled_) { 654 if (!enabled_) {
517 return; 655 return;
518 } 656 }
519 enabled_ = false; 657 enabled_ = false;
520 BrowserThread::PostTask( 658 BrowserThread::PostTask(
521 BrowserThread::IO, 659 BrowserThread::IO,
522 FROM_HERE, 660 FROM_HERE,
523 base::Bind(&ProtocolHandlerRegistry::DisableIO, this)); 661 base::Bind(&Core::Disable, core_));
662
524 ProtocolHandlerMap::const_iterator p; 663 ProtocolHandlerMap::const_iterator p;
525 for (p = default_handlers_.begin(); p != default_handlers_.end(); ++p) { 664 for (p = default_handlers_.begin(); p != default_handlers_.end(); ++p) {
526 delegate_->DeregisterExternalHandler(p->first); 665 delegate_->DeregisterExternalHandler(p->first);
527 } 666 }
528 Save(); 667 Save();
529 NotifyChanged(); 668 NotifyChanged();
530 } 669 }
531 670
532 void ProtocolHandlerRegistry::Finalize() { 671 void ProtocolHandlerRegistry::Shutdown() {
533 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 672 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
534 delegate_.reset(NULL); 673 delegate_.reset(NULL);
535 // We free these now in case there are any outstanding workers running. If 674 // We free these now in case there are any outstanding workers running. If
536 // we didn't free them they could respond to workers and try to update the 675 // we didn't free them they could respond to workers and try to update the
537 // protocol handler registry after it was deleted. 676 // protocol handler registry after it was deleted.
538 // Observers remove themselves from this list when they are deleted; so 677 // Observers remove themselves from this list when they are deleted; so
539 // we delete the last item until none are left in the list. 678 // we delete the last item until none are left in the list.
540 while (!default_client_observers_.empty()) { 679 while (!default_client_observers_.empty()) {
541 delete default_client_observers_.back(); 680 delete default_client_observers_.back();
542 } 681 }
543 } 682 }
544 683
545 // static 684 // static
546 void ProtocolHandlerRegistry::RegisterPrefs(PrefService* pref_service) { 685 void ProtocolHandlerRegistry::RegisterPrefs(PrefService* pref_service) {
547 pref_service->RegisterListPref(prefs::kRegisteredProtocolHandlers, 686 pref_service->RegisterListPref(prefs::kRegisteredProtocolHandlers,
548 PrefService::UNSYNCABLE_PREF); 687 PrefService::UNSYNCABLE_PREF);
549 pref_service->RegisterListPref(prefs::kIgnoredProtocolHandlers, 688 pref_service->RegisterListPref(prefs::kIgnoredProtocolHandlers,
550 PrefService::UNSYNCABLE_PREF); 689 PrefService::UNSYNCABLE_PREF);
551 pref_service->RegisterBooleanPref(prefs::kCustomHandlersEnabled, true, 690 pref_service->RegisterBooleanPref(prefs::kCustomHandlersEnabled, true,
552 PrefService::UNSYNCABLE_PREF); 691 PrefService::UNSYNCABLE_PREF);
553 } 692 }
554 693
555 ProtocolHandlerRegistry::~ProtocolHandlerRegistry() { 694 ProtocolHandlerRegistry::~ProtocolHandlerRegistry() {
556 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 695 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
557 DCHECK(default_client_observers_.empty()); 696 DCHECK(default_client_observers_.empty());
558 } 697 }
559 698
560 void ProtocolHandlerRegistry::PromoteHandler(const ProtocolHandler& handler) { 699 void ProtocolHandlerRegistry::PromoteHandler(const ProtocolHandler& handler) {
561 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 700 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
562 DCHECK(IsRegistered(handler)); 701 DCHECK(IsRegistered(handler));
563 ProtocolHandlerMultiMap::iterator p = 702 ProtocolHandlerMultiMap::iterator p =
564 protocol_handlers_.find(handler.protocol()); 703 protocol_handlers_.find(handler.protocol());
565 ProtocolHandlerList& list = p->second; 704 ProtocolHandlerList& list = p->second;
566 list.erase(std::find(list.begin(), list.end(), handler)); 705 list.erase(std::find(list.begin(), list.end(), handler));
567 list.insert(list.begin(), handler); 706 list.insert(list.begin(), handler);
568 } 707 }
569 708
570 void ProtocolHandlerRegistry::ClearDefaultIO(const std::string& scheme) {
571 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
572 default_handlers_io_.erase(scheme);
573 }
574
575 void ProtocolHandlerRegistry::SetDefaultIO(const ProtocolHandler& handler) {
576 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
577 ClearDefaultIO(handler.protocol());
578 default_handlers_io_.insert(std::make_pair(handler.protocol(), handler));
579 }
580
581 void ProtocolHandlerRegistry::Save() { 709 void ProtocolHandlerRegistry::Save() {
582 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 710 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
583 if (is_loading_) { 711 if (is_loading_) {
584 return; 712 return;
585 } 713 }
586 scoped_ptr<Value> registered_protocol_handlers(EncodeRegisteredHandlers()); 714 scoped_ptr<Value> registered_protocol_handlers(EncodeRegisteredHandlers());
587 scoped_ptr<Value> ignored_protocol_handlers(EncodeIgnoredHandlers()); 715 scoped_ptr<Value> ignored_protocol_handlers(EncodeIgnoredHandlers());
588 scoped_ptr<Value> enabled(Value::CreateBooleanValue(enabled_)); 716 scoped_ptr<Value> enabled(Value::CreateBooleanValue(enabled_));
589 profile_->GetPrefs()->Set(prefs::kRegisteredProtocolHandlers, 717 profile_->GetPrefs()->Set(prefs::kRegisteredProtocolHandlers,
590 *registered_protocol_handlers); 718 *registered_protocol_handlers);
(...skipping 20 matching lines...) Expand all
611 // If we're not loading, and we are setting a default for a new protocol, 739 // If we're not loading, and we are setting a default for a new protocol,
612 // register with the OS. 740 // register with the OS.
613 if (!is_loading_ && p == default_handlers_.end()) 741 if (!is_loading_ && p == default_handlers_.end())
614 delegate_->RegisterWithOSAsDefaultClient(handler.protocol(), this); 742 delegate_->RegisterWithOSAsDefaultClient(handler.protocol(), this);
615 default_handlers_.erase(handler.protocol()); 743 default_handlers_.erase(handler.protocol());
616 default_handlers_.insert(std::make_pair(handler.protocol(), handler)); 744 default_handlers_.insert(std::make_pair(handler.protocol(), handler));
617 PromoteHandler(handler); 745 PromoteHandler(handler);
618 BrowserThread::PostTask( 746 BrowserThread::PostTask(
619 BrowserThread::IO, 747 BrowserThread::IO,
620 FROM_HERE, 748 FROM_HERE,
621 base::Bind(&ProtocolHandlerRegistry::SetDefaultIO, this, handler)); 749 base::Bind(&Core::SetDefault, core_, handler));
622 } 750 }
623 751
624 void ProtocolHandlerRegistry::InsertHandler(const ProtocolHandler& handler) { 752 void ProtocolHandlerRegistry::InsertHandler(const ProtocolHandler& handler) {
625 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 753 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
626 ProtocolHandlerMultiMap::iterator p = 754 ProtocolHandlerMultiMap::iterator p =
627 protocol_handlers_.find(handler.protocol()); 755 protocol_handlers_.find(handler.protocol());
628 756
629 if (p != protocol_handlers_.end()) { 757 if (p != protocol_handlers_.end()) {
630 p->second.push_back(handler); 758 p->second.push_back(handler);
631 return; 759 return;
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
708 } 836 }
709 837
710 void ProtocolHandlerRegistry::IgnoreProtocolHandler( 838 void ProtocolHandlerRegistry::IgnoreProtocolHandler(
711 const ProtocolHandler& handler) { 839 const ProtocolHandler& handler) {
712 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 840 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
713 ignored_protocol_handlers_.push_back(handler); 841 ignored_protocol_handlers_.push_back(handler);
714 } 842 }
715 843
716 void ProtocolHandlerRegistry::AddPredefinedHandler( 844 void ProtocolHandlerRegistry::AddPredefinedHandler(
717 const ProtocolHandler& handler) { 845 const ProtocolHandler& handler) {
718 // If called after the load command was issued this function will fail. 846 DCHECK(!is_loaded_); // Must be called prior InitProtocolSettings.
719 DCHECK(!is_loaded_);
720 RegisterProtocolHandler(handler); 847 RegisterProtocolHandler(handler);
721 SetDefault(handler); 848 SetDefault(handler);
722 } 849 }
723 850
724 851 net::URLRequestJobFactory::Interceptor*
852 ProtocolHandlerRegistry::CreateURLInterceptor() {
853 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
854 // this is always created on the UI thread (in profile_io's
855 // InitializeOnUIThread. Any method calls must be done
856 // on the IO thread (this is checked).
857 return new URLInterceptor(core_);
858 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698