OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 "use strict"; | 5 "use strict"; |
6 | 6 |
7 var installedClasses = {}; | 7 function PrivateScriptController() |
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 { | 8 { |
21 function PrivateScriptException() | 9 this._installedClasses = {}; |
22 { | 10 this._DOMException = {}; |
23 } | 11 this._JSError = {}; |
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 mat
ters. | 12 // This list must be in sync with the enum in ExceptionCode.h. The order mat
ters. |
45 var domExceptions = [ | 13 var domExceptions = [ |
46 "IndexSizeError", | 14 "IndexSizeError", |
47 "HierarchyRequestError", | 15 "HierarchyRequestError", |
48 "WrongDocumentError", | 16 "WrongDocumentError", |
49 "InvalidCharacterError", | 17 "InvalidCharacterError", |
50 "NoModificationAllowedError", | 18 "NoModificationAllowedError", |
51 "NotFoundError", | 19 "NotFoundError", |
52 "NotSupportedError", | 20 "NotSupportedError", |
53 "InUseAttributeError", // Historical. Only used in setAttributeNode etc
which have been removed from the DOM specs. | 21 "InUseAttributeError", // Historical. Only used in setAttributeNode etc
which have been removed from the DOM specs. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 var jsErrors = [ | 66 var jsErrors = [ |
99 "Error", | 67 "Error", |
100 "TypeError", | 68 "TypeError", |
101 "RangeError", | 69 "RangeError", |
102 "SyntaxError", | 70 "SyntaxError", |
103 "ReferenceError", | 71 "ReferenceError", |
104 ]; | 72 ]; |
105 | 73 |
106 var code = 1; | 74 var code = 1; |
107 domExceptions.forEach(function (exception) { | 75 domExceptions.forEach(function (exception) { |
108 global.PrivateScriptDOMException[exception] = code; | 76 this._DOMException[exception] = code; |
109 ++code; | 77 ++code; |
110 }); | 78 }.bind(this)); |
111 | 79 |
112 var code = 1000; | 80 var code = 1000; |
113 jsErrors.forEach(function (exception) { | 81 jsErrors.forEach(function (exception) { |
114 global.PrivateScriptJSError[exception] = code; | 82 this._JSError[exception] = code; |
115 ++code; | 83 ++code; |
116 }); | 84 }.bind(this)); |
| 85 } |
117 | 86 |
118 })(window); | 87 PrivateScriptController.prototype = { |
| 88 get installedClasses() |
| 89 { |
| 90 return this._installedClasses; |
| 91 }, |
| 92 |
| 93 get DOMException() |
| 94 { |
| 95 return this._DOMException; |
| 96 }, |
| 97 |
| 98 get JSError() |
| 99 { |
| 100 return this._JSError; |
| 101 }, |
| 102 |
| 103 installClass: function(className, implementation) |
| 104 { |
| 105 function PrivateScriptClass() |
| 106 { |
| 107 } |
| 108 |
| 109 if (!(className in this._installedClasses)) |
| 110 this._installedClasses[className] = new PrivateScriptClass(); |
| 111 implementation(this._installedClasses[className]); |
| 112 }, |
| 113 |
| 114 // Private scripts can throw JS errors and DOM exceptions as follows: |
| 115 // throwException(privateScriptController.DOMException.IndexSizeError, "
..."); |
| 116 // throwException(privateScriptController.JSError.TypeError, "..."); |
| 117 // |
| 118 // Note that normal JS errors thrown by private scripts are treated |
| 119 // as real JS errors caused by programming mistake and the execution crashes
. |
| 120 // If you want to intentially throw JS errors from private scripts, |
| 121 // you need to use throwException(privateScriptController.JSError.TypeError,
"..."). |
| 122 throwException: function(code, message) |
| 123 { |
| 124 function PrivateScriptException() |
| 125 { |
| 126 } |
| 127 |
| 128 var exception = new PrivateScriptException(); |
| 129 exception.code = code; |
| 130 exception.message = message; |
| 131 exception.name = "PrivateScriptException"; |
| 132 throw exception; |
| 133 }, |
| 134 } |
| 135 |
| 136 if (typeof window.privateScriptController === 'undefined') |
| 137 window.privateScriptController = new PrivateScriptController(); |
119 | 138 |
120 // This line must be the last statement of this JS file. | 139 // This line must be the last statement of this JS file. |
121 // A parenthesis is needed, because the caller of this script (PrivateScriptRunn
er.cpp) | 140 // A parenthesis is needed, because the caller of this script (PrivateScriptRunn
er.cpp) |
122 // is depending on the completion value of this script. | 141 // is depending on the completion value of this script. |
123 (installedClasses); | 142 (privateScriptController.installedClasses); |
OLD | NEW |