Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / file_manager / file_manager / foreground / js / cws_container_client.js
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.
4
5 /**
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.
14  * @constructor
15  * @extends {cr.EventTarget}
16  */
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;
21   this.mime_ = mime;
22   this.searchQuery_ = searchQuery;
23   this.width_ = width;
24   this.height_ = height;
25   this.url_ = url;
26   this.target_ = target;
27
28   this.loaded_ = false;
29   this.loading_ = false;
30
31   this.onMessageBound_ = this.onMessage_.bind(this);
32   this.onLoadStopBound_ = this.onLoadStop_.bind(this);
33   this.onLoadAbortBound_ = this.onLoadAbort_.bind(this);
34 }
35
36 CWSContainerClient.prototype = {
37   __proto__: cr.EventTarget.prototype
38 };
39
40 /**
41  * Events CWSContainerClient fires
42  *
43  * @enum {string}
44  * @const
45  */
46 CWSContainerClient.Events = {
47   LOADED: 'CWSContainerClient.Events.LOADED',
48   LOAD_FAILED: 'CWSContainerClient.Events.LOAD_FAILED',
49   REQUEST_INSTALL: 'CWSContainerClient.Events.REQUEST_INSTALL'
50 };
51 Object.freeze(CWSContainerClient.Events);
52
53 /**
54  * Handles messages from the widget
55  * @param {Event} event Message event.
56  * @private
57  */
58 CWSContainerClient.prototype.onMessage_ = function(event) {
59   if (event.origin != this.target_)
60     return;
61
62   var data = event.data;
63   switch (data['message']) {
64     case 'widget_loaded':
65       this.onWidgetLoaded_();
66       break;
67     case 'widget_load_failed':
68       this.onWidgetLoadFailed_();
69       break;
70     case 'before_install':
71       this.sendInstallRequest_(data['item_id']);
72       break;
73     default:
74       console.error('Unexpected message: ' + data['message'], data);
75   }
76 };
77
78 /**
79  * Called when receiving 'loadstop' event from the <webview>.
80  * @param {Event} event Message event.
81  * @private
82  */
83 CWSContainerClient.prototype.onLoadStop_ = function(event) {
84   if (this.url_ == this.webView_.src && !this.loaded_) {
85     this.loaded_ = true;
86     this.postInitializeMessage_();
87   }
88 };
89
90 /**
91  * Called when the widget is loaded successfully.
92  * @private
93  */
94 CWSContainerClient.prototype.onWidgetLoaded_ = function() {
95   cr.dispatchSimpleEvent(this, CWSContainerClient.Events.LOADED);
96 };
97
98 /**
99  * Called when the widget is failed to load.
100  * @private
101  */
102 CWSContainerClient.prototype.onWidgetLoadFailed_ = function() {
103   this.sendWidgetLoadFailed_();
104 };
105
106 /**
107  * Called when receiving the 'loadabort' event from <webview>.
108  * @param {Event} event Message event.
109  * @private
110  */
111 CWSContainerClient.prototype.onLoadAbort_ = function(event) {
112   this.sendWidgetLoadFailed_();
113 };
114
115 /**
116  * Called when the installation is completed from the suggest-app dialog.
117  *
118  * @param {boolean} result True if the installation is success, false if failed.
119  * @param {string} itemId Item id to be installed.
120  */
121 CWSContainerClient.prototype.onInstallCompleted = function(result, itemId) {
122   if (result)
123     this.postInstallSuccessMessage_(itemId);
124   else
125     this.postInstallFailureMessage_(itemId);
126 };
127
128 /**
129  * Send the fail message to the suggest-app dialog.
130  * @private
131  */
132 CWSContainerClient.prototype.sendWidgetLoadFailed_ = function() {
133   cr.dispatchSimpleEvent(this, CWSContainerClient.Events.LOAD_FAILED);
134 };
135
136 /**
137  * Send the install request to the suggest-app dialog.
138  *
139  * @param {string} itemId Item id to be installed.
140  * @private
141  */
142 CWSContainerClient.prototype.sendInstallRequest_ = function(itemId) {
143   var event = new Event(CWSContainerClient.Events.REQUEST_INSTALL);
144   event.itemId = itemId;
145   this.dispatchEvent(event);
146 };
147
148 /**
149  * Send the 'install_failure' message to the widget.
150  *
151  * @param {string} itemId Item id to be installed.
152  * @private
153  */
154 CWSContainerClient.prototype.postInstallFailureMessage_ = function(itemId) {
155   var message = {
156     message: 'install_failure',
157     item_id: itemId,
158     v: 1
159   };
160
161   this.postMessage_(message);
162 };
163
164 /**
165  * Send the 'install_success' message to the widget.
166  *
167  * @param {string} itemId Item id to be installed.
168  * @private
169  */
170 CWSContainerClient.prototype.postInstallSuccessMessage_ = function(itemId) {
171   var message = {
172     message: 'install_success',
173     item_id: itemId,
174     v: 1
175   };
176
177   this.postMessage_(message);
178 };
179
180 /**
181  * Send the 'initialize' message to the widget.
182  * @private
183  */
184 CWSContainerClient.prototype.postInitializeMessage_ = function() {
185   var message = {
186     message: 'initialize',
187     hl: util.getCurrentLocaleOrDefault(),
188     width: this.width_,
189     height: this.height_,
190     v: 1
191   };
192
193   if (this.searchQuery_) {
194     message['search_query'] = this.searchQuery_;
195   } else {
196     message['file_extension'] = this.ext_;
197     message['mime_type'] = this.mime_;
198   }
199
200   this.postMessage_(message);
201 };
202
203 /**
204  * Send a message to the widget. This method shouldn't be called directly,
205  * should from more specified posting function (eg. postXyzMessage_()).
206  *
207  * @param {Object} message Message object to be posted.
208  * @private
209  */
210 CWSContainerClient.prototype.postMessage_ = function(message) {
211   if (!this.webView_.contentWindow)
212     return;
213
214   this.webView_.contentWindow.postMessage(message, this.target_);
215 };
216
217 /**
218  * Loads the page to <webview>. Can be called only once.
219  */
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;
225
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_);
230 };
231
232 /**
233  * Aborts loading of the embedded dialog and performs cleanup.
234  */
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();
241 };
242
243 /**
244  * Cleans the dialog by removing all handlers.
245  */
246 CWSContainerClient.prototype.dispose = function() {
247   this.abort();
248 };