Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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 /** | |
| 6 * Return the information of asynchronous script execution. | |
| 7 * | |
| 8 * @return {Object.<string, ?>} Information of asynchronous script execution. | |
| 9 */ | |
| 10 function getAsyncScriptInfo() { | |
| 11 var key = 'chromedriverAsyncScriptInfo'; | |
| 12 if (!(key in document)) | |
| 13 document[key] = {'id': 0, 'finished': false}; | |
| 14 return document[key]; | |
| 15 } | |
| 16 | |
| 17 /** | |
| 18 * Execute the given script and save its asynchronous result. | |
| 19 * | |
| 20 * If script1 finishes after script2 is executed, then script1's result will be | |
| 21 * discarded while script2's will be saved. | |
| 22 * | |
| 23 * @param {!string} script The asynchronous script to be executed. | |
| 24 * @param {Array.<*>} args Arguments to be passed to the script. | |
| 25 * @param {!number} timeout The timeout for the asynchronous script. | |
| 26 */ | |
| 27 function executeAsyncScript(script, args, timeout) { | |
| 28 var info = getAsyncScriptInfo(); | |
| 29 info.id++; | |
| 30 info.finished = false; | |
| 31 delete info.result; | |
| 32 var id = info.id; | |
| 33 | |
| 34 function callback(result) { | |
| 35 if (id == info.id) { | |
| 36 info.result = result; | |
| 37 info.finished = true; | |
| 38 } | |
| 39 } | |
| 40 args.push(callback); | |
| 41 | |
| 42 try { | |
| 43 new Function(script).apply(null, args); | |
| 44 } catch (error) { | |
| 45 error.code = 17; // Error code for JavaScriptError. | |
| 46 throw error; | |
| 47 } | |
| 48 | |
| 49 function clearResultIfTimeout() { | |
| 50 if (id == info.id) { | |
| 51 delete info.result; // Delete reference to result if it is already set. | |
| 52 info.id++; // Prevent the script from setting the result if not yet. | |
| 53 } | |
| 54 } | |
| 55 // Allow C++ to pull result in 5 seconds before deletion. | |
| 56 setTimeout(clearResultIfTimeout, timeout + 5000); | |
|
chrisgao (Use stgao instead)
2013/03/08 23:28:25
I think 5 seconds should be long enough for C++ to
| |
| 57 } | |
| OLD | NEW |