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 (function() { |
6 | 6 |
7 // We are going to kill all of the builtins, so hold onto the ones we need. | 7 // We are going to kill all of the builtins, so hold onto the ones we need. |
8 var defineGetter = Object.prototype.__defineGetter__; | 8 var defineGetter = Object.prototype.__defineGetter__; |
9 var defineSetter = Object.prototype.__defineSetter__; | 9 var defineSetter = Object.prototype.__defineSetter__; |
10 var Error = window.Error; | 10 var Error = window.Error; |
11 var forEach = Array.prototype.forEach; | 11 var forEach = Array.prototype.forEach; |
12 var hasOwnProperty = Object.prototype.hasOwnProperty; | 12 var hasOwnProperty = Object.prototype.hasOwnProperty; |
13 var getOwnPropertyNames = Object.getOwnPropertyNames; | 13 var getOwnPropertyNames = Object.getOwnPropertyNames; |
14 var stringify = JSON.stringify; | 14 var stringify = JSON.stringify; |
15 | 15 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 FUNCTION_NOT_DEFINED: 2, | 68 FUNCTION_NOT_DEFINED: 2, |
69 COULD_NOT_ESTABLISH_CONNECTION_ERROR: 3, | 69 COULD_NOT_ESTABLISH_CONNECTION_ERROR: 3, |
70 OTHER_ERROR: 4, | 70 OTHER_ERROR: 4, |
71 INCORRECT_RESPONSE_SENDER: 5, | 71 INCORRECT_RESPONSE_SENDER: 5, |
72 INCORRECT_RESPONSE_MESSAGE: 6 | 72 INCORRECT_RESPONSE_MESSAGE: 6 |
73 }; | 73 }; |
74 | 74 |
75 // Make the messages sent vaguely complex, but unambiguously JSON-ifiable. | 75 // Make the messages sent vaguely complex, but unambiguously JSON-ifiable. |
76 var message = [{'a': {'b': 10}}, 20, 'c\x10\x11']; | 76 var message = [{'a': {'b': 10}}, 20, 'c\x10\x11']; |
77 | 77 |
| 78 // Our tab's location. Normally this would be our document's location but if |
| 79 // we're an iframe it will be the location of the parent - in which case, |
| 80 // expect to be told. |
| 81 var tabLocationHref = null; |
| 82 |
| 83 if (parent == window) { |
| 84 tabLocationHref = document.location.href; |
| 85 } else { |
| 86 window.addEventListener('message', function listener(event) { |
| 87 window.removeEventListener(listener); |
| 88 tabLocationHref = event.data; |
| 89 }); |
| 90 } |
| 91 |
78 function checkLastError(reply) { | 92 function checkLastError(reply) { |
79 if (!chrome.runtime.lastError) | 93 if (!chrome.runtime.lastError) |
80 return true; | 94 return true; |
81 var kCouldNotEstablishConnection = | 95 var kCouldNotEstablishConnection = |
82 'Could not establish connection. Receiving end does not exist.'; | 96 'Could not establish connection. Receiving end does not exist.'; |
83 if (chrome.runtime.lastError.message == kCouldNotEstablishConnection) | 97 if (chrome.runtime.lastError.message == kCouldNotEstablishConnection) |
84 reply(results.COULD_NOT_ESTABLISH_CONNECTION_ERROR); | 98 reply(results.COULD_NOT_ESTABLISH_CONNECTION_ERROR); |
85 else | 99 else |
86 reply(results.OTHER_ERROR); | 100 reply(results.OTHER_ERROR); |
87 return false; | 101 return false; |
88 } | 102 } |
89 | 103 |
90 function checkResponse(response, reply) { | 104 function checkResponse(response, reply) { |
91 // The response will be an echo of both the original message *and* the | 105 // The response will be an echo of both the original message *and* the |
92 // MessageSender (with the tab field stripped down). | 106 // MessageSender (with the tab field stripped down). |
93 // | 107 // |
94 // First check the sender was correct. | 108 // First check the sender was correct. |
95 var incorrectSender = false; | 109 var incorrectSender = false; |
96 if (!hasOwnProperty.call(response.sender, 'tab')) { | 110 if (!hasOwnProperty.call(response.sender, 'tab')) { |
97 console.warn('Expected a tab, got none'); | 111 console.warn('Expected a tab, got none'); |
98 incorrectSender = true; | 112 incorrectSender = true; |
99 } | 113 } |
100 if (response.sender.tab.url != document.location.href) { | 114 if (response.sender.tab.url != tabLocationHref) { |
101 console.warn('Expected tab url ' + document.location.href + ' got ' + | 115 console.warn('Expected tab url ' + tabLocationHref + ' got ' + |
102 response.sender.tab.url); | 116 response.sender.tab.url); |
103 incorrectSender = true; | 117 incorrectSender = true; |
104 } | 118 } |
105 if (hasOwnProperty.call(response.sender, 'id')) { | 119 if (hasOwnProperty.call(response.sender, 'id')) { |
106 console.warn('Expected no id, got "' + response.sender.id + '"'); | 120 console.warn('Expected no id, got "' + response.sender.id + '"'); |
107 incorrectSender = true; | 121 incorrectSender = true; |
108 } | 122 } |
109 if (response.sender.url != document.location.href) { | 123 if (response.sender.url != document.location.href) { |
110 console.warn('Expected url ' + document.location.href + ' got ' + | 124 console.warn('Expected url ' + document.location.href + ' got ' + |
111 response.sender.url); | 125 response.sender.url); |
(...skipping 11 matching lines...) Expand all Loading... |
123 return true; | 137 return true; |
124 console.warn('Expected message ' + expectedJson + ' got ' + actualJson); | 138 console.warn('Expected message ' + expectedJson + ' got ' + actualJson); |
125 reply(results.INCORRECT_RESPONSE_MESSAGE); | 139 reply(results.INCORRECT_RESPONSE_MESSAGE); |
126 return false; | 140 return false; |
127 } | 141 } |
128 | 142 |
129 function sendToBrowser(msg) { | 143 function sendToBrowser(msg) { |
130 domAutomationController.send(msg); | 144 domAutomationController.send(msg); |
131 } | 145 } |
132 | 146 |
133 return { | 147 window.actions = { |
| 148 appendIframe: function(src) { |
| 149 var iframe = document.createElement('iframe'); |
| 150 // When iframe has loaded, notify it of our tab location (probably |
| 151 // document.location) to use in its assertions, then continue. |
| 152 iframe.addEventListener('load', function listener() { |
| 153 iframe.removeEventListener('load', listener); |
| 154 iframe.contentWindow.postMessage(tabLocationHref, '*'); |
| 155 sendToBrowser(true); |
| 156 }); |
| 157 iframe.src = src; |
| 158 document.body.appendChild(iframe); |
| 159 } |
| 160 }; |
| 161 |
| 162 window.assertions = { |
134 canConnectAndSendMessages: function(extensionId) { | 163 canConnectAndSendMessages: function(extensionId) { |
135 if (!chrome.runtime) { | 164 if (!chrome.runtime) { |
136 sendToBrowser(results.NAMESPACE_NOT_DEFINED); | 165 sendToBrowser(results.NAMESPACE_NOT_DEFINED); |
137 return; | 166 return; |
138 } | 167 } |
139 | 168 |
140 if (!chrome.runtime.sendMessage || !chrome.runtime.connect) { | 169 if (!chrome.runtime.sendMessage || !chrome.runtime.connect) { |
141 sendToBrowser(results.FUNCTION_NOT_DEFINED); | 170 sendToBrowser(results.FUNCTION_NOT_DEFINED); |
142 return; | 171 return; |
143 } | 172 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 if (chrome.runtime[name]) { | 211 if (chrome.runtime[name]) { |
183 console.log('runtime.' + name + ' is defined'); | 212 console.log('runtime.' + name + ' is defined'); |
184 result = true; | 213 result = true; |
185 } | 214 } |
186 }); | 215 }); |
187 } | 216 } |
188 sendToBrowser(result); | 217 sendToBrowser(result); |
189 } | 218 } |
190 }; | 219 }; |
191 | 220 |
192 }()); // window.assertions | 221 }()); |
OLD | NEW |