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.
5 // This module implements the ExtensionView <extensionview>.
7 var GuestViewContainer = require('guestViewContainer').GuestViewContainer;
8 var ExtensionViewConstants =
9 require('extensionViewConstants').ExtensionViewConstants;
10 var ExtensionViewEvents = require('extensionViewEvents').ExtensionViewEvents;
11 var ExtensionViewInternal =
12 require('extensionViewInternal').ExtensionViewInternal;
14 function ExtensionViewImpl(extensionviewElement) {
15 GuestViewContainer.call(this, extensionviewElement, 'extensionview');
17 // A queue of objects in the order they should be loaded.
18 // Every load call will add the given src, as well as the resolve and reject
19 // functions. Each src will be loaded in the order they were called.
22 // The current src that is loading.
23 // @type {Object<!string, function, function>}
24 this.pendingLoad = null;
26 new ExtensionViewEvents(this, this.viewInstanceId);
29 ExtensionViewImpl.prototype.__proto__ = GuestViewContainer.prototype;
31 ExtensionViewImpl.VIEW_TYPE = 'ExtensionView';
33 ExtensionViewImpl.setupElement = function(proto) {
34 var apiMethods = ExtensionViewImpl.getApiMethods();
36 GuestViewContainer.forwardApiMethods(proto, apiMethods);
39 ExtensionViewImpl.prototype.createGuest = function(callback) {
40 this.guest.create(this.buildParams(), function() {
46 ExtensionViewImpl.prototype.buildContainerParams = function() {
48 for (var i in this.attributes) {
49 params[i] = this.attributes[i].getValue();
54 ExtensionViewImpl.prototype.onElementDetached = function() {
57 // Reset all attributes.
58 for (var i in this.attributes) {
59 this.attributes[i].setValueIgnoreMutation();
63 // Updates src upon loadcommit.
64 ExtensionViewImpl.prototype.onLoadCommit = function(url) {
65 this.attributes[ExtensionViewConstants.ATTRIBUTE_SRC].
66 setValueIgnoreMutation(url);
69 // Loads the next pending src from |loadQueue| to the extensionview.
70 ExtensionViewImpl.prototype.loadNextSrc = function() {
71 // If extensionview isn't currently loading a src, load the next src
72 // in |loadQueue|. Otherwise, do nothing.
73 if (!this.pendingLoad && this.loadQueue.length) {
74 this.pendingLoad = this.loadQueue.shift();
75 var src = this.pendingLoad.src;
76 var resolve = this.pendingLoad.resolve;
77 var reject = this.pendingLoad.reject;
79 ExtensionViewInternal.parseSrc(src, function(isSrcValid, extensionId) {
80 // Check if the src is valid.
82 reject('Failed to load: src is not valid.');
86 // Destroy the current guest and create a new one if extension ID
89 // This may happen if the extensionview is loads an extension page, and
90 // is then intended to load a page served from a different extension in
91 // the same part of the WebUI.
93 // The two calls may look like the following:
94 // extensionview.load('chrome-extension://firstId/page.html');
95 // extensionview.load('chrome-extension://secondId/page.html');
96 // The second time load is called, we destroy the current guest since
97 // we will be loading content from a different extension.
99 this.attributes[ExtensionViewConstants.ATTRIBUTE_EXTENSION]
101 this.guest.destroy();
103 // Update the extension and src attributes.
104 this.attributes[ExtensionViewConstants.ATTRIBUTE_EXTENSION]
105 .setValueIgnoreMutation(extensionId);
106 this.attributes[ExtensionViewConstants.ATTRIBUTE_SRC]
107 .setValueIgnoreMutation(src);
109 this.createGuest(function() {
110 if (this.guest.getId() <= 0) {
111 reject('Failed to load: guest creation failed.');
113 resolve('Successful load.');
117 ExtensionViewInternal.loadSrc(this.guest.getId(), src,
118 function(hasLoadSucceeded) {
119 if (!hasLoadSucceeded) {
120 reject('Failed to load.');
122 // Update the src attribute.
123 this.attributes[ExtensionViewConstants.ATTRIBUTE_SRC]
124 .setValueIgnoreMutation(src);
125 resolve('Successful load.');
133 GuestViewContainer.registerElement(ExtensionViewImpl);
136 exports.ExtensionViewImpl = ExtensionViewImpl;