1 // Copyright (c) 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.
5 // This module implements the attributes of the <webview> tag.
7 var WebView = require('webView').WebView;
8 var WebViewConstants = require('webViewConstants').WebViewConstants;
10 // -----------------------------------------------------------------------------
13 // Default implementation of a WebView attribute.
14 function WebViewAttribute(name, webViewImpl) {
16 this.webViewImpl = webViewImpl;
17 this.ignoreNextMutation = false;
20 // Retrieves and returns the attribute's value.
21 WebViewAttribute.prototype.getValue = function() {
22 return this.webViewImpl.webviewNode.getAttribute(this.name) || '';
25 // Sets the attribute's value.
26 WebViewAttribute.prototype.setValue = function(value) {
27 this.webViewImpl.webviewNode.setAttribute(this.name, value || '');
30 // Defines this attribute as a property on the webview node.
31 WebViewAttribute.prototype.define = function() {
32 Object.defineProperty(this.webViewImpl.webviewNode, this.name, {
34 return this.getValue();
36 set: function(value) {
43 // Called when the attribute's value changes.
44 WebViewAttribute.prototype.handleMutation = function(oldValue, newValue) {};
46 // An attribute that is treated as a Boolean.
47 function BooleanAttribute(name, webViewImpl) {
48 WebViewAttribute.call(this, name, webViewImpl);
51 BooleanAttribute.prototype = new WebViewAttribute();
53 BooleanAttribute.prototype.getValue = function() {
54 return this.webViewImpl.webviewNode.hasAttribute(this.name);
57 BooleanAttribute.prototype.setValue = function(value) {
59 this.webViewImpl.webviewNode.removeAttribute(this.name);
61 this.webViewImpl.webviewNode.setAttribute(this.name, '');
65 // Attribute representing the state of the storage partition.
66 function Partition(webViewImpl) {
67 WebViewAttribute.call(this,
68 WebViewConstants.ATTRIBUTE_PARTITION,
70 this.validPartitionId = true;
73 Partition.prototype = new WebViewAttribute();
75 Partition.prototype.handleMutation = function(oldValue, newValue) {
76 newValue = newValue || '';
78 // The partition cannot change if the webview has already navigated.
79 if (!this.webViewImpl.beforeFirstNavigation) {
80 window.console.error(WebViewConstants.ERROR_MSG_ALREADY_NAVIGATED);
81 this.ignoreNextMutation = true;
82 this.webViewImpl.webviewNode.setAttribute(this.name, oldValue);
85 if (newValue == 'persist:') {
86 this.validPartitionId = false;
88 WebViewConstants.ERROR_MSG_INVALID_PARTITION_ATTRIBUTE);
92 // -----------------------------------------------------------------------------
94 // Sets up all of the webview attributes.
95 WebView.prototype.setupWebViewAttributes = function() {
98 // Initialize the attributes with special behavior (and custom attribute
100 this.attributes[WebViewConstants.ATTRIBUTE_ALLOWTRANSPARENCY] =
101 new BooleanAttribute(WebViewConstants.ATTRIBUTE_ALLOWTRANSPARENCY, this);
102 this.attributes[WebViewConstants.ATTRIBUTE_AUTOSIZE] =
103 new BooleanAttribute(WebViewConstants.ATTRIBUTE_AUTOSIZE, this);
104 this.attributes[WebViewConstants.ATTRIBUTE_PARTITION] = new Partition(this);
106 // Initialize the remaining attributes, which have default behavior.
107 var defaultAttributes = [WebViewConstants.ATTRIBUTE_MAXHEIGHT,
108 WebViewConstants.ATTRIBUTE_MAXWIDTH,
109 WebViewConstants.ATTRIBUTE_MINHEIGHT,
110 WebViewConstants.ATTRIBUTE_MINWIDTH,
111 WebViewConstants.ATTRIBUTE_NAME,
112 WebViewConstants.ATTRIBUTE_SRC];
113 for (var i = 0; defaultAttributes[i]; ++i) {
114 this.attributes[defaultAttributes[i]] =
115 new WebViewAttribute(defaultAttributes[i], this);