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

Side by Side Diff: chrome/browser/extensions/extension_messages_apitest.cc

Issue 10694085: Refactor extension event distribution to use Values instead of JSON strings. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Build fix. Created 8 years, 5 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 | Annotate | Revision Log
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 "base/values.h"
5 #include "chrome/browser/extensions/extension_apitest.h" 6 #include "chrome/browser/extensions/extension_apitest.h"
6 #include "chrome/browser/extensions/extension_event_router.h" 7 #include "chrome/browser/extensions/extension_event_router.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 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 ListValue* arguments = new ListValue();
30 arguments->Append(event);
31 return arguments;
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 ExtensionEventRouter* event_router = 37 ExtensionEventRouter* 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 event_router->DispatchEventToRenderers("test.onMessage", 46 event_router->DispatchEventToRenderers("test.onMessage",
36 "[{\"lastMessage\":false,\"data\":\"http://a.com/\"}]", 47 BuildEventArguments(false, "http://a.com/"),
37 content::Source<Profile>(source).ptr(), 48 content::Source<Profile>(source).ptr(),
38 GURL("http://a.com/")); 49 GURL("http://a.com/"));
39 event_router->DispatchEventToRenderers("test.onMessage", 50 event_router->DispatchEventToRenderers("test.onMessage",
40 "[{\"lastMessage\":false,\"data\":\"http://b.com/\"}]", 51 BuildEventArguments(false, "http://b.com/"),
41 content::Source<Profile>(source).ptr(), 52 content::Source<Profile>(source).ptr(),
42 GURL("http://b.com/")); 53 GURL("http://b.com/"));
43 event_router->DispatchEventToRenderers("test.onMessage", 54 event_router->DispatchEventToRenderers("test.onMessage",
44 "[{\"lastMessage\":true,\"data\":\"last message\"}]", 55 BuildEventArguments(true, "last message"),
45 content::Source<Profile>(source).ptr(), 56 content::Source<Profile>(source).ptr(),
46 GURL()); 57 GURL());
47 } 58 }
48 59
49 content::NotificationRegistrar registrar_; 60 content::NotificationRegistrar registrar_;
50 }; 61 };
51 62
52 } // namespace 63 } // namespace
53 64
54 // Tests that message passing between extensions and content scripts works. 65 // Tests that message passing between extensions and content scripts works.
(...skipping 13 matching lines...) Expand all
68 79
69 ASSERT_TRUE(RunExtensionTest("messaging/connect_external")) << message_; 80 ASSERT_TRUE(RunExtensionTest("messaging/connect_external")) << message_;
70 } 81 }
71 82
72 // Tests that messages with event_urls are only passed to extensions with 83 // Tests that messages with event_urls are only passed to extensions with
73 // appropriate permissions. 84 // appropriate permissions.
74 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MessagingEventURL) { 85 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MessagingEventURL) {
75 MessageSender sender; 86 MessageSender sender;
76 ASSERT_TRUE(RunExtensionTest("messaging/event_url")) << message_; 87 ASSERT_TRUE(RunExtensionTest("messaging/event_url")) << message_;
77 } 88 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698