dom/workers/test/importScripts_worker.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 /**
     2  * Any copyright is dedicated to the Public Domain.
     3  * http://creativecommons.org/publicdomain/zero/1.0/
     4  */
     5 // Try no args. This shouldn't do anything.
     6 importScripts();
     8 // This caused security exceptions in the past, make sure it doesn't!
     9 var constructor = {}.constructor;
    11 importScripts("importScripts_worker_imported1.js");
    13 // Try to call a function defined in the imported script.
    14 importedScriptFunction();
    16 function tryBadScripts() {
    17   var badScripts = [
    18     // Has a syntax error
    19     "importScripts_worker_imported3.js",
    20     // Throws an exception
    21     "importScripts_worker_imported4.js",
    22     // Shouldn't exist!
    23     "http://example.com/non-existing/importScripts_worker_foo.js",
    24     // Not a valid url
    25     "http://notadomain::notafile aword"
    26   ];
    28   for (var i = 0; i < badScripts.length; i++) {
    29     var caughtException = false;
    30     var url = badScripts[i];
    31     try {
    32       importScripts(url);
    33     }
    34     catch (e) {
    35       caughtException = true;
    36     }
    37     if (!caughtException) {
    38       throw "Bad script didn't throw exception: " + url;
    39     }
    40   }
    41 }
    43 const url = "data:text/plain,const startResponse = 'started';";
    44 importScripts(url);
    46 onmessage = function(event) {
    47   switch (event.data) {
    48     case 'start':
    49       importScripts("importScripts_worker_imported2.js");
    50       importedScriptFunction2();
    51       tryBadScripts();
    52       postMessage(startResponse);
    53       break;
    54     case 'stop':
    55       tryBadScripts();
    56       postMessage('stopped');
    57       break;
    58     default:
    59       throw new Error("Bad message: " + event.data);
    60       break;
    61   }
    62 }
    64 tryBadScripts();

mercurial