| 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_router.h" | 5 #include "extensions/browser/event_router.h" |
| 6 | 6 |
| 7 #include <memory> |
| 7 #include <string> | 8 #include <string> |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/ptr_util.h" |
| 14 #include "base/test/histogram_tester.h" | 15 #include "base/test/histogram_tester.h" |
| 15 #include "base/values.h" | 16 #include "base/values.h" |
| 16 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
| 17 #include "content/public/test/test_browser_thread_bundle.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
| 18 #include "extensions/browser/event_listener_map.h" | 19 #include "extensions/browser/event_listener_map.h" |
| 19 #include "extensions/browser/extensions_test.h" | 20 #include "extensions/browser/extensions_test.h" |
| 20 #include "extensions/common/extension_builder.h" | 21 #include "extensions/common/extension_builder.h" |
| 21 #include "extensions/common/test_util.h" | 22 #include "extensions/common/test_util.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 24 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 } | 56 } |
| 56 | 57 |
| 57 private: | 58 private: |
| 58 int listener_added_count_; | 59 int listener_added_count_; |
| 59 int listener_removed_count_; | 60 int listener_removed_count_; |
| 60 std::string last_event_name_; | 61 std::string last_event_name_; |
| 61 | 62 |
| 62 DISALLOW_COPY_AND_ASSIGN(MockEventRouterObserver); | 63 DISALLOW_COPY_AND_ASSIGN(MockEventRouterObserver); |
| 63 }; | 64 }; |
| 64 | 65 |
| 65 typedef base::Callback<scoped_ptr<EventListener>( | 66 typedef base::Callback<std::unique_ptr<EventListener>( |
| 66 const std::string&, // event_name | 67 const std::string&, // event_name |
| 67 content::RenderProcessHost*, // process | 68 content::RenderProcessHost*, // process |
| 68 base::DictionaryValue* // filter (takes ownership) | 69 base::DictionaryValue* // filter (takes ownership) |
| 69 )> EventListenerConstructor; | 70 )> |
| 71 EventListenerConstructor; |
| 70 | 72 |
| 71 scoped_ptr<EventListener> CreateEventListenerForExtension( | 73 std::unique_ptr<EventListener> CreateEventListenerForExtension( |
| 72 const std::string& extension_id, | 74 const std::string& extension_id, |
| 73 const std::string& event_name, | 75 const std::string& event_name, |
| 74 content::RenderProcessHost* process, | 76 content::RenderProcessHost* process, |
| 75 base::DictionaryValue* filter) { | 77 base::DictionaryValue* filter) { |
| 76 return EventListener::ForExtension( | 78 return EventListener::ForExtension(event_name, extension_id, process, |
| 77 event_name, extension_id, process, make_scoped_ptr(filter)); | 79 base::WrapUnique(filter)); |
| 78 } | 80 } |
| 79 | 81 |
| 80 scoped_ptr<EventListener> CreateEventListenerForURL( | 82 std::unique_ptr<EventListener> CreateEventListenerForURL( |
| 81 const GURL& listener_url, | 83 const GURL& listener_url, |
| 82 const std::string& event_name, | 84 const std::string& event_name, |
| 83 content::RenderProcessHost* process, | 85 content::RenderProcessHost* process, |
| 84 base::DictionaryValue* filter) { | 86 base::DictionaryValue* filter) { |
| 85 return EventListener::ForURL( | 87 return EventListener::ForURL(event_name, listener_url, process, |
| 86 event_name, listener_url, process, make_scoped_ptr(filter)); | 88 base::WrapUnique(filter)); |
| 87 } | 89 } |
| 88 | 90 |
| 89 // Creates an extension. If |component| is true, it is created as a component | 91 // Creates an extension. If |component| is true, it is created as a component |
| 90 // extension. If |persistent| is true, it is created with a persistent | 92 // extension. If |persistent| is true, it is created with a persistent |
| 91 // background page; otherwise it is created with an event page. | 93 // background page; otherwise it is created with an event page. |
| 92 scoped_refptr<Extension> CreateExtension(bool component, bool persistent) { | 94 scoped_refptr<Extension> CreateExtension(bool component, bool persistent) { |
| 93 ExtensionBuilder builder; | 95 ExtensionBuilder builder; |
| 94 scoped_ptr<base::DictionaryValue> manifest = | 96 std::unique_ptr<base::DictionaryValue> manifest = |
| 95 make_scoped_ptr(new base::DictionaryValue()); | 97 base::WrapUnique(new base::DictionaryValue()); |
| 96 manifest->SetString("name", "foo"); | 98 manifest->SetString("name", "foo"); |
| 97 manifest->SetString("version", "1.0.0"); | 99 manifest->SetString("version", "1.0.0"); |
| 98 manifest->SetInteger("manifest_version", 2); | 100 manifest->SetInteger("manifest_version", 2); |
| 99 manifest->SetString("background.page", "background.html"); | 101 manifest->SetString("background.page", "background.html"); |
| 100 manifest->SetBoolean("background.persistent", persistent); | 102 manifest->SetBoolean("background.persistent", persistent); |
| 101 builder.SetManifest(std::move(manifest)); | 103 builder.SetManifest(std::move(manifest)); |
| 102 if (component) | 104 if (component) |
| 103 builder.SetLocation(Manifest::Location::COMPONENT); | 105 builder.SetLocation(Manifest::Location::COMPONENT); |
| 104 | 106 |
| 105 return builder.Build(); | 107 return builder.Build(); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 events::HistogramValue::FOR_TEST, component_suspended_count); | 152 events::HistogramValue::FOR_TEST, component_suspended_count); |
| 151 } | 153 } |
| 152 if (running_count) { | 154 if (running_count) { |
| 153 histogram_tester_.ExpectBucketCount( | 155 histogram_tester_.ExpectBucketCount( |
| 154 "Extensions.Events.DispatchWithRunningEventPage", | 156 "Extensions.Events.DispatchWithRunningEventPage", |
| 155 events::HistogramValue::FOR_TEST, running_count); | 157 events::HistogramValue::FOR_TEST, running_count); |
| 156 } | 158 } |
| 157 } | 159 } |
| 158 | 160 |
| 159 private: | 161 private: |
| 160 scoped_ptr<content::NotificationService> notification_service_; | 162 std::unique_ptr<content::NotificationService> notification_service_; |
| 161 content::TestBrowserThreadBundle thread_bundle_; | 163 content::TestBrowserThreadBundle thread_bundle_; |
| 162 base::HistogramTester histogram_tester_; | 164 base::HistogramTester histogram_tester_; |
| 163 | 165 |
| 164 DISALLOW_COPY_AND_ASSIGN(EventRouterTest); | 166 DISALLOW_COPY_AND_ASSIGN(EventRouterTest); |
| 165 }; | 167 }; |
| 166 | 168 |
| 167 TEST_F(EventRouterTest, GetBaseEventName) { | 169 TEST_F(EventRouterTest, GetBaseEventName) { |
| 168 // Normal event names are passed through unchanged. | 170 // Normal event names are passed through unchanged. |
| 169 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar")); | 171 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar")); |
| 170 | 172 |
| 171 // Sub-events are converted to the part before the slash. | 173 // Sub-events are converted to the part before the slash. |
| 172 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar/123")); | 174 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar/123")); |
| 173 } | 175 } |
| 174 | 176 |
| 175 // Tests adding and removing observers from EventRouter. | 177 // Tests adding and removing observers from EventRouter. |
| 176 void EventRouterTest::RunEventRouterObserverTest( | 178 void EventRouterTest::RunEventRouterObserverTest( |
| 177 const EventListenerConstructor& constructor) { | 179 const EventListenerConstructor& constructor) { |
| 178 EventRouter router(NULL, NULL); | 180 EventRouter router(NULL, NULL); |
| 179 scoped_ptr<EventListener> listener = | 181 std::unique_ptr<EventListener> listener = |
| 180 constructor.Run("event_name", NULL, new base::DictionaryValue()); | 182 constructor.Run("event_name", NULL, new base::DictionaryValue()); |
| 181 | 183 |
| 182 // Add/remove works without any observers. | 184 // Add/remove works without any observers. |
| 183 router.OnListenerAdded(listener.get()); | 185 router.OnListenerAdded(listener.get()); |
| 184 router.OnListenerRemoved(listener.get()); | 186 router.OnListenerRemoved(listener.get()); |
| 185 | 187 |
| 186 // Register observers that both match and don't match the event above. | 188 // Register observers that both match and don't match the event above. |
| 187 MockEventRouterObserver matching_observer; | 189 MockEventRouterObserver matching_observer; |
| 188 router.RegisterObserver(&matching_observer, "event_name"); | 190 router.RegisterObserver(&matching_observer, "event_name"); |
| 189 MockEventRouterObserver non_matching_observer; | 191 MockEventRouterObserver non_matching_observer; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 205 EXPECT_EQ(0, non_matching_observer.listener_added_count()); | 207 EXPECT_EQ(0, non_matching_observer.listener_added_count()); |
| 206 | 208 |
| 207 // Removing the listener again notifies again. | 209 // Removing the listener again notifies again. |
| 208 router.OnListenerRemoved(listener.get()); | 210 router.OnListenerRemoved(listener.get()); |
| 209 EXPECT_EQ(2, matching_observer.listener_removed_count()); | 211 EXPECT_EQ(2, matching_observer.listener_removed_count()); |
| 210 EXPECT_EQ(0, non_matching_observer.listener_removed_count()); | 212 EXPECT_EQ(0, non_matching_observer.listener_removed_count()); |
| 211 | 213 |
| 212 // Adding a listener with a sub-event notifies the main observer with | 214 // Adding a listener with a sub-event notifies the main observer with |
| 213 // proper details. | 215 // proper details. |
| 214 matching_observer.Reset(); | 216 matching_observer.Reset(); |
| 215 scoped_ptr<EventListener> sub_event_listener = | 217 std::unique_ptr<EventListener> sub_event_listener = |
| 216 constructor.Run("event_name/1", NULL, new base::DictionaryValue()); | 218 constructor.Run("event_name/1", NULL, new base::DictionaryValue()); |
| 217 router.OnListenerAdded(sub_event_listener.get()); | 219 router.OnListenerAdded(sub_event_listener.get()); |
| 218 EXPECT_EQ(1, matching_observer.listener_added_count()); | 220 EXPECT_EQ(1, matching_observer.listener_added_count()); |
| 219 EXPECT_EQ(0, matching_observer.listener_removed_count()); | 221 EXPECT_EQ(0, matching_observer.listener_removed_count()); |
| 220 EXPECT_EQ("event_name/1", matching_observer.last_event_name()); | 222 EXPECT_EQ("event_name/1", matching_observer.last_event_name()); |
| 221 | 223 |
| 222 // Ditto for removing the listener. | 224 // Ditto for removing the listener. |
| 223 matching_observer.Reset(); | 225 matching_observer.Reset(); |
| 224 router.OnListenerRemoved(sub_event_listener.get()); | 226 router.OnListenerRemoved(sub_event_listener.get()); |
| 225 EXPECT_EQ(0, matching_observer.listener_added_count()); | 227 EXPECT_EQ(0, matching_observer.listener_added_count()); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 scoped_refptr<Extension> component_event = CreateExtension(true, false); | 272 scoped_refptr<Extension> component_event = CreateExtension(true, false); |
| 271 router.ReportEvent(events::HistogramValue::FOR_TEST, component_event.get(), | 273 router.ReportEvent(events::HistogramValue::FOR_TEST, component_event.get(), |
| 272 false /** did_enqueue */); | 274 false /** did_enqueue */); |
| 273 ExpectHistogramCounts(6, 2, 2, 1, 0, 2); | 275 ExpectHistogramCounts(6, 2, 2, 1, 0, 2); |
| 274 router.ReportEvent(events::HistogramValue::FOR_TEST, component_event.get(), | 276 router.ReportEvent(events::HistogramValue::FOR_TEST, component_event.get(), |
| 275 true /** did_enqueue */); | 277 true /** did_enqueue */); |
| 276 ExpectHistogramCounts(7, 3, 2, 2, 1, 2); | 278 ExpectHistogramCounts(7, 3, 2, 2, 1, 2); |
| 277 } | 279 } |
| 278 | 280 |
| 279 } // namespace extensions | 281 } // namespace extensions |
| OLD | NEW |