• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/extensions/extension_apitest.h"
6 #include "chrome/browser/extensions/extension_event_router.h"
7 #include "chrome/browser/profiles/profile.h"
8 #include "content/common/notification_registrar.h"
9 #include "content/common/notification_service.h"
10 #include "googleurl/src/gurl.h"
11 
12 namespace {
13 
14 class MessageSender : public NotificationObserver {
15  public:
MessageSender()16   MessageSender() {
17     registrar_.Add(this, NotificationType::EXTENSION_HOST_DID_STOP_LOADING,
18                    NotificationService::AllSources());
19   }
20 
21  private:
Observe(NotificationType type,const NotificationSource & source,const NotificationDetails & details)22   virtual void Observe(NotificationType type,
23                        const NotificationSource& source,
24                        const NotificationDetails& details) {
25     ExtensionEventRouter* event_router =
26         Source<Profile>(source).ptr()->GetExtensionEventRouter();
27 
28     // Sends four messages to the extension. All but the third message sent
29     // from the origin http://b.com/ are supposed to arrive.
30     event_router->DispatchEventToRenderers("test.onMessage",
31         "[{\"lastMessage\":false,\"data\":\"no restriction\"}]",
32         Source<Profile>(source).ptr(),
33         GURL());
34     event_router->DispatchEventToRenderers("test.onMessage",
35         "[{\"lastMessage\":false,\"data\":\"http://a.com/\"}]",
36         Source<Profile>(source).ptr(),
37         GURL("http://a.com/"));
38     event_router->DispatchEventToRenderers("test.onMessage",
39         "[{\"lastMessage\":false,\"data\":\"http://b.com/\"}]",
40         Source<Profile>(source).ptr(),
41         GURL("http://b.com/"));
42     event_router->DispatchEventToRenderers("test.onMessage",
43         "[{\"lastMessage\":true,\"data\":\"last message\"}]",
44         Source<Profile>(source).ptr(),
45         GURL());
46   }
47 
48   NotificationRegistrar registrar_;
49 };
50 
51 }  // namespace
52 
53 // Tests that message passing between extensions and content scripts works.
IN_PROC_BROWSER_TEST_F(ExtensionApiTest,Messaging)54 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Messaging) {
55   ASSERT_TRUE(StartTestServer());
56   ASSERT_TRUE(RunExtensionTest("messaging/connect")) << message_;
57 }
58 
59 // Tests that message passing from one extension to another works.
IN_PROC_BROWSER_TEST_F(ExtensionApiTest,MessagingExternal)60 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MessagingExternal) {
61   ASSERT_TRUE(LoadExtension(
62       test_data_dir_.AppendASCII("..").AppendASCII("good")
63                     .AppendASCII("Extensions")
64                     .AppendASCII("bjafgdebaacbbbecmhlhpofkepfkgcpa")
65                     .AppendASCII("1.0")));
66 
67   ASSERT_TRUE(RunExtensionTest("messaging/connect_external")) << message_;
68 }
69 
70 // Tests that messages with event_urls are only passed to extensions with
71 // appropriate permissions.
IN_PROC_BROWSER_TEST_F(ExtensionApiTest,MessagingEventURL)72 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MessagingEventURL) {
73   MessageSender sender;
74   ASSERT_TRUE(RunExtensionTest("messaging/event_url")) << message_;
75 }
76