Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3612)

Unified Diff: extensions/renderer/resources/guest_view/guest_view_attributes.js

Issue 1017863007: Refactored the attributes modules of extension_view and web_view into guest_view_attributes.js. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comment. Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: extensions/renderer/resources/guest_view/guest_view_attributes.js
diff --git a/extensions/renderer/resources/guest_view/guest_view_attributes.js b/extensions/renderer/resources/guest_view/guest_view_attributes.js
new file mode 100644
index 0000000000000000000000000000000000000000..a4c42b69b32a67c1437ff94f78d6e93843a70451
--- /dev/null
+++ b/extensions/renderer/resources/guest_view/guest_view_attributes.js
@@ -0,0 +1,131 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// This module implements the base attributes of the GuestView tags.
+
+// -----------------------------------------------------------------------------
+// Attribute object.
+
+// Default implementation of a GuestView attribute.
+function Attribute(name, view) {
+ this.name = name;
+ this.view = view;
+ this.ignoreMutation = false;
+
+ this.defineProperty();
+}
+
+// Retrieves and returns the attribute's value.
+Attribute.prototype.getValue = function() {
+ return this.view.element.getAttribute(this.name) || '';
+};
+
+// Sets the attribute's value.
+Attribute.prototype.setValue = function(value) {
+ this.view.element.setAttribute(this.name, value || '');
+};
+
+// Changes the attribute's value without triggering its mutation handler.
+Attribute.prototype.setValueIgnoreMutation = function(value) {
+ this.ignoreMutation = true;
+ this.setValue(value);
+ this.ignoreMutation = false;
+};
+
+// Defines this attribute as a property on the view's element.
+Attribute.prototype.defineProperty = function() {
+ $Object.defineProperty(this.view.element, this.name, {
+ get: function() {
+ return this.getValue();
+ }.bind(this),
+ set: function(value) {
+ this.setValue(value);
+ }.bind(this),
+ enumerable: true
+ });
+};
+
+// Called when the attribute's value changes.
+Attribute.prototype.maybeHandleMutation = function(oldValue, newValue) {
+ if (this.ignoreMutation)
+ return;
+
+ this.handleMutation(oldValue, newValue);
+};
+
+// Called when a change that isn't ignored occurs to the attribute's value.
+Attribute.prototype.handleMutation = function(oldValue, newValue) {};
+
+// Called when the view's element is attached to the DOM tree.
+Attribute.prototype.attach = function() {};
+
+// Called when the view's element is detached from the DOM tree.
+Attribute.prototype.detach = function() {};
+
+// -----------------------------------------------------------------------------
+// BooleanAttribute object.
+
+// An attribute that is treated as a Boolean.
+function BooleanAttribute(name, view) {
+ Attribute.call(this, name, view);
+}
+
+BooleanAttribute.prototype.__proto__ = Attribute.prototype;
+
+BooleanAttribute.prototype.getValue = function() {
+ return this.view.element.hasAttribute(this.name);
+};
+
+BooleanAttribute.prototype.setValue = function(value) {
+ if (!value) {
+ this.view.element.removeAttribute(this.name);
+ } else {
+ this.view.element.setAttribute(this.name, '');
+ }
+};
+
+// -----------------------------------------------------------------------------
+// IntegerAttribute object.
+
+// An attribute that is treated as an integer.
+function IntegerAttribute(name, view) {
+ Attribute.call(this, name, view);
+}
+
+IntegerAttribute.prototype.__proto__ = Attribute.prototype;
+
+IntegerAttribute.prototype.getValue = function() {
+ return parseInt(this.view.element.getAttribute(this.name)) || 0;
+};
+
+IntegerAttribute.prototype.setValue = function(value) {
+ this.view.element.setAttribute(this.name, parseInt(value) || 0);
+};
+
+// -----------------------------------------------------------------------------
+// ReadOnlyAttribute object.
+
+// An attribute that cannot be changed (externally). The only way to set it
+// internally is via |setValueIgnoreMutation|.
+function ReadOnlyAttribute(name, view) {
+ Attribute.call(this, name, view);
+}
+
+ReadOnlyAttribute.prototype.__proto__ = Attribute.prototype;
+
+ReadOnlyAttribute.prototype.handleMutation = function(oldValue, newValue) {
+ this.setValueIgnoreMutation(oldValue);
+}
+
+// -----------------------------------------------------------------------------
+
+var GuestViewAttributes = {
+ Attribute: Attribute,
+ BooleanAttribute: BooleanAttribute,
+ IntegerAttribute: IntegerAttribute,
+ ReadOnlyAttribute: ReadOnlyAttribute
+};
+
+// Exports.
+exports.GuestViewAttributes = GuestViewAttributes;

Powered by Google App Engine
This is Rietveld 408576698