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

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

Issue 293983004: Modify ProtocolHandlerRegistry to make room for policy (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 7 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"
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 329
330 if (AttemptReplace(handler)) 330 if (AttemptReplace(handler))
331 return true; 331 return true;
332 332
333 return false; 333 return false;
334 } 334 }
335 335
336 void ProtocolHandlerRegistry::OnAcceptRegisterProtocolHandler( 336 void ProtocolHandlerRegistry::OnAcceptRegisterProtocolHandler(
337 const ProtocolHandler& handler) { 337 const ProtocolHandler& handler) {
338 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 338 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
339 RegisterProtocolHandler(handler); 339 RegisterProtocolHandler(handler, false);
340 SetDefault(handler); 340 SetDefault(handler);
341 Save(); 341 Save();
342 NotifyChanged(); 342 NotifyChanged();
343 } 343 }
344 344
345 void ProtocolHandlerRegistry::OnDenyRegisterProtocolHandler( 345 void ProtocolHandlerRegistry::OnDenyRegisterProtocolHandler(
346 const ProtocolHandler& handler) { 346 const ProtocolHandler& handler) {
347 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 347 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
348 RegisterProtocolHandler(handler); 348 RegisterProtocolHandler(handler, false);
349 Save(); 349 Save();
350 NotifyChanged(); 350 NotifyChanged();
351 } 351 }
352 352
353 void ProtocolHandlerRegistry::OnIgnoreRegisterProtocolHandler( 353 void ProtocolHandlerRegistry::OnIgnoreRegisterProtocolHandler(
354 const ProtocolHandler& handler) { 354 const ProtocolHandler& handler) {
355 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 355 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
356 IgnoreProtocolHandler(handler); 356 IgnoreProtocolHandler(handler, false);
357 Save(); 357 Save();
358 NotifyChanged(); 358 NotifyChanged();
359 } 359 }
360 360
361 bool ProtocolHandlerRegistry::AttemptReplace(const ProtocolHandler& handler) { 361 bool ProtocolHandlerRegistry::AttemptReplace(const ProtocolHandler& handler) {
362 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 362 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
363 ProtocolHandler old_default = GetHandlerFor(handler.protocol()); 363 ProtocolHandler old_default = GetHandlerFor(handler.protocol());
364 bool make_new_handler_default = handler.IsSameOrigin(old_default); 364 bool make_new_handler_default = handler.IsSameOrigin(old_default);
365 ProtocolHandlerList to_replace(GetReplacedHandlers(handler)); 365 ProtocolHandlerList to_replace(GetReplacedHandlers(handler));
366 if (to_replace.empty()) 366 if (to_replace.empty())
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 is_loading_ = true; 436 is_loading_ = true;
437 437
438 PrefService* prefs = profile_->GetPrefs(); 438 PrefService* prefs = profile_->GetPrefs();
439 if (prefs->HasPrefPath(prefs::kCustomHandlersEnabled)) { 439 if (prefs->HasPrefPath(prefs::kCustomHandlersEnabled)) {
440 if (prefs->GetBoolean(prefs::kCustomHandlersEnabled)) { 440 if (prefs->GetBoolean(prefs::kCustomHandlersEnabled)) {
441 Enable(); 441 Enable();
442 } else { 442 } else {
443 Disable(); 443 Disable();
444 } 444 }
445 } 445 }
446 std::vector<const base::DictionaryValue*> registered_handlers = 446 std::vector<const base::DictionaryValue*> registered_handlers =
koz (OOO until 15th September) 2014/05/22 00:51:31 It might be worth introducing some helper function
kaliamoorthi 2014/05/30 10:01:09 Done.
447 GetHandlersFromPref(prefs::kRegisteredProtocolHandlers); 447 GetHandlersFromPref(prefs::kPolicyRegisteredProtocolHandlers);
448 for (std::vector<const base::DictionaryValue*>::const_iterator p = 448 for (std::vector<const base::DictionaryValue*>::const_iterator p =
449 registered_handlers.begin(); 449 registered_handlers.begin();
450 p != registered_handlers.end(); ++p) { 450 p != registered_handlers.end(); ++p) {
451 ProtocolHandler handler = ProtocolHandler::CreateProtocolHandler(*p); 451 ProtocolHandler handler = ProtocolHandler::CreateProtocolHandler(*p);
452 RegisterProtocolHandler(handler); 452 RegisterProtocolHandler(handler, true);
453 bool is_default = false; 453 bool is_default = false;
454 if ((*p)->GetBoolean("default", &is_default) && is_default) { 454 if ((*p)->GetBoolean("default", &is_default) && is_default) {
455 SetDefault(handler); 455 SetDefault(handler);
456 } 456 }
457 } 457 }
458 std::vector<const base::DictionaryValue*> ignored_handlers = 458 std::vector<const base::DictionaryValue*> ignored_handlers =
459 GetHandlersFromPref(prefs::kIgnoredProtocolHandlers); 459 GetHandlersFromPref(prefs::kPolicyIgnoredProtocolHandlers);
460 for (std::vector<const base::DictionaryValue*>::const_iterator p = 460 for (std::vector<const base::DictionaryValue*>::const_iterator p =
461 ignored_handlers.begin(); 461 ignored_handlers.begin();
462 p != ignored_handlers.end(); ++p) { 462 p != ignored_handlers.end(); ++p) {
463 IgnoreProtocolHandler(ProtocolHandler::CreateProtocolHandler(*p)); 463 IgnoreProtocolHandler(ProtocolHandler::CreateProtocolHandler(*p), true);
464 }
465
466 registered_handlers = GetHandlersFromPref(prefs::kRegisteredProtocolHandlers);
467 for (std::vector<const base::DictionaryValue*>::const_iterator p =
468 registered_handlers.begin();
469 p != registered_handlers.end();
470 ++p) {
471 ProtocolHandler handler = ProtocolHandler::CreateProtocolHandler(*p);
472 RegisterProtocolHandler(handler, false);
473 bool is_default = false;
474 if ((*p)->GetBoolean("default", &is_default) && is_default) {
475 SetDefault(handler);
476 }
477 }
478 ignored_handlers = GetHandlersFromPref(prefs::kIgnoredProtocolHandlers);
479 for (std::vector<const base::DictionaryValue*>::const_iterator p =
480 ignored_handlers.begin();
481 p != ignored_handlers.end();
482 ++p) {
483 IgnoreProtocolHandler(ProtocolHandler::CreateProtocolHandler(*p), false);
464 } 484 }
465 is_loading_ = false; 485 is_loading_ = false;
466 486
467 // For each default protocol handler, check that we are still registered 487 // For each default protocol handler, check that we are still registered
468 // with the OS as the default application. 488 // with the OS as the default application.
469 if (ShouldRemoveHandlersNotInOS()) { 489 if (ShouldRemoveHandlersNotInOS()) {
470 for (ProtocolHandlerMap::const_iterator p = default_handlers_.begin(); 490 for (ProtocolHandlerMap::const_iterator p = default_handlers_.begin();
471 p != default_handlers_.end(); ++p) { 491 p != default_handlers_.end(); ++p) {
472 ProtocolHandler handler = p->second; 492 ProtocolHandler handler = p->second;
473 DefaultClientObserver* observer = delegate_->CreateShellObserver(this); 493 DefaultClientObserver* observer = delegate_->CreateShellObserver(this);
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 return true; 604 return true;
585 } 605 }
586 } 606 }
587 return false; 607 return false;
588 } 608 }
589 609
590 void ProtocolHandlerRegistry::RemoveIgnoredHandler( 610 void ProtocolHandlerRegistry::RemoveIgnoredHandler(
591 const ProtocolHandler& handler) { 611 const ProtocolHandler& handler) {
592 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 612 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
593 bool should_notify = false; 613 bool should_notify = false;
594 ProtocolHandlerList::iterator p = std::find( 614 if (HandlerExists(handler, ignored_protocol_handlers_)) {
595 ignored_protocol_handlers_.begin(), ignored_protocol_handlers_.end(), 615 if (HandlerExists(handler, user_ignored_protocol_handlers_)) {
596 handler); 616 EraseHandler(handler, &user_ignored_protocol_handlers_);
597 if (p != ignored_protocol_handlers_.end()) { 617 Save();
koz (OOO until 15th September) 2014/05/22 00:51:31 Save() should be called after removing handler fro
kaliamoorthi 2014/05/30 10:01:09 Please note that only user_ignored_protocol_handle
koz (OOO until 15th September) 2014/06/01 23:44:28 Ah, my bad. In that case feel free to revert to ho
598 ignored_protocol_handlers_.erase(p); 618 if (!HandlerExists(handler, policy_ignored_protocol_handlers_)) {
599 Save(); 619 EraseHandler(handler, &ignored_protocol_handlers_);
600 should_notify = true; 620 should_notify = true;
621 }
622 }
601 } 623 }
602 if (should_notify) 624 if (should_notify)
603 NotifyChanged(); 625 NotifyChanged();
604 } 626 }
605 627
606 bool ProtocolHandlerRegistry::IsHandledProtocol( 628 bool ProtocolHandlerRegistry::IsHandledProtocol(
607 const std::string& scheme) const { 629 const std::string& scheme) const {
608 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 630 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
609 return enabled_ && !GetHandlerFor(scheme).IsEmpty(); 631 return enabled_ && !GetHandlerFor(scheme).IsEmpty();
610 } 632 }
611 633
612 void ProtocolHandlerRegistry::RemoveHandler( 634 void ProtocolHandlerRegistry::RemoveHandler(
613 const ProtocolHandler& handler) { 635 const ProtocolHandler& handler) {
614 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 636 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
615 ProtocolHandlerList& handlers = protocol_handlers_[handler.protocol()]; 637 ProtocolHandlerList& handlers = protocol_handlers_[handler.protocol()];
616 ProtocolHandlerList::iterator p = 638 bool erase_success = false;
617 std::find(handlers.begin(), handlers.end(), handler); 639 if (HandlerExists(handler, handlers)) {
618 if (p != handlers.end()) { 640 if (HandlerExists(handler, &user_protocol_handlers_)) {
619 handlers.erase(p); 641 EraseHandler(handler, &user_protocol_handlers_);
642 if (!HandlerExists(handler, &policy_protocol_handlers_)) {
643 erase_success = true;
644 EraseHandler(handler, &protocol_handlers_);
645 }
646 }
620 } 647 }
621 ProtocolHandlerMap::iterator q = default_handlers_.find(handler.protocol()); 648 ProtocolHandlerMap::iterator q = default_handlers_.find(handler.protocol());
622 if (q != default_handlers_.end() && q->second == handler) { 649 if (erase_success && q != default_handlers_.end() && q->second == handler) {
623 // Make the new top handler in the list the default. 650 // Make the new top handler in the list the default.
624 if (!handlers.empty()) { 651 if (!handlers.empty()) {
625 // NOTE We pass a copy because SetDefault() modifies handlers. 652 // NOTE We pass a copy because SetDefault() modifies handlers.
626 SetDefault(ProtocolHandler(handlers[0])); 653 SetDefault(ProtocolHandler(handlers[0]));
627 } else { 654 } else {
628 BrowserThread::PostTask( 655 BrowserThread::PostTask(
629 BrowserThread::IO, FROM_HERE, 656 BrowserThread::IO, FROM_HERE,
630 base::Bind(&IOThreadDelegate::ClearDefault, io_thread_delegate_, 657 base::Bind(&IOThreadDelegate::ClearDefault, io_thread_delegate_,
631 q->second.protocol())); 658 q->second.protocol()));
632 659
633 default_handlers_.erase(q); 660 default_handlers_.erase(q);
634 } 661 }
635 } 662 }
636 663
637 if (!IsHandledProtocol(handler.protocol())) { 664 if (erase_success && !IsHandledProtocol(handler.protocol())) {
638 delegate_->DeregisterExternalHandler(handler.protocol()); 665 delegate_->DeregisterExternalHandler(handler.protocol());
639 } 666 }
640 Save(); 667 Save();
641 NotifyChanged(); 668 if (erase_success)
669 NotifyChanged();
642 } 670 }
643 671
644 void ProtocolHandlerRegistry::RemoveDefaultHandler(const std::string& scheme) { 672 void ProtocolHandlerRegistry::RemoveDefaultHandler(const std::string& scheme) {
645 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 673 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
646 ProtocolHandler current_default = GetHandlerFor(scheme); 674 ProtocolHandler current_default = GetHandlerFor(scheme);
647 if (!current_default.IsEmpty()) 675 if (!current_default.IsEmpty())
648 RemoveHandler(current_default); 676 RemoveHandler(current_default);
649 } 677 }
650 678
651 const ProtocolHandler& ProtocolHandlerRegistry::GetHandlerFor( 679 const ProtocolHandler& ProtocolHandlerRegistry::GetHandlerFor(
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
788 } 816 }
789 817
790 ProtocolHandlerList new_list; 818 ProtocolHandlerList new_list;
791 new_list.push_back(handler); 819 new_list.push_back(handler);
792 protocol_handlers_[handler.protocol()] = new_list; 820 protocol_handlers_[handler.protocol()] = new_list;
793 } 821 }
794 822
795 base::Value* ProtocolHandlerRegistry::EncodeRegisteredHandlers() { 823 base::Value* ProtocolHandlerRegistry::EncodeRegisteredHandlers() {
796 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 824 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
797 base::ListValue* protocol_handlers = new base::ListValue(); 825 base::ListValue* protocol_handlers = new base::ListValue();
798 for (ProtocolHandlerMultiMap::iterator i = protocol_handlers_.begin(); 826 for (ProtocolHandlerMultiMap::iterator i = user_protocol_handlers_.begin();
799 i != protocol_handlers_.end(); ++i) { 827 i != user_protocol_handlers_.end();
828 ++i) {
800 for (ProtocolHandlerList::iterator j = i->second.begin(); 829 for (ProtocolHandlerList::iterator j = i->second.begin();
801 j != i->second.end(); ++j) { 830 j != i->second.end(); ++j) {
802 base::DictionaryValue* encoded = j->Encode(); 831 base::DictionaryValue* encoded = j->Encode();
803 if (IsDefault(*j)) { 832 if (IsDefault(*j)) {
804 encoded->Set("default", base::Value::CreateBooleanValue(true)); 833 encoded->Set("default", base::Value::CreateBooleanValue(true));
805 } 834 }
806 protocol_handlers->Append(encoded); 835 protocol_handlers->Append(encoded);
807 } 836 }
808 } 837 }
809 return protocol_handlers; 838 return protocol_handlers;
810 } 839 }
811 840
812 base::Value* ProtocolHandlerRegistry::EncodeIgnoredHandlers() { 841 base::Value* ProtocolHandlerRegistry::EncodeIgnoredHandlers() {
813 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 842 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
814 base::ListValue* handlers = new base::ListValue(); 843 base::ListValue* handlers = new base::ListValue();
815 for (ProtocolHandlerList::iterator i = ignored_protocol_handlers_.begin(); 844 for (ProtocolHandlerList::iterator i =
816 i != ignored_protocol_handlers_.end(); ++i) { 845 user_ignored_protocol_handlers_.begin();
846 i != user_ignored_protocol_handlers_.end();
847 ++i) {
817 handlers->Append(i->Encode()); 848 handlers->Append(i->Encode());
818 } 849 }
819 return handlers; 850 return handlers;
820 } 851 }
821 852
822 void ProtocolHandlerRegistry::NotifyChanged() { 853 void ProtocolHandlerRegistry::NotifyChanged() {
823 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 854 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
824 content::NotificationService::current()->Notify( 855 content::NotificationService::current()->Notify(
825 chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, 856 chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED,
826 content::Source<Profile>(profile_), 857 content::Source<Profile>(profile_),
827 content::NotificationService::NoDetails()); 858 content::NotificationService::NoDetails());
828 } 859 }
829 860
830 void ProtocolHandlerRegistry::RegisterProtocolHandler( 861 void ProtocolHandlerRegistry::RegisterProtocolHandler(
831 const ProtocolHandler& handler) { 862 const ProtocolHandler& handler,
863 const bool policy_managed) {
832 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 864 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
833 DCHECK(CanSchemeBeOverridden(handler.protocol())); 865 DCHECK(CanSchemeBeOverridden(handler.protocol()));
834 DCHECK(!handler.IsEmpty()); 866 DCHECK(!handler.IsEmpty());
867 ProtocolHandlerMultiMap& map =
868 policy_managed ? policy_protocol_handlers_ : user_protocol_handlers_;
869 ProtocolHandlerList& list = map[handler.protocol()];
870 if (!HandlerExists(handler, list))
871 list.push_back(handler);
835 if (IsRegistered(handler)) { 872 if (IsRegistered(handler)) {
836 return; 873 return;
837 } 874 }
838 if (enabled_ && !delegate_->IsExternalHandlerRegistered(handler.protocol())) 875 if (enabled_ && !delegate_->IsExternalHandlerRegistered(handler.protocol()))
839 delegate_->RegisterExternalHandler(handler.protocol()); 876 delegate_->RegisterExternalHandler(handler.protocol());
840 InsertHandler(handler); 877 InsertHandler(handler);
841 } 878 }
842 879
843 std::vector<const base::DictionaryValue*> 880 std::vector<const base::DictionaryValue*>
844 ProtocolHandlerRegistry::GetHandlersFromPref(const char* pref_name) const { 881 ProtocolHandlerRegistry::GetHandlersFromPref(const char* pref_name) const {
(...skipping 12 matching lines...) Expand all
857 continue; 894 continue;
858 if (ProtocolHandler::IsValidDict(dict)) { 895 if (ProtocolHandler::IsValidDict(dict)) {
859 result.push_back(dict); 896 result.push_back(dict);
860 } 897 }
861 } 898 }
862 } 899 }
863 return result; 900 return result;
864 } 901 }
865 902
866 void ProtocolHandlerRegistry::IgnoreProtocolHandler( 903 void ProtocolHandlerRegistry::IgnoreProtocolHandler(
867 const ProtocolHandler& handler) { 904 const ProtocolHandler& handler,
905 const bool policy_managed) {
868 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 906 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
869 ignored_protocol_handlers_.push_back(handler); 907 ignored_protocol_handlers_.push_back(handler);
908 ProtocolHandlerList& list = policy_managed ? policy_ignored_protocol_handlers_
909 : user_ignored_protocol_handlers_;
910 if (!HandlerExists(handler, list))
911 list.push_back(handler);
912 }
913
914 bool ProtocolHandlerRegistry::HandlerExists(const ProtocolHandler& handler,
915 ProtocolHandlerMultiMap* map) {
916 return HandlerExists(handler, (*map)[handler.protocol()]);
917 }
918
919 bool ProtocolHandlerRegistry::HandlerExists(const ProtocolHandler& handler,
920 const ProtocolHandlerList& list) {
921 return (std::find(list.begin(), list.end(), handler) != list.end());
koz (OOO until 15th September) 2014/05/22 00:51:31 nit: no need for parens
kaliamoorthi 2014/05/30 10:01:09 Done.
922 }
923
924 void ProtocolHandlerRegistry::EraseHandler(const ProtocolHandler& handler,
925 ProtocolHandlerMultiMap* map) {
926 EraseHandler(handler, &(*map)[handler.protocol()]);
927 }
928
929 void ProtocolHandlerRegistry::EraseHandler(const ProtocolHandler& handler,
930 ProtocolHandlerList* list) {
931 ProtocolHandlerList::iterator p =
932 std::find(list->begin(), list->end(), handler);
933 list->erase(p);
870 } 934 }
871 935
872 void ProtocolHandlerRegistry::AddPredefinedHandler( 936 void ProtocolHandlerRegistry::AddPredefinedHandler(
873 const ProtocolHandler& handler) { 937 const ProtocolHandler& handler) {
874 DCHECK(!is_loaded_); // Must be called prior InitProtocolSettings. 938 DCHECK(!is_loaded_); // Must be called prior InitProtocolSettings.
875 RegisterProtocolHandler(handler); 939 RegisterProtocolHandler(handler, false);
876 SetDefault(handler); 940 SetDefault(handler);
877 } 941 }
878 942
879 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> 943 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
880 ProtocolHandlerRegistry::CreateJobInterceptorFactory() { 944 ProtocolHandlerRegistry::CreateJobInterceptorFactory() {
881 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 945 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
882 // this is always created on the UI thread (in profile_io's 946 // this is always created on the UI thread (in profile_io's
883 // InitializeOnUIThread. Any method calls must be done 947 // InitializeOnUIThread. Any method calls must be done
884 // on the IO thread (this is checked). 948 // on the IO thread (this is checked).
885 return scoped_ptr<JobInterceptorFactory>( 949 return scoped_ptr<JobInterceptorFactory>(
886 new JobInterceptorFactory(io_thread_delegate_.get())); 950 new JobInterceptorFactory(io_thread_delegate_.get()));
887 } 951 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698