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" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
13 #include "chrome/browser/chrome_notification_types.h" | 13 #include "chrome/browser/chrome_notification_types.h" |
14 #include "chrome/browser/prefs/pref_service_syncable.h" | 14 #include "chrome/browser/prefs/pref_service_syncable.h" |
15 #include "chrome/common/custom_handlers/protocol_handler.h" | 15 #include "chrome/common/custom_handlers/protocol_handler.h" |
| 16 #include "chrome/common/pref_names.h" |
16 #include "chrome/test/base/testing_browser_process.h" | 17 #include "chrome/test/base/testing_browser_process.h" |
17 #include "chrome/test/base/testing_profile.h" | 18 #include "chrome/test/base/testing_profile.h" |
18 #include "components/pref_registry/pref_registry_syncable.h" | 19 #include "components/pref_registry/pref_registry_syncable.h" |
19 #include "content/public/browser/notification_observer.h" | 20 #include "content/public/browser/notification_observer.h" |
20 #include "content/public/browser/notification_registrar.h" | 21 #include "content/public/browser/notification_registrar.h" |
21 #include "content/public/browser/notification_source.h" | 22 #include "content/public/browser/notification_source.h" |
22 #include "content/public/test/test_browser_thread.h" | 23 #include "content/public/test/test_browser_thread.h" |
23 #include "content/public/test/test_renderer_host.h" | 24 #include "content/public/test/test_renderer_host.h" |
24 #include "net/base/request_priority.h" | 25 #include "net/base/request_priority.h" |
25 #include "net/url_request/url_request.h" | 26 #include "net/url_request/url_request.h" |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
97 BrowserThread::PostTask(BrowserThread::IO, | 98 BrowserThread::PostTask(BrowserThread::IO, |
98 FROM_HERE, | 99 FROM_HERE, |
99 base::Bind(AssertWillHandleIO, | 100 base::Bind(AssertWillHandleIO, |
100 scheme, | 101 scheme, |
101 expected, | 102 expected, |
102 base::Unretained(interceptor))); | 103 base::Unretained(interceptor))); |
103 base::MessageLoop::current()->RunUntilIdle(); | 104 base::MessageLoop::current()->RunUntilIdle(); |
104 } | 105 } |
105 | 106 |
| 107 base::DictionaryValue* GetProtocolHandlerValue(std::string protocol, |
| 108 std::string url) { |
| 109 base::DictionaryValue* value = new base::DictionaryValue(); |
| 110 value->SetString("protocol", protocol); |
| 111 value->SetString("url", url); |
| 112 return value; |
| 113 } |
| 114 |
| 115 base::DictionaryValue* GetProtocolHandlerValueWithDefault(std::string protocol, |
| 116 std::string url, |
| 117 bool is_default) { |
| 118 base::DictionaryValue* value = GetProtocolHandlerValue(protocol, url); |
| 119 value->SetBoolean("default", is_default); |
| 120 return value; |
| 121 } |
| 122 |
106 class FakeDelegate : public ProtocolHandlerRegistry::Delegate { | 123 class FakeDelegate : public ProtocolHandlerRegistry::Delegate { |
107 public: | 124 public: |
108 FakeDelegate() : force_os_failure_(false) {} | 125 FakeDelegate() : force_os_failure_(false) {} |
109 virtual ~FakeDelegate() { } | 126 virtual ~FakeDelegate() { } |
110 virtual void RegisterExternalHandler(const std::string& protocol) OVERRIDE { | 127 virtual void RegisterExternalHandler(const std::string& protocol) OVERRIDE { |
111 ASSERT_TRUE( | 128 ASSERT_TRUE( |
112 registered_protocols_.find(protocol) == registered_protocols_.end()); | 129 registered_protocols_.find(protocol) == registered_protocols_.end()); |
113 registered_protocols_.insert(protocol); | 130 registered_protocols_.insert(protocol); |
114 } | 131 } |
115 | 132 |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 ProtocolHandler CreateProtocolHandler(const std::string& protocol, | 344 ProtocolHandler CreateProtocolHandler(const std::string& protocol, |
328 const std::string& name) { | 345 const std::string& name) { |
329 return CreateProtocolHandler(protocol, GURL("http://" + name + "/%s")); | 346 return CreateProtocolHandler(protocol, GURL("http://" + name + "/%s")); |
330 } | 347 } |
331 | 348 |
332 void RecreateRegistry(bool initialize) { | 349 void RecreateRegistry(bool initialize) { |
333 TeadDownRegistry(); | 350 TeadDownRegistry(); |
334 SetUpRegistry(initialize); | 351 SetUpRegistry(initialize); |
335 } | 352 } |
336 | 353 |
| 354 int InPrefHandlerCount() { |
| 355 const base::ListValue* in_pref_handlers = |
| 356 profile()->GetPrefs()->GetList(prefs::kRegisteredProtocolHandlers); |
| 357 return static_cast<int>(in_pref_handlers->GetSize()); |
| 358 } |
| 359 |
| 360 int InMemoryHandlerCount() { |
| 361 int in_memory_handler_count = 0; |
| 362 ProtocolHandlerRegistry::ProtocolHandlerMultiMap::iterator it = |
| 363 registry()->protocol_handlers_.begin(); |
| 364 for (; it != registry()->protocol_handlers_.end(); ++it) |
| 365 in_memory_handler_count += it->second.size(); |
| 366 return in_memory_handler_count; |
| 367 } |
| 368 |
| 369 int InPrefIgnoredHandlerCount() { |
| 370 const base::ListValue* in_pref_ignored_handlers = |
| 371 profile()->GetPrefs()->GetList(prefs::kIgnoredProtocolHandlers); |
| 372 return static_cast<int>(in_pref_ignored_handlers->GetSize()); |
| 373 } |
| 374 |
| 375 int InMemoryIgnoredHandlerCount() { |
| 376 int in_memory_ignored_handler_count = 0; |
| 377 ProtocolHandlerRegistry::ProtocolHandlerList::iterator it = |
| 378 registry()->ignored_protocol_handlers_.begin(); |
| 379 for (; it != registry()->ignored_protocol_handlers_.end(); ++it) |
| 380 in_memory_ignored_handler_count++; |
| 381 return in_memory_ignored_handler_count; |
| 382 } |
| 383 |
337 // Returns a new registry, initializing it if |initialize| is true. | 384 // Returns a new registry, initializing it if |initialize| is true. |
338 // Caller assumes ownership for the object | 385 // Caller assumes ownership for the object |
339 void SetUpRegistry(bool initialize) { | 386 void SetUpRegistry(bool initialize) { |
340 delegate_ = new FakeDelegate(); | 387 delegate_ = new FakeDelegate(); |
341 registry_.reset(new ProtocolHandlerRegistry(profile(), delegate())); | 388 registry_.reset(new ProtocolHandlerRegistry(profile(), delegate())); |
342 if (initialize) registry_->InitProtocolSettings(); | 389 if (initialize) registry_->InitProtocolSettings(); |
343 } | 390 } |
344 | 391 |
345 void TeadDownRegistry() { | 392 void TeadDownRegistry() { |
346 registry_->Shutdown(); | 393 registry_->Shutdown(); |
(...skipping 562 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
909 | 956 |
910 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestInstallDefaultHandler) { | 957 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestInstallDefaultHandler) { |
911 RecreateRegistry(false); | 958 RecreateRegistry(false); |
912 registry()->AddPredefinedHandler( | 959 registry()->AddPredefinedHandler( |
913 CreateProtocolHandler("test", GURL("http://test.com/%s"))); | 960 CreateProtocolHandler("test", GURL("http://test.com/%s"))); |
914 registry()->InitProtocolSettings(); | 961 registry()->InitProtocolSettings(); |
915 std::vector<std::string> protocols; | 962 std::vector<std::string> protocols; |
916 registry()->GetRegisteredProtocols(&protocols); | 963 registry()->GetRegisteredProtocols(&protocols); |
917 ASSERT_EQ(static_cast<size_t>(1), protocols.size()); | 964 ASSERT_EQ(static_cast<size_t>(1), protocols.size()); |
918 } | 965 } |
| 966 |
| 967 #define URL_p1u1 "http://p1u1.com/%s" |
| 968 #define URL_p1u2 "http://p1u2.com/%s" |
| 969 #define URL_p1u3 "http://p1u3.com/%s" |
| 970 #define URL_p2u1 "http://p2u1.com/%s" |
| 971 #define URL_p2u2 "http://p2u2.com/%s" |
| 972 #define URL_p3u1 "http://p3u1.com/%s" |
| 973 |
| 974 TEST_F(ProtocolHandlerRegistryTest, TestPrefPolicyOverlapRegister) { |
| 975 base::ListValue* handlers_registered_by_pref = new base::ListValue(); |
| 976 base::ListValue* handlers_registered_by_policy = new base::ListValue(); |
| 977 |
| 978 handlers_registered_by_pref->Append( |
| 979 GetProtocolHandlerValueWithDefault("p1", URL_p1u2, true)); |
| 980 handlers_registered_by_pref->Append( |
| 981 GetProtocolHandlerValueWithDefault("p1", URL_p1u1, true)); |
| 982 handlers_registered_by_pref->Append( |
| 983 GetProtocolHandlerValueWithDefault("p1", URL_p1u2, false)); |
| 984 |
| 985 handlers_registered_by_policy->Append( |
| 986 GetProtocolHandlerValueWithDefault("p1", URL_p1u1, false)); |
| 987 handlers_registered_by_policy->Append( |
| 988 GetProtocolHandlerValueWithDefault("p3", URL_p3u1, true)); |
| 989 |
| 990 profile()->GetPrefs()->Set( |
| 991 prefs::kRegisteredProtocolHandlers, |
| 992 *static_cast<base::Value*>(handlers_registered_by_pref)); |
| 993 profile()->GetPrefs()->Set( |
| 994 prefs::kPolicyRegisteredProtocolHandlers, |
| 995 *static_cast<base::Value*>(handlers_registered_by_policy)); |
| 996 registry()->InitProtocolSettings(); |
| 997 |
| 998 // Duplicate p1u2 eliminated in memory but not yet saved in pref |
| 999 ProtocolHandler p1u1 = CreateProtocolHandler("p1", GURL(URL_p1u1)); |
| 1000 ProtocolHandler p1u2 = CreateProtocolHandler("p1", GURL(URL_p1u2)); |
| 1001 ASSERT_EQ(InPrefHandlerCount(), 3); |
| 1002 ASSERT_EQ(InMemoryHandlerCount(), 3); |
| 1003 ASSERT_TRUE(registry()->IsDefault(p1u1)); |
| 1004 ASSERT_FALSE(registry()->IsDefault(p1u2)); |
| 1005 |
| 1006 ProtocolHandler p2u1 = CreateProtocolHandler("p2", GURL(URL_p2u1)); |
| 1007 registry()->OnDenyRegisterProtocolHandler(p2u1); |
| 1008 |
| 1009 // Duplicate p1u2 saved in pref and a new handler added to pref and memory |
| 1010 ASSERT_EQ(InPrefHandlerCount(), 3); |
| 1011 ASSERT_EQ(InMemoryHandlerCount(), 4); |
| 1012 ASSERT_FALSE(registry()->IsDefault(p2u1)); |
| 1013 |
| 1014 registry()->RemoveHandler(p1u1); |
| 1015 |
| 1016 // p1u1 removed from user pref but not from memory due to policy. |
| 1017 ASSERT_EQ(InPrefHandlerCount(), 2); |
| 1018 ASSERT_EQ(InMemoryHandlerCount(), 4); |
| 1019 ASSERT_TRUE(registry()->IsDefault(p1u1)); |
| 1020 |
| 1021 ProtocolHandler p3u1 = CreateProtocolHandler("p3", GURL(URL_p3u1)); |
| 1022 registry()->RemoveHandler(p3u1); |
| 1023 |
| 1024 // p3u1 not removed from memory due to policy and it was never in pref. |
| 1025 ASSERT_EQ(InPrefHandlerCount(), 2); |
| 1026 ASSERT_EQ(InMemoryHandlerCount(), 4); |
| 1027 ASSERT_TRUE(registry()->IsDefault(p3u1)); |
| 1028 |
| 1029 registry()->RemoveHandler(p1u2); |
| 1030 |
| 1031 // p1u2 removed from user pref and memory. |
| 1032 ASSERT_EQ(InPrefHandlerCount(), 1); |
| 1033 ASSERT_EQ(InMemoryHandlerCount(), 3); |
| 1034 ASSERT_TRUE(registry()->IsDefault(p1u1)); |
| 1035 |
| 1036 ProtocolHandler p1u3 = CreateProtocolHandler("p1", GURL(URL_p1u3)); |
| 1037 registry()->OnAcceptRegisterProtocolHandler(p1u3); |
| 1038 |
| 1039 // p1u3 added to pref and memory. |
| 1040 ASSERT_EQ(InPrefHandlerCount(), 2); |
| 1041 ASSERT_EQ(InMemoryHandlerCount(), 4); |
| 1042 ASSERT_FALSE(registry()->IsDefault(p1u1)); |
| 1043 ASSERT_TRUE(registry()->IsDefault(p1u3)); |
| 1044 |
| 1045 registry()->RemoveHandler(p1u3); |
| 1046 |
| 1047 // p1u3 the default handler for p1 removed from user pref and memory. |
| 1048 ASSERT_EQ(InPrefHandlerCount(), 1); |
| 1049 ASSERT_EQ(InMemoryHandlerCount(), 3); |
| 1050 ASSERT_FALSE(registry()->IsDefault(p1u3)); |
| 1051 ASSERT_TRUE(registry()->IsDefault(p1u1)); |
| 1052 ASSERT_TRUE(registry()->IsDefault(p3u1)); |
| 1053 ASSERT_FALSE(registry()->IsDefault(p2u1)); |
| 1054 } |
| 1055 |
| 1056 TEST_F(ProtocolHandlerRegistryTest, TestPrefPolicyOverlapIgnore) { |
| 1057 base::ListValue* handlers_ignored_by_pref = new base::ListValue(); |
| 1058 base::ListValue* handlers_ignored_by_policy = new base::ListValue(); |
| 1059 |
| 1060 handlers_ignored_by_pref->Append(GetProtocolHandlerValue("p1", URL_p1u1)); |
| 1061 handlers_ignored_by_pref->Append(GetProtocolHandlerValue("p1", URL_p1u2)); |
| 1062 handlers_ignored_by_pref->Append(GetProtocolHandlerValue("p1", URL_p1u2)); |
| 1063 handlers_ignored_by_pref->Append(GetProtocolHandlerValue("p3", URL_p3u1)); |
| 1064 |
| 1065 handlers_ignored_by_policy->Append(GetProtocolHandlerValue("p1", URL_p1u2)); |
| 1066 handlers_ignored_by_policy->Append(GetProtocolHandlerValue("p1", URL_p1u3)); |
| 1067 handlers_ignored_by_policy->Append(GetProtocolHandlerValue("p2", URL_p2u1)); |
| 1068 |
| 1069 profile()->GetPrefs()->Set( |
| 1070 prefs::kIgnoredProtocolHandlers, |
| 1071 *static_cast<base::Value*>(handlers_ignored_by_pref)); |
| 1072 profile()->GetPrefs()->Set( |
| 1073 prefs::kPolicyIgnoredProtocolHandlers, |
| 1074 *static_cast<base::Value*>(handlers_ignored_by_policy)); |
| 1075 registry()->InitProtocolSettings(); |
| 1076 |
| 1077 // Duplicate p1u2 eliminated in memory but not yet saved in pref |
| 1078 ASSERT_EQ(InPrefIgnoredHandlerCount(), 4); |
| 1079 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 5); |
| 1080 |
| 1081 ProtocolHandler p2u2 = CreateProtocolHandler("p2", GURL(URL_p2u2)); |
| 1082 registry()->OnIgnoreRegisterProtocolHandler(p2u2); |
| 1083 |
| 1084 // Duplicate p1u2 eliminated in pref, p2u2 added to pref and memory. |
| 1085 ASSERT_EQ(InPrefIgnoredHandlerCount(), 4); |
| 1086 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 6); |
| 1087 |
| 1088 ProtocolHandler p2u1 = CreateProtocolHandler("p2", GURL(URL_p2u1)); |
| 1089 registry()->RemoveIgnoredHandler(p2u1); |
| 1090 |
| 1091 // p2u1 installed by policy so cant be removed. |
| 1092 ASSERT_EQ(InPrefIgnoredHandlerCount(), 4); |
| 1093 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 6); |
| 1094 |
| 1095 ProtocolHandler p1u2 = CreateProtocolHandler("p1", GURL(URL_p1u2)); |
| 1096 registry()->RemoveIgnoredHandler(p1u2); |
| 1097 |
| 1098 // p1u2 installed by policy and pref so it is removed from pref and not from |
| 1099 // memory. |
| 1100 ASSERT_EQ(InPrefIgnoredHandlerCount(), 3); |
| 1101 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 6); |
| 1102 |
| 1103 ProtocolHandler p1u1 = CreateProtocolHandler("p1", GURL(URL_p1u1)); |
| 1104 registry()->RemoveIgnoredHandler(p1u1); |
| 1105 |
| 1106 // p1u1 installed by pref so it is removed from pref and memory. |
| 1107 ASSERT_EQ(InPrefIgnoredHandlerCount(), 2); |
| 1108 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 5); |
| 1109 |
| 1110 registry()->RemoveIgnoredHandler(p2u2); |
| 1111 |
| 1112 // p2u2 installed by user so it is removed from pref and memory. |
| 1113 ASSERT_EQ(InPrefIgnoredHandlerCount(), 1); |
| 1114 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4); |
| 1115 |
| 1116 registry()->OnIgnoreRegisterProtocolHandler(p2u1); |
| 1117 |
| 1118 // p2u1 installed by user but it is already installed by policy, so it is |
| 1119 // added to pref. |
| 1120 ASSERT_EQ(InPrefIgnoredHandlerCount(), 2); |
| 1121 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4); |
| 1122 |
| 1123 registry()->RemoveIgnoredHandler(p2u1); |
| 1124 |
| 1125 // p2u1 installed by user and policy, so it is removed from pref alone. |
| 1126 ASSERT_EQ(InPrefIgnoredHandlerCount(), 1); |
| 1127 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4); |
| 1128 } |
OLD | NEW |