Chromium Code Reviews| 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 } | |
| 67 | |
| 68 SrcAttribute.prototype.__proto__ = ExtensionViewAttribute.prototype; | |
| 69 | |
| 70 SrcAttribute.prototype.setValueIgnoreMutation = function(value) { | |
| 71 this.observer.takeRecords(); | |
| 72 this.ignoreMutation = true; | |
| 73 this.extensionViewImpl.element.setAttribute(this.name, value || ''); | |
| 74 this.ignoreMutation = false; | |
| 75 } | |
| 76 | |
| 77 SrcAttribute.prototype.handleMutation = function(oldValue, newValue) { | |
| 78 if (!newValue && oldValue) { | |
| 79 this.setValueIgnoreMutation(oldValue); | |
| 80 return; | |
| 81 } | |
| 82 this.parse(); | |
| 83 }; | |
| 84 | |
| 85 SrcAttribute.prototype.setupMutationObserver = | |
| 86 function() { | |
| 87 this.observer = new MutationObserver(function(mutations) { | |
| 88 $Array.forEach(mutations, function(mutation) { | |
| 89 var oldValue = mutation.oldValue; | |
| 90 var newValue = this.getValue(); | |
| 91 if (oldValue != newValue) { | |
| 92 return; | |
| 93 } | |
| 94 this.handleMutation(oldValue, newValue); | |
| 95 }.bind(this)); | |
| 96 }.bind(this)); | |
| 97 var params = { | |
| 98 attributes: true, | |
| 99 attributeOldValue: true, | |
| 100 attributeFilter: [this.name] | |
| 101 }; | |
| 102 this.observer.observe(this.extensionViewImpl.element, params); | |
| 103 }; | |
| 104 | |
| 105 SrcAttribute.prototype.parse = function() { | |
| 106 if (!this.extensionViewImpl.elementAttached || !this.getValue() || | |
|
Fady Samuel
2015/01/26 12:52:02
I'd create the guest here on first call to parse,
apacible
2015/01/26 22:12:33
Done.
| |
| 107 !this.extensionViewImpl.guest.getId()) { | |
| 108 return; | |
| 109 } | |
| 110 | |
| 111 ExtensionViewInternal.navigate(this.extensionViewImpl.guest.getId(), | |
| 112 this.getValue()); | |
| 113 }; | |
| 114 | |
| 115 // ----------------------------------------------------------------------------- | |
| 116 | |
| 117 // Sets up all of the extensionview attributes. | |
| 118 ExtensionViewImpl.prototype.setupExtensionViewAttributes = function() { | |
| 119 this.attributes = {}; | |
| 120 this.attributes[ExtensionViewConstants.ATTRIBUTE_SRC] = | |
| 121 new SrcAttribute(this); | |
| 122 }; | |
| OLD | NEW |