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