Commit 28483c47 authored by haraken@chromium.org's avatar haraken@chromium.org

Refactor PrivateScriptRunner.js to reduce the number of globally exposed variables

It's not a good idea to use global variables in PrivateScriptRunner.js
because they are exposed to all window objects of the private scripts.
This CL reduces the number of global variables used in PrivateScriptRunner.js.

Also this CL removes a |global| parameter from installClass()
since the |global| is always equal to the window object of the JS file.

BUG=341031

Review URL: https://codereview.chromium.org/466623002

git-svn-id: svn://svn.chromium.org/blink/trunk@180053 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 8fc482ed
......@@ -4,70 +4,8 @@
"use strict";
// This list must be in sync with the enum in ExceptionCode.h. The order matters.
var domExceptions = [
"IndexSizeError",
"HierarchyRequestError",
"WrongDocumentError",
"InvalidCharacterError",
"NoModificationAllowedError",
"NotFoundError",
"NotSupportedError",
"InUseAttributeError", // Historical. Only used in setAttributeNode etc which have been removed from the DOM specs.
// Introduced in DOM Level 2:
"InvalidStateError",
"SyntaxError",
"InvalidModificationError",
"NamespaceError",
"InvalidAccessError",
// Introduced in DOM Level 3:
"TypeMismatchError", // Historical; use TypeError instead
// XMLHttpRequest extension:
"SecurityError",
// Others introduced in HTML5:
"NetworkError",
"AbortError",
"URLMismatchError",
"QuotaExceededError",
"TimeoutError",
"InvalidNodeTypeError",
"DataCloneError",
// These are IDB-specific.
"UnknownError",
"ConstraintError",
"DataError",
"TransactionInactiveError",
"ReadOnlyError",
"VersionError",
// File system
"NotReadableError",
"EncodingError",
"PathExistsError",
// SQL
"SQLDatabaseError", // Naming conflict with DatabaseError class.
// Web Crypto
"OperationError",
];
var domExceptionCode = {};
var installedClasses = {};
function init()
{
var code = 1;
domExceptions.forEach(function (exception) {
domExceptionCode[exception] = code;
code++;
});
}
var domExceptionCode = {};
function DOMExceptionInPrivateScript(code, message)
{
......@@ -76,18 +14,77 @@ function DOMExceptionInPrivateScript(code, message)
this.name = "DOMExceptionInPrivateScript";
}
function privateScriptClass()
{
}
function installClass(className, implementation)
{
function privateScriptClass()
{
}
if (!(className in installedClasses))
installedClasses[className] = new privateScriptClass();
implementation(window, installedClasses[className]);
implementation(installedClasses[className]);
}
init();
(function (global) {
// This list must be in sync with the enum in ExceptionCode.h. The order matters.
var domExceptions = [
"IndexSizeError",
"HierarchyRequestError",
"WrongDocumentError",
"InvalidCharacterError",
"NoModificationAllowedError",
"NotFoundError",
"NotSupportedError",
"InUseAttributeError", // Historical. Only used in setAttributeNode etc which have been removed from the DOM specs.
// Introduced in DOM Level 2:
"InvalidStateError",
"SyntaxError",
"InvalidModificationError",
"NamespaceError",
"InvalidAccessError",
// Introduced in DOM Level 3:
"TypeMismatchError", // Historical; use TypeError instead
// XMLHttpRequest extension:
"SecurityError",
// Others introduced in HTML5:
"NetworkError",
"AbortError",
"URLMismatchError",
"QuotaExceededError",
"TimeoutError",
"InvalidNodeTypeError",
"DataCloneError",
// These are IDB-specific.
"UnknownError",
"ConstraintError",
"DataError",
"TransactionInactiveError",
"ReadOnlyError",
"VersionError",
// File system
"NotReadableError",
"EncodingError",
"PathExistsError",
// SQL
"SQLDatabaseError", // Naming conflict with DatabaseError class.
// Web Crypto
"OperationError",
];
var code = 1;
domExceptions.forEach(function (exception) {
global.domExceptionCode[exception] = code;
code++;
});
})(window);
// This line must be the last statement of this JS file.
// A parenthesis is needed, because the caller of this script (PrivateScriptRunner.cpp)
......
......@@ -4,7 +4,7 @@
'use strict';
installClass('HTMLMarqueeElement', function(global, HTMLMarqueeElementPrototype) {
installClass('HTMLMarqueeElement', function(HTMLMarqueeElementPrototype) {
var kDefaultScrollAmount = 6;
var kDefaultScrollDelayMS = 85;
......@@ -122,15 +122,15 @@ installClass('HTMLMarqueeElement', function(global, HTMLMarqueeElementPrototype)
HTMLMarqueeElementPrototype.createdCallback = function() {
var shadow = this.createShadowRoot();
var style = global.document.createElement('style');
var style = document.createElement('style');
style.textContent = ':host { display: inline-block; width: -webkit-fill-available; overflow: hidden; text-align: initial; }' +
':host([direction="up"]), :host([direction="down"]) { height: 200px; }';
shadow.appendChild(style);
var mover = global.document.createElement('div');
var mover = document.createElement('div');
shadow.appendChild(mover);
mover.appendChild(global.document.createElement('content'));
mover.appendChild(document.createElement('content'));
this.loopCount_ = 0;
this.mover_ = mover;
......@@ -234,8 +234,8 @@ installClass('HTMLMarqueeElement', function(global, HTMLMarqueeElementPrototype)
HTMLMarqueeElementPrototype.getGetMetrics_ = function() {
this.mover_.style.width = '-webkit-max-content';
var moverStyle = global.getComputedStyle(this.mover_);
var marqueeStyle = global.getComputedStyle(this);
var moverStyle = getComputedStyle(this.mover_);
var marqueeStyle = getComputedStyle(this);
var metrics = {};
metrics.contentWidth = parseInt(moverStyle.width);
......@@ -391,7 +391,7 @@ installClass('HTMLMarqueeElement', function(global, HTMLMarqueeElementPrototype)
HTMLMarqueeElementPrototype.start = function() {
if (this.continueCallback_ || this.player_)
return;
this.continueCallback_ = global.requestAnimationFrame(function() {
this.continueCallback_ = requestAnimationFrame(function() {
this.continueCallback_ = null;
this.continue_();
}.bind(this));
......@@ -403,7 +403,7 @@ installClass('HTMLMarqueeElement', function(global, HTMLMarqueeElementPrototype)
return;
if (this.continueCallback_) {
global.cancelAnimationFrame(this.continueCallback_);
cancelAnimationFrame(this.continueCallback_);
this.continueCallback_ = null;
return;
}
......@@ -420,7 +420,7 @@ installClass('HTMLMarqueeElement', function(global, HTMLMarqueeElementPrototype)
// FIXME: We have to inject this HTMLMarqueeElement as a custom element in order to make
// createdCallback, attachedCallback, detachedCallback and attributeChangedCallback workable.
// global.document.registerElement('i-marquee', {
// document.registerElement('i-marquee', {
// prototype: HTMLMarqueeElementPrototype,
// });
});
......@@ -4,7 +4,7 @@
"use strict";
installClass("PrivateScriptTest", function(global, PrivateScriptTestPrototype) {
installClass("PrivateScriptTest", function(PrivateScriptTestPrototype) {
PrivateScriptTestPrototype.addIntegerInPartial = function(value1, value2) {
return value1 + value2;
......
......@@ -4,7 +4,7 @@
"use strict";
installClass("PrivateScriptTest", function(global, PrivateScriptTestPrototype) {
installClass("PrivateScriptTest", function(PrivateScriptTestPrototype) {
PrivateScriptTestPrototype.initialize = function() {
this.m_shortAttribute = -1;
......@@ -91,7 +91,7 @@ installClass("PrivateScriptTest", function(global, PrivateScriptTestPrototype) {
}
PrivateScriptTestPrototype.clickNode = function(document, node) {
var event = new MouseEvent("click", { bubbles: true, cancelable: true, view: global });
var event = new MouseEvent("click", { bubbles: true, cancelable: true, view: window });
node.dispatchEvent(event);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment