Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(371)

Unified Diff: components/chrome_apps/webstore_widget/app/main.js

Issue 1123373003: Launch webstore widget app when unsupported printer is plugged in (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@move_cws_widget_scripts
Patch Set: bump app version Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/chrome_apps/webstore_widget/app/main.js
diff --git a/components/chrome_apps/webstore_widget/app/main.js b/components/chrome_apps/webstore_widget/app/main.js
new file mode 100644
index 0000000000000000000000000000000000000000..aca72b3d642b7c262da309588ae230bd10b882f1
--- /dev/null
+++ b/components/chrome_apps/webstore_widget/app/main.js
@@ -0,0 +1,188 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+//<include src="../../../../ui/webui/resources/js/cr.js">
+//<include src="../../../../ui/webui/resources/js/load_time_data.js">
+//<include src="../../../../ui/webui/resources/js/i18n_template_no_process.js">
+//<include src="../../../../ui/webui/resources/js/cr/event_target.js">
+//<include src="../../../../ui/webui/resources/js/cr/ui/dialogs.js">
stevenjb 2015/05/14 23:28:04 Note: We should avoid committing commented out cod
Dan Beam 2015/05/14 23:59:31 these are functional. <include> is pre-processed.
+
+(function() {
+'use strict';
+
+//<include src="../cws_widget/app_installer.js">
+//<include src="../cws_widget/cws_webview_client.js">
+//<include src="../cws_widget/cws_widget_container.js">
+//<include src="../cws_widget/cws_widget_container_error_dialog.js">
+
+/**
+ * @type {?{
+ * filter: !Object.<string, *>,
+ * webstoreUrl: ?string
+ * }}
+ */
+window.params = window.params || null;
+
+/**
+ * @param {string} id Element id.
+ * @return {HTMLElement} The found element, or null.
+ */
+function $(id) {
+ return document.getElementById(id);
+}
+
+/**
+ * Default strings.
+ */
+var defaultStrings = {
+ 'language': 'en',
+ 'LINK_TO_WEBSTORE': '[LOCALIZE ME] Learn more...',
+ 'INSTALLATION_FAILED_MESSAGE': '[LOCALIZE ME] Installation failed!',
+ 'OK_BUTTON': '[LOCALIZE ME] OK',
+ 'TITLE_PRINTER_PROVIDERS': '[LOCALIZE ME] Select app for your printer'
+};
+
+/**
+ * @param {string} id The string id.
+ * @return {string}
+ */
+function getString(id) {
+ return loadTimeData.getString(id) || defaultStrings[id] || '';
+}
+
+/**
+ * @param {Object.<string, string>} strings Localized strings used by the
+ * container.
+ * @return {!CWSWidgetContainer.PlatformDelegate}
+ */
+function createPlatformDelegate(strings) {
+ return {
+ strings: {
+ UI_LOCALE: getString('language'),
+ LINK_TO_WEBSTORE: getString('LINK_TO_WEBSTORE'),
+ INSTALLATION_FAILED_MESSAGE: getString('INSTALLATION_FAILED_MESSAGE')
+ },
+
+ metricsImpl: {
+ /**
+ * @param {string} enumName
+ * @param {number} value
+ * @param {number} enumSize
+ */
+ recordEnum: function(enumName, value, enumSize) {},
+
+ /** @param {string} actionName */
+ recordUserAction: function(actionName) {},
+
+ /** @param {string} intervalName */
+ startInterval: function(intervalName) {},
+
+ /** @param {string} intervalName */
+ recordInterval: function(intervalName) {}
+ },
+
+ /**
+ * @param {string} itemId Item to be installed.
+ * @param {function(?string)} callback Callback param is the error message,
+ * which is set to null on success.
+ */
+ installWebstoreItem: function(itemId, callback) {
+ chrome.webstoreWidgetPrivate.installWebstoreItem(
+ itemId,
+ false,
+ function() {
+ callback(chrome.runtime.lastError ?
+ chrome.runtime.lastError.message || 'UNKNOWN_ERROR' : null);
+ });
+ },
+
+ /** @param {function(Array.<string>)} callback */
+ getInstalledItems: function(callback) { callback([]); },
+
+ /**
+ * @param {function(?string)} callback The argument is the fetche3d access
+ * token. Null on error.
+ */
+ requestWebstoreAccessToken: function(callback) {
+ chrome.fileManagerPrivate.requestWebStoreAccessToken(function(token) {
+ if (chrome.runtime.lastError) {
+ console.error('Error getting access token: ' +
+ chrome.runtime.lastError.message);
+ callback(null);
+ return;
+ }
+ callback(token);
+ });
+ }
+ };
+}
+
+function initializeTopbarButtons() {
+ $('close-button').addEventListener('click', function(e) {
+ e.preventDefault();
+ chrome.app.window.current().close();
+ });
+
+ $('close-button').addEventListener('mousedown', function(e) {
+ e.preventDefault();
+ });
+
+ $('minimize-button').addEventListener('click', function(e) {
+ e.preventDefault();
+ chrome.app.window.current().minimize();
+ });
+
+ $('minimize-button').addEventListener('mousedown', function(e) {
+ e.preventDefault();
+ });
+}
+
+window.addEventListener('DOMContentLoaded', function() {
+ initializeTopbarButtons();
+
+ chrome.webstoreWidgetPrivate.getStrings(function(strings) {
+ loadTimeData.data = strings;
+ i18nTemplate.process(document, loadTimeData);
+
+ cr.ui.dialogs.BaseDialog.OK_LABEL = getString('OK_BUTTON');
+
+ document.title = getString('TITLE_PRINTER_PROVIDERS');
+ $('title').textContent = document.title;
+
+ if (!window.params) {
+ console.error('Params not set!');
+ return;
+ }
+
+ /** @type {!CWSWidgetContainer.PlatformDelegate} */
+ var platformDelegate = createPlatformDelegate(strings);
+
+ var root = $('widget-container-root');
+ if (!root) {
+ console.error('No root element');
+ return;
+ }
+
+ /** @type {!CWSWidgetContainer} */
+ var widgetContainer = new CWSWidgetContainer(
+ document, root, platformDelegate, {} /* state */);
+
+ widgetContainer.ready()
+ /** @return {!Promise.<CWSWidgetContainer.ResolveReason>} */
+ .then(function() {
+ return widgetContainer.start(window.params.filter,
+ window.params.webstoreUrl);
+ })
+ /** @param {!CWSWidgetContainer.ResolveReason} reason */
+ .then(function(reason) {
+ chrome.app.window.current().close();
+ })
+ /** @param {*} error */
+ .catch(function(error) {
+ // TODO(tbarzic): Add error UI.
+ console.error(error);
+ });
+ });
+});
+})();

Powered by Google App Engine
This is Rietveld 408576698