Chromium Code Reviews| Index: chrome/test/chromedriver/js/execute_async_script.js |
| diff --git a/chrome/test/chromedriver/js/execute_async_script.js b/chrome/test/chromedriver/js/execute_async_script.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3522dc4863583d3608a561e27de89ea7df00e809 |
| --- /dev/null |
| +++ b/chrome/test/chromedriver/js/execute_async_script.js |
| @@ -0,0 +1,57 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +/** |
| +* Return the information of asynchronous script execution. |
| +* |
| +* @return {Object.<string, ?>} Information of asynchronous script execution. |
| +*/ |
| +function getAsyncScriptInfo() { |
| + var key = 'chromedriverAsyncScriptInfo'; |
| + if (!(key in document)) |
| + document[key] = {'id': 0, 'finished': false}; |
| + return document[key]; |
| +} |
| + |
| +/** |
| +* Execute the given script and save its asynchronous result. |
| +* |
| +* If script1 finishes after script2 is executed, then script1's result will be |
| +* discarded while script2's will be saved. |
| +* |
| +* @param {!string} script The asynchronous script to be executed. |
| +* @param {Array.<*>} args Arguments to be passed to the script. |
| +* @param {!number} timeout The timeout for the asynchronous script. |
| +*/ |
| +function executeAsyncScript(script, args, timeout) { |
| + var info = getAsyncScriptInfo(); |
| + info.id++; |
| + info.finished = false; |
| + delete info.result; |
| + var id = info.id; |
| + |
| + function callback(result) { |
| + if (id == info.id) { |
| + info.result = result; |
| + info.finished = true; |
| + } |
| + } |
| + args.push(callback); |
| + |
| + try { |
| + new Function(script).apply(null, args); |
| + } catch (error) { |
| + error.code = 17; // Error code for JavaScriptError. |
| + throw error; |
| + } |
| + |
| + function clearResultIfTimeout() { |
| + if (id == info.id) { |
| + delete info.result; // Delete reference to result if it is already set. |
| + info.id++; // Prevent the script from setting the result if not yet. |
| + } |
| + } |
| + // Allow C++ to pull result in 5 seconds before deletion. |
| + 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
|
| +} |