- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / good / Extensions / bjafgdebaacbbbecmhlhpofkepfkgcpa / 1.0 / page.js
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 var win = window;
6 if (typeof(contentWindow) != 'undefined') {
7   win = contentWindow;
8 }
9
10 chrome.runtime.onConnect.addListener(function(port) {
11   console.log('connected');
12   port.onMessage.addListener(function(msg) {
13     console.log('got ' + msg);
14     if (msg.testPostMessage) {
15       port.postMessage({success: true});
16     } else if (msg.testPostMessageFromTab) {
17       testPostMessageFromTab(port);
18     } else if (msg.testDisconnect) {
19       port.disconnect();
20     } else if (msg.testDisconnectOnClose) {
21       win.location = "about:blank";
22     } else if (msg.testPortName) {
23       port.postMessage({portName:port.name});
24     }
25     // Ignore other messages since they are from us.
26   });
27 });
28
29 // Tests that postMessage to the extension and its response works.
30 function testPostMessageFromTab(origPort) {
31   console.log('testPostMessageFromTab');
32   var portName = "peter";
33   var port = chrome.runtime.connect({name: portName});
34   port.postMessage({testPostMessageFromTab: true});
35   port.onMessage.addListener(function(msg) {
36     origPort.postMessage({success: (msg.success && (msg.portName == portName))});
37     console.log('sent ' + msg.success);
38     port.disconnect();
39   });
40   console.log('posted message');
41 }
42
43 // Workaround two bugs: shutdown crash if we hook 'unload', and content script
44 // GC if we don't register any event handlers.
45 // http://code.google.com/p/chromium/issues/detail?id=17410
46 // http://code.google.com/p/chromium/issues/detail?id=17582
47 function foo() {}
48 win.addEventListener('error', foo);