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