OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | 7 /** |
8 * @param {WebView} webView Web View tag. | 8 * @param {WebView} webView Web View tag. |
9 * @param {string} ext File extension. | 9 * @param {string} ext File extension. |
10 * @param {string} mime File mime type. | 10 * @param {string} mime File mime type. |
(...skipping 18 matching lines...) Expand all Loading... |
29 this.onMessageBound_ = this.onMessage_.bind(this); | 29 this.onMessageBound_ = this.onMessage_.bind(this); |
30 this.onLoadStopBound_ = this.onLoadStop_.bind(this); | 30 this.onLoadStopBound_ = this.onLoadStop_.bind(this); |
31 this.onLoadAbortBound_ = this.onLoadAbort_.bind(this); | 31 this.onLoadAbortBound_ = this.onLoadAbort_.bind(this); |
32 } | 32 } |
33 | 33 |
34 CWSContainerClient.prototype = { | 34 CWSContainerClient.prototype = { |
35 __proto__: cr.EventTarget.prototype | 35 __proto__: cr.EventTarget.prototype |
36 }; | 36 }; |
37 | 37 |
38 /** | 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 /** |
39 * Handles messages from the widget | 52 * Handles messages from the widget |
40 * @param {Event} event Message event. | 53 * @param {Event} event Message event. |
41 * @private | 54 * @private |
42 */ | 55 */ |
43 CWSContainerClient.prototype.onMessage_ = function(event) { | 56 CWSContainerClient.prototype.onMessage_ = function(event) { |
44 if (event.origin != this.target_) | 57 if (event.origin != this.target_) |
45 return; | 58 return; |
46 | 59 |
47 var data = event.data; | 60 var data = event.data; |
48 switch (data['message']) { | 61 switch (data['message']) { |
49 case 'widget_loaded': | 62 case 'widget_loaded': |
50 // Do nothing. Waits for user action and next message. | 63 this.onWidgetLoaded_(); |
| 64 break; |
| 65 case 'widget_load_failed': |
| 66 this.onWidgetLoadFailed_(); |
51 break; | 67 break; |
52 case 'before_install': | 68 case 'before_install': |
53 this.sendInstallRequest_(data['item_id']); | 69 this.sendInstallRequest_(data['item_id']); |
54 break; | 70 break; |
55 default: | 71 default: |
56 console.error('Unexpected message: ' + data['message'], data); | 72 console.error('Unexpected message: ' + data['message'], data); |
57 } | 73 } |
58 }; | 74 }; |
59 | 75 |
60 /** | 76 /** |
61 * Called when receiving 'loadstop' event from the <wevview>. | 77 * Called when receiving 'loadstop' event from the <wevview>. |
62 * @param {Event} event Message event. | 78 * @param {Event} event Message event. |
63 * @private | 79 * @private |
64 */ | 80 */ |
65 CWSContainerClient.prototype.onLoadStop_ = function(event) { | 81 CWSContainerClient.prototype.onLoadStop_ = function(event) { |
66 if (this.url_ == this.webView_.src && !this.loaded_) { | 82 if (this.url_ == this.webView_.src && !this.loaded_) { |
67 this.loaded_ = true; | 83 this.loaded_ = true; |
68 this.postInitializeMessage_(); | 84 this.postInitializeMessage_(); |
69 } | 85 } |
70 }; | 86 }; |
71 | 87 |
72 /** | 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 /** |
73 * Called when receiving the 'loadabort' event from <webview>. | 105 * Called when receiving the 'loadabort' event from <webview>. |
74 * @param {Event} event Message event. | 106 * @param {Event} event Message event. |
75 * @private | 107 * @private |
76 */ | 108 */ |
77 CWSContainerClient.prototype.onLoadAbort_ = function(event) { | 109 CWSContainerClient.prototype.onLoadAbort_ = function(event) { |
78 this.sendWebviewLoadAbort_(); | 110 this.sendWidgetLoadFailed_(); |
79 }; | 111 }; |
80 | 112 |
81 /** | 113 /** |
82 * Called when the installation is completed from the suggest-app dialog. | 114 * Called when the installation is completed from the suggest-app dialog. |
83 * | 115 * |
84 * @param {boolean} result True if the installation is success, false if failed. | 116 * @param {boolean} result True if the installation is success, false if failed. |
85 * @param {string} itemId Item id to be installed. | 117 * @param {string} itemId Item id to be installed. |
86 */ | 118 */ |
87 CWSContainerClient.prototype.onInstallCompleted = function(result, itemId) { | 119 CWSContainerClient.prototype.onInstallCompleted = function(result, itemId) { |
88 if (result) | 120 if (result) |
89 this.postInstallSuccessMessage_(itemId); | 121 this.postInstallSuccessMessage_(itemId); |
90 else | 122 else |
91 this.postInstallFailureMessage_(itemId); | 123 this.postInstallFailureMessage_(itemId); |
92 }; | 124 }; |
93 | 125 |
94 /** | 126 /** |
95 * Send the abort event to the suggest-app dialog. | 127 * Send the fail message to the suggest-app dialog. |
96 * @private | 128 * @private |
97 */ | 129 */ |
98 CWSContainerClient.prototype.sendWebviewLoadAbort_ = function() { | 130 CWSContainerClient.prototype.sendWidgetLoadFailed_ = function() { |
99 this.dispatchEvent(new cr.Event('webview-load-abort')); | 131 cr.dispatchSimpleEvent(this, CWSContainerClient.Events.LOAD_FAILED); |
100 }; | 132 }; |
101 | 133 |
102 /** | 134 /** |
103 * Send the install request to the suggest-app dialog. | 135 * Send the install request to the suggest-app dialog. |
104 * | 136 * |
105 * @param {string} itemId Item id to be installed. | 137 * @param {string} itemId Item id to be installed. |
106 * @private | 138 * @private |
107 */ | 139 */ |
108 CWSContainerClient.prototype.sendInstallRequest_ = function(itemId) { | 140 CWSContainerClient.prototype.sendInstallRequest_ = function(itemId) { |
109 var event = new cr.Event('install-request'); | 141 var event = new cr.Event(CWSContainerClient.Events.REQUEST_INSTALL); |
110 event.itemId = itemId; | 142 event.itemId = itemId; |
111 this.dispatchEvent(event); | 143 this.dispatchEvent(event); |
112 }; | 144 }; |
113 | 145 |
114 /** | 146 /** |
115 * Send the 'install_failure' message to the widget. | 147 * Send the 'install_failure' message to the widget. |
116 * | 148 * |
117 * @param {string} itemId Item id to be installed. | 149 * @param {string} itemId Item id to be installed. |
118 * @private | 150 * @private |
119 */ | 151 */ |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 this.webView_.stop(); | 233 this.webView_.stop(); |
202 }; | 234 }; |
203 | 235 |
204 /** | 236 /** |
205 * Cleans the dialog by removing all handlers. | 237 * Cleans the dialog by removing all handlers. |
206 */ | 238 */ |
207 CWSContainerClient.prototype.dispose = function() { | 239 CWSContainerClient.prototype.dispose = function() { |
208 this.abort(); | 240 this.abort(); |
209 }; | 241 }; |
210 | 242 |
OLD | NEW |