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 "base/values.h" |
5 #include "chrome/browser/extensions/event_router.h" | 6 #include "chrome/browser/extensions/event_router.h" |
6 #include "chrome/browser/extensions/extension_apitest.h" | 7 #include "chrome/browser/extensions/extension_apitest.h" |
7 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
8 #include "chrome/common/chrome_notification_types.h" | 9 #include "chrome/common/chrome_notification_types.h" |
9 #include "content/public/browser/notification_registrar.h" | 10 #include "content/public/browser/notification_registrar.h" |
10 #include "content/public/browser/notification_service.h" | 11 #include "content/public/browser/notification_service.h" |
11 #include "googleurl/src/gurl.h" | 12 #include "googleurl/src/gurl.h" |
12 | 13 |
13 namespace { | 14 namespace { |
14 | 15 |
15 class MessageSender : public content::NotificationObserver { | 16 class MessageSender : public content::NotificationObserver { |
16 public: | 17 public: |
17 MessageSender() { | 18 MessageSender() { |
18 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, | 19 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, |
19 content::NotificationService::AllSources()); | 20 content::NotificationService::AllSources()); |
20 } | 21 } |
21 | 22 |
22 private: | 23 private: |
| 24 static scoped_ptr<ListValue> BuildEventArguments(const bool last_message, |
| 25 const std::string& data) { |
| 26 DictionaryValue* event = new DictionaryValue(); |
| 27 event->SetBoolean("lastMessage", last_message); |
| 28 event->SetString("data", data); |
| 29 scoped_ptr<ListValue> arguments(new ListValue()); |
| 30 arguments->Append(event); |
| 31 return arguments.Pass(); |
| 32 } |
| 33 |
23 virtual void Observe(int type, | 34 virtual void Observe(int type, |
24 const content::NotificationSource& source, | 35 const content::NotificationSource& source, |
25 const content::NotificationDetails& details) { | 36 const content::NotificationDetails& details) { |
26 extensions::EventRouter* event_router = | 37 extensions::EventRouter* event_router = |
27 content::Source<Profile>(source).ptr()->GetExtensionEventRouter(); | 38 content::Source<Profile>(source).ptr()->GetExtensionEventRouter(); |
28 | 39 |
29 // Sends four messages to the extension. All but the third message sent | 40 // Sends four messages to the extension. All but the third message sent |
30 // from the origin http://b.com/ are supposed to arrive. | 41 // from the origin http://b.com/ are supposed to arrive. |
31 event_router->DispatchEventToRenderers("test.onMessage", | 42 event_router->DispatchEventToRenderers("test.onMessage", |
32 "[{\"lastMessage\":false,\"data\":\"no restriction\"}]", | 43 BuildEventArguments(false, "no restriction"), |
33 content::Source<Profile>(source).ptr(), | 44 content::Source<Profile>(source).ptr(), |
34 GURL(), | 45 GURL(), |
35 extensions::EventFilteringInfo()); | 46 extensions::EventFilteringInfo()); |
36 event_router->DispatchEventToRenderers("test.onMessage", | 47 event_router->DispatchEventToRenderers("test.onMessage", |
37 "[{\"lastMessage\":false,\"data\":\"http://a.com/\"}]", | 48 BuildEventArguments(false, "http://a.com/"), |
38 content::Source<Profile>(source).ptr(), | 49 content::Source<Profile>(source).ptr(), |
39 GURL("http://a.com/"), | 50 GURL("http://a.com/"), |
40 extensions::EventFilteringInfo()); | 51 extensions::EventFilteringInfo()); |
41 event_router->DispatchEventToRenderers("test.onMessage", | 52 event_router->DispatchEventToRenderers("test.onMessage", |
42 "[{\"lastMessage\":false,\"data\":\"http://b.com/\"}]", | 53 BuildEventArguments(false, "http://b.com/"), |
43 content::Source<Profile>(source).ptr(), | 54 content::Source<Profile>(source).ptr(), |
44 GURL("http://b.com/"), | 55 GURL("http://b.com/"), |
45 extensions::EventFilteringInfo()); | 56 extensions::EventFilteringInfo()); |
46 event_router->DispatchEventToRenderers("test.onMessage", | 57 event_router->DispatchEventToRenderers("test.onMessage", |
47 "[{\"lastMessage\":true,\"data\":\"last message\"}]", | 58 BuildEventArguments(true, "last message"), |
48 content::Source<Profile>(source).ptr(), | 59 content::Source<Profile>(source).ptr(), |
49 GURL(), | 60 GURL(), |
50 extensions::EventFilteringInfo()); | 61 extensions::EventFilteringInfo()); |
51 } | 62 } |
52 | 63 |
53 content::NotificationRegistrar registrar_; | 64 content::NotificationRegistrar registrar_; |
54 }; | 65 }; |
55 | 66 |
56 } // namespace | 67 } // namespace |
57 | 68 |
(...skipping 14 matching lines...) Expand all Loading... |
72 | 83 |
73 ASSERT_TRUE(RunExtensionTest("messaging/connect_external")) << message_; | 84 ASSERT_TRUE(RunExtensionTest("messaging/connect_external")) << message_; |
74 } | 85 } |
75 | 86 |
76 // Tests that messages with event_urls are only passed to extensions with | 87 // Tests that messages with event_urls are only passed to extensions with |
77 // appropriate permissions. | 88 // appropriate permissions. |
78 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MessagingEventURL) { | 89 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MessagingEventURL) { |
79 MessageSender sender; | 90 MessageSender sender; |
80 ASSERT_TRUE(RunExtensionTest("messaging/event_url")) << message_; | 91 ASSERT_TRUE(RunExtensionTest("messaging/event_url")) << message_; |
81 } | 92 } |
OLD | NEW |