Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 window.assertions = (function() { | 5 window.assertions = (function() { |
| 6 | 6 |
| 7 // We are going to kill all of the builtins, so hold onto the ones we need. | |
| 8 var defineGetter = Object.prototype.__defineGetter__; | |
| 9 var defineSetter = Object.prototype.__defineSetter__; | |
| 10 var Error = window.Error; | |
| 11 var forEach = Array.prototype.forEach; | |
| 12 var hasOwnProperty = Object.prototype.hasOwnProperty; | |
| 13 var getOwnPropertyNames = Object.getOwnPropertyNames; | |
| 14 var stringify = JSON.stringify; | |
| 15 | |
| 16 // Kill all of the builtins functions to give us a fairly high confidence that | |
| 17 // the environment our bindings run in can't interfere with our code. | |
| 18 // These are taken from the ECMAScript spec. | |
| 19 var builtinTypes = [ | |
| 20 Object, Function, Array, String, Boolean, Number, Math, Date, RegExp, JSON, | |
| 21 ]; | |
| 22 | |
| 23 function clobber(obj, name, description) { | |
|
Jeffrey Yasskin
2013/06/20 23:33:04
s/description/qualifiedName/?
not at google - send to devlin
2013/06/21 00:01:44
Done.
| |
| 24 // Clobbering constructors would break everything. | |
| 25 // Clobbering toString is annoying. | |
| 26 // Clobbering __proto__ breaks in ways that grep can't find. | |
| 27 // Clobbering Function.call would make it impossible to implement these tests. | |
| 28 // Clobbering Object.valueOf breaks v8. | |
| 29 if (name == 'constructor' || | |
| 30 name == 'toString' || | |
| 31 name == '__proto__' || | |
| 32 description == 'Function.call' || | |
| 33 description == 'Object.valueOf') { | |
| 34 return; | |
| 35 } | |
| 36 if (typeof(obj[name]) == 'function') { | |
| 37 obj[name] = function() { | |
| 38 throw new Error('Clobbered ' + description + ' function'); | |
| 39 }; | |
| 40 } else { | |
| 41 defineGetter.call(obj, name, function() { | |
| 42 throw new Error('Clobbered ' + description + ' getter'); | |
| 43 }); | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 forEach.call(builtinTypes, function(builtin) { | |
| 48 var prototype = builtin.prototype; | |
| 49 if (prototype) { | |
| 50 forEach.call(getOwnPropertyNames(prototype), function(name) { | |
| 51 clobber(prototype, name, prototype.constructor.name + '.' + name); | |
| 52 }); | |
| 53 } | |
| 54 forEach.call(getOwnPropertyNames(builtin), function(name) { | |
| 55 clobber(builtin, name, name); | |
|
Jeffrey Yasskin
2013/06/20 23:33:04
'name' doesn't seem right for the qualified name h
not at google - send to devlin
2013/06/21 00:01:44
Done
| |
| 56 }); | |
| 57 if (builtin.name) | |
| 58 clobber(window, builtin.name, 'window.' + builtin.name); | |
| 59 }); | |
| 60 | |
| 7 // Codes for test results. Must match ExternallyConnectableMessagingTest::Result | 61 // Codes for test results. Must match ExternallyConnectableMessagingTest::Result |
| 8 // in c/b/extensions/extension_messages_apitest.cc. | 62 // in c/b/extensions/extension_messages_apitest.cc. |
| 9 var results = { | 63 var results = { |
| 10 OK: 0, | 64 OK: 0, |
| 11 NAMESPACE_NOT_DEFINED: 1, | 65 NAMESPACE_NOT_DEFINED: 1, |
| 12 FUNCTION_NOT_DEFINED: 2, | 66 FUNCTION_NOT_DEFINED: 2, |
| 13 COULD_NOT_ESTABLISH_CONNECTION_ERROR: 3, | 67 COULD_NOT_ESTABLISH_CONNECTION_ERROR: 3, |
| 14 OTHER_ERROR: 4, | 68 OTHER_ERROR: 4, |
| 15 INCORRECT_RESPONSE_SENDER: 5, | 69 INCORRECT_RESPONSE_SENDER: 5, |
| 16 INCORRECT_RESPONSE_MESSAGE: 6 | 70 INCORRECT_RESPONSE_MESSAGE: 6 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 30 reply(results.OTHER_ERROR); | 84 reply(results.OTHER_ERROR); |
| 31 return false; | 85 return false; |
| 32 } | 86 } |
| 33 | 87 |
| 34 function checkResponse(response, reply) { | 88 function checkResponse(response, reply) { |
| 35 // The response will be an echo of both the original message *and* the | 89 // The response will be an echo of both the original message *and* the |
| 36 // MessageSender (with the tab field stripped down). | 90 // MessageSender (with the tab field stripped down). |
| 37 // | 91 // |
| 38 // First check the sender was correct. | 92 // First check the sender was correct. |
| 39 var incorrectSender = false; | 93 var incorrectSender = false; |
| 40 if (!response.sender.hasOwnProperty('tab')) { | 94 if (!hasOwnProperty.call(response.sender, 'tab')) { |
| 41 console.warn('Expected a tab, got none'); | 95 console.warn('Expected a tab, got none'); |
| 42 incorrectSender = true; | 96 incorrectSender = true; |
| 43 } | 97 } |
| 44 if (response.sender.tab.url != document.location.href) { | 98 if (response.sender.tab.url != document.location.href) { |
| 45 console.warn('Expected tab url ' + document.location.href + ' got ' + | 99 console.warn('Expected tab url ' + document.location.href + ' got ' + |
| 46 response.sender.tab.url); | 100 response.sender.tab.url); |
| 47 incorrectSender = true; | 101 incorrectSender = true; |
| 48 } | 102 } |
| 49 if (response.sender.hasOwnProperty('id')) { | 103 if (hasOwnProperty.call(response.sender, 'id')) { |
| 50 console.warn('Expected no id, got "' + response.sender.id + '"'); | 104 console.warn('Expected no id, got "' + response.sender.id + '"'); |
| 51 incorrectSender = true; | 105 incorrectSender = true; |
| 52 } | 106 } |
| 53 if (response.sender.url != document.location.href) { | 107 if (response.sender.url != document.location.href) { |
| 54 console.warn('Expected url ' + document.location.href + ' got ' + | 108 console.warn('Expected url ' + document.location.href + ' got ' + |
| 55 response.sender.url); | 109 response.sender.url); |
| 56 incorrectSender = true; | 110 incorrectSender = true; |
| 57 } | 111 } |
| 58 if (incorrectSender) { | 112 if (incorrectSender) { |
| 59 reply(results.INCORRECT_RESPONSE_SENDER); | 113 reply(results.INCORRECT_RESPONSE_SENDER); |
| 60 return false; | 114 return false; |
| 61 } | 115 } |
| 62 | 116 |
| 63 // Check the correct content was echoed. | 117 // Check the correct content was echoed. |
| 64 var expectedJson = JSON.stringify(message); | 118 var expectedJson = stringify(message); |
| 65 var actualJson = JSON.stringify(response.message); | 119 var actualJson = stringify(response.message); |
| 66 if (actualJson == expectedJson) | 120 if (actualJson == expectedJson) |
| 67 return true; | 121 return true; |
| 68 console.warn('Expected message ' + expectedJson + ' got ' + actualJson); | 122 console.warn('Expected message ' + expectedJson + ' got ' + actualJson); |
| 69 reply(results.INCORRECT_RESPONSE_MESSAGE); | 123 reply(results.INCORRECT_RESPONSE_MESSAGE); |
| 70 return false; | 124 return false; |
| 71 } | 125 } |
| 72 | 126 |
| 73 var sendToBrowser = domAutomationController.send.bind(domAutomationController); | 127 function sendToBrowser(msg) { |
| 128 domAutomationController.send(msg); | |
| 129 } | |
| 74 | 130 |
| 75 return { | 131 return { |
| 76 canConnectAndSendMessages: function(extensionId) { | 132 canConnectAndSendMessages: function(extensionId) { |
| 77 if (!chrome.runtime) { | 133 if (!chrome.runtime) { |
| 78 sendToBrowser(results.NAMESPACE_NOT_DEFINED); | 134 sendToBrowser(results.NAMESPACE_NOT_DEFINED); |
| 79 return; | 135 return; |
| 80 } | 136 } |
| 81 | 137 |
| 82 if (!chrome.runtime.sendMessage || !chrome.runtime.connect) { | 138 if (!chrome.runtime.sendMessage || !chrome.runtime.connect) { |
| 83 sendToBrowser(results.FUNCTION_NOT_DEFINED); | 139 sendToBrowser(results.FUNCTION_NOT_DEFINED); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 113 if (result != results.OK) | 169 if (result != results.OK) |
| 114 sendToBrowser(result); | 170 sendToBrowser(result); |
| 115 else | 171 else |
| 116 canConnectAndSendMessages(sendToBrowser); | 172 canConnectAndSendMessages(sendToBrowser); |
| 117 }); | 173 }); |
| 118 }, | 174 }, |
| 119 | 175 |
| 120 areAnyRuntimePropertiesDefined: function(names) { | 176 areAnyRuntimePropertiesDefined: function(names) { |
| 121 var result = false; | 177 var result = false; |
| 122 if (chrome.runtime) { | 178 if (chrome.runtime) { |
| 123 names.forEach(function(name) { | 179 forEach.call(names, function(name) { |
| 124 if (chrome.runtime[name]) { | 180 if (chrome.runtime[name]) { |
| 125 console.log('runtime.' + name + ' is defined'); | 181 console.log('runtime.' + name + ' is defined'); |
| 126 result = true; | 182 result = true; |
| 127 } | 183 } |
| 128 }); | 184 }); |
| 129 } | 185 } |
| 130 sendToBrowser(result); | 186 sendToBrowser(result); |
| 131 } | 187 } |
| 132 }; | 188 }; |
| 133 | 189 |
| 134 }()); // window.assertions | 190 }()); // window.assertions |
| OLD | NEW |