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

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

Issue 293983004: Modify ProtocolHandlerRegistry to make room for policy (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added unit test for the changes Created 6 years, 6 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
« no previous file with comments | « chrome/browser/custom_handlers/protocol_handler_registry.cc ('k') | chrome/common/pref_names.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <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
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
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
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 TEST_F(ProtocolHandlerRegistryTest, TestPrefPolicyOverlapRegister) {
968 base::ListValue* handlers_registered_by_pref = new base::ListValue();
969 base::ListValue* handlers_registered_by_policy = new base::ListValue();
970
971 handlers_registered_by_pref->Append(
972 GetProtocolHandlerValueWithDefault("p1", "http://p1u2.com/%s", true));
koz (OOO until 15th September) 2014/06/01 23:44:29 Cool tests! One nit: Would this be easier to read
kaliamoorthi 2014/06/02 09:47:31 Done.
973 handlers_registered_by_pref->Append(
974 GetProtocolHandlerValueWithDefault("p1", "http://p1u1.com/%s", true));
975 handlers_registered_by_pref->Append(
976 GetProtocolHandlerValueWithDefault("p1", "http://p1u2.com/%s", false));
977
978 handlers_registered_by_policy->Append(
979 GetProtocolHandlerValueWithDefault("p1", "http://p1u1.com/%s", false));
980 handlers_registered_by_policy->Append(
981 GetProtocolHandlerValueWithDefault("p3", "http://p3u1.com/%s", true));
982
983 profile()->GetPrefs()->Set(
984 prefs::kRegisteredProtocolHandlers,
985 *static_cast<base::Value*>(handlers_registered_by_pref));
986 profile()->GetPrefs()->Set(
987 prefs::kPolicyRegisteredProtocolHandlers,
988 *static_cast<base::Value*>(handlers_registered_by_policy));
989 registry()->InitProtocolSettings();
990
991 // Duplicate p1u2 eliminated in memory but not yet saved in pref
992 ASSERT_EQ(InPrefHandlerCount(), 3);
993 ASSERT_EQ(InMemoryHandlerCount(), 3);
994 ASSERT_TRUE(registry()->IsDefault(
995 CreateProtocolHandler("p1", GURL("http://p1u1.com/%s"))));
996 ASSERT_FALSE(registry()->IsDefault(
997 CreateProtocolHandler("p1", GURL("http://p1u2.com/%s"))));
998
999 registry()->OnDenyRegisterProtocolHandler(
1000 CreateProtocolHandler("p2", GURL("http://p2u1.com/%s")));
1001
1002 // Duplicate p1u2 saved in pref and a new handler added to pref and memory
1003 ASSERT_EQ(InPrefHandlerCount(), 3);
1004 ASSERT_EQ(InMemoryHandlerCount(), 4);
1005 ASSERT_FALSE(registry()->IsDefault(
1006 CreateProtocolHandler("p2", GURL("http://p2u1.com/%s"))));
1007
1008 registry()->RemoveHandler(
1009 CreateProtocolHandler("p1", GURL("http://p1u1.com/%s")));
1010
1011 // p1u1 removed from user pref but not from memory due to policy.
1012 ASSERT_EQ(InPrefHandlerCount(), 2);
1013 ASSERT_EQ(InMemoryHandlerCount(), 4);
1014 ASSERT_TRUE(registry()->IsDefault(
1015 CreateProtocolHandler("p1", GURL("http://p1u1.com/%s"))));
1016
1017 registry()->RemoveHandler(
1018 CreateProtocolHandler("p3", GURL("http://p3u1.com/%s")));
1019
1020 // p3u1 not removed from memory due to policy and it was never in pref.
1021 ASSERT_EQ(InPrefHandlerCount(), 2);
1022 ASSERT_EQ(InMemoryHandlerCount(), 4);
1023 ASSERT_TRUE(registry()->IsDefault(
1024 CreateProtocolHandler("p3", GURL("http://p3u1.com/%s"))));
1025
1026 registry()->RemoveHandler(
1027 CreateProtocolHandler("p1", GURL("http://p1u2.com/%s")));
1028
1029 // p1u2 removed from user pref and memory.
1030 ASSERT_EQ(InPrefHandlerCount(), 1);
1031 ASSERT_EQ(InMemoryHandlerCount(), 3);
1032 ASSERT_TRUE(registry()->IsDefault(
1033 CreateProtocolHandler("p1", GURL("http://p1u1.com/%s"))));
1034
1035 registry()->OnAcceptRegisterProtocolHandler(
1036 CreateProtocolHandler("p1", GURL("http://p1u3.com/%s")));
1037
1038 // p1u3 added to pref and memory.
1039 ASSERT_EQ(InPrefHandlerCount(), 2);
1040 ASSERT_EQ(InMemoryHandlerCount(), 4);
1041 ASSERT_FALSE(registry()->IsDefault(
1042 CreateProtocolHandler("p1", GURL("http://p1u1.com/%s"))));
1043 ASSERT_TRUE(registry()->IsDefault(
1044 CreateProtocolHandler("p1", GURL("http://p1u3.com/%s"))));
1045
1046 registry()->RemoveHandler(
1047 CreateProtocolHandler("p1", GURL("http://p1u3.com/%s")));
1048
1049 // p1u3 the default handler for p1 removed from user pref and memory.
1050 ASSERT_EQ(InPrefHandlerCount(), 1);
1051 ASSERT_EQ(InMemoryHandlerCount(), 3);
1052 ASSERT_FALSE(registry()->IsDefault(
1053 CreateProtocolHandler("p1", GURL("http://p1u3.com/%s"))));
1054 ASSERT_TRUE(registry()->IsDefault(
1055 CreateProtocolHandler("p1", GURL("http://p1u1.com/%s"))));
1056 ASSERT_TRUE(registry()->IsDefault(
1057 CreateProtocolHandler("p3", GURL("http://p3u1.com/%s"))));
1058 ASSERT_FALSE(registry()->IsDefault(
1059 CreateProtocolHandler("p2", GURL("http://p2u1.com/%s"))));
1060 }
1061
1062 TEST_F(ProtocolHandlerRegistryTest, TestPrefPolicyOverlapIgnore) {
1063 base::ListValue* handlers_ignored_by_pref = new base::ListValue();
1064 base::ListValue* handlers_ignored_by_policy = new base::ListValue();
1065
1066 handlers_ignored_by_pref->Append(
1067 GetProtocolHandlerValue("p1", "http://p1u1.com/%s"));
1068 handlers_ignored_by_pref->Append(
1069 GetProtocolHandlerValue("p1", "http://p1u2.com/%s"));
1070 handlers_ignored_by_pref->Append(
1071 GetProtocolHandlerValue("p1", "http://p1u2.com/%s"));
1072 handlers_ignored_by_pref->Append(
1073 GetProtocolHandlerValue("p3", "http://p3u1.com/%s"));
1074
1075 handlers_ignored_by_policy->Append(
1076 GetProtocolHandlerValue("p1", "http://p1u2.com/%s"));
1077 handlers_ignored_by_policy->Append(
1078 GetProtocolHandlerValue("p1", "http://p1u3.com/%s"));
1079 handlers_ignored_by_policy->Append(
1080 GetProtocolHandlerValue("p2", "http://p2u1.com/%s"));
1081
1082 profile()->GetPrefs()->Set(
1083 prefs::kIgnoredProtocolHandlers,
1084 *static_cast<base::Value*>(handlers_ignored_by_pref));
1085 profile()->GetPrefs()->Set(
1086 prefs::kPolicyIgnoredProtocolHandlers,
1087 *static_cast<base::Value*>(handlers_ignored_by_policy));
1088 registry()->InitProtocolSettings();
1089
1090 // Duplicate p1u2 eliminated in memory but not yet saved in pref
1091 ASSERT_EQ(InPrefIgnoredHandlerCount(), 4);
1092 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 5);
1093
1094 registry()->OnIgnoreRegisterProtocolHandler(
1095 CreateProtocolHandler("p2", GURL("http://p2u2.com/%s")));
1096
1097 // Duplicate p1u2 eliminated in pref, p2u2 added to pref and memory.
1098 ASSERT_EQ(InPrefIgnoredHandlerCount(), 4);
1099 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 6);
1100
1101 registry()->RemoveIgnoredHandler(
1102 CreateProtocolHandler("p2", GURL("http://p2u1.com/%s")));
1103
1104 // p2u1 installed by policy so cant be removed.
1105 ASSERT_EQ(InPrefIgnoredHandlerCount(), 4);
1106 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 6);
1107
1108 registry()->RemoveIgnoredHandler(
1109 CreateProtocolHandler("p1", GURL("http://p1u2.com/%s")));
1110
1111 // p1u2 installed by policy and pref so it is removed from pref and not from
1112 // memory.
1113 ASSERT_EQ(InPrefIgnoredHandlerCount(), 3);
1114 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 6);
1115
1116 registry()->RemoveIgnoredHandler(
1117 CreateProtocolHandler("p1", GURL("http://p1u1.com/%s")));
1118
1119 // p1u1 installed by pref so it is removed from pref and memory.
1120 ASSERT_EQ(InPrefIgnoredHandlerCount(), 2);
1121 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 5);
1122
1123 registry()->RemoveIgnoredHandler(
1124 CreateProtocolHandler("p2", GURL("http://p2u2.com/%s")));
1125
1126 // p2u2 installed by user so it is removed from pref and memory.
1127 ASSERT_EQ(InPrefIgnoredHandlerCount(), 1);
1128 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4);
1129
1130 registry()->OnIgnoreRegisterProtocolHandler(
1131 CreateProtocolHandler("p2", GURL("http://p2u1.com/%s")));
1132
1133 // p2u1 installed by user but it is already installed by policy, so it is
1134 // added to pref.
1135 ASSERT_EQ(InPrefIgnoredHandlerCount(), 2);
1136 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4);
1137
1138 registry()->RemoveIgnoredHandler(
1139 CreateProtocolHandler("p2", GURL("http://p2u1.com/%s")));
1140
1141 // p2u1 installed by user and policy, so it is removed from pref alone.
1142 ASSERT_EQ(InPrefIgnoredHandlerCount(), 1);
1143 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4);
1144 }
OLDNEW
« no previous file with comments | « chrome/browser/custom_handlers/protocol_handler_registry.cc ('k') | chrome/common/pref_names.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698