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 // The extensionview validates the |src| twice, once in |parseSrc| and then
80 // in |loadSrc|. The |src| isn't checked directly in |loadNextSrc| for
81 // validity since the sending renderer (WebUI) is trusted.
82 ExtensionViewInternal
.parseSrc(src
, function(isSrcValid
, extensionId
) {
83 // Check if the src is valid.
85 reject('Failed to load: src is not valid.');
89 // Destroy the current guest and create a new one if extension ID
92 // This may happen if the extensionview is loads an extension page, and
93 // is then intended to load a page served from a different extension in
94 // the same part of the WebUI.
96 // The two calls may look like the following:
97 // extensionview.load('chrome-extension://firstId/page.html');
98 // extensionview.load('chrome-extension://secondId/page.html');
99 // The second time load is called, we destroy the current guest since
100 // we will be loading content from a different extension.
102 this.attributes
[ExtensionViewConstants
.ATTRIBUTE_EXTENSION
]
104 this.guest
.destroy();
106 // Update the extension and src attributes.
107 this.attributes
[ExtensionViewConstants
.ATTRIBUTE_EXTENSION
]
108 .setValueIgnoreMutation(extensionId
);
109 this.attributes
[ExtensionViewConstants
.ATTRIBUTE_SRC
]
110 .setValueIgnoreMutation(src
);
112 this.createGuest(function() {
113 if (this.guest
.getId() <= 0) {
114 reject('Failed to load: guest creation failed.');
116 resolve('Successful load.');
120 ExtensionViewInternal
.loadSrc(this.guest
.getId(), src
,
121 function(hasLoadSucceeded
) {
122 if (!hasLoadSucceeded
) {
123 reject('Failed to load.');
125 // Update the src attribute.
126 this.attributes
[ExtensionViewConstants
.ATTRIBUTE_SRC
]
127 .setValueIgnoreMutation(src
);
128 resolve('Successful load.');
136 GuestViewContainer
.registerElement(ExtensionViewImpl
);
139 exports
.ExtensionViewImpl
= ExtensionViewImpl
;