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.
6 * @param {WebView} webView Web View tag.
7 * @param {?string} ext File extension.
8 * @param {?string} mime File mime type.
9 * @param {?string} searchQuery Search query.
10 * @param {number} width Width of the CWS widget.
11 * @param {number} height Height of the CWS widget.
12 * @param {string} url Share Url for an entry.
13 * @param {string} target Target (scheme + host + port) of the widget.
15 * @extends {cr.EventTarget}
17 function CWSContainerClient(
18 webView, ext, mime, searchQuery, width, height, url, target) {
19 this.webView_ = webView;
20 this.ext_ = (ext && ext[0] == '.') ? ext.substr(1) : ext;
22 this.searchQuery_ = searchQuery;
24 this.height_ = height;
26 this.target_ = target;
29 this.loading_ = false;
31 this.onMessageBound_ = this.onMessage_.bind(this);
32 this.onLoadStopBound_ = this.onLoadStop_.bind(this);
33 this.onLoadAbortBound_ = this.onLoadAbort_.bind(this);
36 CWSContainerClient.prototype = {
37 __proto__: cr.EventTarget.prototype
41 * Events CWSContainerClient fires
46 CWSContainerClient.Events = {
47 LOADED: 'CWSContainerClient.Events.LOADED',
48 LOAD_FAILED: 'CWSContainerClient.Events.LOAD_FAILED',
49 REQUEST_INSTALL: 'CWSContainerClient.Events.REQUEST_INSTALL'
51 Object.freeze(CWSContainerClient.Events);
54 * Handles messages from the widget
55 * @param {Event} event Message event.
58 CWSContainerClient.prototype.onMessage_ = function(event) {
59 if (event.origin != this.target_)
62 var data = event.data;
63 switch (data['message']) {
65 this.onWidgetLoaded_();
67 case 'widget_load_failed':
68 this.onWidgetLoadFailed_();
70 case 'before_install':
71 this.sendInstallRequest_(data['item_id']);
74 console.error('Unexpected message: ' + data['message'], data);
79 * Called when receiving 'loadstop' event from the <webview>.
80 * @param {Event} event Message event.
83 CWSContainerClient.prototype.onLoadStop_ = function(event) {
84 if (this.url_ == this.webView_.src && !this.loaded_) {
86 this.postInitializeMessage_();
91 * Called when the widget is loaded successfully.
94 CWSContainerClient.prototype.onWidgetLoaded_ = function() {
95 cr.dispatchSimpleEvent(this, CWSContainerClient.Events.LOADED);
99 * Called when the widget is failed to load.
102 CWSContainerClient.prototype.onWidgetLoadFailed_ = function() {
103 this.sendWidgetLoadFailed_();
107 * Called when receiving the 'loadabort' event from <webview>.
108 * @param {Event} event Message event.
111 CWSContainerClient.prototype.onLoadAbort_ = function(event) {
112 this.sendWidgetLoadFailed_();
116 * Called when the installation is completed from the suggest-app dialog.
118 * @param {boolean} result True if the installation is success, false if failed.
119 * @param {string} itemId Item id to be installed.
121 CWSContainerClient.prototype.onInstallCompleted = function(result, itemId) {
123 this.postInstallSuccessMessage_(itemId);
125 this.postInstallFailureMessage_(itemId);
129 * Send the fail message to the suggest-app dialog.
132 CWSContainerClient.prototype.sendWidgetLoadFailed_ = function() {
133 cr.dispatchSimpleEvent(this, CWSContainerClient.Events.LOAD_FAILED);
137 * Send the install request to the suggest-app dialog.
139 * @param {string} itemId Item id to be installed.
142 CWSContainerClient.prototype.sendInstallRequest_ = function(itemId) {
143 var event = new Event(CWSContainerClient.Events.REQUEST_INSTALL);
144 event.itemId = itemId;
145 this.dispatchEvent(event);
149 * Send the 'install_failure' message to the widget.
151 * @param {string} itemId Item id to be installed.
154 CWSContainerClient.prototype.postInstallFailureMessage_ = function(itemId) {
156 message: 'install_failure',
161 this.postMessage_(message);
165 * Send the 'install_success' message to the widget.
167 * @param {string} itemId Item id to be installed.
170 CWSContainerClient.prototype.postInstallSuccessMessage_ = function(itemId) {
172 message: 'install_success',
177 this.postMessage_(message);
181 * Send the 'initialize' message to the widget.
184 CWSContainerClient.prototype.postInitializeMessage_ = function() {
186 message: 'initialize',
187 hl: util.getCurrentLocaleOrDefault(),
189 height: this.height_,
193 if (this.searchQuery_) {
194 message['search_query'] = this.searchQuery_;
196 message['file_extension'] = this.ext_;
197 message['mime_type'] = this.mime_;
200 this.postMessage_(message);
204 * Send a message to the widget. This method shouldn't be called directly,
205 * should from more specified posting function (eg. postXyzMessage_()).
207 * @param {Object} message Message object to be posted.
210 CWSContainerClient.prototype.postMessage_ = function(message) {
211 if (!this.webView_.contentWindow)
214 this.webView_.contentWindow.postMessage(message, this.target_);
218 * Loads the page to <webview>. Can be called only once.
220 CWSContainerClient.prototype.load = function() {
221 if (this.loading_ || this.loaded_)
222 throw new Error('Already loaded.');
223 this.loading_ = true;
224 this.loaded_ = false;
226 window.addEventListener('message', this.onMessageBound_);
227 this.webView_.addEventListener('loadstop', this.onLoadStopBound_);
228 this.webView_.addEventListener('loadabort', this.onLoadAbortBound_);
229 this.webView_.setAttribute('src', this.url_);
233 * Aborts loading of the embedded dialog and performs cleanup.
235 CWSContainerClient.prototype.abort = function() {
236 window.removeEventListener('message', this.onMessageBound_);
237 this.webView_.removeEventListener('loadstop', this.onLoadStopBound_);
238 this.webView_.removeEventListener(
239 'loadabort', this.onLoadAbortBound_);
240 this.webView_.stop();
244 * Cleans the dialog by removing all handlers.
246 CWSContainerClient.prototype.dispose = function() {