1 // Copyright 2014 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 "use strict"; 6 7 var installedClasses = {}; 8 var PrivateScriptDOMException = {}; 9 var PrivateScriptJSError = {}; 10 11 // Private scripts can throw JS errors and DOM exceptions as follows: 12 // throwException(PrivateScriptDOMException.IndexSizeError, "..."); 13 // throwException(PrivateScriptJSError.TypeError, "..."); 14 // 15 // Note that normal JS errors thrown by private scripts are treated 16 // as real JS errors caused by programming mistake and the execution crashes. 17 // If you want to intentially throw JS errors from private scripts, 18 // you need to use throwException(PrivateScriptJSError.TypeError, "..."). 19 function throwException(code, message) 20 { 21 function PrivateScriptException() 22 { 23 } 24 25 var exception = new PrivateScriptException(); 26 exception.code = code; 27 exception.message = message; 28 exception.name = "PrivateScriptException"; 29 throw exception; 30 } 31 32 function installClass(className, implementation) 33 { 34 function PrivateScriptClass() 35 { 36 } 37 38 if (!(className in installedClasses)) 39 installedClasses[className] = new PrivateScriptClass(); 40 implementation(installedClasses[className]); 41 } 42 43 (function (global) { 44 // This list must be in sync with the enum in ExceptionCode.h. The order matters. 45 var domExceptions = [ 46 "IndexSizeError", 47 "HierarchyRequestError", 48 "WrongDocumentError", 49 "InvalidCharacterError", 50 "NoModificationAllowedError", 51 "NotFoundError", 52 "NotSupportedError", 53 "InUseAttributeError", // Historical. Only used in setAttributeNode etc which have been removed from the DOM specs. 54 55 // Introduced in DOM Level 2: 56 "InvalidStateError", 57 "SyntaxError", 58 "InvalidModificationError", 59 "NamespaceError", 60 "InvalidAccessError", 61 62 // Introduced in DOM Level 3: 63 "TypeMismatchError", // Historical; use TypeError instead 64 65 // XMLHttpRequest extension: 66 "SecurityError", 67 68 // Others introduced in HTML5: 69 "NetworkError", 70 "AbortError", 71 "URLMismatchError", 72 "QuotaExceededError", 73 "TimeoutError", 74 "InvalidNodeTypeError", 75 "DataCloneError", 76 77 // These are IDB-specific. 78 "UnknownError", 79 "ConstraintError", 80 "DataError", 81 "TransactionInactiveError", 82 "ReadOnlyError", 83 "VersionError", 84 85 // File system 86 "NotReadableError", 87 "EncodingError", 88 "PathExistsError", 89 90 // SQL 91 "SQLDatabaseError", // Naming conflict with DatabaseError class. 92 93 // Web Crypto 94 "OperationError", 95 ]; 96 97 // This list must be in sync with the enum in ExceptionCode.h. The order matters. 98 var jsErrors = [ 99 "Error", 100 "TypeError", 101 "RangeError", 102 "SyntaxError", 103 "ReferenceError", 104 ]; 105 106 var code = 1; 107 domExceptions.forEach(function (exception) { 108 global.PrivateScriptDOMException[exception] = code; 109 ++code; 110 }); 111 112 var code = 1000; 113 jsErrors.forEach(function (exception) { 114 global.PrivateScriptJSError[exception] = code; 115 ++code; 116 }); 117 118 })(window); 119 120 // This line must be the last statement of this JS file. 121 // A parenthesis is needed, because the caller of this script (PrivateScriptRunner.cpp) 122 // is depending on the completion value of this script. 123 (installedClasses); 124