| OLD | NEW |
| 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 <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 test_protocol_handler_(CreateProtocolHandler("test", "test")) {} | 313 test_protocol_handler_(CreateProtocolHandler("test", "test")) {} |
| 314 | 314 |
| 315 FakeDelegate* delegate() const { return delegate_; } | 315 FakeDelegate* delegate() const { return delegate_; } |
| 316 ProtocolHandlerRegistry* registry() { return registry_.get(); } | 316 ProtocolHandlerRegistry* registry() { return registry_.get(); } |
| 317 TestingProfile* profile() const { return profile_.get(); } | 317 TestingProfile* profile() const { return profile_.get(); } |
| 318 const ProtocolHandler& test_protocol_handler() const { | 318 const ProtocolHandler& test_protocol_handler() const { |
| 319 return test_protocol_handler_; | 319 return test_protocol_handler_; |
| 320 } | 320 } |
| 321 | 321 |
| 322 ProtocolHandler CreateProtocolHandler(const std::string& protocol, | 322 ProtocolHandler CreateProtocolHandler(const std::string& protocol, |
| 323 const GURL& url, | 323 const GURL& url) { |
| 324 const std::string& title) { | 324 return ProtocolHandler::CreateProtocolHandler(protocol, url); |
| 325 return ProtocolHandler::CreateProtocolHandler(protocol, url, | |
| 326 base::UTF8ToUTF16(title)); | |
| 327 } | 325 } |
| 328 | 326 |
| 329 ProtocolHandler CreateProtocolHandler(const std::string& protocol, | 327 ProtocolHandler CreateProtocolHandler(const std::string& protocol, |
| 330 const std::string& name) { | 328 const std::string& name) { |
| 331 return CreateProtocolHandler(protocol, GURL("http://" + name + "/%s"), | 329 return CreateProtocolHandler(protocol, GURL("http://" + name + "/%s")); |
| 332 name); | |
| 333 } | 330 } |
| 334 | 331 |
| 335 void RecreateRegistry(bool initialize) { | 332 void RecreateRegistry(bool initialize) { |
| 336 TeadDownRegistry(); | 333 TeadDownRegistry(); |
| 337 SetUpRegistry(initialize); | 334 SetUpRegistry(initialize); |
| 338 } | 335 } |
| 339 | 336 |
| 340 // Returns a new registry, initializing it if |initialize| is true. | 337 // Returns a new registry, initializing it if |initialize| is true. |
| 341 // Caller assumes ownership for the object | 338 // Caller assumes ownership for the object |
| 342 void SetUpRegistry(bool initialize) { | 339 void SetUpRegistry(bool initialize) { |
| 343 delegate_ = new FakeDelegate(); | 340 delegate_ = new FakeDelegate(); |
| 344 registry_.reset(new ProtocolHandlerRegistry(profile(), delegate())); | 341 registry_.reset(new ProtocolHandlerRegistry(profile(), delegate())); |
| 345 if (initialize) registry_->InitProtocolSettings(); | 342 if (initialize) registry_->InitProtocolSettings(); |
| 346 } | 343 } |
| 347 | 344 |
| 348 void TeadDownRegistry() { | 345 void TeadDownRegistry() { |
| 349 registry_->Shutdown(); | 346 registry_->Shutdown(); |
| 350 registry_.reset(); | 347 registry_.reset(); |
| 351 // Registry owns the delegate_ it handles deletion of that object. | 348 // Registry owns the delegate_ it handles deletion of that object. |
| 352 } | 349 } |
| 353 | 350 |
| 354 virtual void SetUp() { | 351 virtual void SetUp() { |
| 355 profile_.reset(new TestingProfile()); | 352 profile_.reset(new TestingProfile()); |
| 356 CHECK(profile_->GetPrefs()); | 353 CHECK(profile_->GetPrefs()); |
| 357 SetUpRegistry(true); | 354 SetUpRegistry(true); |
| 358 test_protocol_handler_ = | 355 test_protocol_handler_ = |
| 359 CreateProtocolHandler("test", GURL("http://test.com/%s"), "Test"); | 356 CreateProtocolHandler("test", GURL("http://test.com/%s")); |
| 360 } | 357 } |
| 361 | 358 |
| 362 virtual void TearDown() { | 359 virtual void TearDown() { |
| 363 TeadDownRegistry(); | 360 TeadDownRegistry(); |
| 364 } | 361 } |
| 365 | 362 |
| 366 TestMessageLoop loop_; | 363 TestMessageLoop loop_; |
| 367 | 364 |
| 368 private: | 365 private: |
| 369 content::TestBrowserThread ui_thread_; | 366 content::TestBrowserThread ui_thread_; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 | 438 |
| 442 TEST_F(ProtocolHandlerRegistryTest, IgnoreProtocolHandler) { | 439 TEST_F(ProtocolHandlerRegistryTest, IgnoreProtocolHandler) { |
| 443 registry()->OnIgnoreRegisterProtocolHandler(test_protocol_handler()); | 440 registry()->OnIgnoreRegisterProtocolHandler(test_protocol_handler()); |
| 444 ASSERT_TRUE(registry()->IsIgnored(test_protocol_handler())); | 441 ASSERT_TRUE(registry()->IsIgnored(test_protocol_handler())); |
| 445 | 442 |
| 446 registry()->RemoveIgnoredHandler(test_protocol_handler()); | 443 registry()->RemoveIgnoredHandler(test_protocol_handler()); |
| 447 ASSERT_FALSE(registry()->IsIgnored(test_protocol_handler())); | 444 ASSERT_FALSE(registry()->IsIgnored(test_protocol_handler())); |
| 448 } | 445 } |
| 449 | 446 |
| 450 TEST_F(ProtocolHandlerRegistryTest, IgnoreEquivalentProtocolHandler) { | 447 TEST_F(ProtocolHandlerRegistryTest, IgnoreEquivalentProtocolHandler) { |
| 451 ProtocolHandler ph1 = CreateProtocolHandler("test", GURL("http://test/%s"), | 448 ProtocolHandler ph1 = CreateProtocolHandler("test", GURL("http://test/%s")); |
| 452 "test1"); | 449 ProtocolHandler ph2 = CreateProtocolHandler("test", GURL("http://test/%s")); |
| 453 ProtocolHandler ph2 = CreateProtocolHandler("test", GURL("http://test/%s"), | |
| 454 "test2"); | |
| 455 | 450 |
| 456 registry()->OnIgnoreRegisterProtocolHandler(ph1); | 451 registry()->OnIgnoreRegisterProtocolHandler(ph1); |
| 457 ASSERT_TRUE(registry()->IsIgnored(ph1)); | 452 ASSERT_TRUE(registry()->IsIgnored(ph1)); |
| 458 ASSERT_TRUE(registry()->HasIgnoredEquivalent(ph2)); | 453 ASSERT_TRUE(registry()->HasIgnoredEquivalent(ph2)); |
| 459 | 454 |
| 460 registry()->RemoveIgnoredHandler(ph1); | 455 registry()->RemoveIgnoredHandler(ph1); |
| 461 ASSERT_FALSE(registry()->IsIgnored(ph1)); | 456 ASSERT_FALSE(registry()->IsIgnored(ph1)); |
| 462 ASSERT_FALSE(registry()->HasIgnoredEquivalent(ph2)); | 457 ASSERT_FALSE(registry()->HasIgnoredEquivalent(ph2)); |
| 463 } | 458 } |
| 464 | 459 |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 TEST_F(ProtocolHandlerRegistryTest, TestIsRegistered) { | 576 TEST_F(ProtocolHandlerRegistryTest, TestIsRegistered) { |
| 582 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); | 577 ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
| 583 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); | 578 ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); |
| 584 registry()->OnAcceptRegisterProtocolHandler(ph1); | 579 registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 585 registry()->OnAcceptRegisterProtocolHandler(ph2); | 580 registry()->OnAcceptRegisterProtocolHandler(ph2); |
| 586 | 581 |
| 587 ASSERT_TRUE(registry()->IsRegistered(ph1)); | 582 ASSERT_TRUE(registry()->IsRegistered(ph1)); |
| 588 } | 583 } |
| 589 | 584 |
| 590 TEST_F(ProtocolHandlerRegistryTest, TestIsEquivalentRegistered) { | 585 TEST_F(ProtocolHandlerRegistryTest, TestIsEquivalentRegistered) { |
| 591 ProtocolHandler ph1 = CreateProtocolHandler("test", GURL("http://test/%s"), | 586 ProtocolHandler ph1 = CreateProtocolHandler("test", GURL("http://test/%s")); |
| 592 "test1"); | 587 ProtocolHandler ph2 = CreateProtocolHandler("test", GURL("http://test/%s")); |
| 593 ProtocolHandler ph2 = CreateProtocolHandler("test", GURL("http://test/%s"), | |
| 594 "test2"); | |
| 595 registry()->OnAcceptRegisterProtocolHandler(ph1); | 588 registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 596 | 589 |
| 597 ASSERT_TRUE(registry()->IsRegistered(ph1)); | 590 ASSERT_TRUE(registry()->IsRegistered(ph1)); |
| 598 ASSERT_TRUE(registry()->HasRegisteredEquivalent(ph2)); | 591 ASSERT_TRUE(registry()->HasRegisteredEquivalent(ph2)); |
| 599 } | 592 } |
| 600 | 593 |
| 601 TEST_F(ProtocolHandlerRegistryTest, TestSilentlyRegisterHandler) { | 594 TEST_F(ProtocolHandlerRegistryTest, TestSilentlyRegisterHandler) { |
| 602 ProtocolHandler ph1 = CreateProtocolHandler("test", GURL("http://test/%s"), | 595 ProtocolHandler ph1 = CreateProtocolHandler("test", GURL("http://test/1/%s")); |
| 603 "test1"); | 596 ProtocolHandler ph2 = CreateProtocolHandler("test", GURL("http://test/2/%s")); |
| 604 ProtocolHandler ph2 = CreateProtocolHandler("test", GURL("http://test/%s"), | 597 ProtocolHandler ph3 = CreateProtocolHandler("ignore", GURL("http://test/%s")); |
| 605 "test2"); | 598 ProtocolHandler ph4 = CreateProtocolHandler("ignore", GURL("http://test/%s")); |
| 606 ProtocolHandler ph3 = CreateProtocolHandler("ignore", GURL("http://test/%s"), | |
| 607 "ignore1"); | |
| 608 ProtocolHandler ph4 = CreateProtocolHandler("ignore", GURL("http://test/%s"), | |
| 609 "ignore2"); | |
| 610 | 599 |
| 611 ASSERT_FALSE(registry()->SilentlyHandleRegisterHandlerRequest(ph1)); | 600 ASSERT_FALSE(registry()->SilentlyHandleRegisterHandlerRequest(ph1)); |
| 612 ASSERT_FALSE(registry()->IsRegistered(ph1)); | 601 ASSERT_FALSE(registry()->IsRegistered(ph1)); |
| 613 | 602 |
| 614 registry()->OnAcceptRegisterProtocolHandler(ph1); | 603 registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 615 ASSERT_TRUE(registry()->IsRegistered(ph1)); | 604 ASSERT_TRUE(registry()->IsRegistered(ph1)); |
| 616 | 605 |
| 617 ASSERT_TRUE(registry()->SilentlyHandleRegisterHandlerRequest(ph2)); | 606 ASSERT_TRUE(registry()->SilentlyHandleRegisterHandlerRequest(ph2)); |
| 618 ASSERT_FALSE(registry()->IsRegistered(ph1)); | 607 ASSERT_FALSE(registry()->IsRegistered(ph1)); |
| 619 ASSERT_TRUE(registry()->IsRegistered(ph2)); | 608 ASSERT_TRUE(registry()->IsRegistered(ph2)); |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 852 registry()->OnAcceptRegisterProtocolHandler(ph1); | 841 registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 853 | 842 |
| 854 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> interceptor( | 843 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> interceptor( |
| 855 registry()->CreateJobInterceptorFactory()); | 844 registry()->CreateJobInterceptorFactory()); |
| 856 AssertWillHandle(mailto, true, interceptor.get()); | 845 AssertWillHandle(mailto, true, interceptor.get()); |
| 857 registry()->Disable(); | 846 registry()->Disable(); |
| 858 AssertWillHandle(mailto, false, interceptor.get()); | 847 AssertWillHandle(mailto, false, interceptor.get()); |
| 859 } | 848 } |
| 860 | 849 |
| 861 TEST_F(ProtocolHandlerRegistryTest, TestReplaceHandler) { | 850 TEST_F(ProtocolHandlerRegistryTest, TestReplaceHandler) { |
| 862 ProtocolHandler ph1 = CreateProtocolHandler("mailto", | 851 ProtocolHandler ph1 = |
| 863 GURL("http://test.com/%s"), "test1"); | 852 CreateProtocolHandler("mailto", GURL("http://test.com/%s")); |
| 864 ProtocolHandler ph2 = CreateProtocolHandler("mailto", | 853 ProtocolHandler ph2 = |
| 865 GURL("http://test.com/updated-url/%s"), "test2"); | 854 CreateProtocolHandler("mailto", GURL("http://test.com/updated-url/%s")); |
| 866 registry()->OnAcceptRegisterProtocolHandler(ph1); | 855 registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 867 ASSERT_TRUE(registry()->AttemptReplace(ph2)); | 856 ASSERT_TRUE(registry()->AttemptReplace(ph2)); |
| 868 const ProtocolHandler& handler(registry()->GetHandlerFor("mailto")); | 857 const ProtocolHandler& handler(registry()->GetHandlerFor("mailto")); |
| 869 ASSERT_EQ(handler.url(), ph2.url()); | 858 ASSERT_EQ(handler.url(), ph2.url()); |
| 870 } | 859 } |
| 871 | 860 |
| 872 TEST_F(ProtocolHandlerRegistryTest, TestReplaceNonDefaultHandler) { | 861 TEST_F(ProtocolHandlerRegistryTest, TestReplaceNonDefaultHandler) { |
| 873 ProtocolHandler ph1 = CreateProtocolHandler("mailto", | 862 ProtocolHandler ph1 = |
| 874 GURL("http://test.com/%s"), "test1"); | 863 CreateProtocolHandler("mailto", GURL("http://test.com/%s")); |
| 875 ProtocolHandler ph2 = CreateProtocolHandler("mailto", | 864 ProtocolHandler ph2 = |
| 876 GURL("http://test.com/updated-url/%s"), "test2"); | 865 CreateProtocolHandler("mailto", GURL("http://test.com/updated-url/%s")); |
| 877 ProtocolHandler ph3 = CreateProtocolHandler("mailto", | 866 ProtocolHandler ph3 = |
| 878 GURL("http://else.com/%s"), "test3"); | 867 CreateProtocolHandler("mailto", GURL("http://else.com/%s")); |
| 879 registry()->OnAcceptRegisterProtocolHandler(ph1); | 868 registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 880 registry()->OnAcceptRegisterProtocolHandler(ph3); | 869 registry()->OnAcceptRegisterProtocolHandler(ph3); |
| 881 ASSERT_TRUE(registry()->AttemptReplace(ph2)); | 870 ASSERT_TRUE(registry()->AttemptReplace(ph2)); |
| 882 const ProtocolHandler& handler(registry()->GetHandlerFor("mailto")); | 871 const ProtocolHandler& handler(registry()->GetHandlerFor("mailto")); |
| 883 ASSERT_EQ(handler.url(), ph3.url()); | 872 ASSERT_EQ(handler.url(), ph3.url()); |
| 884 } | 873 } |
| 885 | 874 |
| 886 TEST_F(ProtocolHandlerRegistryTest, TestReplaceRemovesStaleHandlers) { | 875 TEST_F(ProtocolHandlerRegistryTest, TestReplaceRemovesStaleHandlers) { |
| 887 ProtocolHandler ph1 = CreateProtocolHandler("mailto", | 876 ProtocolHandler ph1 = |
| 888 GURL("http://test.com/%s"), "test1"); | 877 CreateProtocolHandler("mailto", GURL("http://test.com/%s")); |
| 889 ProtocolHandler ph2 = CreateProtocolHandler("mailto", | 878 ProtocolHandler ph2 = |
| 890 GURL("http://test.com/updated-url/%s"), "test2"); | 879 CreateProtocolHandler("mailto", GURL("http://test.com/updated-url/%s")); |
| 891 ProtocolHandler ph3 = CreateProtocolHandler("mailto", | 880 ProtocolHandler ph3 = |
| 892 GURL("http://test.com/third/%s"), "test"); | 881 CreateProtocolHandler("mailto", GURL("http://test.com/third/%s")); |
| 893 registry()->OnAcceptRegisterProtocolHandler(ph1); | 882 registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 894 registry()->OnAcceptRegisterProtocolHandler(ph2); | 883 registry()->OnAcceptRegisterProtocolHandler(ph2); |
| 895 | 884 |
| 896 // This should replace the previous two handlers. | 885 // This should replace the previous two handlers. |
| 897 ASSERT_TRUE(registry()->AttemptReplace(ph3)); | 886 ASSERT_TRUE(registry()->AttemptReplace(ph3)); |
| 898 const ProtocolHandler& handler(registry()->GetHandlerFor("mailto")); | 887 const ProtocolHandler& handler(registry()->GetHandlerFor("mailto")); |
| 899 ASSERT_EQ(handler.url(), ph3.url()); | 888 ASSERT_EQ(handler.url(), ph3.url()); |
| 900 registry()->RemoveHandler(ph3); | 889 registry()->RemoveHandler(ph3); |
| 901 ASSERT_TRUE(registry()->GetHandlerFor("mailto").IsEmpty()); | 890 ASSERT_TRUE(registry()->GetHandlerFor("mailto").IsEmpty()); |
| 902 } | 891 } |
| 903 | 892 |
| 904 TEST_F(ProtocolHandlerRegistryTest, TestIsSameOrigin) { | 893 TEST_F(ProtocolHandlerRegistryTest, TestIsSameOrigin) { |
| 905 ProtocolHandler ph1 = CreateProtocolHandler("mailto", | 894 ProtocolHandler ph1 = |
| 906 GURL("http://test.com/%s"), "test1"); | 895 CreateProtocolHandler("mailto", GURL("http://test.com/%s")); |
| 907 ProtocolHandler ph2 = CreateProtocolHandler("mailto", | 896 ProtocolHandler ph2 = |
| 908 GURL("http://test.com/updated-url/%s"), "test2"); | 897 CreateProtocolHandler("mailto", GURL("http://test.com/updated-url/%s")); |
| 909 ProtocolHandler ph3 = CreateProtocolHandler("mailto", | 898 ProtocolHandler ph3 = |
| 910 GURL("http://other.com/%s"), "test"); | 899 CreateProtocolHandler("mailto", GURL("http://other.com/%s")); |
| 911 ASSERT_EQ(ph1.url().GetOrigin() == ph2.url().GetOrigin(), | 900 ASSERT_EQ(ph1.url().GetOrigin() == ph2.url().GetOrigin(), |
| 912 ph1.IsSameOrigin(ph2)); | 901 ph1.IsSameOrigin(ph2)); |
| 913 ASSERT_EQ(ph1.url().GetOrigin() == ph2.url().GetOrigin(), | 902 ASSERT_EQ(ph1.url().GetOrigin() == ph2.url().GetOrigin(), |
| 914 ph2.IsSameOrigin(ph1)); | 903 ph2.IsSameOrigin(ph1)); |
| 915 ASSERT_EQ(ph2.url().GetOrigin() == ph3.url().GetOrigin(), | 904 ASSERT_EQ(ph2.url().GetOrigin() == ph3.url().GetOrigin(), |
| 916 ph2.IsSameOrigin(ph3)); | 905 ph2.IsSameOrigin(ph3)); |
| 917 ASSERT_EQ(ph3.url().GetOrigin() == ph2.url().GetOrigin(), | 906 ASSERT_EQ(ph3.url().GetOrigin() == ph2.url().GetOrigin(), |
| 918 ph3.IsSameOrigin(ph2)); | 907 ph3.IsSameOrigin(ph2)); |
| 919 } | 908 } |
| 920 | 909 |
| 921 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestInstallDefaultHandler) { | 910 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestInstallDefaultHandler) { |
| 922 RecreateRegistry(false); | 911 RecreateRegistry(false); |
| 923 registry()->AddPredefinedHandler(CreateProtocolHandler( | 912 registry()->AddPredefinedHandler( |
| 924 "test", GURL("http://test.com/%s"), "Test")); | 913 CreateProtocolHandler("test", GURL("http://test.com/%s"))); |
| 925 registry()->InitProtocolSettings(); | 914 registry()->InitProtocolSettings(); |
| 926 std::vector<std::string> protocols; | 915 std::vector<std::string> protocols; |
| 927 registry()->GetRegisteredProtocols(&protocols); | 916 registry()->GetRegisteredProtocols(&protocols); |
| 928 ASSERT_EQ(static_cast<size_t>(1), protocols.size()); | 917 ASSERT_EQ(static_cast<size_t>(1), protocols.size()); |
| 929 } | 918 } |
| OLD | NEW |