f332dd6b7a3745431913360aa83d0f53387e0d9e
[platform/framework/web/crosswalk.git] / src / ui / file_manager / file_manager / foreground / js / share_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} url Share Url for an entry.
8  * @param {ShareClient.Observer} observer Observer instance.
9  * @constructor
10  */
11 function ShareClient(webView, url, observer) {
12   this.webView_ = webView;
13   this.url_ = url;
14   this.observer_ = observer;
15   this.loaded_ = false;
16   this.loading_ = false;
17   this.onMessageBound_ = this.onMessage_.bind(this);
18   this.onLoadStopBound_ = this.onLoadStop_.bind(this);
19   this.onLoadAbortBound_ = this.onLoadAbort_.bind(this);
20 }
21
22 /**
23  * Target origin of the embedded dialog.
24  * @type {string}
25  * @const
26  */
27 ShareClient.SHARE_TARGET = 'https://drive.google.com';
28
29 /**
30  * Observes for state changes of the embedded dialog.
31  * @interface
32  */
33 ShareClient.Observer = function() {
34 };
35
36 /**
37  * Notifies about the embedded dialog being loaded.
38  */
39 ShareClient.Observer.prototype.onLoaded = function() {
40 };
41
42 /**
43  * Notifies when the the embedded dialog failed to load.
44  */
45 ShareClient.Observer.prototype.onLoadFailed = function() {
46 };
47
48 /**
49  * Notifies about changed dimensions of the embedded dialog.
50  * @param {number} width Width in pixels.
51  * @param {number} height Height in pixels.
52  * @param {function()} callback Completion callback. Call when finished
53  *     handling the resize.
54  */
55 ShareClient.Observer.prototype.onResized = function(width, height, callback) {
56 };
57
58 /**
59  * Notifies about the embedded dialog being closed.
60  */
61 ShareClient.Observer.prototype.onClosed = function() {
62 };
63
64 /**
65  * Handles messages from the embedded dialog.
66  * @param {Event} e Message event.
67  * @private
68  */
69 ShareClient.prototype.onMessage_ = function(e) {
70   if (e.origin != ShareClient.SHARE_TARGET && !window.IN_TEST) {
71     // Logs added temporarily to track crbug.com/288783.
72     console.debug('Received a message from an illegal origin: ' + e.origin);
73     return;
74   }
75
76   var data = JSON.parse(e.data);
77   // Logs added temporarily to track crbug.com/288783.
78   console.debug('Received message: ' + data.type);
79
80   switch (data.type) {
81     case 'resize':
82       this.observer_.onResized(data.args.width,
83                                data.args.height,
84                                this.postMessage_.bind(this, 'resizeComplete'));
85       break;
86     case 'prepareForVisible':
87       this.postMessage_('prepareComplete');
88       if (!this.loaded_) {
89         this.loading_ = false;
90         this.loaded_ = true;
91         this.observer_.onLoaded();
92       }
93       break;
94     case 'setVisible':
95       if (!data.args.visible)
96         this.observer_.onClosed();
97       break;
98   }
99 };
100
101 /**
102  * Handles completion of the web view request.
103  * @param {Event} e Message event.
104  * @private
105  */
106 ShareClient.prototype.onLoadStop_ = function(e) {
107   // Logs added temporarily to track crbug.com/288783.
108   console.debug('Web View loaded.');
109
110   this.postMessage_('makeBodyVisible');
111 };
112
113 /**
114  * Handles termination of the web view request.
115  * @param {Event} e Message event.
116  * @private
117  */
118 ShareClient.prototype.onLoadAbort_ = function(e) {
119   // Logs added temporarily to track crbug.com/288783.
120   console.debug('Web View failed to load with error: ' + e.reason + ', url: ' +
121       e.url + ' while requested: ' + this.url_);
122
123   this.observer_.onLoadFailed();
124 };
125
126 /**
127  * Sends a message to the embedded dialog.
128  * @param {string} type Message type.
129  * @param {Object=} opt_args Optional arguments.
130  * @private
131  */
132 ShareClient.prototype.postMessage_ = function(type, opt_args) {
133   // Logs added temporarily to track crbug.com/288783.
134   console.debug('Sending message: ' + type);
135
136   var message = {
137     type: type,
138     args: opt_args
139   };
140   this.webView_.contentWindow.postMessage(
141       JSON.stringify(message),
142       !window.IN_TEST ? ShareClient.SHARE_TARGET : '*');
143 };
144
145 /**
146  * Loads the embedded dialog. Can be called only one.
147  */
148 ShareClient.prototype.load = function() {
149   if (this.loading_ || this.loaded_)
150     throw new Error('Already loaded.');
151   this.loading_ = true;
152
153   // Logs added temporarily to track crbug.com/288783.
154   console.debug('Loading.');
155
156   window.addEventListener('message', this.onMessageBound_);
157   this.webView_.addEventListener('loadstop', this.onLoadStopBound_);
158   this.webView_.addEventListener('loadabort', this.onLoadAbortBound_);
159   this.webView_.setAttribute('src', this.url_);
160 };
161
162 /**
163  * Aborts loading of the embedded dialog and performs cleanup.
164  */
165 ShareClient.prototype.abort = function() {
166   window.removeEventListener('message', this.onMessageBound_);
167   this.webView_.removeEventListener('loadstop', this.onLoadStopBound_);
168   this.webView_.removeEventListener(
169       'loadabort', this.onLoadAbortBound_);
170   this.webView_.stop();
171 };
172
173 /**
174  * Cleans the dialog by removing all handlers.
175  */
176 ShareClient.prototype.dispose = function() {
177   this.abort();
178 };