From 59431b055176c172e95dacdeb7e0172679d5e6f5 Mon Sep 17 00:00:00 2001 From: Daosheng Mu Date: Thu, 31 Dec 2015 14:47:27 +0800 Subject: [PATCH] Fix race condition in embed.js --- core/embed.js | 62 +++++++++++++++++++++++++++----------------------- core/loader.js | 32 +++++++++++++++++--------- 2 files changed, 54 insertions(+), 40 deletions(-) diff --git a/core/embed.js b/core/embed.js index fb2555f3..b066b618 100644 --- a/core/embed.js +++ b/core/embed.js @@ -71,7 +71,7 @@ gliloader.pathRoot = pathRoot; if (useDebug) { // In debug mode load all the scripts - gliloader.load(["host", "replay", "ui"]); + gliloader.load(["host", "replay", "ui"], setupContext); } }; script.onreadystatechange = function () { @@ -86,40 +86,44 @@ scriptLoaded(); } }; + } else { + setupContext(); } - // Hook canvas.getContext - var originalGetContext = HTMLCanvasElement.prototype.getContext; - if (!HTMLCanvasElement.prototype.getContextRaw) { - HTMLCanvasElement.prototype.getContextRaw = originalGetContext; - } - HTMLCanvasElement.prototype.getContext = function () { - var ignoreCanvas = this.internalInspectorSurface; - if (ignoreCanvas) { - return originalGetContext.apply(this, arguments); + function setupContext() { + // Hook canvas.getContext + var originalGetContext = HTMLCanvasElement.prototype.getContext; + if (!HTMLCanvasElement.prototype.getContextRaw) { + HTMLCanvasElement.prototype.getContextRaw = originalGetContext; } - - var contextNames = ["moz-webgl", "webkit-3d", "experimental-webgl", "webgl"]; - var requestingWebGL = contextNames.indexOf(arguments[0]) != -1; + HTMLCanvasElement.prototype.getContext = function () { + var ignoreCanvas = this.internalInspectorSurface; + if (ignoreCanvas) { + return originalGetContext.apply(this, arguments); + } + + var contextNames = ["moz-webgl", "webkit-3d", "experimental-webgl", "webgl"]; + var requestingWebGL = contextNames.indexOf(arguments[0]) != -1; - if (requestingWebGL) { - // Page is requesting a WebGL context! - // TODO: something - } + if (requestingWebGL) { + // Page is requesting a WebGL context! + // TODO: something + } - var result = originalGetContext.apply(this, arguments); - if (result == null) { - return null; - } + var result = originalGetContext.apply(this, arguments); + if (result == null) { + return null; + } - if (requestingWebGL) { - // TODO: pull options from somewhere? - result = gli.host.inspectContext(this, result); - var hostUI = new gli.host.HostUI(result); - result.hostUI = hostUI; // just so we can access it later for debugging - } + if (requestingWebGL) { + // TODO: pull options from somewhere? + result = gli.host.inspectContext(this, result); + var hostUI = new gli.host.HostUI(result); + result.hostUI = hostUI; // just so we can access it later for debugging + } - return result; - }; + return result; + }; + } })(); diff --git a/core/loader.js b/core/loader.js index 521df2b5..7997c4ca 100644 --- a/core/loader.js +++ b/core/loader.js @@ -25,9 +25,11 @@ var gliloader = {}; }; function injectScript(filename, injectState) { - var doc = injectState.window.document; - - injectState.toLoad++; + var url = injectState.pathRoot + filename; + injectState.fileList.push(url); + }; + + function loadFiles(injectState) { function scriptsLoaded() { if (injectState.callback) { injectState.callback(); @@ -35,32 +37,38 @@ var gliloader = {}; injectState.callback = null; // To prevent future calls } - var url = injectState.pathRoot + filename; - + var doc = injectState.window.document; + var url = injectState.fileList[injectState.loaded]; var script = doc.createElement("script"); script.type = "text/javascript"; script.src = url; script.onload = function () { - if (--injectState.toLoad == 0) { + ++injectState.loaded; + + if (injectState.loaded == injectState.fileList.length) { scriptsLoaded(); } + else { + loadFiles(injectState); + } }; script.onreadystatechange = function () { if ("loaded" === script.readyState || "complete" === script.readyState) { - if (--injectState.toLoad == 0) { + if (--injectState.loaded == 0) { scriptsLoaded(); } } }; - (doc.head || doc.body || doc.documentElement).appendChild(script); - }; + (doc.head || doc.body || doc.documentElement).appendChild(script); + } function load(modules, callback, targetWindow) { var injectState = { window: targetWindow || window, pathRoot: gliloader.pathRoot, - toLoad: 0, - callback: callback + loaded: 0, // loaded index + callback: callback, + fileList: [] }; var hasInjectedShared = false; @@ -165,6 +173,8 @@ var gliloader = {}; break; } } + + loadFiles(injectState); }; gliloader.pathRoot = null;