OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "extensions/browser/event_listener_map.h" | 5 #include "extensions/browser/event_listener_map.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 namespace extensions { | 21 namespace extensions { |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 const char kExt1Id[] = "extension_1"; | 25 const char kExt1Id[] = "extension_1"; |
26 const char kExt2Id[] = "extension_2"; | 26 const char kExt2Id[] = "extension_2"; |
27 const char kEvent1Name[] = "event1"; | 27 const char kEvent1Name[] = "event1"; |
28 const char kEvent2Name[] = "event2"; | 28 const char kEvent2Name[] = "event2"; |
29 const char kURL[] = "https://google.com/some/url"; | 29 const char kURL[] = "https://google.com/some/url"; |
30 | 30 |
31 typedef base::Callback<std::unique_ptr<EventListener>( | 31 using EventListenerConstructor = base::Callback<std::unique_ptr<EventListener>( |
32 const std::string&, // event_name | 32 const std::string& /* event_name */, |
33 content::RenderProcessHost*, // process | 33 content::RenderProcessHost* /* process */, |
34 base::DictionaryValue* // filter (takes ownership) | 34 std::unique_ptr<base::DictionaryValue> /* filter */)>; |
35 )> | |
36 EventListenerConstructor; | |
37 | 35 |
38 class EmptyDelegate : public EventListenerMap::Delegate { | 36 class EmptyDelegate : public EventListenerMap::Delegate { |
39 void OnListenerAdded(const EventListener* listener) override{}; | 37 void OnListenerAdded(const EventListener* listener) override {} |
40 void OnListenerRemoved(const EventListener* listener) override{}; | 38 void OnListenerRemoved(const EventListener* listener) override {} |
41 }; | 39 }; |
42 | 40 |
43 class EventListenerMapTest : public testing::Test { | 41 class EventListenerMapTest : public testing::Test { |
44 public: | 42 public: |
45 EventListenerMapTest() | 43 EventListenerMapTest() |
46 : delegate_(new EmptyDelegate), | 44 : delegate_(base::MakeUnique<EmptyDelegate>()), |
47 listeners_(new EventListenerMap(delegate_.get())), | 45 listeners_(base::MakeUnique<EventListenerMap>(delegate_.get())), |
48 browser_context_(new content::TestBrowserContext), | 46 browser_context_(base::MakeUnique<content::TestBrowserContext>()), |
49 process_(new content::MockRenderProcessHost(browser_context_.get())) {} | 47 process_(base::MakeUnique<content::MockRenderProcessHost>( |
| 48 browser_context_.get())) {} |
50 | 49 |
51 std::unique_ptr<DictionaryValue> CreateHostSuffixFilter( | 50 std::unique_ptr<DictionaryValue> CreateHostSuffixFilter( |
52 const std::string& suffix) { | 51 const std::string& suffix) { |
53 std::unique_ptr<DictionaryValue> filter(new DictionaryValue); | 52 auto filter_dict = base::MakeUnique<DictionaryValue>(); |
54 std::unique_ptr<ListValue> filter_list(new ListValue); | 53 filter_dict->Set("hostSuffix", base::MakeUnique<Value>(suffix)); |
55 std::unique_ptr<DictionaryValue> filter_dict(new DictionaryValue); | |
56 | 54 |
57 filter_dict->Set("hostSuffix", new Value(suffix)); | 55 auto filter_list = base::MakeUnique<ListValue>(); |
| 56 filter_list->Append(std::move(filter_dict)); |
58 | 57 |
59 filter_list->Append(std::move(filter_dict)); | 58 auto filter = base::MakeUnique<DictionaryValue>(); |
60 filter->Set("url", filter_list.release()); | 59 filter->Set("url", std::move(filter_list)); |
61 return filter; | 60 return filter; |
62 } | 61 } |
63 | 62 |
64 std::unique_ptr<Event> CreateNamedEvent(const std::string& event_name) { | 63 std::unique_ptr<Event> CreateNamedEvent(const std::string& event_name) { |
65 return CreateEvent(event_name, GURL()); | 64 return CreateEvent(event_name, GURL()); |
66 } | 65 } |
67 | 66 |
68 std::unique_ptr<Event> CreateEvent(const std::string& event_name, | 67 std::unique_ptr<Event> CreateEvent(const std::string& event_name, |
69 const GURL& url) { | 68 const GURL& url) { |
70 EventFilteringInfo info; | 69 EventFilteringInfo info; |
71 info.SetURL(url); | 70 info.SetURL(url); |
72 std::unique_ptr<Event> result( | 71 return base::MakeUnique<Event>( |
73 new Event(events::FOR_TEST, event_name, base::MakeUnique<ListValue>(), | 72 events::FOR_TEST, event_name, base::MakeUnique<ListValue>(), nullptr, |
74 NULL, GURL(), EventRouter::USER_GESTURE_UNKNOWN, info)); | 73 GURL(), EventRouter::USER_GESTURE_UNKNOWN, info); |
75 return result; | |
76 } | 74 } |
77 | 75 |
78 protected: | 76 protected: |
79 void TestUnfilteredEventsGoToAllListeners( | 77 void TestUnfilteredEventsGoToAllListeners( |
80 const EventListenerConstructor& constructor); | 78 const EventListenerConstructor& constructor); |
81 void TestRemovingByProcess(const EventListenerConstructor& constructor); | 79 void TestRemovingByProcess(const EventListenerConstructor& constructor); |
82 void TestRemovingByListener(const EventListenerConstructor& constructor); | 80 void TestRemovingByListener(const EventListenerConstructor& constructor); |
83 void TestAddExistingUnfilteredListener( | 81 void TestAddExistingUnfilteredListener( |
84 const EventListenerConstructor& constructor); | 82 const EventListenerConstructor& constructor); |
85 void TestHasListenerForEvent(const EventListenerConstructor& constructor); | 83 void TestHasListenerForEvent(const EventListenerConstructor& constructor); |
86 | 84 |
87 std::unique_ptr<EventListenerMap::Delegate> delegate_; | 85 std::unique_ptr<EventListenerMap::Delegate> delegate_; |
88 std::unique_ptr<EventListenerMap> listeners_; | 86 std::unique_ptr<EventListenerMap> listeners_; |
89 std::unique_ptr<content::TestBrowserContext> browser_context_; | 87 std::unique_ptr<content::TestBrowserContext> browser_context_; |
90 std::unique_ptr<content::MockRenderProcessHost> process_; | 88 std::unique_ptr<content::MockRenderProcessHost> process_; |
91 }; | 89 }; |
92 | 90 |
93 std::unique_ptr<EventListener> CreateEventListenerForExtension( | 91 std::unique_ptr<EventListener> CreateEventListenerForExtension( |
94 const std::string& extension_id, | 92 const std::string& extension_id, |
95 const std::string& event_name, | 93 const std::string& event_name, |
96 content::RenderProcessHost* process, | 94 content::RenderProcessHost* process, |
97 base::DictionaryValue* filter) { | 95 std::unique_ptr<base::DictionaryValue> filter) { |
98 return EventListener::ForExtension(event_name, extension_id, process, | 96 return EventListener::ForExtension(event_name, extension_id, process, |
99 base::WrapUnique(filter)); | 97 std::move(filter)); |
100 } | 98 } |
101 | 99 |
102 std::unique_ptr<EventListener> CreateEventListenerForURL( | 100 std::unique_ptr<EventListener> CreateEventListenerForURL( |
103 const GURL& listener_url, | 101 const GURL& listener_url, |
104 const std::string& event_name, | 102 const std::string& event_name, |
105 content::RenderProcessHost* process, | 103 content::RenderProcessHost* process, |
106 base::DictionaryValue* filter) { | 104 std::unique_ptr<base::DictionaryValue> filter) { |
107 return EventListener::ForURL(event_name, listener_url, process, | 105 return EventListener::ForURL(event_name, listener_url, process, |
108 base::WrapUnique(filter)); | 106 std::move(filter)); |
109 } | 107 } |
110 | 108 |
111 void EventListenerMapTest::TestUnfilteredEventsGoToAllListeners( | 109 void EventListenerMapTest::TestUnfilteredEventsGoToAllListeners( |
112 const EventListenerConstructor& constructor) { | 110 const EventListenerConstructor& constructor) { |
113 listeners_->AddListener( | 111 listeners_->AddListener(constructor.Run( |
114 constructor.Run(kEvent1Name, NULL, new DictionaryValue())); | 112 kEvent1Name, nullptr, base::MakeUnique<base::DictionaryValue>())); |
115 std::unique_ptr<Event> event(CreateNamedEvent(kEvent1Name)); | 113 std::unique_ptr<Event> event(CreateNamedEvent(kEvent1Name)); |
116 ASSERT_EQ(1u, listeners_->GetEventListeners(*event).size()); | 114 ASSERT_EQ(1u, listeners_->GetEventListeners(*event).size()); |
117 } | 115 } |
118 | 116 |
119 TEST_F(EventListenerMapTest, UnfilteredEventsGoToAllListenersForExtensions) { | 117 TEST_F(EventListenerMapTest, UnfilteredEventsGoToAllListenersForExtensions) { |
120 TestUnfilteredEventsGoToAllListeners( | 118 TestUnfilteredEventsGoToAllListeners( |
121 base::Bind(&CreateEventListenerForExtension, kExt1Id)); | 119 base::Bind(&CreateEventListenerForExtension, kExt1Id)); |
122 } | 120 } |
123 | 121 |
124 TEST_F(EventListenerMapTest, UnfilteredEventsGoToAllListenersForURLs) { | 122 TEST_F(EventListenerMapTest, UnfilteredEventsGoToAllListenersForURLs) { |
125 TestUnfilteredEventsGoToAllListeners( | 123 TestUnfilteredEventsGoToAllListeners( |
126 base::Bind(&CreateEventListenerForURL, GURL(kURL))); | 124 base::Bind(&CreateEventListenerForURL, GURL(kURL))); |
127 } | 125 } |
128 | 126 |
129 TEST_F(EventListenerMapTest, FilteredEventsGoToAllMatchingListeners) { | 127 TEST_F(EventListenerMapTest, FilteredEventsGoToAllMatchingListeners) { |
130 listeners_->AddListener(EventListener::ForExtension( | 128 listeners_->AddListener(EventListener::ForExtension( |
131 kEvent1Name, kExt1Id, NULL, CreateHostSuffixFilter("google.com"))); | 129 kEvent1Name, kExt1Id, nullptr, CreateHostSuffixFilter("google.com"))); |
132 listeners_->AddListener(EventListener::ForExtension( | 130 listeners_->AddListener(EventListener::ForExtension( |
133 kEvent1Name, kExt1Id, NULL, | 131 kEvent1Name, kExt1Id, nullptr, base::MakeUnique<DictionaryValue>())); |
134 std::unique_ptr<DictionaryValue>(new DictionaryValue))); | |
135 | 132 |
136 std::unique_ptr<Event> event(CreateNamedEvent(kEvent1Name)); | 133 std::unique_ptr<Event> event(CreateNamedEvent(kEvent1Name)); |
137 event->filter_info.SetURL(GURL("http://www.google.com")); | 134 event->filter_info.SetURL(GURL("http://www.google.com")); |
138 std::set<const EventListener*> targets(listeners_->GetEventListeners(*event)); | 135 std::set<const EventListener*> targets(listeners_->GetEventListeners(*event)); |
139 ASSERT_EQ(2u, targets.size()); | 136 ASSERT_EQ(2u, targets.size()); |
140 } | 137 } |
141 | 138 |
142 TEST_F(EventListenerMapTest, FilteredEventsOnlyGoToMatchingListeners) { | 139 TEST_F(EventListenerMapTest, FilteredEventsOnlyGoToMatchingListeners) { |
143 listeners_->AddListener(EventListener::ForExtension( | 140 listeners_->AddListener(EventListener::ForExtension( |
144 kEvent1Name, kExt1Id, NULL, CreateHostSuffixFilter("google.com"))); | 141 kEvent1Name, kExt1Id, nullptr, CreateHostSuffixFilter("google.com"))); |
145 listeners_->AddListener(EventListener::ForExtension( | 142 listeners_->AddListener(EventListener::ForExtension( |
146 kEvent1Name, kExt1Id, NULL, CreateHostSuffixFilter("yahoo.com"))); | 143 kEvent1Name, kExt1Id, nullptr, CreateHostSuffixFilter("yahoo.com"))); |
147 | 144 |
148 std::unique_ptr<Event> event(CreateNamedEvent(kEvent1Name)); | 145 std::unique_ptr<Event> event(CreateNamedEvent(kEvent1Name)); |
149 event->filter_info.SetURL(GURL("http://www.google.com")); | 146 event->filter_info.SetURL(GURL("http://www.google.com")); |
150 std::set<const EventListener*> targets(listeners_->GetEventListeners(*event)); | 147 std::set<const EventListener*> targets(listeners_->GetEventListeners(*event)); |
151 ASSERT_EQ(1u, targets.size()); | 148 ASSERT_EQ(1u, targets.size()); |
152 } | 149 } |
153 | 150 |
154 TEST_F(EventListenerMapTest, LazyAndUnlazyListenersGetReturned) { | 151 TEST_F(EventListenerMapTest, LazyAndUnlazyListenersGetReturned) { |
155 listeners_->AddListener(EventListener::ForExtension( | 152 listeners_->AddListener(EventListener::ForExtension( |
156 kEvent1Name, kExt1Id, NULL, CreateHostSuffixFilter("google.com"))); | 153 kEvent1Name, kExt1Id, nullptr, CreateHostSuffixFilter("google.com"))); |
157 | 154 |
158 listeners_->AddListener( | 155 listeners_->AddListener( |
159 EventListener::ForExtension(kEvent1Name, | 156 EventListener::ForExtension(kEvent1Name, |
160 kExt1Id, | 157 kExt1Id, |
161 process_.get(), | 158 process_.get(), |
162 CreateHostSuffixFilter("google.com"))); | 159 CreateHostSuffixFilter("google.com"))); |
163 | 160 |
164 std::unique_ptr<Event> event(CreateNamedEvent(kEvent1Name)); | 161 std::unique_ptr<Event> event(CreateNamedEvent(kEvent1Name)); |
165 event->filter_info.SetURL(GURL("http://www.google.com")); | 162 event->filter_info.SetURL(GURL("http://www.google.com")); |
166 std::set<const EventListener*> targets(listeners_->GetEventListeners(*event)); | 163 std::set<const EventListener*> targets(listeners_->GetEventListeners(*event)); |
167 ASSERT_EQ(2u, targets.size()); | 164 ASSERT_EQ(2u, targets.size()); |
168 } | 165 } |
169 | 166 |
170 void EventListenerMapTest::TestRemovingByProcess( | 167 void EventListenerMapTest::TestRemovingByProcess( |
171 const EventListenerConstructor& constructor) { | 168 const EventListenerConstructor& constructor) { |
172 listeners_->AddListener(constructor.Run( | 169 listeners_->AddListener(constructor.Run( |
173 kEvent1Name, NULL, CreateHostSuffixFilter("google.com").release())); | 170 kEvent1Name, nullptr, CreateHostSuffixFilter("google.com"))); |
174 | 171 |
175 listeners_->AddListener( | 172 listeners_->AddListener(constructor.Run( |
176 constructor.Run(kEvent1Name, | 173 kEvent1Name, process_.get(), CreateHostSuffixFilter("google.com"))); |
177 process_.get(), | |
178 CreateHostSuffixFilter("google.com").release())); | |
179 | 174 |
180 listeners_->RemoveListenersForProcess(process_.get()); | 175 listeners_->RemoveListenersForProcess(process_.get()); |
181 | 176 |
182 std::unique_ptr<Event> event(CreateNamedEvent(kEvent1Name)); | 177 std::unique_ptr<Event> event(CreateNamedEvent(kEvent1Name)); |
183 event->filter_info.SetURL(GURL("http://www.google.com")); | 178 event->filter_info.SetURL(GURL("http://www.google.com")); |
184 ASSERT_EQ(1u, listeners_->GetEventListeners(*event).size()); | 179 ASSERT_EQ(1u, listeners_->GetEventListeners(*event).size()); |
185 } | 180 } |
186 | 181 |
187 TEST_F(EventListenerMapTest, TestRemovingByProcessForExtension) { | 182 TEST_F(EventListenerMapTest, TestRemovingByProcessForExtension) { |
188 TestRemovingByProcess(base::Bind(&CreateEventListenerForExtension, kExt1Id)); | 183 TestRemovingByProcess(base::Bind(&CreateEventListenerForExtension, kExt1Id)); |
189 } | 184 } |
190 | 185 |
191 TEST_F(EventListenerMapTest, TestRemovingByProcessForURL) { | 186 TEST_F(EventListenerMapTest, TestRemovingByProcessForURL) { |
192 TestRemovingByProcess(base::Bind(&CreateEventListenerForURL, GURL(kURL))); | 187 TestRemovingByProcess(base::Bind(&CreateEventListenerForURL, GURL(kURL))); |
193 } | 188 } |
194 | 189 |
195 void EventListenerMapTest::TestRemovingByListener( | 190 void EventListenerMapTest::TestRemovingByListener( |
196 const EventListenerConstructor& constructor) { | 191 const EventListenerConstructor& constructor) { |
197 listeners_->AddListener(constructor.Run( | 192 listeners_->AddListener(constructor.Run( |
198 kEvent1Name, NULL, CreateHostSuffixFilter("google.com").release())); | 193 kEvent1Name, nullptr, CreateHostSuffixFilter("google.com"))); |
199 | 194 |
200 listeners_->AddListener( | 195 listeners_->AddListener(constructor.Run( |
201 constructor.Run(kEvent1Name, | 196 kEvent1Name, process_.get(), CreateHostSuffixFilter("google.com"))); |
202 process_.get(), | |
203 CreateHostSuffixFilter("google.com").release())); | |
204 | 197 |
205 std::unique_ptr<EventListener> listener( | 198 std::unique_ptr<EventListener> listener(constructor.Run( |
206 constructor.Run(kEvent1Name, process_.get(), | 199 kEvent1Name, process_.get(), CreateHostSuffixFilter("google.com"))); |
207 CreateHostSuffixFilter("google.com").release())); | |
208 listeners_->RemoveListener(listener.get()); | 200 listeners_->RemoveListener(listener.get()); |
209 | 201 |
210 std::unique_ptr<Event> event(CreateNamedEvent(kEvent1Name)); | 202 std::unique_ptr<Event> event(CreateNamedEvent(kEvent1Name)); |
211 event->filter_info.SetURL(GURL("http://www.google.com")); | 203 event->filter_info.SetURL(GURL("http://www.google.com")); |
212 ASSERT_EQ(1u, listeners_->GetEventListeners(*event).size()); | 204 ASSERT_EQ(1u, listeners_->GetEventListeners(*event).size()); |
213 } | 205 } |
214 | 206 |
215 TEST_F(EventListenerMapTest, TestRemovingByListenerForExtension) { | 207 TEST_F(EventListenerMapTest, TestRemovingByListenerForExtension) { |
216 TestRemovingByListener(base::Bind(&CreateEventListenerForExtension, kExt1Id)); | 208 TestRemovingByListener(base::Bind(&CreateEventListenerForExtension, kExt1Id)); |
217 } | 209 } |
218 | 210 |
219 TEST_F(EventListenerMapTest, TestRemovingByListenerForURL) { | 211 TEST_F(EventListenerMapTest, TestRemovingByListenerForURL) { |
220 TestRemovingByListener(base::Bind(&CreateEventListenerForURL, GURL(kURL))); | 212 TestRemovingByListener(base::Bind(&CreateEventListenerForURL, GURL(kURL))); |
221 } | 213 } |
222 | 214 |
223 TEST_F(EventListenerMapTest, TestLazyDoubleAddIsUndoneByRemove) { | 215 TEST_F(EventListenerMapTest, TestLazyDoubleAddIsUndoneByRemove) { |
224 listeners_->AddListener(EventListener::ForExtension( | 216 listeners_->AddListener(EventListener::ForExtension( |
225 kEvent1Name, kExt1Id, NULL, CreateHostSuffixFilter("google.com"))); | 217 kEvent1Name, kExt1Id, nullptr, CreateHostSuffixFilter("google.com"))); |
226 listeners_->AddListener(EventListener::ForExtension( | 218 listeners_->AddListener(EventListener::ForExtension( |
227 kEvent1Name, kExt1Id, NULL, CreateHostSuffixFilter("google.com"))); | 219 kEvent1Name, kExt1Id, nullptr, CreateHostSuffixFilter("google.com"))); |
228 | 220 |
229 std::unique_ptr<EventListener> listener(EventListener::ForExtension( | 221 std::unique_ptr<EventListener> listener(EventListener::ForExtension( |
230 kEvent1Name, kExt1Id, NULL, CreateHostSuffixFilter("google.com"))); | 222 kEvent1Name, kExt1Id, nullptr, CreateHostSuffixFilter("google.com"))); |
231 listeners_->RemoveListener(listener.get()); | 223 listeners_->RemoveListener(listener.get()); |
232 | 224 |
233 std::unique_ptr<Event> event(CreateNamedEvent(kEvent1Name)); | 225 std::unique_ptr<Event> event(CreateNamedEvent(kEvent1Name)); |
234 event->filter_info.SetURL(GURL("http://www.google.com")); | 226 event->filter_info.SetURL(GURL("http://www.google.com")); |
235 std::set<const EventListener*> targets(listeners_->GetEventListeners(*event)); | 227 std::set<const EventListener*> targets(listeners_->GetEventListeners(*event)); |
236 ASSERT_EQ(0u, targets.size()); | 228 ASSERT_EQ(0u, targets.size()); |
237 } | 229 } |
238 | 230 |
239 TEST_F(EventListenerMapTest, HostSuffixFilterEquality) { | 231 TEST_F(EventListenerMapTest, HostSuffixFilterEquality) { |
240 std::unique_ptr<DictionaryValue> filter1( | 232 std::unique_ptr<DictionaryValue> filter1( |
241 CreateHostSuffixFilter("google.com")); | 233 CreateHostSuffixFilter("google.com")); |
242 std::unique_ptr<DictionaryValue> filter2( | 234 std::unique_ptr<DictionaryValue> filter2( |
243 CreateHostSuffixFilter("google.com")); | 235 CreateHostSuffixFilter("google.com")); |
244 ASSERT_TRUE(filter1->Equals(filter2.get())); | 236 ASSERT_TRUE(filter1->Equals(filter2.get())); |
245 } | 237 } |
246 | 238 |
247 TEST_F(EventListenerMapTest, RemoveListenersForExtension) { | 239 TEST_F(EventListenerMapTest, RemoveListenersForExtension) { |
248 listeners_->AddListener(EventListener::ForExtension( | 240 listeners_->AddListener(EventListener::ForExtension( |
249 kEvent1Name, kExt1Id, NULL, CreateHostSuffixFilter("google.com"))); | 241 kEvent1Name, kExt1Id, nullptr, CreateHostSuffixFilter("google.com"))); |
250 listeners_->AddListener(EventListener::ForExtension( | 242 listeners_->AddListener(EventListener::ForExtension( |
251 kEvent2Name, kExt1Id, NULL, CreateHostSuffixFilter("google.com"))); | 243 kEvent2Name, kExt1Id, nullptr, CreateHostSuffixFilter("google.com"))); |
252 | 244 |
253 listeners_->RemoveListenersForExtension(kExt1Id); | 245 listeners_->RemoveListenersForExtension(kExt1Id); |
254 | 246 |
255 std::unique_ptr<Event> event(CreateNamedEvent(kEvent1Name)); | 247 std::unique_ptr<Event> event1(CreateNamedEvent(kEvent1Name)); |
256 event->filter_info.SetURL(GURL("http://www.google.com")); | 248 event1->filter_info.SetURL(GURL("http://www.google.com")); |
257 std::set<const EventListener*> targets(listeners_->GetEventListeners(*event)); | 249 std::set<const EventListener*> targets( |
| 250 listeners_->GetEventListeners(*event1)); |
258 ASSERT_EQ(0u, targets.size()); | 251 ASSERT_EQ(0u, targets.size()); |
259 | 252 |
260 event->event_name = kEvent2Name; | 253 std::unique_ptr<Event> event2(CreateNamedEvent(kEvent2Name)); |
261 targets = listeners_->GetEventListeners(*event); | 254 event2->filter_info.SetURL(GURL("http://www.google.com")); |
| 255 targets = listeners_->GetEventListeners(*event2); |
262 ASSERT_EQ(0u, targets.size()); | 256 ASSERT_EQ(0u, targets.size()); |
263 } | 257 } |
264 | 258 |
265 TEST_F(EventListenerMapTest, AddExistingFilteredListener) { | 259 TEST_F(EventListenerMapTest, AddExistingFilteredListener) { |
266 bool first_new = listeners_->AddListener(EventListener::ForExtension( | 260 bool first_new = listeners_->AddListener(EventListener::ForExtension( |
267 kEvent1Name, kExt1Id, NULL, CreateHostSuffixFilter("google.com"))); | 261 kEvent1Name, kExt1Id, nullptr, CreateHostSuffixFilter("google.com"))); |
268 bool second_new = listeners_->AddListener(EventListener::ForExtension( | 262 bool second_new = listeners_->AddListener(EventListener::ForExtension( |
269 kEvent1Name, kExt1Id, NULL, CreateHostSuffixFilter("google.com"))); | 263 kEvent1Name, kExt1Id, nullptr, CreateHostSuffixFilter("google.com"))); |
270 | 264 |
271 ASSERT_TRUE(first_new); | 265 ASSERT_TRUE(first_new); |
272 ASSERT_FALSE(second_new); | 266 ASSERT_FALSE(second_new); |
273 } | 267 } |
274 | 268 |
275 void EventListenerMapTest::TestAddExistingUnfilteredListener( | 269 void EventListenerMapTest::TestAddExistingUnfilteredListener( |
276 const EventListenerConstructor& constructor) { | 270 const EventListenerConstructor& constructor) { |
277 bool first_add = listeners_->AddListener( | 271 bool first_add = listeners_->AddListener(constructor.Run( |
278 constructor.Run(kEvent1Name, NULL, new DictionaryValue())); | 272 kEvent1Name, nullptr, base::MakeUnique<base::DictionaryValue>())); |
279 bool second_add = listeners_->AddListener( | 273 bool second_add = listeners_->AddListener(constructor.Run( |
280 constructor.Run(kEvent1Name, NULL, new DictionaryValue())); | 274 kEvent1Name, nullptr, base::MakeUnique<base::DictionaryValue>())); |
281 | 275 |
282 std::unique_ptr<EventListener> listener( | 276 std::unique_ptr<EventListener> listener(constructor.Run( |
283 constructor.Run(kEvent1Name, NULL, new DictionaryValue())); | 277 kEvent1Name, nullptr, base::MakeUnique<base::DictionaryValue>())); |
284 bool first_remove = listeners_->RemoveListener(listener.get()); | 278 bool first_remove = listeners_->RemoveListener(listener.get()); |
285 bool second_remove = listeners_->RemoveListener(listener.get()); | 279 bool second_remove = listeners_->RemoveListener(listener.get()); |
286 | 280 |
287 ASSERT_TRUE(first_add); | 281 ASSERT_TRUE(first_add); |
288 ASSERT_FALSE(second_add); | 282 ASSERT_FALSE(second_add); |
289 ASSERT_TRUE(first_remove); | 283 ASSERT_TRUE(first_remove); |
290 ASSERT_FALSE(second_remove); | 284 ASSERT_FALSE(second_remove); |
291 } | 285 } |
292 | 286 |
293 TEST_F(EventListenerMapTest, AddExistingUnfilteredListenerForExtensions) { | 287 TEST_F(EventListenerMapTest, AddExistingUnfilteredListenerForExtensions) { |
(...skipping 14 matching lines...) Expand all Loading... |
308 EventListener::ForURL(kEvent1Name, GURL(kURL), process_.get(), | 302 EventListener::ForURL(kEvent1Name, GURL(kURL), process_.get(), |
309 std::unique_ptr<DictionaryValue>())); | 303 std::unique_ptr<DictionaryValue>())); |
310 listeners_->RemoveListenersForProcess(process_.get()); | 304 listeners_->RemoveListenersForProcess(process_.get()); |
311 ASSERT_FALSE(listeners_->HasListenerForEvent(kEvent1Name)); | 305 ASSERT_FALSE(listeners_->HasListenerForEvent(kEvent1Name)); |
312 } | 306 } |
313 | 307 |
314 void EventListenerMapTest::TestHasListenerForEvent( | 308 void EventListenerMapTest::TestHasListenerForEvent( |
315 const EventListenerConstructor& constructor) { | 309 const EventListenerConstructor& constructor) { |
316 ASSERT_FALSE(listeners_->HasListenerForEvent(kEvent1Name)); | 310 ASSERT_FALSE(listeners_->HasListenerForEvent(kEvent1Name)); |
317 | 311 |
318 listeners_->AddListener( | 312 listeners_->AddListener(constructor.Run( |
319 constructor.Run(kEvent1Name, process_.get(), new DictionaryValue())); | 313 kEvent1Name, process_.get(), base::MakeUnique<base::DictionaryValue>())); |
320 | 314 |
321 ASSERT_FALSE(listeners_->HasListenerForEvent(kEvent2Name)); | 315 ASSERT_FALSE(listeners_->HasListenerForEvent(kEvent2Name)); |
322 ASSERT_TRUE(listeners_->HasListenerForEvent(kEvent1Name)); | 316 ASSERT_TRUE(listeners_->HasListenerForEvent(kEvent1Name)); |
323 listeners_->RemoveListenersForProcess(process_.get()); | 317 listeners_->RemoveListenersForProcess(process_.get()); |
324 ASSERT_FALSE(listeners_->HasListenerForEvent(kEvent1Name)); | 318 ASSERT_FALSE(listeners_->HasListenerForEvent(kEvent1Name)); |
325 } | 319 } |
326 | 320 |
327 TEST_F(EventListenerMapTest, HasListenerForEventForExtension) { | 321 TEST_F(EventListenerMapTest, HasListenerForEventForExtension) { |
328 TestHasListenerForEvent( | 322 TestHasListenerForEvent( |
329 base::Bind(&CreateEventListenerForExtension, kExt1Id)); | 323 base::Bind(&CreateEventListenerForExtension, kExt1Id)); |
330 } | 324 } |
331 | 325 |
332 TEST_F(EventListenerMapTest, HasListenerForEventForURL) { | 326 TEST_F(EventListenerMapTest, HasListenerForEventForURL) { |
333 TestHasListenerForEvent(base::Bind(&CreateEventListenerForURL, GURL(kURL))); | 327 TestHasListenerForEvent(base::Bind(&CreateEventListenerForURL, GURL(kURL))); |
334 } | 328 } |
335 | 329 |
336 TEST_F(EventListenerMapTest, HasListenerForExtension) { | 330 TEST_F(EventListenerMapTest, HasListenerForExtension) { |
337 ASSERT_FALSE(listeners_->HasListenerForExtension(kExt1Id, kEvent1Name)); | 331 ASSERT_FALSE(listeners_->HasListenerForExtension(kExt1Id, kEvent1Name)); |
338 | 332 |
339 // Non-lazy listener. | 333 // Non-lazy listener. |
340 listeners_->AddListener( | 334 listeners_->AddListener( |
341 EventListener::ForExtension(kEvent1Name, kExt1Id, process_.get(), | 335 EventListener::ForExtension(kEvent1Name, kExt1Id, process_.get(), |
342 std::unique_ptr<DictionaryValue>())); | 336 std::unique_ptr<DictionaryValue>())); |
343 // Lazy listener. | 337 // Lazy listener. |
344 listeners_->AddListener(EventListener::ForExtension( | 338 listeners_->AddListener(EventListener::ForExtension( |
345 kEvent1Name, kExt1Id, NULL, std::unique_ptr<DictionaryValue>())); | 339 kEvent1Name, kExt1Id, nullptr, std::unique_ptr<DictionaryValue>())); |
346 | 340 |
347 ASSERT_FALSE(listeners_->HasListenerForExtension(kExt1Id, kEvent2Name)); | 341 ASSERT_FALSE(listeners_->HasListenerForExtension(kExt1Id, kEvent2Name)); |
348 ASSERT_TRUE(listeners_->HasListenerForExtension(kExt1Id, kEvent1Name)); | 342 ASSERT_TRUE(listeners_->HasListenerForExtension(kExt1Id, kEvent1Name)); |
349 ASSERT_FALSE(listeners_->HasListenerForExtension(kExt2Id, kEvent1Name)); | 343 ASSERT_FALSE(listeners_->HasListenerForExtension(kExt2Id, kEvent1Name)); |
350 listeners_->RemoveListenersForProcess(process_.get()); | 344 listeners_->RemoveListenersForProcess(process_.get()); |
351 ASSERT_TRUE(listeners_->HasListenerForExtension(kExt1Id, kEvent1Name)); | 345 ASSERT_TRUE(listeners_->HasListenerForExtension(kExt1Id, kEvent1Name)); |
352 listeners_->RemoveListenersForExtension(kExt1Id); | 346 listeners_->RemoveListenersForExtension(kExt1Id); |
353 ASSERT_FALSE(listeners_->HasListenerForExtension(kExt1Id, kEvent1Name)); | 347 ASSERT_FALSE(listeners_->HasListenerForExtension(kExt1Id, kEvent1Name)); |
354 } | 348 } |
355 | 349 |
356 TEST_F(EventListenerMapTest, AddLazyListenersFromPreferences) { | 350 TEST_F(EventListenerMapTest, AddLazyListenersFromPreferences) { |
357 std::unique_ptr<DictionaryValue> filter1( | 351 auto filter_list = base::MakeUnique<ListValue>(); |
358 CreateHostSuffixFilter("google.com")); | 352 filter_list->Append(CreateHostSuffixFilter("google.com")); |
359 std::unique_ptr<DictionaryValue> filter2(CreateHostSuffixFilter("yahoo.com")); | 353 filter_list->Append(CreateHostSuffixFilter("yahoo.com")); |
360 | 354 |
361 DictionaryValue filtered_listeners; | 355 DictionaryValue filtered_listeners; |
362 ListValue* filter_list = new ListValue(); | 356 filtered_listeners.Set(kEvent1Name, std::move(filter_list)); |
363 filtered_listeners.Set(kEvent1Name, filter_list); | |
364 | |
365 filter_list->Append(std::move(filter1)); | |
366 filter_list->Append(std::move(filter2)); | |
367 | |
368 listeners_->LoadFilteredLazyListeners(kExt1Id, filtered_listeners); | 357 listeners_->LoadFilteredLazyListeners(kExt1Id, filtered_listeners); |
369 | 358 |
370 std::unique_ptr<Event> event( | 359 std::unique_ptr<Event> event( |
371 CreateEvent(kEvent1Name, GURL("http://www.google.com"))); | 360 CreateEvent(kEvent1Name, GURL("http://www.google.com"))); |
372 std::set<const EventListener*> targets(listeners_->GetEventListeners(*event)); | 361 std::set<const EventListener*> targets(listeners_->GetEventListeners(*event)); |
373 ASSERT_EQ(1u, targets.size()); | 362 ASSERT_EQ(1u, targets.size()); |
374 std::unique_ptr<EventListener> listener(EventListener::ForExtension( | 363 std::unique_ptr<EventListener> listener(EventListener::ForExtension( |
375 kEvent1Name, kExt1Id, NULL, CreateHostSuffixFilter("google.com"))); | 364 kEvent1Name, kExt1Id, nullptr, CreateHostSuffixFilter("google.com"))); |
376 ASSERT_TRUE((*targets.begin())->Equals(listener.get())); | 365 ASSERT_TRUE((*targets.begin())->Equals(listener.get())); |
377 } | 366 } |
378 | 367 |
379 TEST_F(EventListenerMapTest, CorruptedExtensionPrefsShouldntCrash) { | 368 TEST_F(EventListenerMapTest, CorruptedExtensionPrefsShouldntCrash) { |
380 std::unique_ptr<DictionaryValue> filter1( | |
381 CreateHostSuffixFilter("google.com")); | |
382 | |
383 DictionaryValue filtered_listeners; | 369 DictionaryValue filtered_listeners; |
384 // kEvent1Name should be associated with a list, not a dictionary. | 370 // kEvent1Name should be associated with a list, not a dictionary. |
385 filtered_listeners.Set(kEvent1Name, filter1.release()); | 371 filtered_listeners.Set(kEvent1Name, CreateHostSuffixFilter("google.com")); |
386 | 372 |
387 listeners_->LoadFilteredLazyListeners(kExt1Id, filtered_listeners); | 373 listeners_->LoadFilteredLazyListeners(kExt1Id, filtered_listeners); |
388 | 374 |
389 std::unique_ptr<Event> event( | 375 std::unique_ptr<Event> event( |
390 CreateEvent(kEvent1Name, GURL("http://www.google.com"))); | 376 CreateEvent(kEvent1Name, GURL("http://www.google.com"))); |
391 std::set<const EventListener*> targets(listeners_->GetEventListeners(*event)); | 377 std::set<const EventListener*> targets(listeners_->GetEventListeners(*event)); |
392 ASSERT_EQ(0u, targets.size()); | 378 ASSERT_EQ(0u, targets.size()); |
393 } | 379 } |
394 | 380 |
395 } // namespace | 381 } // namespace |
396 | 382 |
397 } // namespace extensions | 383 } // namespace extensions |
OLD | NEW |