OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 // This module implements the attributes of the <extensionview> tag. |
| 6 |
| 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; |
| 14 |
| 15 // ----------------------------------------------------------------------------- |
| 16 // Attribute objects. |
| 17 |
| 18 // Default implementation of a ExtensionView attribute. |
| 19 function ExtensionViewAttribute(name, extensionViewImpl) { |
| 20 this.name = name; |
| 21 this.extensionViewImpl = extensionViewImpl; |
| 22 this.ignoreMutation = false; |
| 23 |
| 24 this.defineProperty(); |
| 25 } |
| 26 |
| 27 // Retrieves and returns the attribute's value. |
| 28 ExtensionViewAttribute.prototype.getValue = function() { |
| 29 return this.extensionViewImpl.element.getAttribute(this.name) || ''; |
| 30 }; |
| 31 |
| 32 // Sets the attribute's value. |
| 33 ExtensionViewAttribute.prototype.setValue = function(value) { |
| 34 this.extensionViewImpl.element.setAttribute(this.name, value || ''); |
| 35 }; |
| 36 |
| 37 // Changes the attribute's value without triggering its mutation handler. |
| 38 ExtensionViewAttribute.prototype.setValueIgnoreMutation = function(value) { |
| 39 this.ignoreMutation = true; |
| 40 this.extensionViewImpl.element.setAttribute(this.name, value || ''); |
| 41 this.ignoreMutation = false; |
| 42 } |
| 43 |
| 44 // Defines this attribute as a property on the extensionview node. |
| 45 ExtensionViewAttribute.prototype.defineProperty = function() { |
| 46 Object.defineProperty(this.extensionViewImpl.element, this.name, { |
| 47 get: function() { |
| 48 return this.getValue(); |
| 49 }.bind(this), |
| 50 set: function(value) { |
| 51 this.setValue(value); |
| 52 }.bind(this), |
| 53 enumerable: true |
| 54 }); |
| 55 }; |
| 56 |
| 57 // Called when the attribute's value changes. |
| 58 ExtensionViewAttribute.prototype.handleMutation = |
| 59 function(oldValue, newValue) {}; |
| 60 |
| 61 // Attribute that handles the location and navigation of the extensionview. |
| 62 function SrcAttribute(extensionViewImpl) { |
| 63 ExtensionViewAttribute.call(this, ExtensionViewConstants.ATTRIBUTE_SRC, |
| 64 extensionViewImpl); |
| 65 this.setupMutationObserver(); |
| 66 this.beforeFirstNavigation = true; |
| 67 } |
| 68 |
| 69 SrcAttribute.prototype.__proto__ = ExtensionViewAttribute.prototype; |
| 70 |
| 71 SrcAttribute.prototype.setValueIgnoreMutation = function(value) { |
| 72 this.observer.takeRecords(); |
| 73 this.ignoreMutation = true; |
| 74 this.extensionViewImpl.element.setAttribute(this.name, value || ''); |
| 75 this.ignoreMutation = false; |
| 76 } |
| 77 |
| 78 SrcAttribute.prototype.handleMutation = function(oldValue, newValue) { |
| 79 if (this.attributes[attributeName].ignoreMutation) |
| 80 return; |
| 81 |
| 82 if (!newValue && oldValue) { |
| 83 this.setValueIgnoreMutation(oldValue); |
| 84 return; |
| 85 } |
| 86 this.parse(); |
| 87 }; |
| 88 |
| 89 SrcAttribute.prototype.setupMutationObserver = |
| 90 function() { |
| 91 this.observer = new MutationObserver(function(mutations) { |
| 92 $Array.forEach(mutations, function(mutation) { |
| 93 var oldValue = mutation.oldValue; |
| 94 var newValue = this.getValue(); |
| 95 if (oldValue != newValue) { |
| 96 return; |
| 97 } |
| 98 this.handleMutation(oldValue, newValue); |
| 99 }.bind(this)); |
| 100 }.bind(this)); |
| 101 var params = { |
| 102 attributes: true, |
| 103 attributeOldValue: true, |
| 104 attributeFilter: [this.name] |
| 105 }; |
| 106 this.observer.observe(this.extensionViewImpl.element, params); |
| 107 }; |
| 108 |
| 109 SrcAttribute.prototype.parse = function() { |
| 110 if (!this.extensionViewImpl.elementAttached || !this.getValue()) |
| 111 return; |
| 112 |
| 113 if (!this.extensionViewImpl.guest.getId()) { |
| 114 if (this.beforeFirstNavigation) { |
| 115 this.beforeFirstNavigation = false; |
| 116 this.extensionViewImpl.createGuest(); |
| 117 } |
| 118 return; |
| 119 } |
| 120 |
| 121 ExtensionViewInternal.navigate(this.extensionViewImpl.guest.getId(), |
| 122 this.getValue()); |
| 123 }; |
| 124 |
| 125 SrcAttribute.prototype.reset = function() { |
| 126 this.beforeFirstNavigation = true; |
| 127 }; |
| 128 |
| 129 // ----------------------------------------------------------------------------- |
| 130 |
| 131 // Sets up all of the extensionview attributes. |
| 132 ExtensionViewImpl.prototype.setupExtensionViewAttributes = function() { |
| 133 this.attributes = {}; |
| 134 this.attributes[ExtensionViewConstants.ATTRIBUTE_SRC] = |
| 135 new SrcAttribute(this); |
| 136 }; |
OLD | NEW |