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

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: Fix merge issue (correctly) in PHR test. Created 8 years, 6 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.
willchan no longer on Chromium 2012/06/22 23:15:44 s/only/Only/
smckay 2012/06/22 23:44:31 Done.
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
87 public:
88
89 explicit Core(bool);
willchan no longer on Chromium 2012/06/22 23:15:44 please name the variable
smckay 2012/06/22 23:44:31 Done.
90
91 // Returns true if the protocol has a default protocol handler.
92 // Should be called only from the IO thread.
93 bool IsHandledProtocol(const std::string& scheme) const;
94
95 // Clears the default for the provided protocol.
96 // Should be called only from the IO thread.
97 void ClearDefault(const std::string& scheme);
98
99 // Makes this ProtocolHandler the default handler for its protocol.
100 // Should be called only from the IO thread.
101 void SetDefault(const ProtocolHandler& handler);
102
103 // Creates a URL request job for the given request if there is a matching
104 // protocol handler, returns NULL otherwise.
105 net::URLRequestJob* MaybeCreateJob(net::URLRequest* request) const;
106
107 // Indicate that the registry has been enabled in the IO thread's
108 // copy of the data.
109 void Enable() { enabled_ = true; }
110
111 // Indicate that the registry has been disabled in the IO thread's copy of
112 // the data.
113 void Disable() { enabled_ = false; }
114
115 private:
116 friend class base::RefCountedThreadSafe<Core>;
117 virtual ~Core();
118
119 // Copy of default_handlers_ that is only accessed on the IO thread.
120 ProtocolHandlerRegistry::ProtocolHandlerMap default_handlers_;
121 // Copy of enabled_ that is only accessed on the IO thread.
122 bool enabled_;
123
124 DISALLOW_COPY_AND_ASSIGN(Core);
125 };
126
127 ProtocolHandlerRegistry::Core::Core(bool) : enabled_(true) {}
128 ProtocolHandlerRegistry::Core::~Core() {}
129
130 bool ProtocolHandlerRegistry::Core::IsHandledProtocol(
131 const std::string& scheme) const {
132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
133 return enabled_ && !LookupHandler(default_handlers_, scheme).IsEmpty();
134 }
135
136 void ProtocolHandlerRegistry::Core::ClearDefault(const std::string& scheme) {
137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
138 default_handlers_.erase(scheme);
139 }
140
141 void ProtocolHandlerRegistry::Core::SetDefault(const ProtocolHandler& handler) {
142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
143 ClearDefault(handler.protocol());
144 default_handlers_.insert(std::make_pair(handler.protocol(), handler));
145 }
146
147 net::URLRequestJob* ProtocolHandlerRegistry::Core::MaybeCreateJob(
148 net::URLRequest* request) const {
149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
150
151 ProtocolHandler handler = LookupHandler(default_handlers_,
152 request->url().scheme());
153 if (handler.IsEmpty()) {
154 return NULL;
56 } 155 }
57 return ProtocolHandler::EmptyProtocolHandler(); 156 GURL translated_url(handler.TranslateUrl(request->url()));
157 if (!translated_url.is_valid()) {
158 return NULL;
159 }
160 return new net::URLRequestRedirectJob(request, translated_url);
161 }
162
163 // IOURLInterceptor ------------------------------------------------------------
164
165 // Instances of this class are produced for ownership by the IO
166 // thread where it handler URL requests. We should never hold
167 // any pointers on this class, only produce them in response to
168 // requests via |ProtocolHandlerRegistry::CreateIOURLInterceptor|.
169 class ProtocolHandlerRegistry::IOURLInterceptor
170 : public net::URLRequestJobFactory::Interceptor {
171 public:
172 explicit IOURLInterceptor(Core* core);
173 virtual ~IOURLInterceptor();
174
175 virtual net::URLRequestJob* MaybeIntercept(
176 net::URLRequest* request) const OVERRIDE;
177
178 virtual bool WillHandleProtocol(const std::string& protocol) const OVERRIDE;
179
180 virtual net::URLRequestJob* MaybeInterceptRedirect(
181 const GURL& url, net::URLRequest* request) const OVERRIDE {
182 return NULL;
183 }
184
185 virtual net::URLRequestJob* MaybeInterceptResponse(
186 net::URLRequest* request) const OVERRIDE {
187 return NULL;
188 }
189
190 private:
191 scoped_refptr<Core> core_;
192 DISALLOW_COPY_AND_ASSIGN(IOURLInterceptor);
193 };
194
195 ProtocolHandlerRegistry::IOURLInterceptor::IOURLInterceptor(Core* core)
196 : core_(core) {
197 DCHECK(core_);
198 }
199
200 ProtocolHandlerRegistry::IOURLInterceptor::~IOURLInterceptor() {
201 // TODO(smckay): release what ever type of pointer we have
willchan no longer on Chromium 2012/06/22 23:15:44 ?
smckay 2012/06/22 23:44:31 Didn't know if I had to do something special to de
202 // on the C instance.
203 }
204
205 net::URLRequestJob* ProtocolHandlerRegistry::IOURLInterceptor::MaybeIntercept(
206 net::URLRequest* request) const {
207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
208
209 return core_->MaybeCreateJob(request);
210 }
211
212 bool ProtocolHandlerRegistry::IOURLInterceptor::WillHandleProtocol(
213 const std::string& protocol) const {
214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
215
216 return core_->IsHandledProtocol(protocol);
58 } 217 }
59 218
60 // DefaultClientObserver ------------------------------------------------------ 219 // DefaultClientObserver ------------------------------------------------------
61 220
62 ProtocolHandlerRegistry::DefaultClientObserver::DefaultClientObserver( 221 ProtocolHandlerRegistry::DefaultClientObserver::DefaultClientObserver(
63 ProtocolHandlerRegistry* registry) 222 ProtocolHandlerRegistry* registry)
64 : worker_(NULL), 223 : worker_(NULL),
65 registry_(registry) { 224 registry_(registry) {
66 DCHECK(registry_); 225 DCHECK(registry_);
67 } 226 }
68 227
69 ProtocolHandlerRegistry::DefaultClientObserver::~DefaultClientObserver() { 228 ProtocolHandlerRegistry::DefaultClientObserver::~DefaultClientObserver() {
70 if (worker_) 229 if (worker_)
71 worker_->ObserverDestroyed(); 230 worker_->ObserverDestroyed();
72 231
73 DefaultClientObserverList::iterator iter = std::find( 232 DefaultClientObserverList::iterator iter = std::find(
74 registry_->default_client_observers_.begin(), 233 registry_->default_client_observers_.begin(),
75 registry_->default_client_observers_.end(), this); 234 registry_->default_client_observers_.end(), this);
76 registry_->default_client_observers_.erase(iter); 235 registry_->default_client_observers_.erase(iter);
77 } 236 }
78 237
79 void 238 void ProtocolHandlerRegistry::DefaultClientObserver::SetDefaultWebClientUIState(
80 ProtocolHandlerRegistry::DefaultClientObserver::SetDefaultWebClientUIState(
81 ShellIntegration::DefaultWebClientUIState state) { 239 ShellIntegration::DefaultWebClientUIState state) {
82 if (worker_) { 240 if (worker_) {
83 if (ShouldRemoveHandlersNotInOS() && 241 if (ShouldRemoveHandlersNotInOS() &&
84 (state == ShellIntegration::STATE_NOT_DEFAULT)) { 242 (state == ShellIntegration::STATE_NOT_DEFAULT)) {
85 registry_->ClearDefault(worker_->protocol()); 243 registry_->ClearDefault(worker_->protocol());
86 } 244 }
87 } else { 245 } else {
88 NOTREACHED(); 246 NOTREACHED();
89 } 247 }
90 } 248 }
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 worker->StartSetAsDefault(); 302 worker->StartSetAsDefault();
145 } 303 }
146 304
147 // ProtocolHandlerRegistry ----------------------------------------------------- 305 // ProtocolHandlerRegistry -----------------------------------------------------
148 306
149 ProtocolHandlerRegistry::ProtocolHandlerRegistry(Profile* profile, 307 ProtocolHandlerRegistry::ProtocolHandlerRegistry(Profile* profile,
150 Delegate* delegate) 308 Delegate* delegate)
151 : profile_(profile), 309 : profile_(profile),
152 delegate_(delegate), 310 delegate_(delegate),
153 enabled_(true), 311 enabled_(true),
154 enabled_io_(enabled_),
155 is_loading_(false), 312 is_loading_(false),
156 is_loaded_(false) { 313 is_loaded_(false),
314 core_(new Core(enabled_)){
157 } 315 }
158 316
159 bool ProtocolHandlerRegistry::SilentlyHandleRegisterHandlerRequest( 317 bool ProtocolHandlerRegistry::SilentlyHandleRegisterHandlerRequest(
160 const ProtocolHandler& handler) { 318 const ProtocolHandler& handler) {
161 if (handler.IsEmpty() || !CanSchemeBeOverridden(handler.protocol())) 319 if (handler.IsEmpty() || !CanSchemeBeOverridden(handler.protocol()))
162 return true; 320 return true;
163 321
164 if (!enabled() || IsRegistered(handler) || HasIgnoredEquivalent(handler)) 322 if (!enabled() || IsRegistered(handler) || HasIgnoredEquivalent(handler))
165 return true; 323 return true;
166 324
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 p != handlers->end(); p++) { 384 p != handlers->end(); p++) {
227 if (handler.IsSameOrigin(*p)) { 385 if (handler.IsSameOrigin(*p)) {
228 replaced_handlers.push_back(*p); 386 replaced_handlers.push_back(*p);
229 } 387 }
230 } 388 }
231 return replaced_handlers; 389 return replaced_handlers;
232 } 390 }
233 391
234 void ProtocolHandlerRegistry::ClearDefault(const std::string& scheme) { 392 void ProtocolHandlerRegistry::ClearDefault(const std::string& scheme) {
235 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 393 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
394
236 default_handlers_.erase(scheme); 395 default_handlers_.erase(scheme);
237 BrowserThread::PostTask( 396 BrowserThread::PostTask(
238 BrowserThread::IO, 397 BrowserThread::IO,
239 FROM_HERE, 398 FROM_HERE,
240 base::Bind(&ProtocolHandlerRegistry::ClearDefaultIO, this, scheme)); 399 base::Bind(&Core::ClearDefault, core_, scheme));
241 Save(); 400 Save();
242 NotifyChanged(); 401 NotifyChanged();
243 } 402 }
244 403
245 bool ProtocolHandlerRegistry::IsDefault( 404 bool ProtocolHandlerRegistry::IsDefault(
246 const ProtocolHandler& handler) const { 405 const ProtocolHandler& handler) const {
247 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 406 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
248 return GetHandlerFor(handler.protocol()) == handler; 407 return GetHandlerFor(handler.protocol()) == handler;
249 } 408 }
250 409
251 void ProtocolHandlerRegistry::Load() { 410 void ProtocolHandlerRegistry::InitProtocolSettings() {
411 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
412
413 // Install predefined protocol handlers.
414 InstallDefaultProtocolHandlers(this);
415
252 // Any further default additions to the table will get rejected from now on. 416 // Any further default additions to the table will get rejected from now on.
253 is_loaded_ = true; 417 is_loaded_ = true;
254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
255 is_loading_ = true; 418 is_loading_ = true;
419
256 PrefService* prefs = profile_->GetPrefs(); 420 PrefService* prefs = profile_->GetPrefs();
257 if (prefs->HasPrefPath(prefs::kCustomHandlersEnabled)) { 421 if (prefs->HasPrefPath(prefs::kCustomHandlersEnabled)) {
258 enabled_ = prefs->GetBoolean(prefs::kCustomHandlersEnabled); 422 if (prefs->GetBoolean(prefs::kCustomHandlersEnabled)) {
259 BrowserThread::PostTask( 423 Enable();
260 BrowserThread::IO, 424 } else {
261 FROM_HERE, 425 Disable();
262 base::Bind(enabled_ ? &ProtocolHandlerRegistry::EnableIO : 426 }
263 &ProtocolHandlerRegistry::DisableIO, this));
264 } 427 }
265 std::vector<const DictionaryValue*> registered_handlers = 428 std::vector<const DictionaryValue*> registered_handlers =
266 GetHandlersFromPref(prefs::kRegisteredProtocolHandlers); 429 GetHandlersFromPref(prefs::kRegisteredProtocolHandlers);
267 for (std::vector<const DictionaryValue*>::const_iterator p = 430 for (std::vector<const DictionaryValue*>::const_iterator p =
268 registered_handlers.begin(); 431 registered_handlers.begin();
269 p != registered_handlers.end(); ++p) { 432 p != registered_handlers.end(); ++p) {
270 ProtocolHandler handler = ProtocolHandler::CreateProtocolHandler(*p); 433 ProtocolHandler handler = ProtocolHandler::CreateProtocolHandler(*p);
271 RegisterProtocolHandler(handler); 434 RegisterProtocolHandler(handler);
272 bool is_default = false; 435 bool is_default = false;
273 if ((*p)->GetBoolean("default", &is_default) && is_default) { 436 if ((*p)->GetBoolean("default", &is_default) && is_default) {
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 if (should_notify) 584 if (should_notify)
422 NotifyChanged(); 585 NotifyChanged();
423 } 586 }
424 587
425 bool ProtocolHandlerRegistry::IsHandledProtocol( 588 bool ProtocolHandlerRegistry::IsHandledProtocol(
426 const std::string& scheme) const { 589 const std::string& scheme) const {
427 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 590 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
428 return enabled_ && !GetHandlerFor(scheme).IsEmpty(); 591 return enabled_ && !GetHandlerFor(scheme).IsEmpty();
429 } 592 }
430 593
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( 594 void ProtocolHandlerRegistry::RemoveHandler(
438 const ProtocolHandler& handler) { 595 const ProtocolHandler& handler) {
439 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 596 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
440 ProtocolHandlerList& handlers = protocol_handlers_[handler.protocol()]; 597 ProtocolHandlerList& handlers = protocol_handlers_[handler.protocol()];
441 ProtocolHandlerList::iterator p = 598 ProtocolHandlerList::iterator p =
442 std::find(handlers.begin(), handlers.end(), handler); 599 std::find(handlers.begin(), handlers.end(), handler);
443 if (p != handlers.end()) { 600 if (p != handlers.end()) {
444 handlers.erase(p); 601 handlers.erase(p);
445 } 602 }
446 ProtocolHandlerMap::iterator q = default_handlers_.find(handler.protocol()); 603 ProtocolHandlerMap::iterator q = default_handlers_.find(handler.protocol());
447 if (q != default_handlers_.end() && q->second == handler) { 604 if (q != default_handlers_.end() && q->second == handler) {
448 // Make the new top handler in the list the default. 605 // Make the new top handler in the list the default.
449 if (!handlers.empty()) { 606 if (!handlers.empty()) {
450 // NOTE We pass a copy because SetDefault() modifies handlers. 607 // NOTE We pass a copy because SetDefault() modifies handlers.
451 SetDefault(ProtocolHandler(handlers[0])); 608 SetDefault(ProtocolHandler(handlers[0]));
452 } else { 609 } else {
453 BrowserThread::PostTask( 610 BrowserThread::PostTask(
454 BrowserThread::IO, FROM_HERE, 611 BrowserThread::IO, FROM_HERE,
455 base::Bind(&ProtocolHandlerRegistry::ClearDefaultIO, this, 612 base::Bind(&Core::ClearDefault, core_, q->second.protocol()));
456 q->second.protocol())); 613
457 default_handlers_.erase(q); 614 default_handlers_.erase(q);
458 } 615 }
459 } 616 }
460 617
461 if (!IsHandledProtocol(handler.protocol())) { 618 if (!IsHandledProtocol(handler.protocol())) {
462 delegate_->DeregisterExternalHandler(handler.protocol()); 619 delegate_->DeregisterExternalHandler(handler.protocol());
463 } 620 }
464 Save(); 621 Save();
465 NotifyChanged(); 622 NotifyChanged();
466 } 623 }
467 624
468 void ProtocolHandlerRegistry::RemoveDefaultHandler(const std::string& scheme) { 625 void ProtocolHandlerRegistry::RemoveDefaultHandler(const std::string& scheme) {
469 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 626 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
470 ProtocolHandler current_default = GetHandlerFor(scheme); 627 ProtocolHandler current_default = GetHandlerFor(scheme);
471 if (!current_default.IsEmpty()) 628 if (!current_default.IsEmpty())
472 RemoveHandler(current_default); 629 RemoveHandler(current_default);
473 } 630 }
474 631
475 const ProtocolHandler& ProtocolHandlerRegistry::GetHandlerFor( 632 const ProtocolHandler& ProtocolHandlerRegistry::GetHandlerFor(
476 const std::string& scheme) const { 633 const std::string& scheme) const {
477 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 634 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
478 return LookupHandler(default_handlers_, scheme); 635 return LookupHandler(default_handlers_, scheme);
479 } 636 }
480 637
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() { 638 void ProtocolHandlerRegistry::Enable() {
497 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 639 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
498 if (enabled_) { 640 if (enabled_) {
499 return; 641 return;
500 } 642 }
501 enabled_ = true; 643 enabled_ = true;
502 BrowserThread::PostTask( 644 BrowserThread::PostTask(
503 BrowserThread::IO, 645 BrowserThread::IO,
504 FROM_HERE, 646 FROM_HERE,
505 base::Bind(&ProtocolHandlerRegistry::EnableIO, this)); 647 base::Bind(&Core::Enable, core_));
648
506 ProtocolHandlerMap::const_iterator p; 649 ProtocolHandlerMap::const_iterator p;
507 for (p = default_handlers_.begin(); p != default_handlers_.end(); ++p) { 650 for (p = default_handlers_.begin(); p != default_handlers_.end(); ++p) {
508 delegate_->RegisterExternalHandler(p->first); 651 delegate_->RegisterExternalHandler(p->first);
509 } 652 }
510 Save(); 653 Save();
511 NotifyChanged(); 654 NotifyChanged();
512 } 655 }
513 656
514 void ProtocolHandlerRegistry::Disable() { 657 void ProtocolHandlerRegistry::Disable() {
515 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 658 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
516 if (!enabled_) { 659 if (!enabled_) {
517 return; 660 return;
518 } 661 }
519 enabled_ = false; 662 enabled_ = false;
520 BrowserThread::PostTask( 663 BrowserThread::PostTask(
521 BrowserThread::IO, 664 BrowserThread::IO,
522 FROM_HERE, 665 FROM_HERE,
523 base::Bind(&ProtocolHandlerRegistry::DisableIO, this)); 666 base::Bind(&Core::Disable, core_));
667
524 ProtocolHandlerMap::const_iterator p; 668 ProtocolHandlerMap::const_iterator p;
525 for (p = default_handlers_.begin(); p != default_handlers_.end(); ++p) { 669 for (p = default_handlers_.begin(); p != default_handlers_.end(); ++p) {
526 delegate_->DeregisterExternalHandler(p->first); 670 delegate_->DeregisterExternalHandler(p->first);
527 } 671 }
528 Save(); 672 Save();
529 NotifyChanged(); 673 NotifyChanged();
530 } 674 }
531 675
532 void ProtocolHandlerRegistry::Finalize() { 676 void ProtocolHandlerRegistry::Shutdown() {
533 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 677 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
534 delegate_.reset(NULL); 678 delegate_.reset(NULL);
535 // We free these now in case there are any outstanding workers running. If 679 // 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 680 // we didn't free them they could respond to workers and try to update the
537 // protocol handler registry after it was deleted. 681 // protocol handler registry after it was deleted.
538 // Observers remove themselves from this list when they are deleted; so 682 // Observers remove themselves from this list when they are deleted; so
539 // we delete the last item until none are left in the list. 683 // we delete the last item until none are left in the list.
540 while (!default_client_observers_.empty()) { 684 while (!default_client_observers_.empty()) {
541 delete default_client_observers_.back(); 685 delete default_client_observers_.back();
542 } 686 }
543 } 687 }
544 688
545 // static 689 // static
546 void ProtocolHandlerRegistry::RegisterPrefs(PrefService* pref_service) { 690 void ProtocolHandlerRegistry::RegisterPrefs(PrefService* pref_service) {
547 pref_service->RegisterListPref(prefs::kRegisteredProtocolHandlers, 691 pref_service->RegisterListPref(prefs::kRegisteredProtocolHandlers,
548 PrefService::UNSYNCABLE_PREF); 692 PrefService::UNSYNCABLE_PREF);
549 pref_service->RegisterListPref(prefs::kIgnoredProtocolHandlers, 693 pref_service->RegisterListPref(prefs::kIgnoredProtocolHandlers,
550 PrefService::UNSYNCABLE_PREF); 694 PrefService::UNSYNCABLE_PREF);
551 pref_service->RegisterBooleanPref(prefs::kCustomHandlersEnabled, true, 695 pref_service->RegisterBooleanPref(prefs::kCustomHandlersEnabled, true,
552 PrefService::UNSYNCABLE_PREF); 696 PrefService::UNSYNCABLE_PREF);
553 } 697 }
554 698
555 ProtocolHandlerRegistry::~ProtocolHandlerRegistry() { 699 ProtocolHandlerRegistry::~ProtocolHandlerRegistry() {
556 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 700 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
557 DCHECK(default_client_observers_.empty()); 701 DCHECK(default_client_observers_.empty());
558 } 702 }
559 703
560 void ProtocolHandlerRegistry::PromoteHandler(const ProtocolHandler& handler) { 704 void ProtocolHandlerRegistry::PromoteHandler(const ProtocolHandler& handler) {
561 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 705 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
562 DCHECK(IsRegistered(handler)); 706 DCHECK(IsRegistered(handler));
563 ProtocolHandlerMultiMap::iterator p = 707 ProtocolHandlerMultiMap::iterator p =
564 protocol_handlers_.find(handler.protocol()); 708 protocol_handlers_.find(handler.protocol());
565 ProtocolHandlerList& list = p->second; 709 ProtocolHandlerList& list = p->second;
566 list.erase(std::find(list.begin(), list.end(), handler)); 710 list.erase(std::find(list.begin(), list.end(), handler));
567 list.insert(list.begin(), handler); 711 list.insert(list.begin(), handler);
568 } 712 }
569 713
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() { 714 void ProtocolHandlerRegistry::Save() {
582 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 715 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
583 if (is_loading_) { 716 if (is_loading_) {
584 return; 717 return;
585 } 718 }
586 scoped_ptr<Value> registered_protocol_handlers(EncodeRegisteredHandlers()); 719 scoped_ptr<Value> registered_protocol_handlers(EncodeRegisteredHandlers());
587 scoped_ptr<Value> ignored_protocol_handlers(EncodeIgnoredHandlers()); 720 scoped_ptr<Value> ignored_protocol_handlers(EncodeIgnoredHandlers());
588 scoped_ptr<Value> enabled(Value::CreateBooleanValue(enabled_)); 721 scoped_ptr<Value> enabled(Value::CreateBooleanValue(enabled_));
589 profile_->GetPrefs()->Set(prefs::kRegisteredProtocolHandlers, 722 profile_->GetPrefs()->Set(prefs::kRegisteredProtocolHandlers,
590 *registered_protocol_handlers); 723 *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, 744 // If we're not loading, and we are setting a default for a new protocol,
612 // register with the OS. 745 // register with the OS.
613 if (!is_loading_ && p == default_handlers_.end()) 746 if (!is_loading_ && p == default_handlers_.end())
614 delegate_->RegisterWithOSAsDefaultClient(handler.protocol(), this); 747 delegate_->RegisterWithOSAsDefaultClient(handler.protocol(), this);
615 default_handlers_.erase(handler.protocol()); 748 default_handlers_.erase(handler.protocol());
616 default_handlers_.insert(std::make_pair(handler.protocol(), handler)); 749 default_handlers_.insert(std::make_pair(handler.protocol(), handler));
617 PromoteHandler(handler); 750 PromoteHandler(handler);
618 BrowserThread::PostTask( 751 BrowserThread::PostTask(
619 BrowserThread::IO, 752 BrowserThread::IO,
620 FROM_HERE, 753 FROM_HERE,
621 base::Bind(&ProtocolHandlerRegistry::SetDefaultIO, this, handler)); 754 base::Bind(&Core::SetDefault, core_, handler));
622 } 755 }
623 756
624 void ProtocolHandlerRegistry::InsertHandler(const ProtocolHandler& handler) { 757 void ProtocolHandlerRegistry::InsertHandler(const ProtocolHandler& handler) {
625 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 758 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
626 ProtocolHandlerMultiMap::iterator p = 759 ProtocolHandlerMultiMap::iterator p =
627 protocol_handlers_.find(handler.protocol()); 760 protocol_handlers_.find(handler.protocol());
628 761
629 if (p != protocol_handlers_.end()) { 762 if (p != protocol_handlers_.end()) {
630 p->second.push_back(handler); 763 p->second.push_back(handler);
631 return; 764 return;
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
714 } 847 }
715 848
716 void ProtocolHandlerRegistry::AddPredefinedHandler( 849 void ProtocolHandlerRegistry::AddPredefinedHandler(
717 const ProtocolHandler& handler) { 850 const ProtocolHandler& handler) {
718 // If called after the load command was issued this function will fail. 851 // If called after the load command was issued this function will fail.
719 DCHECK(!is_loaded_); 852 DCHECK(!is_loaded_);
720 RegisterProtocolHandler(handler); 853 RegisterProtocolHandler(handler);
721 SetDefault(handler); 854 SetDefault(handler);
722 } 855 }
723 856
724 857 net::URLRequestJobFactory::Interceptor*
858 ProtocolHandlerRegistry::CreateIOURLInterceptor() {
859 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
860 // this is always created on the UI thread (in profile_io's
861 // InitializeOnUIThread. Any method calls must be done
862 // on the IO thread (this is checked).
863 return new IOURLInterceptor(core_);
864 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698