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 // FakeURLRequestJobFactory returns NULL for all job creation requests and false |
| 56 // for all IsHandled*() requests. FakeURLRequestJobFactory can be chained to |
| 57 // ProtocolHandlerRegistry::JobInterceptorFactory so the result of |
| 58 // MaybeCreateJobWithProtocolHandler() indicates whether the |
| 59 // ProtocolHandlerRegistry properly handled a job creation request. |
| 60 class FakeURLRequestJobFactory : public net::URLRequestJobFactory { |
| 61 // net::URLRequestJobFactory implementation: |
| 62 virtual bool SetProtocolHandler(const std::string& scheme, |
| 63 ProtocolHandler* protocol_handler) OVERRIDE { |
| 64 return false; |
| 65 } |
| 66 virtual void AddInterceptor(Interceptor* interceptor) OVERRIDE { |
| 67 } |
| 68 virtual net::URLRequestJob* MaybeCreateJobWithInterceptor( |
| 69 net::URLRequest* request, |
| 70 net::NetworkDelegate* network_delegate) const OVERRIDE { |
| 71 return NULL; |
| 72 } |
| 73 net::URLRequestJob* MaybeCreateJobWithProtocolHandler( |
| 74 const std::string& scheme, |
| 75 net::URLRequest* request, |
| 76 net::NetworkDelegate* network_delegate) const OVERRIDE { |
| 77 return NULL; |
| 78 } |
| 79 net::URLRequestJob* MaybeInterceptRedirect( |
| 80 const GURL& location, |
| 81 net::URLRequest* request, |
| 82 net::NetworkDelegate* network_delegate) const OVERRIDE { |
| 83 return NULL; |
| 84 } |
| 85 net::URLRequestJob* MaybeInterceptResponse( |
| 86 net::URLRequest* request, |
| 87 net::NetworkDelegate* network_delegate) const OVERRIDE { |
| 88 return NULL; |
| 89 } |
| 90 virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE { |
| 91 return false; |
| 92 } |
| 93 virtual bool IsHandledURL(const GURL& url) const OVERRIDE { |
| 94 return false; |
| 95 } |
| 96 }; |
| 97 |
54 void AssertWillHandleIO( | 98 void AssertWillHandleIO( |
55 const std::string& scheme, | 99 const std::string& scheme, |
56 bool expected, | 100 bool expected, |
57 net::URLRequestJobFactory::Interceptor* interceptor) { | 101 ProtocolHandlerRegistry::JobInterceptorFactory* interceptor) { |
58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
59 ASSERT_EQ(expected, interceptor->WillHandleProtocol(scheme)); | 103 interceptor->Chain(scoped_ptr<net::URLRequestJobFactory>( |
| 104 new FakeURLRequestJobFactory())); |
| 105 ASSERT_EQ(expected, interceptor->IsHandledProtocol(scheme)); |
| 106 interceptor->Chain(scoped_ptr<net::URLRequestJobFactory>(NULL)); |
60 } | 107 } |
61 | 108 |
62 void AssertWillHandle( | 109 void AssertWillHandle( |
63 const std::string& scheme, | 110 const std::string& scheme, |
64 bool expected, | 111 bool expected, |
65 net::URLRequestJobFactory::Interceptor* interceptor) { | 112 ProtocolHandlerRegistry::JobInterceptorFactory* interceptor) { |
66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
67 BrowserThread::PostTask(BrowserThread::IO, | 114 BrowserThread::PostTask(BrowserThread::IO, |
68 FROM_HERE, | 115 FROM_HERE, |
69 base::Bind(AssertWillHandleIO, | 116 base::Bind(AssertWillHandleIO, |
70 scheme, | 117 scheme, |
71 expected, | 118 expected, |
72 base::Unretained(interceptor))); | 119 base::Unretained(interceptor))); |
73 MessageLoop::current()->RunUntilIdle(); | 120 MessageLoop::current()->RunUntilIdle(); |
74 } | 121 } |
75 | 122 |
(...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()); | 794 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("do").size()); |
748 ASSERT_FALSE(registry()->IsHandledProtocol("dont")); | 795 ASSERT_FALSE(registry()->IsHandledProtocol("dont")); |
749 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("dont").size()); | 796 ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("dont").size()); |
750 } | 797 } |
751 | 798 |
752 TEST_F(ProtocolHandlerRegistryTest, TestMaybeCreateTaskWorksFromIOThread) { | 799 TEST_F(ProtocolHandlerRegistryTest, TestMaybeCreateTaskWorksFromIOThread) { |
753 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); | 800 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); |
754 registry()->OnAcceptRegisterProtocolHandler(ph1); | 801 registry()->OnAcceptRegisterProtocolHandler(ph1); |
755 GURL url("mailto:someone@something.com"); | 802 GURL url("mailto:someone@something.com"); |
756 | 803 |
757 scoped_ptr<net::URLRequestJobFactory::Interceptor> interceptor( | 804 scoped_ptr<net::URLRequestJobFactory> interceptor( |
758 registry()->CreateURLInterceptor()); | 805 registry()->CreateJobInterceptorFactory()); |
759 AssertIntercepted(url, interceptor.get()); | 806 AssertIntercepted(url, interceptor.get()); |
760 } | 807 } |
761 | 808 |
762 TEST_F(ProtocolHandlerRegistryTest, | 809 TEST_F(ProtocolHandlerRegistryTest, |
763 MAYBE_TestIsHandledProtocolWorksOnIOThread) { | 810 MAYBE_TestIsHandledProtocolWorksOnIOThread) { |
764 std::string scheme("mailto"); | 811 std::string scheme("mailto"); |
765 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); | 812 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); |
766 registry()->OnAcceptRegisterProtocolHandler(ph1); | 813 registry()->OnAcceptRegisterProtocolHandler(ph1); |
767 | 814 |
768 scoped_ptr<net::URLRequestJobFactory::Interceptor> interceptor( | 815 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> interceptor( |
769 registry()->CreateURLInterceptor()); | 816 registry()->CreateJobInterceptorFactory()); |
770 AssertWillHandle(scheme, true, interceptor.get()); | 817 AssertWillHandle(scheme, true, interceptor.get()); |
771 } | 818 } |
772 | 819 |
773 TEST_F(ProtocolHandlerRegistryTest, TestRemovingDefaultFallsBackToOldDefault) { | 820 TEST_F(ProtocolHandlerRegistryTest, TestRemovingDefaultFallsBackToOldDefault) { |
774 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); | 821 ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); |
775 ProtocolHandler ph2 = CreateProtocolHandler("mailto", "test2"); | 822 ProtocolHandler ph2 = CreateProtocolHandler("mailto", "test2"); |
776 ProtocolHandler ph3 = CreateProtocolHandler("mailto", "test3"); | 823 ProtocolHandler ph3 = CreateProtocolHandler("mailto", "test3"); |
777 registry()->OnAcceptRegisterProtocolHandler(ph1); | 824 registry()->OnAcceptRegisterProtocolHandler(ph1); |
778 registry()->OnAcceptRegisterProtocolHandler(ph2); | 825 registry()->OnAcceptRegisterProtocolHandler(ph2); |
779 registry()->OnAcceptRegisterProtocolHandler(ph3); | 826 registry()->OnAcceptRegisterProtocolHandler(ph3); |
(...skipping 25 matching lines...) Expand all Loading... |
805 ASSERT_EQ(ph2, handlers[0]); | 852 ASSERT_EQ(ph2, handlers[0]); |
806 ASSERT_EQ(ph1, handlers[1]); | 853 ASSERT_EQ(ph1, handlers[1]); |
807 } | 854 } |
808 | 855 |
809 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestClearDefaultGetsPropagatedToIO) { | 856 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestClearDefaultGetsPropagatedToIO) { |
810 std::string scheme("mailto"); | 857 std::string scheme("mailto"); |
811 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); | 858 ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); |
812 registry()->OnAcceptRegisterProtocolHandler(ph1); | 859 registry()->OnAcceptRegisterProtocolHandler(ph1); |
813 registry()->ClearDefault(scheme); | 860 registry()->ClearDefault(scheme); |
814 | 861 |
815 scoped_ptr<net::URLRequestJobFactory::Interceptor> interceptor( | 862 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> interceptor( |
816 registry()->CreateURLInterceptor()); | 863 registry()->CreateJobInterceptorFactory()); |
817 AssertWillHandle(scheme, false, interceptor.get()); | 864 AssertWillHandle(scheme, false, interceptor.get()); |
818 } | 865 } |
819 | 866 |
820 TEST_F(ProtocolHandlerRegistryTest, TestLoadEnabledGetsPropogatedToIO) { | 867 TEST_F(ProtocolHandlerRegistryTest, TestLoadEnabledGetsPropogatedToIO) { |
821 std::string mailto("mailto"); | 868 std::string mailto("mailto"); |
822 ProtocolHandler ph1 = CreateProtocolHandler(mailto, "MailtoHandler"); | 869 ProtocolHandler ph1 = CreateProtocolHandler(mailto, "MailtoHandler"); |
823 registry()->OnAcceptRegisterProtocolHandler(ph1); | 870 registry()->OnAcceptRegisterProtocolHandler(ph1); |
824 | 871 |
825 scoped_ptr<net::URLRequestJobFactory::Interceptor> interceptor( | 872 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> interceptor( |
826 registry()->CreateURLInterceptor()); | 873 registry()->CreateJobInterceptorFactory()); |
827 AssertWillHandle(mailto, true, interceptor.get()); | 874 AssertWillHandle(mailto, true, interceptor.get()); |
828 registry()->Disable(); | 875 registry()->Disable(); |
829 AssertWillHandle(mailto, false, interceptor.get()); | 876 AssertWillHandle(mailto, false, interceptor.get()); |
830 } | 877 } |
831 | 878 |
832 TEST_F(ProtocolHandlerRegistryTest, TestReplaceHandler) { | 879 TEST_F(ProtocolHandlerRegistryTest, TestReplaceHandler) { |
833 ProtocolHandler ph1 = CreateProtocolHandler("mailto", | 880 ProtocolHandler ph1 = CreateProtocolHandler("mailto", |
834 GURL("http://test.com/%s"), "test1"); | 881 GURL("http://test.com/%s"), "test1"); |
835 ProtocolHandler ph2 = CreateProtocolHandler("mailto", | 882 ProtocolHandler ph2 = CreateProtocolHandler("mailto", |
836 GURL("http://test.com/updated-url/%s"), "test2"); | 883 GURL("http://test.com/updated-url/%s"), "test2"); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
891 | 938 |
892 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestInstallDefaultHandler) { | 939 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestInstallDefaultHandler) { |
893 RecreateRegistry(false); | 940 RecreateRegistry(false); |
894 registry()->AddPredefinedHandler(CreateProtocolHandler( | 941 registry()->AddPredefinedHandler(CreateProtocolHandler( |
895 "test", GURL("http://test.com/%s"), "Test")); | 942 "test", GURL("http://test.com/%s"), "Test")); |
896 registry()->InitProtocolSettings(); | 943 registry()->InitProtocolSettings(); |
897 std::vector<std::string> protocols; | 944 std::vector<std::string> protocols; |
898 registry()->GetRegisteredProtocols(&protocols); | 945 registry()->GetRegisteredProtocols(&protocols); |
899 ASSERT_EQ(static_cast<size_t>(1), protocols.size()); | 946 ASSERT_EQ(static_cast<size_t>(1), protocols.size()); |
900 } | 947 } |
OLD | NEW |