summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/resources/file_manager/foreground/js/cws_container_client.js
blob: fae6787a5adc168232e038afef4ed99afd0a501a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

'use strict';

/**
 * @param {WebView} webView Web View tag.
 * @param {?string} ext File extension.
 * @param {?string} mime File mime type.
 * @param {?string} searchQuery Search query.
 * @param {number} width Width of the CWS widget.
 * @param {number} height Height of the CWS widget.
 * @param {string} url Share Url for an entry.
 * @param {string} target Target (scheme + host + port) of the widget.
 * @constructor
 */
function CWSContainerClient(
    webView, ext, mime, searchQuery, width, height, url, target) {
  this.webView_ = webView;
  this.ext_ = (ext && ext[0] == '.') ? ext.substr(1) : ext;
  this.mime_ = mime;
  this.searchQuery_ = searchQuery;
  this.width_ = width;
  this.height_ = height;
  this.url_ = url;
  this.target_ = target;

  this.loaded_ = false;
  this.loading_ = false;

  this.onMessageBound_ = this.onMessage_.bind(this);
  this.onLoadStopBound_ = this.onLoadStop_.bind(this);
  this.onLoadAbortBound_ = this.onLoadAbort_.bind(this);
}

CWSContainerClient.prototype = {
  __proto__: cr.EventTarget.prototype
};

/**
 * Events CWSContainerClient fires
 *
 * @enum {string}
 * @const
 */
CWSContainerClient.Events = {
  LOADED: 'CWSContainerClient.Events.LOADED',
  LOAD_FAILED: 'CWSContainerClient.Events.LOAD_FAILED',
  REQUEST_INSTALL: 'CWSContainerClient.Events.REQUEST_INSTALL'
};
Object.freeze(CWSContainerClient.Events);

/**
 * Handles messages from the widget
 * @param {Event} event Message event.
 * @private
 */
CWSContainerClient.prototype.onMessage_ = function(event) {
  if (event.origin != this.target_)
    return;

  var data = event.data;
  switch (data['message']) {
    case 'widget_loaded':
      this.onWidgetLoaded_();
      break;
    case 'widget_load_failed':
      this.onWidgetLoadFailed_();
      break;
    case 'before_install':
      this.sendInstallRequest_(data['item_id']);
      break;
    default:
      console.error('Unexpected message: ' + data['message'], data);
  }
};

/**
 * Called when receiving 'loadstop' event from the <wevview>.
 * @param {Event} event Message event.
 * @private
 */
CWSContainerClient.prototype.onLoadStop_ = function(event) {
  if (this.url_ == this.webView_.src && !this.loaded_) {
    this.loaded_ = true;
    this.postInitializeMessage_();
  }
};

/**
 * Called when the widget is loaded successfully.
 * @private
 */
CWSContainerClient.prototype.onWidgetLoaded_ = function() {
  cr.dispatchSimpleEvent(this, CWSContainerClient.Events.LOADED);
};

/**
 * Called when the widget is failed to load.
 * @private
 */
CWSContainerClient.prototype.onWidgetLoadFailed_ = function() {
  this.sendWidgetLoadFailed_();
};

/**
 * Called when receiving the 'loadabort' event from <webview>.
 * @param {Event} event Message event.
 * @private
 */
CWSContainerClient.prototype.onLoadAbort_ = function(event) {
  this.sendWidgetLoadFailed_();
};

/**
 * Called when the installation is completed from the suggest-app dialog.
 *
 * @param {boolean} result True if the installation is success, false if failed.
 * @param {string} itemId Item id to be installed.
 */
CWSContainerClient.prototype.onInstallCompleted = function(result, itemId) {
  if (result)
    this.postInstallSuccessMessage_(itemId);
  else
    this.postInstallFailureMessage_(itemId);
};

/**
 * Send the fail message to the suggest-app dialog.
 * @private
 */
CWSContainerClient.prototype.sendWidgetLoadFailed_ = function() {
  cr.dispatchSimpleEvent(this, CWSContainerClient.Events.LOAD_FAILED);
};

/**
 * Send the install request to the suggest-app dialog.
 *
 * @param {string} itemId Item id to be installed.
 * @private
 */
CWSContainerClient.prototype.sendInstallRequest_ = function(itemId) {
  var event = new Event(CWSContainerClient.Events.REQUEST_INSTALL);
  event.itemId = itemId;
  this.dispatchEvent(event);
};

/**
 * Send the 'install_failure' message to the widget.
 *
 * @param {string} itemId Item id to be installed.
 * @private
 */
CWSContainerClient.prototype.postInstallFailureMessage_ = function(itemId) {
  var message = {
    message: 'install_failure',
    item_id: itemId,
    v: 1
  };

  this.postMessage_(message);
};

/**
 * Send the 'install_success' message to the widget.
 *
 * @param {string} itemId Item id to be installed.
 * @private
 */
CWSContainerClient.prototype.postInstallSuccessMessage_ = function(itemId) {
  var message = {
    message: 'install_success',
    item_id: itemId,
    v: 1
  };

  this.postMessage_(message);
};

/**
 * Send the 'initialize' message to the widget.
 * @private
 */
CWSContainerClient.prototype.postInitializeMessage_ = function() {
  var message = {
    message: 'initialize',
    hl: util.getCurrentLocaleOrDefault(),
    widgth: this.width_,
    height: this.height_,
    v: 1
  };

  if (this.searchQuery_) {
    message['search_query'] = this.searchQuery_;
  } else {
    message['file_extension'] = this.ext_;
    message['mime_type'] = this.mime_;
  }

  this.postMessage_(message);
};

/**
 * Send a message to the widget. This method shouldn't be called directly,
 * should from more specified posting function (eg. postXyzMessage_()).
 *
 * @param {object} message Message object to be posted.
 * @private
 */
CWSContainerClient.prototype.postMessage_ = function(message) {
  if (!this.webView_.contentWindow)
    return;

  this.webView_.contentWindow.postMessage(message, this.target_);
};

/**
 * Loads the page to <webview>. Can be called only once.
 */
CWSContainerClient.prototype.load = function() {
  if (this.loading_ || this.loaded_)
    throw new Error('Already loaded.');
  this.loading_ = true;
  this.loaded_ = false;

  window.addEventListener('message', this.onMessageBound_);
  this.webView_.addEventListener('loadstop', this.onLoadStopBound_);
  this.webView_.addEventListener('loadabort', this.onLoadAbortBound_);
  this.webView_.setAttribute('src', this.url_);
};

/**
 * Aborts loading of the embedded dialog and performs cleanup.
 */
CWSContainerClient.prototype.abort = function() {
  window.removeEventListener('message', this.onMessageBound_);
  this.webView_.removeEventListener('loadstop', this.onLoadStopBound_);
  this.webView_.removeEventListener(
      'loadabort', this.onLoadAbortBound_);
  this.webView_.stop();
};

/**
 * Cleans the dialog by removing all handlers.
 */
CWSContainerClient.prototype.dispose = function() {
  this.abort();
};