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.h" | 10 #include "base/message_loop.h" |
(...skipping 12 matching lines...) Expand all Loading... | |
23 #include "net/url_request/url_request.h" | 23 #include "net/url_request/url_request.h" |
24 #include "net/url_request/url_request_context.h" | 24 #include "net/url_request/url_request_context.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
26 | 26 |
27 using content::BrowserThread; | 27 using content::BrowserThread; |
28 | 28 |
29 namespace { | 29 namespace { |
30 | 30 |
31 void AssertInterceptedIO( | 31 void AssertInterceptedIO( |
32 const GURL& url, | 32 const GURL& url, |
33 net::URLRequestJobFactory::Interceptor* interceptor) { | 33 net::URLRequestJobFactory* interceptor) { |
34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
35 net::URLRequestContext context; | 35 net::URLRequestContext context; |
36 net::URLRequest request(url, NULL, &context); | 36 net::URLRequest request(url, NULL, &context); |
37 scoped_refptr<net::URLRequestJob> job = interceptor->MaybeIntercept( | 37 scoped_refptr<net::URLRequestJob> job = |
38 &request, context.network_delegate()); | 38 interceptor->MaybeCreateJobWithProtocolHandler( |
39 url.scheme(), &request, context.network_delegate()); | |
39 ASSERT_TRUE(job.get() != NULL); | 40 ASSERT_TRUE(job.get() != NULL); |
40 } | 41 } |
41 | 42 |
42 void AssertIntercepted( | 43 void AssertIntercepted( |
43 const GURL& url, | 44 const GURL& url, |
44 net::URLRequestJobFactory::Interceptor* interceptor) { | 45 net::URLRequestJobFactory* interceptor) { |
45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
46 BrowserThread::PostTask(BrowserThread::IO, | 47 BrowserThread::PostTask(BrowserThread::IO, |
47 FROM_HERE, | 48 FROM_HERE, |
48 base::Bind(AssertInterceptedIO, | 49 base::Bind(AssertInterceptedIO, |
49 url, | 50 url, |
50 base::Unretained(interceptor))); | 51 base::Unretained(interceptor))); |
51 MessageLoop::current()->RunUntilIdle(); | 52 MessageLoop::current()->RunUntilIdle(); |
52 } | 53 } |
53 | 54 |
55 class FakeURLRequestJobFactory : public net::URLRequestJobFactory { | |
James Hawkins
2013/01/07 18:57:49
nit: Document the class.
| |
56 // net::URLRequestJobFactory implementation: | |
57 virtual bool SetProtocolHandler(const std::string& scheme, | |
58 ProtocolHandler* protocol_handler) OVERRIDE { | |
59 return false; | |
60 } | |
61 virtual void AddInterceptor(Interceptor* interceptor) OVERRIDE { | |
62 } | |
63 virtual net::URLRequestJob* MaybeCreateJobWithInterceptor( | |
64 net::URLRequest* request, | |
65 net::NetworkDelegate* network_delegate) const OVERRIDE { | |
66 return NULL; | |
67 } | |
68 net::URLRequestJob* MaybeCreateJobWithProtocolHandler( | |
69 const std::string& scheme, | |
70 net::URLRequest* request, | |
71 net::NetworkDelegate* network_delegate) const OVERRIDE { | |
72 return NULL; | |
73 } | |
74 net::URLRequestJob* MaybeInterceptRedirect( | |
75 const GURL& location, | |
76 net::URLRequest* request, | |
77 net::NetworkDelegate* network_delegate) const OVERRIDE { | |
78 return NULL; | |
79 } | |
80 net::URLRequestJob* MaybeInterceptResponse( | |
81 net::URLRequest* request, | |
82 net::NetworkDelegate* network_delegate) const OVERRIDE { | |
83 return NULL; | |
84 } | |
85 virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE { | |
86 return false; | |
87 } | |
88 virtual bool IsHandledURL(const GURL& url) const OVERRIDE { | |
89 return false; | |
90 } | |
91 }; | |
92 | |
54 void AssertWillHandleIO( | 93 void AssertWillHandleIO( |
55 const std::string& scheme, | 94 const std::string& scheme, |
56 bool expected, | 95 bool expected, |
57 net::URLRequestJobFactory::Interceptor* interceptor) { | 96 ProtocolHandlerRegistry::JobInterceptorFactory* interceptor) { |
58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
59 ASSERT_EQ(expected, interceptor->WillHandleProtocol(scheme)); | 98 interceptor->Chain(scoped_ptr<net::URLRequestJobFactory>( |
99 new FakeURLRequestJobFactory())); | |
100 ASSERT_EQ(expected, interceptor->IsHandledProtocol(scheme)); | |
101 interceptor->Chain(scoped_ptr<net::URLRequestJobFactory>(NULL)); | |
60 } | 102 } |
61 | 103 |
62 void AssertWillHandle( | 104 void AssertWillHandle( |
63 const std::string& scheme, | 105 const std::string& scheme, |
64 bool expected, | 106 bool expected, |
65 net::URLRequestJobFactory::Interceptor* interceptor) { | 107 ProtocolHandlerRegistry::JobInterceptorFactory* interceptor) { |
66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
67 BrowserThread::PostTask(BrowserThread::IO, | 109 BrowserThread::PostTask(BrowserThread::IO, |
68 FROM_HERE, | 110 FROM_HERE, |
69 base::Bind(AssertWillHandleIO, | 111 base::Bind(AssertWillHandleIO, |
70 scheme, | 112 scheme, |
71 expected, | 113 expected, |
72 base::Unretained(interceptor))); | 114 base::Unretained(interceptor))); |
73 MessageLoop::current()->RunUntilIdle(); | 115 MessageLoop::current()->RunUntilIdle(); |
74 } | 116 } |
75 | 117 |
(...skipping 671 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
747 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("do").size()); | 789 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("do").size()); |
748 ASSERT_FALSE(registry()->IsHandledProtocol("dont")); | 790 ASSERT_FALSE(registry()->IsHandledProtocol("dont")); |
749 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("dont").size()); | 791 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("dont").size()); |
750 } | 792 } |
751 | 793 |
752 TEST_F(ProtocolHandlerRegistryTest, TestMaybeCreateTaskWorksFromIOThread) { | 794 TEST_F(ProtocolHandlerRegistryTest, TestMaybeCreateTaskWorksFromIOThread) { |
753 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); | 795 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); |
754 registry()->OnAcceptRegisterProtocolHandler(ph1); | 796 registry()->OnAcceptRegisterProtocolHandler(ph1); |
755 GURL url("mailto:someone@something.com"); | 797 GURL url("mailto:someone@something.com"); |
756 | 798 |
757 scoped_ptr<net::URLRequestJobFactory::Interceptor> interceptor( | 799 scoped_ptr<net::URLRequestJobFactory> interceptor( |
758 registry()->CreateURLInterceptor()); | 800 registry()->CreateJobInterceptorFactory()); |
759 AssertIntercepted(url, interceptor.get()); | 801 AssertIntercepted(url, interceptor.get()); |
760 } | 802 } |
761 | 803 |
762 TEST_F(ProtocolHandlerRegistryTest, | 804 TEST_F(ProtocolHandlerRegistryTest, |
763 MAYBE_TestIsHandledProtocolWorksOnIOThread) { | 805 MAYBE_TestIsHandledProtocolWorksOnIOThread) { |
764 std::string scheme("mailto"); | 806 std::string scheme("mailto"); |
765 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); | 807 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); |
766 registry()->OnAcceptRegisterProtocolHandler(ph1); | 808 registry()->OnAcceptRegisterProtocolHandler(ph1); |
767 | 809 |
768 scoped_ptr<net::URLRequestJobFactory::Interceptor> interceptor( | 810 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> interceptor( |
769 registry()->CreateURLInterceptor()); | 811 registry()->CreateJobInterceptorFactory()); |
770 AssertWillHandle(scheme, true, interceptor.get()); | 812 AssertWillHandle(scheme, true, interceptor.get()); |
771 } | 813 } |
772 | 814 |
773 TEST_F(ProtocolHandlerRegistryTest, TestRemovingDefaultFallsBackToOldDefault) { | 815 TEST_F(ProtocolHandlerRegistryTest, TestRemovingDefaultFallsBackToOldDefault) { |
774 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); | 816 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); |
775 ProtocolHandler ph2 = CreateProtocolHandler("mailto", "test2"); | 817 ProtocolHandler ph2 = CreateProtocolHandler("mailto", "test2"); |
776 ProtocolHandler ph3 = CreateProtocolHandler("mailto", "test3"); | 818 ProtocolHandler ph3 = CreateProtocolHandler("mailto", "test3"); |
777 registry()->OnAcceptRegisterProtocolHandler(ph1); | 819 registry()->OnAcceptRegisterProtocolHandler(ph1); |
778 registry()->OnAcceptRegisterProtocolHandler(ph2); | 820 registry()->OnAcceptRegisterProtocolHandler(ph2); |
779 registry()->OnAcceptRegisterProtocolHandler(ph3); | 821 registry()->OnAcceptRegisterProtocolHandler(ph3); |
(...skipping 25 matching lines...) Expand all Loading... | |
805 ASSERT_EQ(ph2, handlers[0]); | 847 ASSERT_EQ(ph2, handlers[0]); |
806 ASSERT_EQ(ph1, handlers[1]); | 848 ASSERT_EQ(ph1, handlers[1]); |
807 } | 849 } |
808 | 850 |
809 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestClearDefaultGetsPropagatedToIO) { | 851 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestClearDefaultGetsPropagatedToIO) { |
810 std::string scheme("mailto"); | 852 std::string scheme("mailto"); |
811 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); | 853 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); |
812 registry()->OnAcceptRegisterProtocolHandler(ph1); | 854 registry()->OnAcceptRegisterProtocolHandler(ph1); |
813 registry()->ClearDefault(scheme); | 855 registry()->ClearDefault(scheme); |
814 | 856 |
815 scoped_ptr<net::URLRequestJobFactory::Interceptor> interceptor( | 857 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> interceptor( |
816 registry()->CreateURLInterceptor()); | 858 registry()->CreateJobInterceptorFactory()); |
817 AssertWillHandle(scheme, false, interceptor.get()); | 859 AssertWillHandle(scheme, false, interceptor.get()); |
818 } | 860 } |
819 | 861 |
820 TEST_F(ProtocolHandlerRegistryTest, TestLoadEnabledGetsPropogatedToIO) { | 862 TEST_F(ProtocolHandlerRegistryTest, TestLoadEnabledGetsPropogatedToIO) { |
821 std::string mailto("mailto"); | 863 std::string mailto("mailto"); |
822 ProtocolHandler ph1 = CreateProtocolHandler(mailto, "MailtoHandler"); | 864 ProtocolHandler ph1 = CreateProtocolHandler(mailto, "MailtoHandler"); |
823 registry()->OnAcceptRegisterProtocolHandler(ph1); | 865 registry()->OnAcceptRegisterProtocolHandler(ph1); |
824 | 866 |
825 scoped_ptr<net::URLRequestJobFactory::Interceptor> interceptor( | 867 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> interceptor( |
826 registry()->CreateURLInterceptor()); | 868 registry()->CreateJobInterceptorFactory()); |
827 AssertWillHandle(mailto, true, interceptor.get()); | 869 AssertWillHandle(mailto, true, interceptor.get()); |
828 registry()->Disable(); | 870 registry()->Disable(); |
829 AssertWillHandle(mailto, false, interceptor.get()); | 871 AssertWillHandle(mailto, false, interceptor.get()); |
830 } | 872 } |
831 | 873 |
832 TEST_F(ProtocolHandlerRegistryTest, TestReplaceHandler) { | 874 TEST_F(ProtocolHandlerRegistryTest, TestReplaceHandler) { |
833 ProtocolHandler ph1 = CreateProtocolHandler("mailto", | 875 ProtocolHandler ph1 = CreateProtocolHandler("mailto", |
834 GURL("http://test.com/%s"), "test1"); | 876 GURL("http://test.com/%s"), "test1"); |
835 ProtocolHandler ph2 = CreateProtocolHandler("mailto", | 877 ProtocolHandler ph2 = CreateProtocolHandler("mailto", |
836 GURL("http://test.com/updated-url/%s"), "test2"); | 878 GURL("http://test.com/updated-url/%s"), "test2"); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
891 | 933 |
892 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestInstallDefaultHandler) { | 934 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestInstallDefaultHandler) { |
893 RecreateRegistry(false); | 935 RecreateRegistry(false); |
894 registry()->AddPredefinedHandler(CreateProtocolHandler( | 936 registry()->AddPredefinedHandler(CreateProtocolHandler( |
895 "test", GURL("http://test.com/%s"), "Test")); | 937 "test", GURL("http://test.com/%s"), "Test")); |
896 registry()->InitProtocolSettings(); | 938 registry()->InitProtocolSettings(); |
897 std::vector<std::string> protocols; | 939 std::vector<std::string> protocols; |
898 registry()->GetRegisteredProtocols(&protocols); | 940 registry()->GetRegisteredProtocols(&protocols); |
899 ASSERT_EQ(static_cast<size_t>(1), protocols.size()); | 941 ASSERT_EQ(static_cast<size_t>(1), protocols.size()); |
900 } | 942 } |
OLD | NEW |