Reland "Non-SFI mode: Switch to newlib. (patchset #4 id:60001 of https://codereview...
[chromium-blink-merge.git] / components / chrome_apps / webstore_widget / app / main.js
blob1151da84a367588a199a5f6da0440fa529f86c89
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">
11 (function() {
12 'use strict';
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">
19 /**
20 * @type {?{
21 * filter: !Object<*>,
22 * webstoreUrl: ?string
23 * }}
25 window.params = window.params || null;
27 /**
28 * @param {string} id Element id.
29 * @return {HTMLElement} The found element, or null.
31 function $(id) {
32 return document.getElementById(id);
35 /**
36 * Default strings.
38 var defaultStrings = {
39 'language': 'en',
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'
47 /**
48 * @param {string} id The string id.
49 * @return {string}
51 function getString(id) {
52 return loadTimeData.getString(id) || defaultStrings[id] || '';
55 /**
56 * @param {Object<string>} strings Localized strings used by the container.
57 * @return {!CWSWidgetContainer.PlatformDelegate}
59 function createPlatformDelegate(strings) {
60 return {
61 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')
69 metricsImpl: {
70 /**
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) {}
87 /**
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(
94 itemId,
95 false,
96 function() {
97 callback(chrome.runtime.lastError ?
98 chrome.runtime.lastError.message || 'UNKNOWN_ERROR' : null);
99 });
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);
114 callback(null);
115 return;
117 callback(token);
123 function initializeTopbarButtons() {
124 $('close-button').addEventListener('click', function(e) {
125 e.preventDefault();
126 closeAppWindow();
129 $('close-button').addEventListener('mousedown', function(e) {
130 e.preventDefault();
133 $('minimize-button').addEventListener('click', function(e) {
134 e.preventDefault();
135 chrome.app.window.current().minimize();
138 $('minimize-button').addEventListener('mousedown', function(e) {
139 e.preventDefault();
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) {
149 closeAppWindow();
150 return;
153 var dialog = new CWSWidgetContainerErrorDialog($('widget-container-root'));
154 dialog.show(getString('DEFAULT_ERROR_MESSAGE'),
155 closeAppWindow,
156 closeAppWindow);
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!');
178 return;
181 /** @type {!CWSWidgetContainer.PlatformDelegate} */
182 var platformDelegate = createPlatformDelegate(strings);
184 var root = $('widget-container-root');
185 if (!root) {
186 console.error('No root element');
187 return;
190 /** @type {!CWSWidgetContainer} */
191 var widgetContainer = new CWSWidgetContainer(
192 document, root, platformDelegate, {} /* state */);
194 widgetContainer.ready()
195 /** @return {!Promise.<CWSWidgetContainer.ResolveReason>} */
196 .then(function() {
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)
203 return;
205 var result = widgetContainer.finalizeAndGetResult();
206 showWidgetResult(result.result);
208 /** @param {*} error */
209 .catch(function(error) {
210 showWidgetResult(CWSWidgetContainer.Result.FAILED);
214 })();