Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(164)

Side by Side Diff: chrome/browser/resources/file_manager/js/cws_container_client.js

Issue 39123003: [Files.app] Split the JavaScript files into subdirectories: common, background, and foreground (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixed test failure. Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 'use strict';
6
7 /**
8 * @param {WebView} webView Web View tag.
9 * @param {string} ext File extension.
10 * @param {string} mime File mime type.
11 * @param {number} width Width of the CWS widget.
12 * @param {number} height Height of the CWS widget.
13 * @param {string} url Share Url for an entry.
14 * @param {string} target Target (scheme + host + port) of the widget.
15 * @constructor
16 */
17 function CWSContainerClient(webView, ext, mime, width, height, url, target) {
18 this.webView_ = webView;
19 this.ext_ = ext;
20 this.mime_ = mime;
21 this.width_ = width;
22 this.height_ = height;
23 this.url_ = url;
24 this.target_ = target;
25
26 this.loaded_ = false;
27 this.loading_ = false;
28
29 this.onMessageBound_ = this.onMessage_.bind(this);
30 this.onLoadStopBound_ = this.onLoadStop_.bind(this);
31 this.onLoadAbortBound_ = this.onLoadAbort_.bind(this);
32 }
33
34 CWSContainerClient.prototype = {
35 __proto__: cr.EventTarget.prototype
36 };
37
38 /**
39 * Events CWSContainerClient fires
40 *
41 * @enum {string}
42 * @const
43 */
44 CWSContainerClient.Events = {
45 LOADED: 'CWSContainerClient.Events.LOADED',
46 LOAD_FAILED: 'CWSContainerClient.Events.LOAD_FAILED',
47 REQUEST_INSTALL: 'CWSContainerClient.Events.REQUEST_INSTALL'
48 };
49 Object.freeze(CWSContainerClient.Events);
50
51 /**
52 * Handles messages from the widget
53 * @param {Event} event Message event.
54 * @private
55 */
56 CWSContainerClient.prototype.onMessage_ = function(event) {
57 if (event.origin != this.target_)
58 return;
59
60 var data = event.data;
61 switch (data['message']) {
62 case 'widget_loaded':
63 this.onWidgetLoaded_();
64 break;
65 case 'widget_load_failed':
66 this.onWidgetLoadFailed_();
67 break;
68 case 'before_install':
69 this.sendInstallRequest_(data['item_id']);
70 break;
71 default:
72 console.error('Unexpected message: ' + data['message'], data);
73 }
74 };
75
76 /**
77 * Called when receiving 'loadstop' event from the <wevview>.
78 * @param {Event} event Message event.
79 * @private
80 */
81 CWSContainerClient.prototype.onLoadStop_ = function(event) {
82 if (this.url_ == this.webView_.src && !this.loaded_) {
83 this.loaded_ = true;
84 this.postInitializeMessage_();
85 }
86 };
87
88 /**
89 * Called when the widget is loaded successfully.
90 * @private
91 */
92 CWSContainerClient.prototype.onWidgetLoaded_ = function() {
93 cr.dispatchSimpleEvent(this, CWSContainerClient.Events.LOADED);
94 };
95
96 /**
97 * Called when the widget is failed to load.
98 * @private
99 */
100 CWSContainerClient.prototype.onWidgetLoadFailed_ = function() {
101 this.sendWidgetLoadFailed_();
102 };
103
104 /**
105 * Called when receiving the 'loadabort' event from <webview>.
106 * @param {Event} event Message event.
107 * @private
108 */
109 CWSContainerClient.prototype.onLoadAbort_ = function(event) {
110 this.sendWidgetLoadFailed_();
111 };
112
113 /**
114 * Called when the installation is completed from the suggest-app dialog.
115 *
116 * @param {boolean} result True if the installation is success, false if failed.
117 * @param {string} itemId Item id to be installed.
118 */
119 CWSContainerClient.prototype.onInstallCompleted = function(result, itemId) {
120 if (result)
121 this.postInstallSuccessMessage_(itemId);
122 else
123 this.postInstallFailureMessage_(itemId);
124 };
125
126 /**
127 * Send the fail message to the suggest-app dialog.
128 * @private
129 */
130 CWSContainerClient.prototype.sendWidgetLoadFailed_ = function() {
131 cr.dispatchSimpleEvent(this, CWSContainerClient.Events.LOAD_FAILED);
132 };
133
134 /**
135 * Send the install request to the suggest-app dialog.
136 *
137 * @param {string} itemId Item id to be installed.
138 * @private
139 */
140 CWSContainerClient.prototype.sendInstallRequest_ = function(itemId) {
141 var event = new Event(CWSContainerClient.Events.REQUEST_INSTALL);
142 event.itemId = itemId;
143 this.dispatchEvent(event);
144 };
145
146 /**
147 * Send the 'install_failure' message to the widget.
148 *
149 * @param {string} itemId Item id to be installed.
150 * @private
151 */
152 CWSContainerClient.prototype.postInstallFailureMessage_ = function(itemId) {
153 var message = {
154 message: 'install_failure',
155 item_id: itemId,
156 v: 1
157 };
158
159 this.postMessage_(message);
160 };
161
162 /**
163 * Send the 'install_success' message to the widget.
164 *
165 * @param {string} itemId Item id to be installed.
166 * @private
167 */
168 CWSContainerClient.prototype.postInstallSuccessMessage_ = function(itemId) {
169 var message = {
170 message: 'install_success',
171 item_id: itemId,
172 v: 1
173 };
174
175 this.postMessage_(message);
176 };
177
178 /**
179 * Send the 'initialize' message to the widget.
180 * @private
181 */
182 CWSContainerClient.prototype.postInitializeMessage_ = function() {
183 var message = {
184 message: 'initialize',
185 hl: util.getCurrentLocaleOrDefault(),
186 widgth: this.width_,
187 height: this.height_,
188 file_extension: this.ext_,
189 mime_type: this.mime_,
190 v: 1
191 };
192
193 this.postMessage_(message);
194 };
195
196 /**
197 * Send a message to the widget. This method shouldn't be called directly,
198 * should from more specified posting function (eg. postXyzMessage_()).
199 *
200 * @param {object} message Message object to be posted.
201 * @private
202 */
203 CWSContainerClient.prototype.postMessage_ = function(message) {
204 if (!this.webView_.contentWindow)
205 return;
206
207 this.webView_.contentWindow.postMessage(message, this.target_);
208 };
209
210 /**
211 * Loads the page to <webview>. Can be called only once.
212 */
213 CWSContainerClient.prototype.load = function() {
214 if (this.loading_ || this.loaded_)
215 throw new Error('Already loaded.');
216 this.loading_ = true;
217 this.loaded_ = false;
218
219 window.addEventListener('message', this.onMessageBound_);
220 this.webView_.addEventListener('loadstop', this.onLoadStopBound_);
221 this.webView_.addEventListener('loadabort', this.onLoadAbortBound_);
222 this.webView_.setAttribute('src', this.url_);
223 };
224
225 /**
226 * Aborts loading of the embedded dialog and performs cleanup.
227 */
228 CWSContainerClient.prototype.abort = function() {
229 window.removeEventListener('message', this.onMessageBound_);
230 this.webView_.removeEventListener('loadstop', this.onLoadStopBound_);
231 this.webView_.removeEventListener(
232 'loadabort', this.onLoadAbortBound_);
233 this.webView_.stop();
234 };
235
236 /**
237 * Cleans the dialog by removing all handlers.
238 */
239 CWSContainerClient.prototype.dispose = function() {
240 this.abort();
241 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698