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.
5 var DocumentNatives = requireNative('document_natives');
6 var GuestViewInternal =
7 require('binding').Binding.create('guestViewInternal').generate();
8 var IdGenerator = requireNative('id_generator');
9 var guestViewInternalNatives = requireNative('guest_view_internal');
11 function AppViewInternal(appviewNode) {
12 privates(appviewNode).internal = this;
13 this.appviewNode = appviewNode;
15 this.browserPluginNode = this.createBrowserPluginNode();
16 var shadowRoot = this.appviewNode.createShadowRoot();
17 shadowRoot.appendChild(this.browserPluginNode);
18 this.viewInstanceId = IdGenerator.GetNextId();
21 AppViewInternal.prototype.getErrorNode = function() {
22 if (!this.errorNode) {
23 this.errorNode = document.createElement('div');
24 this.errorNode.innerText = 'Unable to connect to app.';
25 this.errorNode.style.position = 'absolute';
26 this.errorNode.style.left = '0px';
27 this.errorNode.style.top = '0px';
28 this.errorNode.style.width = '100%';
29 this.errorNode.style.height = '100%';
30 this.appviewNode.shadowRoot.appendChild(this.errorNode);
32 return this.errorNode;
35 AppViewInternal.prototype.createBrowserPluginNode = function() {
36 // We create BrowserPlugin as a custom element in order to observe changes
37 // to attributes synchronously.
38 var browserPluginNode = new AppViewInternal.BrowserPlugin();
39 privates(browserPluginNode).internal = this;
40 return browserPluginNode;
43 AppViewInternal.prototype.connect = function(app, data, callback) {
49 GuestViewInternal.createGuest(
52 function(guestInstanceId) {
53 if (!guestInstanceId) {
54 this.browserPluginNode.style.visibility = 'hidden';
55 var errorMsg = 'Unable to connect to app "' + app + '".';
56 window.console.warn(errorMsg);
57 this.getErrorNode().innerText = errorMsg;
63 this.attachWindow(guestInstanceId);
71 AppViewInternal.prototype.attachWindow = function(guestInstanceId) {
72 this.guestInstanceId = guestInstanceId;
74 'instanceId': this.viewInstanceId,
76 this.browserPluginNode.style.visibility = 'visible';
77 return guestViewInternalNatives.AttachGuest(
78 parseInt(this.browserPluginNode.getAttribute('internalinstanceid')),
83 function registerBrowserPluginElement() {
84 var proto = Object.create(HTMLObjectElement.prototype);
86 proto.createdCallback = function() {
87 this.setAttribute('type', 'application/browser-plugin');
88 this.style.width = '100%';
89 this.style.height = '100%';
92 proto.attachedCallback = function() {
93 // Load the plugin immediately.
94 var unused = this.nonExistentAttribute;
97 AppViewInternal.BrowserPlugin =
98 DocumentNatives.RegisterElement('appplugin', {extends: 'object',
101 delete proto.createdCallback;
102 delete proto.attachedCallback;
103 delete proto.detachedCallback;
104 delete proto.attributeChangedCallback;
107 function registerAppViewElement() {
108 var proto = Object.create(HTMLElement.prototype);
110 proto.createdCallback = function() {
111 new AppViewInternal(this);
114 proto.connect = function() {
115 var internal = privates(this).internal;
116 $Function.apply(internal.connect, internal, arguments);
119 DocumentNatives.RegisterElement('appview', {prototype: proto});
121 // Delete the callbacks so developers cannot call them and produce unexpected
123 delete proto.createdCallback;
124 delete proto.attachedCallback;
125 delete proto.detachedCallback;
126 delete proto.attributeChangedCallback;
129 var useCapture = true;
130 window.addEventListener('readystatechange', function listener(event) {
131 if (document.readyState == 'loading')
134 registerBrowserPluginElement();
135 registerAppViewElement();
136 window.removeEventListener(event.type, listener, useCapture);