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, qualifiedName) { |
| 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 qualifiedName == 'Function.call' || |
| 33 qualifiedName == 'Object.valueOf') { |
| 34 return; |
| 35 } |
| 36 if (typeof(obj[name]) == 'function') { |
| 37 obj[name] = function() { |
| 38 throw new Error('Clobbered ' + qualifiedName + ' function'); |
| 39 }; |
| 40 } else { |
| 41 defineGetter.call(obj, name, function() { |
| 42 throw new Error('Clobbered ' + qualifiedName + ' getter'); |
| 43 }); |
| 44 } |
| 45 } |
| 46 |
| 47 forEach.call(builtinTypes, function(builtin) { |
| 48 var prototype = builtin.prototype; |
| 49 var typename = '<unknown>'; |
| 50 if (prototype) { |
| 51 typename = prototype.constructor.name; |
| 52 forEach.call(getOwnPropertyNames(prototype), function(name) { |
| 53 clobber(prototype, name, typename + '.' + name); |
| 54 }); |
| 55 } |
| 56 forEach.call(getOwnPropertyNames(builtin), function(name) { |
| 57 clobber(builtin, name, typename + '.' + name); |
| 58 }); |
| 59 if (builtin.name) |
| 60 clobber(window, builtin.name, 'window.' + builtin.name); |
| 61 }); |
| 62 |
7 // Codes for test results. Must match ExternallyConnectableMessagingTest::Result | 63 // Codes for test results. Must match ExternallyConnectableMessagingTest::Result |
8 // in c/b/extensions/extension_messages_apitest.cc. | 64 // in c/b/extensions/extension_messages_apitest.cc. |
9 var results = { | 65 var results = { |
10 OK: 0, | 66 OK: 0, |
11 NAMESPACE_NOT_DEFINED: 1, | 67 NAMESPACE_NOT_DEFINED: 1, |
12 FUNCTION_NOT_DEFINED: 2, | 68 FUNCTION_NOT_DEFINED: 2, |
13 COULD_NOT_ESTABLISH_CONNECTION_ERROR: 3, | 69 COULD_NOT_ESTABLISH_CONNECTION_ERROR: 3, |
14 OTHER_ERROR: 4, | 70 OTHER_ERROR: 4, |
15 INCORRECT_RESPONSE_SENDER: 5, | 71 INCORRECT_RESPONSE_SENDER: 5, |
16 INCORRECT_RESPONSE_MESSAGE: 6 | 72 INCORRECT_RESPONSE_MESSAGE: 6 |
(...skipping 13 matching lines...) Expand all Loading... |
30 reply(results.OTHER_ERROR); | 86 reply(results.OTHER_ERROR); |
31 return false; | 87 return false; |
32 } | 88 } |
33 | 89 |
34 function checkResponse(response, reply) { | 90 function checkResponse(response, reply) { |
35 // The response will be an echo of both the original message *and* the | 91 // The response will be an echo of both the original message *and* the |
36 // MessageSender (with the tab field stripped down). | 92 // MessageSender (with the tab field stripped down). |
37 // | 93 // |
38 // First check the sender was correct. | 94 // First check the sender was correct. |
39 var incorrectSender = false; | 95 var incorrectSender = false; |
40 if (!response.sender.hasOwnProperty('tab')) { | 96 if (!hasOwnProperty.call(response.sender, 'tab')) { |
41 console.warn('Expected a tab, got none'); | 97 console.warn('Expected a tab, got none'); |
42 incorrectSender = true; | 98 incorrectSender = true; |
43 } | 99 } |
44 if (response.sender.tab.url != document.location.href) { | 100 if (response.sender.tab.url != document.location.href) { |
45 console.warn('Expected tab url ' + document.location.href + ' got ' + | 101 console.warn('Expected tab url ' + document.location.href + ' got ' + |
46 response.sender.tab.url); | 102 response.sender.tab.url); |
47 incorrectSender = true; | 103 incorrectSender = true; |
48 } | 104 } |
49 if (response.sender.hasOwnProperty('id')) { | 105 if (hasOwnProperty.call(response.sender, 'id')) { |
50 console.warn('Expected no id, got "' + response.sender.id + '"'); | 106 console.warn('Expected no id, got "' + response.sender.id + '"'); |
51 incorrectSender = true; | 107 incorrectSender = true; |
52 } | 108 } |
53 if (response.sender.url != document.location.href) { | 109 if (response.sender.url != document.location.href) { |
54 console.warn('Expected url ' + document.location.href + ' got ' + | 110 console.warn('Expected url ' + document.location.href + ' got ' + |
55 response.sender.url); | 111 response.sender.url); |
56 incorrectSender = true; | 112 incorrectSender = true; |
57 } | 113 } |
58 if (incorrectSender) { | 114 if (incorrectSender) { |
59 reply(results.INCORRECT_RESPONSE_SENDER); | 115 reply(results.INCORRECT_RESPONSE_SENDER); |
60 return false; | 116 return false; |
61 } | 117 } |
62 | 118 |
63 // Check the correct content was echoed. | 119 // Check the correct content was echoed. |
64 var expectedJson = JSON.stringify(message); | 120 var expectedJson = stringify(message); |
65 var actualJson = JSON.stringify(response.message); | 121 var actualJson = stringify(response.message); |
66 if (actualJson == expectedJson) | 122 if (actualJson == expectedJson) |
67 return true; | 123 return true; |
68 console.warn('Expected message ' + expectedJson + ' got ' + actualJson); | 124 console.warn('Expected message ' + expectedJson + ' got ' + actualJson); |
69 reply(results.INCORRECT_RESPONSE_MESSAGE); | 125 reply(results.INCORRECT_RESPONSE_MESSAGE); |
70 return false; | 126 return false; |
71 } | 127 } |
72 | 128 |
73 var sendToBrowser = domAutomationController.send.bind(domAutomationController); | 129 function sendToBrowser(msg) { |
| 130 domAutomationController.send(msg); |
| 131 } |
74 | 132 |
75 return { | 133 return { |
76 canConnectAndSendMessages: function(extensionId) { | 134 canConnectAndSendMessages: function(extensionId) { |
77 if (!chrome.runtime) { | 135 if (!chrome.runtime) { |
78 sendToBrowser(results.NAMESPACE_NOT_DEFINED); | 136 sendToBrowser(results.NAMESPACE_NOT_DEFINED); |
79 return; | 137 return; |
80 } | 138 } |
81 | 139 |
82 if (!chrome.runtime.sendMessage || !chrome.runtime.connect) { | 140 if (!chrome.runtime.sendMessage || !chrome.runtime.connect) { |
83 sendToBrowser(results.FUNCTION_NOT_DEFINED); | 141 sendToBrowser(results.FUNCTION_NOT_DEFINED); |
(...skipping 29 matching lines...) Expand all Loading... |
113 if (result != results.OK) | 171 if (result != results.OK) |
114 sendToBrowser(result); | 172 sendToBrowser(result); |
115 else | 173 else |
116 canConnectAndSendMessages(sendToBrowser); | 174 canConnectAndSendMessages(sendToBrowser); |
117 }); | 175 }); |
118 }, | 176 }, |
119 | 177 |
120 areAnyRuntimePropertiesDefined: function(names) { | 178 areAnyRuntimePropertiesDefined: function(names) { |
121 var result = false; | 179 var result = false; |
122 if (chrome.runtime) { | 180 if (chrome.runtime) { |
123 names.forEach(function(name) { | 181 forEach.call(names, function(name) { |
124 if (chrome.runtime[name]) { | 182 if (chrome.runtime[name]) { |
125 console.log('runtime.' + name + ' is defined'); | 183 console.log('runtime.' + name + ' is defined'); |
126 result = true; | 184 result = true; |
127 } | 185 } |
128 }); | 186 }); |
129 } | 187 } |
130 sendToBrowser(result); | 188 sendToBrowser(result); |
131 } | 189 } |
132 }; | 190 }; |
133 | 191 |
134 }()); // window.assertions | 192 }()); // window.assertions |
OLD | NEW |