1 // Copyright 2013 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.
8 * @param {WebView} webView Web View tag.
9 * @param {?string} ext File extension.
10 * @param {?string} mime File mime type.
11 * @param {?string} searchQuery Search query.
12 * @param {number} width Width of the CWS widget.
13 * @param {number} height Height of the CWS widget.
14 * @param {string} url Share Url for an entry.
15 * @param {string} target Target (scheme + host + port) of the widget.
18 function CWSContainerClient(
19 webView, ext, mime, searchQuery, width, height, url, target) {
20 this.webView_ = webView;
21 this.ext_ = (ext && ext[0] == '.') ? ext.substr(1) : ext;
23 this.searchQuery_ = searchQuery;
25 this.height_ = height;
27 this.target_ = target;
30 this.loading_ = false;
32 this.onMessageBound_ = this.onMessage_.bind(this);
33 this.onLoadStopBound_ = this.onLoadStop_.bind(this);
34 this.onLoadAbortBound_ = this.onLoadAbort_.bind(this);
37 CWSContainerClient.prototype = {
38 __proto__: cr.EventTarget.prototype
42 * Events CWSContainerClient fires
47 CWSContainerClient.Events = {
48 LOADED: 'CWSContainerClient.Events.LOADED',
49 LOAD_FAILED: 'CWSContainerClient.Events.LOAD_FAILED',
50 REQUEST_INSTALL: 'CWSContainerClient.Events.REQUEST_INSTALL'
52 Object.freeze(CWSContainerClient.Events);
55 * Handles messages from the widget
56 * @param {Event} event Message event.
59 CWSContainerClient.prototype.onMessage_ = function(event) {
60 if (event.origin != this.target_)
63 var data = event.data;
64 switch (data['message']) {
66 this.onWidgetLoaded_();
68 case 'widget_load_failed':
69 this.onWidgetLoadFailed_();
71 case 'before_install':
72 this.sendInstallRequest_(data['item_id']);
75 console.error('Unexpected message: ' + data['message'], data);
80 * Called when receiving 'loadstop' event from the <wevview>.
81 * @param {Event} event Message event.
84 CWSContainerClient.prototype.onLoadStop_ = function(event) {
85 if (this.url_ == this.webView_.src && !this.loaded_) {
87 this.postInitializeMessage_();
92 * Called when the widget is loaded successfully.
95 CWSContainerClient.prototype.onWidgetLoaded_ = function() {
96 cr.dispatchSimpleEvent(this, CWSContainerClient.Events.LOADED);
100 * Called when the widget is failed to load.
103 CWSContainerClient.prototype.onWidgetLoadFailed_ = function() {
104 this.sendWidgetLoadFailed_();
108 * Called when receiving the 'loadabort' event from <webview>.
109 * @param {Event} event Message event.
112 CWSContainerClient.prototype.onLoadAbort_ = function(event) {
113 this.sendWidgetLoadFailed_();
117 * Called when the installation is completed from the suggest-app dialog.
119 * @param {boolean} result True if the installation is success, false if failed.
120 * @param {string} itemId Item id to be installed.
122 CWSContainerClient.prototype.onInstallCompleted = function(result, itemId) {
124 this.postInstallSuccessMessage_(itemId);
126 this.postInstallFailureMessage_(itemId);
130 * Send the fail message to the suggest-app dialog.
133 CWSContainerClient.prototype.sendWidgetLoadFailed_ = function() {
134 cr.dispatchSimpleEvent(this, CWSContainerClient.Events.LOAD_FAILED);
138 * Send the install request to the suggest-app dialog.
140 * @param {string} itemId Item id to be installed.
143 CWSContainerClient.prototype.sendInstallRequest_ = function(itemId) {
144 var event = new Event(CWSContainerClient.Events.REQUEST_INSTALL);
145 event.itemId = itemId;
146 this.dispatchEvent(event);
150 * Send the 'install_failure' message to the widget.
152 * @param {string} itemId Item id to be installed.
155 CWSContainerClient.prototype.postInstallFailureMessage_ = function(itemId) {
157 message: 'install_failure',
162 this.postMessage_(message);
166 * Send the 'install_success' message to the widget.
168 * @param {string} itemId Item id to be installed.
171 CWSContainerClient.prototype.postInstallSuccessMessage_ = function(itemId) {
173 message: 'install_success',
178 this.postMessage_(message);
182 * Send the 'initialize' message to the widget.
185 CWSContainerClient.prototype.postInitializeMessage_ = function() {
187 message: 'initialize',
188 hl: util.getCurrentLocaleOrDefault(),
190 height: this.height_,
194 if (this.searchQuery_) {
195 message['search_query'] = this.searchQuery_;
197 message['file_extension'] = this.ext_;
198 message['mime_type'] = this.mime_;
201 this.postMessage_(message);
205 * Send a message to the widget. This method shouldn't be called directly,
206 * should from more specified posting function (eg. postXyzMessage_()).
208 * @param {object} message Message object to be posted.
211 CWSContainerClient.prototype.postMessage_ = function(message) {
212 if (!this.webView_.contentWindow)
215 this.webView_.contentWindow.postMessage(message, this.target_);
219 * Loads the page to <webview>. Can be called only once.
221 CWSContainerClient.prototype.load = function() {
222 if (this.loading_ || this.loaded_)
223 throw new Error('Already loaded.');
224 this.loading_ = true;
225 this.loaded_ = false;
227 window.addEventListener('message', this.onMessageBound_);
228 this.webView_.addEventListener('loadstop', this.onLoadStopBound_);
229 this.webView_.addEventListener('loadabort', this.onLoadAbortBound_);
230 this.webView_.setAttribute('src', this.url_);
234 * Aborts loading of the embedded dialog and performs cleanup.
236 CWSContainerClient.prototype.abort = function() {
237 window.removeEventListener('message', this.onMessageBound_);
238 this.webView_.removeEventListener('loadstop', this.onLoadStopBound_);
239 this.webView_.removeEventListener(
240 'loadabort', this.onLoadAbortBound_);
241 this.webView_.stop();
245 * Cleans the dialog by removing all handlers.
247 CWSContainerClient.prototype.dispose = function() {