OLD | NEW |
(Empty) | |
| 1 // Copyright 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. |
| 4 |
| 5 var DocumentNatives = requireNative('document_natives'); |
| 6 var GuestViewInternal = |
| 7 require('binding').Binding.create('guestViewInternal').generate(); |
| 8 var IdGenerator = requireNative('id_generator'); |
| 9 |
| 10 function AppViewInternal(appviewNode) { |
| 11 privates(appviewNode).internal = this; |
| 12 this.appviewNode = appviewNode; |
| 13 |
| 14 this.browserPluginNode = this.createBrowserPluginNode(); |
| 15 var shadowRoot = this.appviewNode.createShadowRoot(); |
| 16 shadowRoot.appendChild(this.browserPluginNode); |
| 17 this.viewInstanceId = IdGenerator.GetNextId(); |
| 18 } |
| 19 |
| 20 AppViewInternal.prototype.createBrowserPluginNode = function() { |
| 21 // We create BrowserPlugin as a custom element in order to observe changes |
| 22 // to attributes synchronously. |
| 23 var browserPluginNode = new AppViewInternal.BrowserPlugin(); |
| 24 privates(browserPluginNode).internal = this; |
| 25 return browserPluginNode; |
| 26 }; |
| 27 |
| 28 AppViewInternal.prototype.connect = function(src, callback) { |
| 29 var params = { |
| 30 }; |
| 31 var self = this; |
| 32 GuestViewInternal.createGuest( |
| 33 'appview', |
| 34 params, |
| 35 function(instanceId) { |
| 36 self.attachWindow(instanceId, src); |
| 37 if (callback) { |
| 38 callback(); |
| 39 } |
| 40 } |
| 41 ); |
| 42 }; |
| 43 |
| 44 AppViewInternal.prototype.attachWindow = function(instanceId, src) { |
| 45 this.instanceId = instanceId; |
| 46 var params = { |
| 47 'instanceId': this.viewInstanceId, |
| 48 'src': src |
| 49 }; |
| 50 return this.browserPluginNode['-internal-attach'](instanceId, params); |
| 51 }; |
| 52 |
| 53 function registerBrowserPluginElement() { |
| 54 var proto = Object.create(HTMLObjectElement.prototype); |
| 55 |
| 56 proto.createdCallback = function() { |
| 57 this.setAttribute('type', 'application/browser-plugin'); |
| 58 this.style.width = '100%'; |
| 59 this.style.height = '100%'; |
| 60 }; |
| 61 |
| 62 proto.attachedCallback = function() { |
| 63 // Load the plugin immediately. |
| 64 var unused = this.nonExistentAttribute; |
| 65 }; |
| 66 |
| 67 AppViewInternal.BrowserPlugin = |
| 68 DocumentNatives.RegisterElement('appplugin', {extends: 'object', |
| 69 prototype: proto}); |
| 70 |
| 71 delete proto.createdCallback; |
| 72 delete proto.attachedCallback; |
| 73 delete proto.detachedCallback; |
| 74 delete proto.attributeChangedCallback; |
| 75 } |
| 76 |
| 77 function registerAppViewElement() { |
| 78 var proto = Object.create(HTMLElement.prototype); |
| 79 |
| 80 proto.createdCallback = function() { |
| 81 new AppViewInternal(this); |
| 82 }; |
| 83 |
| 84 proto.connect = function() { |
| 85 var internal = privates(this).internal; |
| 86 $Function.apply(internal.connect, internal, arguments); |
| 87 } |
| 88 window.AppView = |
| 89 DocumentNatives.RegisterElement('appview', {prototype: proto}); |
| 90 |
| 91 // Delete the callbacks so developers cannot call them and produce unexpected |
| 92 // behavior. |
| 93 delete proto.createdCallback; |
| 94 delete proto.attachedCallback; |
| 95 delete proto.detachedCallback; |
| 96 delete proto.attributeChangedCallback; |
| 97 } |
| 98 |
| 99 var useCapture = true; |
| 100 window.addEventListener('readystatechange', function listener(event) { |
| 101 if (document.readyState == 'loading') |
| 102 return; |
| 103 |
| 104 registerBrowserPluginElement(); |
| 105 registerAppViewElement(); |
| 106 window.removeEventListener(event.type, listener, useCapture); |
| 107 }, useCapture); |
OLD | NEW |