1 // Copyright 2015 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.
5 // This module implements the attributes of the <extensionview> tag.
7 var GuestViewInternal =
8 require('binding').Binding.create('guestViewInternal').generate();
9 var ExtensionViewImpl = require('extensionView').ExtensionViewImpl;
10 var ExtensionViewConstants =
11 require('extensionViewConstants').ExtensionViewConstants;
12 var ExtensionViewInternal =
13 require('extensionViewInternal').ExtensionViewInternal;
15 // -----------------------------------------------------------------------------
18 // Default implementation of a ExtensionView attribute.
19 function ExtensionViewAttribute(name, extensionViewImpl) {
21 this.extensionViewImpl = extensionViewImpl;
22 this.ignoreMutation = false;
24 this.defineProperty();
27 // Retrieves and returns the attribute's value.
28 ExtensionViewAttribute.prototype.getValue = function() {
29 return this.extensionViewImpl.element.getAttribute(this.name) || '';
32 // Sets the attribute's value.
33 ExtensionViewAttribute.prototype.setValue = function(value) {
34 this.extensionViewImpl.element.setAttribute(this.name, value || '');
37 // Changes the attribute's value without triggering its mutation handler.
38 ExtensionViewAttribute.prototype.setValueIgnoreMutation = function(value) {
39 this.ignoreMutation = true;
41 this.ignoreMutation = false;
44 // Defines this attribute as a property on the extensionview node.
45 ExtensionViewAttribute.prototype.defineProperty = function() {
46 Object.defineProperty(this.extensionViewImpl.element, this.name, {
48 return this.getValue();
50 set: function(value) {
57 // Called when the attribute's value changes.
58 ExtensionViewAttribute.prototype.maybeHandleMutation =
59 function(oldValue, newValue) {
60 if (this.ignoreMutation)
63 this.handleMutation(oldValue, newValue);
66 // Called when a change that isn't ignored occurs to the attribute's value.
67 ExtensionViewAttribute.prototype.handleMutation =
68 function(oldValue, newValue) {};
70 ExtensionViewAttribute.prototype.reset = function() {
71 this.setValueIgnoreMutation();
74 // Attribute that handles extension binded to the extensionview.
75 function ExtensionAttribute(extensionViewImpl) {
76 ExtensionViewAttribute.call(this, ExtensionViewConstants.ATTRIBUTE_EXTENSION,
80 ExtensionAttribute.prototype.__proto__ = ExtensionViewAttribute.prototype;
82 ExtensionAttribute.prototype.handleMutation = function(oldValue, newValue) {
83 this.setValueIgnoreMutation(oldValue);
86 // Attribute that handles the location and navigation of the extensionview.
87 function SrcAttribute(extensionViewImpl) {
88 ExtensionViewAttribute.call(this, ExtensionViewConstants.ATTRIBUTE_SRC,
92 SrcAttribute.prototype.__proto__ = ExtensionViewAttribute.prototype;
94 SrcAttribute.prototype.handleMutation = function(oldValue, newValue) {
95 if (!newValue && oldValue) {
96 this.setValueIgnoreMutation(oldValue);
102 SrcAttribute.prototype.parse = function() {
103 if (!this.extensionViewImpl.elementAttached || !this.getValue())
106 if (!this.extensionViewImpl.guest.getId())
109 ExtensionViewInternal.navigate(this.extensionViewImpl.guest.getId(),
113 // -----------------------------------------------------------------------------
115 // Sets up all of the extensionview attributes.
116 ExtensionViewImpl.prototype.setupExtensionViewAttributes = function() {
117 this.attributes = {};
118 this.attributes[ExtensionViewConstants.ATTRIBUTE_EXTENSION] =
119 new ExtensionAttribute(this);
120 this.attributes[ExtensionViewConstants.ATTRIBUTE_SRC] =
121 new SrcAttribute(this);