Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / extensions / renderer / resources / guest_view / extension_view / extension_view.js
blob7858e810e27fbaf8bff730fb1a459ebf4a02f724
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.
20 this.loadQueue = [];
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() {
41 this.attachWindow$();
42 callback();
43 }.bind(this));
46 ExtensionViewImpl.prototype.buildContainerParams = function() {
47 var params = {};
48 for (var i in this.attributes) {
49 params[i] = this.attributes[i].getValue();
51 return params;
54 ExtensionViewImpl.prototype.onElementDetached = function() {
55 this.guest.destroy();
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.
84 if (!isSrcValid) {
85 reject('Failed to load: src is not valid.');
86 return;
89 // Destroy the current guest and create a new one if extension ID
90 // is different.
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.
101 if (extensionId !=
102 this.attributes[ExtensionViewConstants.ATTRIBUTE_EXTENSION]
103 .getValue()) {
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.');
115 } else {
116 resolve('Successful load.');
118 }.bind(this));
119 } else {
120 ExtensionViewInternal.loadSrc(this.guest.getId(), src,
121 function(hasLoadSucceeded) {
122 if (!hasLoadSucceeded) {
123 reject('Failed to load.');
124 } else {
125 // Update the src attribute.
126 this.attributes[ExtensionViewConstants.ATTRIBUTE_SRC]
127 .setValueIgnoreMutation(src);
128 resolve('Successful load.');
130 }.bind(this));
132 }.bind(this));
136 GuestViewContainer.registerElement(ExtensionViewImpl);
138 // Exports.
139 exports.ExtensionViewImpl = ExtensionViewImpl;