OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/extensions/api/mdns/mdns_api.h" | 5 #include "chrome/browser/extensions/api/mdns/mdns_api.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 |
| 9 #include <memory> |
8 #include <utility> | 10 #include <utility> |
9 #include <vector> | 11 #include <vector> |
10 | 12 |
11 #include "base/memory/linked_ptr.h" | 13 #include "base/memory/linked_ptr.h" |
12 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/ptr_util.h" |
13 #include "chrome/browser/extensions/extension_service.h" | 15 #include "chrome/browser/extensions/extension_service.h" |
14 #include "chrome/browser/extensions/extension_service_test_base.h" | 16 #include "chrome/browser/extensions/extension_service_test_base.h" |
15 #include "chrome/browser/extensions/test_extension_system.h" | 17 #include "chrome/browser/extensions/test_extension_system.h" |
16 #include "chrome/common/extensions/api/mdns.h" | 18 #include "chrome/common/extensions/api/mdns.h" |
17 #include "content/public/browser/browser_context.h" | 19 #include "content/public/browser/browser_context.h" |
18 #include "content/public/test/mock_render_process_host.h" | 20 #include "content/public/test/mock_render_process_host.h" |
19 #include "extensions/browser/event_listener_map.h" | 21 #include "extensions/browser/event_listener_map.h" |
20 #include "extensions/browser/event_router_factory.h" | 22 #include "extensions/browser/event_router_factory.h" |
21 #include "extensions/browser/extension_prefs.h" | 23 #include "extensions/browser/extension_prefs.h" |
22 #include "extensions/browser/extension_registry.h" | 24 #include "extensions/browser/extension_registry.h" |
(...skipping 10 matching lines...) Expand all Loading... |
33 | 35 |
34 const char kExtId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; | 36 const char kExtId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; |
35 const char kService1[] = "service1"; | 37 const char kService1[] = "service1"; |
36 const char kService2[] = "service2"; | 38 const char kService2[] = "service2"; |
37 | 39 |
38 // Registers a new EventListener for |service_types| in |listener_list|. | 40 // Registers a new EventListener for |service_types| in |listener_list|. |
39 void AddEventListener( | 41 void AddEventListener( |
40 const std::string& extension_id, | 42 const std::string& extension_id, |
41 const std::string& service_type, | 43 const std::string& service_type, |
42 extensions::EventListenerMap::ListenerList* listener_list) { | 44 extensions::EventListenerMap::ListenerList* listener_list) { |
43 scoped_ptr<base::DictionaryValue> filter(new base::DictionaryValue); | 45 std::unique_ptr<base::DictionaryValue> filter(new base::DictionaryValue); |
44 filter->SetString(kEventFilterServiceTypeKey, service_type); | 46 filter->SetString(kEventFilterServiceTypeKey, service_type); |
45 listener_list->push_back(make_linked_ptr( | 47 listener_list->push_back(make_linked_ptr( |
46 EventListener::ForExtension(kEventFilterServiceTypeKey, extension_id, | 48 EventListener::ForExtension(kEventFilterServiceTypeKey, extension_id, |
47 nullptr, std::move(filter)) | 49 nullptr, std::move(filter)) |
48 .release())); | 50 .release())); |
49 } | 51 } |
50 | 52 |
51 class NullDelegate : public EventListenerMap::Delegate { | 53 class NullDelegate : public EventListenerMap::Delegate { |
52 public: | 54 public: |
53 void OnListenerAdded(const EventListener* listener) override {} | 55 void OnListenerAdded(const EventListener* listener) override {} |
54 void OnListenerRemoved(const EventListener* listener) override {} | 56 void OnListenerRemoved(const EventListener* listener) override {} |
55 }; | 57 }; |
56 | 58 |
57 // Testing subclass of MDnsAPI which replaces calls to core browser components | 59 // Testing subclass of MDnsAPI which replaces calls to core browser components |
58 // that we don't want to have to instantiate or mock for these tests. | 60 // that we don't want to have to instantiate or mock for these tests. |
59 class MockedMDnsAPI : public MDnsAPI { | 61 class MockedMDnsAPI : public MDnsAPI { |
60 public: | 62 public: |
61 explicit MockedMDnsAPI(content::BrowserContext* context) : MDnsAPI(context) {} | 63 explicit MockedMDnsAPI(content::BrowserContext* context) : MDnsAPI(context) {} |
62 | 64 |
63 public: | 65 public: |
64 MOCK_CONST_METHOD2(IsMDnsAllowed, | 66 MOCK_CONST_METHOD2(IsMDnsAllowed, |
65 bool(const std::string& extension_id, | 67 bool(const std::string& extension_id, |
66 const std::string& service_type)); | 68 const std::string& service_type)); |
67 | 69 |
68 MOCK_METHOD0(GetEventListeners, | 70 MOCK_METHOD0(GetEventListeners, |
69 const extensions::EventListenerMap::ListenerList&()); | 71 const extensions::EventListenerMap::ListenerList&()); |
70 }; | 72 }; |
71 | 73 |
72 scoped_ptr<KeyedService> MockedMDnsAPITestingFactoryFunction( | 74 std::unique_ptr<KeyedService> MockedMDnsAPITestingFactoryFunction( |
73 content::BrowserContext* context) { | 75 content::BrowserContext* context) { |
74 return make_scoped_ptr(new MockedMDnsAPI(context)); | 76 return base::WrapUnique(new MockedMDnsAPI(context)); |
75 } | 77 } |
76 | 78 |
77 scoped_ptr<KeyedService> MDnsAPITestingFactoryFunction( | 79 std::unique_ptr<KeyedService> MDnsAPITestingFactoryFunction( |
78 content::BrowserContext* context) { | 80 content::BrowserContext* context) { |
79 return make_scoped_ptr(new MDnsAPI(context)); | 81 return base::WrapUnique(new MDnsAPI(context)); |
80 } | 82 } |
81 | 83 |
82 scoped_ptr<KeyedService> BuildEventRouter(content::BrowserContext* context) { | 84 std::unique_ptr<KeyedService> BuildEventRouter( |
83 return make_scoped_ptr( | 85 content::BrowserContext* context) { |
| 86 return base::WrapUnique( |
84 new extensions::EventRouter(context, ExtensionPrefs::Get(context))); | 87 new extensions::EventRouter(context, ExtensionPrefs::Get(context))); |
85 } | 88 } |
86 | 89 |
87 // For ExtensionService interface when it requires a path that is not used. | 90 // For ExtensionService interface when it requires a path that is not used. |
88 base::FilePath bogus_file_pathname(const std::string& name) { | 91 base::FilePath bogus_file_pathname(const std::string& name) { |
89 return base::FilePath(FILE_PATH_LITERAL("//foobar_nonexistent")) | 92 return base::FilePath(FILE_PATH_LITERAL("//foobar_nonexistent")) |
90 .AppendASCII(name); | 93 .AppendASCII(name); |
91 } | 94 } |
92 | 95 |
93 class MockDnsSdRegistry : public DnsSdRegistry { | 96 class MockDnsSdRegistry : public DnsSdRegistry { |
(...skipping 17 matching lines...) Expand all Loading... |
111 extensions::DnsSdRegistry::DnsSdObserver* api_; | 114 extensions::DnsSdRegistry::DnsSdObserver* api_; |
112 }; | 115 }; |
113 | 116 |
114 class MockEventRouter : public EventRouter { | 117 class MockEventRouter : public EventRouter { |
115 public: | 118 public: |
116 explicit MockEventRouter(content::BrowserContext* browser_context, | 119 explicit MockEventRouter(content::BrowserContext* browser_context, |
117 ExtensionPrefs* extension_prefs) | 120 ExtensionPrefs* extension_prefs) |
118 : EventRouter(browser_context, extension_prefs) {} | 121 : EventRouter(browser_context, extension_prefs) {} |
119 virtual ~MockEventRouter() {} | 122 virtual ~MockEventRouter() {} |
120 | 123 |
121 virtual void BroadcastEvent(scoped_ptr<Event> event) { | 124 virtual void BroadcastEvent(std::unique_ptr<Event> event) { |
122 BroadcastEventPtr(event.get()); | 125 BroadcastEventPtr(event.get()); |
123 } | 126 } |
124 MOCK_METHOD1(BroadcastEventPtr, void(Event* event)); | 127 MOCK_METHOD1(BroadcastEventPtr, void(Event* event)); |
125 }; | 128 }; |
126 | 129 |
127 scoped_ptr<KeyedService> MockEventRouterFactoryFunction( | 130 std::unique_ptr<KeyedService> MockEventRouterFactoryFunction( |
128 content::BrowserContext* context) { | 131 content::BrowserContext* context) { |
129 return make_scoped_ptr( | 132 return base::WrapUnique( |
130 new MockEventRouter(context, ExtensionPrefs::Get(context))); | 133 new MockEventRouter(context, ExtensionPrefs::Get(context))); |
131 } | 134 } |
132 | 135 |
133 class EventServiceListSizeMatcher | 136 class EventServiceListSizeMatcher |
134 : public testing::MatcherInterface<const Event&> { | 137 : public testing::MatcherInterface<const Event&> { |
135 public: | 138 public: |
136 explicit EventServiceListSizeMatcher(size_t expected_size) | 139 explicit EventServiceListSizeMatcher(size_t expected_size) |
137 : expected_size_(expected_size) {} | 140 : expected_size_(expected_size) {} |
138 | 141 |
139 virtual bool MatchAndExplain(const Event& e, | 142 virtual bool MatchAndExplain(const Event& e, |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 &BuildEventRouter); | 203 &BuildEventRouter); |
201 | 204 |
202 // Do some sanity checking | 205 // Do some sanity checking |
203 ASSERT_TRUE(MDnsAPI::Get(browser_context())); // constructs MDnsAPI | 206 ASSERT_TRUE(MDnsAPI::Get(browser_context())); // constructs MDnsAPI |
204 ASSERT_TRUE(EventRouter::Get(browser_context())); // constructs EventRouter | 207 ASSERT_TRUE(EventRouter::Get(browser_context())); // constructs EventRouter |
205 | 208 |
206 registry_ = new MockDnsSdRegistry(MDnsAPI::Get(browser_context())); | 209 registry_ = new MockDnsSdRegistry(MDnsAPI::Get(browser_context())); |
207 EXPECT_CALL(*dns_sd_registry(), | 210 EXPECT_CALL(*dns_sd_registry(), |
208 AddObserver(MDnsAPI::Get(browser_context()))) | 211 AddObserver(MDnsAPI::Get(browser_context()))) |
209 .Times(1); | 212 .Times(1); |
210 MDnsAPI::Get(browser_context())->SetDnsSdRegistryForTesting( | 213 MDnsAPI::Get(browser_context()) |
211 scoped_ptr<DnsSdRegistry>(registry_)); | 214 ->SetDnsSdRegistryForTesting(std::unique_ptr<DnsSdRegistry>(registry_)); |
212 | 215 |
213 render_process_host_.reset( | 216 render_process_host_.reset( |
214 new content::MockRenderProcessHost(browser_context())); | 217 new content::MockRenderProcessHost(browser_context())); |
215 } | 218 } |
216 | 219 |
217 // Returns the mDNS API factory function (mock vs. real) to use for the test. | 220 // Returns the mDNS API factory function (mock vs. real) to use for the test. |
218 virtual BrowserContextKeyedServiceFactory::TestingFactoryFunction | 221 virtual BrowserContextKeyedServiceFactory::TestingFactoryFunction |
219 GetMDnsFactory() { | 222 GetMDnsFactory() { |
220 return MDnsAPITestingFactoryFunction; | 223 return MDnsAPITestingFactoryFunction; |
221 } | 224 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 | 263 |
261 content::RenderProcessHost* render_process_host() const { | 264 content::RenderProcessHost* render_process_host() const { |
262 return render_process_host_.get(); | 265 return render_process_host_.get(); |
263 } | 266 } |
264 | 267 |
265 private: | 268 private: |
266 // The registry is owned by MDnsAPI, but MDnsAPI does not have an accessor | 269 // The registry is owned by MDnsAPI, but MDnsAPI does not have an accessor |
267 // for it, so use a private member. | 270 // for it, so use a private member. |
268 MockDnsSdRegistry* registry_; | 271 MockDnsSdRegistry* registry_; |
269 | 272 |
270 scoped_ptr<content::RenderProcessHost> render_process_host_; | 273 std::unique_ptr<content::RenderProcessHost> render_process_host_; |
271 }; | 274 }; |
272 | 275 |
273 class MDnsAPIMaxServicesTest : public MDnsAPITest { | 276 class MDnsAPIMaxServicesTest : public MDnsAPITest { |
274 public: | 277 public: |
275 MockEventRouter* event_router() { | 278 MockEventRouter* event_router() { |
276 return static_cast<MockEventRouter*>(EventRouter::Get(browser_context())); | 279 return static_cast<MockEventRouter*>(EventRouter::Get(browser_context())); |
277 } | 280 } |
278 }; | 281 }; |
279 | 282 |
280 class MDnsAPIDiscoveryTest : public MDnsAPITest { | 283 class MDnsAPIDiscoveryTest : public MDnsAPITest { |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 render_process_host(), kExtId, filter, false); | 439 render_process_host(), kExtId, filter, false); |
437 | 440 |
438 EXPECT_CALL(*dns_sd_registry(), UnregisterDnsSdListener("_trex._tcp.local")); | 441 EXPECT_CALL(*dns_sd_registry(), UnregisterDnsSdListener("_trex._tcp.local")); |
439 EventRouter::Get(browser_context()) | 442 EventRouter::Get(browser_context()) |
440 ->RemoveFilteredEventListener(api::mdns::OnServiceList::kEventName, | 443 ->RemoveFilteredEventListener(api::mdns::OnServiceList::kEventName, |
441 render_process_host(), kExtId, filter, | 444 render_process_host(), kExtId, filter, |
442 false); | 445 false); |
443 } | 446 } |
444 | 447 |
445 } // namespace extensions | 448 } // namespace extensions |
OLD | NEW |