1 // Copyright 2015 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.
5 //<include src="../../../../ui/webui/resources/js/cr.js">
6 //<include src="../../../../ui/webui/resources/js/load_time_data.js">
7 //<include src="../../../../ui/webui/resources/js/i18n_template_no_process.js">
8 //<include src="../../../../ui/webui/resources/js/cr/event_target.js">
9 //<include src="../../../../ui/webui/resources/js/cr/ui/dialogs.js">
14 //<include src="../cws_widget/app_installer.js">
15 //<include src="../cws_widget/cws_webview_client.js">
16 //<include src="../cws_widget/cws_widget_container.js">
17 //<include src="../cws_widget/cws_widget_container_error_dialog.js">
22 * webstoreUrl: ?string
25 window
.params
= window
.params
|| null;
28 * @param {string} id Element id.
29 * @return {HTMLElement} The found element, or null.
32 return document
.getElementById(id
);
38 var defaultStrings
= {
40 'LINK_TO_WEBSTORE': '[LOCALIZE ME] Learn more...',
41 'INSTALLATION_FAILED_MESSAGE': '[LOCALIZE ME] Installation failed!',
42 'OK_BUTTON': '[LOCALIZE ME] OK',
43 'TITLE_PRINTER_PROVIDERS': '[LOCALIZE ME] Select app for your printer',
44 'DEFAULT_ERROR_MESSAGE': '[LOCALIZE ME] Failure'
48 * @param {string} id The string id.
51 function getString(id
) {
52 return loadTimeData
.getString(id
) || defaultStrings
[id
] || '';
56 * @param {Object<string>} strings Localized strings used by the container.
57 * @return {!CWSWidgetContainer.PlatformDelegate}
59 function createPlatformDelegate(strings
) {
62 UI_LOCALE
: getString('language'),
63 LINK_TO_WEBSTORE
: getString('LINK_TO_WEBSTORE'),
64 INSTALLATION_FAILED_MESSAGE
: getString('INSTALLATION_FAILED_MESSAGE'),
65 LOADING_SPINNER_ALT
: getString('LOADING_SPINNER_ALT'),
66 INSTALLING_SPINNER_ALT
: getString('INSTALLING_SPINNER_ALT')
71 * @param {string} enumName
72 * @param {number} value
73 * @param {number} enumSize
75 recordEnum: function(enumName
, value
, enumSize
) {},
77 /** @param {string} actionName */
78 recordUserAction: function(actionName
) {},
80 /** @param {string} intervalName */
81 startInterval: function(intervalName
) {},
83 /** @param {string} intervalName */
84 recordInterval: function(intervalName
) {}
88 * @param {string} itemId Item to be installed.
89 * @param {function(?string)} callback Callback param is the error message,
90 * which is set to null on success.
92 installWebstoreItem: function(itemId
, callback
) {
93 chrome
.webstoreWidgetPrivate
.installWebstoreItem(
97 callback(chrome
.runtime
.lastError
?
98 chrome
.runtime
.lastError
.message
|| 'UNKNOWN_ERROR' : null);
102 /** @param {function(Array<string>)} callback */
103 getInstalledItems: function(callback
) { callback([]); },
106 * @param {function(?string)} callback The argument is the fetche3d access
107 * token. Null on error.
109 requestWebstoreAccessToken: function(callback
) {
110 chrome
.fileManagerPrivate
.requestWebStoreAccessToken(function(token
) {
111 if (chrome
.runtime
.lastError
) {
112 console
.error('Error getting access token: ' +
113 chrome
.runtime
.lastError
.message
);
123 function initializeTopbarButtons() {
124 $('close-button').addEventListener('click', function(e
) {
129 $('close-button').addEventListener('mousedown', function(e
) {
133 $('minimize-button').addEventListener('click', function(e
) {
135 chrome
.app
.window
.current().minimize();
138 $('minimize-button').addEventListener('mousedown', function(e
) {
144 * @param {!CWSWidgetContainer.Result} result The result reported by the widget.
146 function showWidgetResult(result
) {
147 // TODO(tbarzic): Add some UI to show on success.
148 if (result
!= CWSWidgetContainer
.Result
.FAILED
) {
153 var dialog
= new CWSWidgetContainerErrorDialog($('widget-container-root'));
154 dialog
.show(getString('DEFAULT_ERROR_MESSAGE'),
159 /** Closes the current app window. */
160 function closeAppWindow() {
161 chrome
.app
.window
.current().close();
164 window
.addEventListener('DOMContentLoaded', function() {
165 initializeTopbarButtons();
167 chrome
.webstoreWidgetPrivate
.getStrings(function(strings
) {
168 loadTimeData
.data
= strings
;
169 i18nTemplate
.process(document
, loadTimeData
);
171 cr
.ui
.dialogs
.BaseDialog
.OK_LABEL
= getString('OK_BUTTON');
173 document
.title
= getString('TITLE_PRINTER_PROVIDERS');
174 $('title').textContent
= document
.title
;
176 if (!window
.params
) {
177 console
.error('Params not set!');
181 /** @type {!CWSWidgetContainer.PlatformDelegate} */
182 var platformDelegate
= createPlatformDelegate(strings
);
184 var root
= $('widget-container-root');
186 console
.error('No root element');
190 /** @type {!CWSWidgetContainer} */
191 var widgetContainer
= new CWSWidgetContainer(
192 document
, root
, platformDelegate
, {} /* state */);
194 widgetContainer
.ready()
195 /** @return {!Promise.<CWSWidgetContainer.ResolveReason>} */
197 return widgetContainer
.start(window
.params
.filter
,
198 window
.params
.webstoreUrl
);
200 /** @param {!CWSWidgetContainer.ResolveReason} reason */
201 .then(function(reason
) {
202 if (reason
!= CWSWidgetContainer
.ResolveReason
.DONE
)
205 var result
= widgetContainer
.finalizeAndGetResult();
206 showWidgetResult(result
.result
);
208 /** @param {*} error */
209 .catch(function(error
) {
210 showWidgetResult(CWSWidgetContainer
.Result
.FAILED
);