Commit bfe56a30 authored by rbpotter's avatar rbpotter Committed by Commit Bot

Print Preview: Move user account management out of destination-settings

Rename print-preview-user-info to print-preview-user-manager and move
management of user account state to print-preview-user-manager,
instead of sharing this responsibility between user-info and
print-preview-destination-settings.

Remove cloudPrintState in favor of checking the activeUser and
a single cloudPrintDisabled boolean.

Finally, send user account updates from PrintPreviewHandler, instead
of just sending reload-print-destinations event.

This should not change any user visible behavior.

Bug: 958767
Change-Id: I4ff2545f78b35537a8cc54a8a11bf0c5aed9ae07
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1628330
Commit-Queue: Rebekah Potter <rbpotter@chromium.org>
Reviewed-by: default avatarDemetrios Papadopoulos <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#664533}
parent ba205d5d
...@@ -144,12 +144,13 @@ js_library("coordinate2d") { ...@@ -144,12 +144,13 @@ js_library("coordinate2d") {
] ]
} }
js_library("user_info") { js_library("user_manager") {
deps = [ deps = [
":destination_store", ":destination_store",
":invitation_store", ":invitation_store",
"..:cloud_print_interface", "..:cloud_print_interface",
"//ui/webui/resources/js:cr", "//ui/webui/resources/js:cr",
"//ui/webui/resources/js:event_tracker", "//ui/webui/resources/js:event_tracker",
"//ui/webui/resources/js:web_ui_listener_behavior",
] ]
} }
...@@ -281,7 +281,7 @@ cr.define('print_preview', function() { ...@@ -281,7 +281,7 @@ cr.define('print_preview', function() {
addListenerCallback('printers-added', this.onPrintersAdded_.bind(this)); addListenerCallback('printers-added', this.onPrintersAdded_.bind(this));
addListenerCallback( addListenerCallback(
'reload-printer-list', this.onDestinationsReload.bind(this)); 'user-accounts-updated', this.onDestinationsReload.bind(this));
} }
/** /**
......
...@@ -2,8 +2,9 @@ ...@@ -2,8 +2,9 @@
<link rel="import" href="chrome://resources/html/cr.html"> <link rel="import" href="chrome://resources/html/cr.html">
<link rel="import" href="chrome://resources/html/event_tracker.html"> <link rel="import" href="chrome://resources/html/event_tracker.html">
<link rel="import" href="chrome://resources/html/web_ui_listener_behavior.html">
<link rel="import" href="../cloud_print_interface.html"> <link rel="import" href="../cloud_print_interface.html">
<link rel="import" href="destination_store.html"> <link rel="import" href="destination_store.html">
<link rel="import" href="invitation_store.html"> <link rel="import" href="invitation_store.html">
<script src="user_info.js"></script> <script src="user_manager.js"></script>
...@@ -4,14 +4,6 @@ ...@@ -4,14 +4,6 @@
cr.exportPath('print_preview'); cr.exportPath('print_preview');
/** @enum {number} */
print_preview.CloudPrintState = {
DISABLED: 0,
ENABLED: 1,
SIGNED_IN: 2,
NOT_SIGNED_IN: 3,
};
(function() { (function() {
'use strict'; 'use strict';
...@@ -22,7 +14,9 @@ print_preview.CloudPrintState = { ...@@ -22,7 +14,9 @@ print_preview.CloudPrintState = {
let UpdateUsersPayload; let UpdateUsersPayload;
Polymer({ Polymer({
is: 'print-preview-user-info', is: 'print-preview-user-manager',
behaviors: [WebUIListenerBehavior],
properties: { properties: {
activeUser: { activeUser: {
...@@ -30,12 +24,22 @@ Polymer({ ...@@ -30,12 +24,22 @@ Polymer({
notify: true, notify: true,
}, },
appKioskMode: Boolean,
cloudPrintDisabled: {
type: Boolean,
value: true,
notify: true,
},
/** @type {?print_preview.DestinationStore} */ /** @type {?print_preview.DestinationStore} */
destinationStore: Object, destinationStore: Object,
/** @type {?print_preview.InvitationStore} */ /** @type {?print_preview.InvitationStore} */
invitationStore: Object, invitationStore: Object,
shouldReloadCookies: Boolean,
/** @type {!Array<string>} */ /** @type {!Array<string>} */
users: { users: {
type: Array, type: Array,
...@@ -46,12 +50,33 @@ Polymer({ ...@@ -46,12 +50,33 @@ Polymer({
}, },
}, },
/** @private {boolean} */
initialized_: false,
/** @private {!EventTracker} */ /** @private {!EventTracker} */
tracker_: new EventTracker(), tracker_: new EventTracker(),
/** @override */ /** @override */
detached: function() { detached: function() {
this.tracker_.removeAll(); this.tracker_.removeAll();
this.initialized_ = false;
},
/** @param {?Array<string>} userAccounts */
initUserAccounts: function(userAccounts) {
assert(!this.initialized_);
this.initialized_ = true;
if (!userAccounts) {
assert(this.cloudPrintDisabled);
return;
}
// If cloud print is enabled, listen for account changes.
assert(!this.cloudPrintDisabled);
this.addWebUIListener(
'user-accounts-updated', this.updateUsers_.bind(this));
this.updateUsers_(userAccounts);
}, },
/** @param {!cloudprint.CloudPrintInterface} cloudPrintInterface */ /** @param {!cloudprint.CloudPrintInterface} cloudPrintInterface */
...@@ -60,6 +85,34 @@ Polymer({ ...@@ -60,6 +85,34 @@ Polymer({
cloudPrintInterface.getEventTarget(), cloudPrintInterface.getEventTarget(),
cloudprint.CloudPrintInterfaceEventType.UPDATE_USERS, cloudprint.CloudPrintInterfaceEventType.UPDATE_USERS,
this.onCloudPrintUpdateUsers_.bind(this)); this.onCloudPrintUpdateUsers_.bind(this));
[cloudprint.CloudPrintInterfaceEventType.SEARCH_FAILED,
cloudprint.CloudPrintInterfaceEventType.PRINTER_FAILED,
].forEach(eventType => {
this.tracker_.add(
cloudPrintInterface.getEventTarget(), eventType,
this.checkCloudPrintStatus_.bind(this));
});
assert(this.cloudPrintDisabled);
this.cloudPrintDisabled = false;
},
/**
* Updates the cloud print status to NOT_SIGNED_IN if there is an
* authentication error.
* @param {!CustomEvent<!cloudprint.CloudPrintInterfaceErrorEventDetail>}
* event Contains the error status
* @private
*/
checkCloudPrintStatus_: function(event) {
if (event.detail.status != 403 || this.appKioskMode) {
return;
}
// Should not have sent a message to Cloud Print if cloud print is
// disabled.
assert(!this.cloudPrintDisabled);
this.updateActiveUser('');
console.warn('Google Cloud Print Error: HTTP status 403');
}, },
/** /**
...@@ -68,26 +121,35 @@ Polymer({ ...@@ -68,26 +121,35 @@ Polymer({
* @private * @private
*/ */
onCloudPrintUpdateUsers_: function(e) { onCloudPrintUpdateUsers_: function(e) {
this.updateActiveUser(e.detail.activeUser, false); this.updateActiveUser(e.detail.activeUser);
if (e.detail.users) { if (e.detail.users) {
this.updateUsers(e.detail.users); this.updateUsers_(e.detail.users);
} }
}, },
/** @param {!Array<string>} users The full list of signed in users. */ /**
updateUsers: function(users) { * @param {!Array<string>} users The full list of signed in users.
* @private
*/
updateUsers_: function(users) {
const updateActiveUser = (users.length > 0 && this.users.length === 0) ||
!users.includes(this.activeUser);
this.users = users; this.users = users;
if (updateActiveUser) {
this.updateActiveUser(this.users[0] || '');
}
}, },
/** /** @param {string} user The new active user. */
* @param {string} user The new active user. updateActiveUser: function(user) {
* @param {boolean} reloadCookies Whether to reload cookie based destinations if (user === this.activeUser) {
* and invitations. return;
*/ }
updateActiveUser: function(user, reloadCookies) {
this.destinationStore.setActiveUser(user); this.destinationStore.setActiveUser(user);
this.activeUser = user; this.activeUser = user;
if (!reloadCookies) {
if (!this.shouldReloadCookies || !user) {
return; return;
} }
......
...@@ -115,11 +115,11 @@ ...@@ -115,11 +115,11 @@
<structure name="IDR_PRINT_PREVIEW_DATA_MARGINS_JS" <structure name="IDR_PRINT_PREVIEW_DATA_MARGINS_JS"
file="data/margins.js" file="data/margins.js"
type="chrome_html" /> type="chrome_html" />
<structure name="IDR_PRINT_PREVIEW_DATA_USER_INFO_HTML" <structure name="IDR_PRINT_PREVIEW_DATA_USER_MANAGER_HTML"
file="data/user_info.html" file="data/user_manager.html"
type="chrome_html" /> type="chrome_html" />
<structure name="IDR_PRINT_PREVIEW_DATA_USER_INFO_JS" <structure name="IDR_PRINT_PREVIEW_DATA_USER_MANAGER_JS"
file="data/user_info.js" file="data/user_manager.js"
type="chrome_html" /> type="chrome_html" />
<structure name="IDR_PRINT_PREVIEW_METRICS_HTML" <structure name="IDR_PRINT_PREVIEW_METRICS_HTML"
file="metrics.html" file="metrics.html"
......
...@@ -138,7 +138,7 @@ js_library("destination_settings") { ...@@ -138,7 +138,7 @@ js_library("destination_settings") {
"../data:destination_store", "../data:destination_store",
"../data:invitation_store", "../data:invitation_store",
"../data:state", "../data:state",
"../data:user_info", "../data:user_manager",
"//ui/webui/resources/cr_elements/cr_lazy_render:cr_lazy_render", "//ui/webui/resources/cr_elements/cr_lazy_render:cr_lazy_render",
"//ui/webui/resources/js:i18n_behavior", "//ui/webui/resources/js:i18n_behavior",
"//ui/webui/resources/js:web_ui_listener_behavior", "//ui/webui/resources/js:web_ui_listener_behavior",
...@@ -150,7 +150,6 @@ js_library("destination_select") { ...@@ -150,7 +150,6 @@ js_library("destination_select") {
":select_behavior", ":select_behavior",
"..:print_preview_utils", "..:print_preview_utils",
"../data:destination", "../data:destination",
"../data:user_info",
"//third_party/polymer/v1_0/components-chromium/iron-meta:iron-meta-extracted", "//third_party/polymer/v1_0/components-chromium/iron-meta:iron-meta-extracted",
"//ui/webui/resources/js:i18n_behavior", "//ui/webui/resources/js:i18n_behavior",
] ]
...@@ -370,7 +369,6 @@ js_library("destination_dialog") { ...@@ -370,7 +369,6 @@ js_library("destination_dialog") {
"../data:destination_store", "../data:destination_store",
"../data:invitation", "../data:invitation",
"../data:invitation_store", "../data:invitation_store",
"../data:user_info",
"//ui/webui/resources/cr_elements/cr_dialog:cr_dialog", "//ui/webui/resources/cr_elements/cr_dialog:cr_dialog",
"//ui/webui/resources/js:event_tracker", "//ui/webui/resources/js:event_tracker",
"//ui/webui/resources/js:i18n_behavior", "//ui/webui/resources/js:i18n_behavior",
......
...@@ -25,7 +25,6 @@ ...@@ -25,7 +25,6 @@
<link rel="import" href="../data/destination_store.html"> <link rel="import" href="../data/destination_store.html">
<link rel="import" href="../data/invitation.html"> <link rel="import" href="../data/invitation.html">
<link rel="import" href="../data/invitation_store.html"> <link rel="import" href="../data/invitation_store.html">
<link rel="import" href="../data/user_info.html">
<link rel="import" href="destination_list.html"> <link rel="import" href="destination_list.html">
<link rel="import" href="print_preview_search_box.html"> <link rel="import" href="print_preview_search_box.html">
<link rel="import" href="print_preview_shared_css.html"> <link rel="import" href="print_preview_shared_css.html">
...@@ -189,10 +188,9 @@ ...@@ -189,10 +188,9 @@
</paper-button> </paper-button>
</div> </div>
<div id="promos" slot="footer" hidden="[[!shouldShowFooter_( <div id="promos" slot="footer" hidden="[[!shouldShowFooter_(
cloudPrintPromoDismissed_, cloudPrintState, invitation_)]]"> shouldShowCloudPrintPromo_, invitation_)]]">
<div class="promo" id="cloudprintPromo" <div class="promo" id="cloudprintPromo"
hidden$="[[!shouldShowCloudPrintPromo_( hidden$="[[!shouldShowCloudPrintPromo_]]">
cloudPrintPromoDismissed_, cloudPrintState)]]">
<iron-icon icon="print-preview:cloud-queue" alt=""></iron-icon> <iron-icon icon="print-preview:cloud-queue" alt=""></iron-icon>
<div class="promo-text"></div> <div class="promo-text"></div>
<cr-icon-button id="cloudPrintClose" class="icon-clear" <cr-icon-button id="cloudPrintClose" class="icon-clear"
......
...@@ -20,7 +20,10 @@ Polymer({ ...@@ -20,7 +20,10 @@ Polymer({
observer: 'onInvitationStoreSet_', observer: 'onInvitationStoreSet_',
}, },
activeUser: String, activeUser: {
type: String,
observer: 'updateDestinationsAndInvitations_',
},
currentDestinationAccount: String, currentDestinationAccount: String,
...@@ -33,11 +36,7 @@ Polymer({ ...@@ -33,11 +36,7 @@ Polymer({
value: null, value: null,
}, },
/** @type {!print_preview.CloudPrintState} */ cloudPrintDisabled: Boolean,
cloudPrintState: {
type: Number,
observer: 'onCloudPrintStateChanged_',
},
/** @private */ /** @private */
cloudPrintPromoDismissed_: { cloudPrintPromoDismissed_: {
...@@ -62,6 +61,14 @@ Polymer({ ...@@ -62,6 +61,14 @@ Polymer({
type: Object, type: Object,
value: null, value: null,
}, },
/** @private {boolean} */
shouldShowCloudPrintPromo_: {
type: Boolean,
computed: 'computeShouldShowCloudPrintPromo_(' +
'cloudPrintDisabled, activeUser, cloudPrintPromoDismissed_)',
observer: 'onShouldShowCloudPrintPromoChanged_',
},
}, },
listeners: { listeners: {
...@@ -79,6 +86,9 @@ Polymer({ ...@@ -79,6 +86,9 @@ Polymer({
destinationInConfiguring_: null, destinationInConfiguring_: null,
// </if> // </if>
/** @private {boolean} */
initialized_: false,
/** @override */ /** @override */
ready: function() { ready: function() {
this.$$('.promo-text').innerHTML = this.$$('.promo-text').innerHTML =
...@@ -133,7 +143,8 @@ Polymer({ ...@@ -133,7 +143,8 @@ Polymer({
this.tracker_.add( this.tracker_.add(
destinationStore, destinationStore,
print_preview.DestinationStore.EventType.DESTINATION_SEARCH_DONE, print_preview.DestinationStore.EventType.DESTINATION_SEARCH_DONE,
this.onDestinationSearchDone_.bind(this)); this.updateDestinationsAndInvitations_.bind(this));
this.initialized_ = true;
}, },
/** @private */ /** @private */
...@@ -150,9 +161,13 @@ Polymer({ ...@@ -150,9 +161,13 @@ Polymer({
}, },
/** @private */ /** @private */
onDestinationSearchDone_: function() { updateDestinationsAndInvitations_: function() {
if (!this.initialized_) {
return;
}
this.updateDestinations_(); this.updateDestinations_();
if (this.activeUser) { if (this.activeUser && !!this.invitationStore) {
this.invitationStore.startLoadingInvitations(this.activeUser); this.invitationStore.startLoadingInvitations(this.activeUser);
} }
}, },
...@@ -371,7 +386,6 @@ Polymer({ ...@@ -371,7 +386,6 @@ Polymer({
const select = this.$$('select'); const select = this.$$('select');
const account = select.value; const account = select.value;
if (account) { if (account) {
this.showCloudPrintPromo = false;
this.loadingDestinations_ = true; this.loadingDestinations_ = true;
this.destinations_ = []; this.destinations_ = [];
this.fire('account-change', account); this.fire('account-change', account);
...@@ -391,30 +405,29 @@ Polymer({ ...@@ -391,30 +405,29 @@ Polymer({
} }
}, },
/** @private */
onCloudPrintStateChanged_: function() {
if (this.cloudPrintState === print_preview.CloudPrintState.NOT_SIGNED_IN) {
this.metrics_.record(
print_preview.Metrics.DestinationSearchBucket.SIGNIN_PROMPT);
}
},
/** /**
* @return {boolean} Whether to show the cloud print promo. * @return {boolean} Whether to show the cloud print promo.
* @private * @private
*/ */
shouldShowCloudPrintPromo_: function() { computeShouldShowCloudPrintPromo_: function() {
return this.cloudPrintState === return !this.activeUser && !this.cloudPrintDisabled &&
print_preview.CloudPrintState.NOT_SIGNED_IN &&
!this.cloudPrintPromoDismissed_; !this.cloudPrintPromoDismissed_;
}, },
/** @private */
onShouldShowCloudPrintPromoChanged_: function() {
if (this.shouldShowCloudPrintPromo_) {
this.metrics_.record(
print_preview.Metrics.DestinationSearchBucket.SIGNIN_PROMPT);
}
},
/** /**
* @return {boolean} Whether to show the footer. * @return {boolean} Whether to show the footer.
* @private * @private
*/ */
shouldShowFooter_: function() { shouldShowFooter_: function() {
return this.shouldShowCloudPrintPromo_() || !!this.invitation_; return this.shouldShowCloudPrintPromo_ || !!this.invitation_;
}, },
/** @private */ /** @private */
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
<link rel="import" href="../icons.html"> <link rel="import" href="../icons.html">
<link rel="import" href="../print_preview_utils.html"> <link rel="import" href="../print_preview_utils.html">
<link rel="import" href="../data/destination.html"> <link rel="import" href="../data/destination.html">
<link rel="import" href="../data/user_info.html">
<link rel="import" href="print_preview_shared_css.html"> <link rel="import" href="print_preview_shared_css.html">
<link rel="import" href="select_behavior.html"> <link rel="import" href="select_behavior.html">
<link rel="import" href="strings.html"> <link rel="import" href="strings.html">
...@@ -49,7 +48,7 @@ ...@@ -49,7 +48,7 @@
$i18n{printToPDF} $i18n{printToPDF}
</option> </option>
<option value="[[getGoogleDriveDestinationKey_(activeUser)]]" <option value="[[getGoogleDriveDestinationKey_(activeUser)]]"
hidden$="[[!showGoogleDrive_]]"> hidden$="[[!activeUser]]">
$i18n{printToGoogleDrive} $i18n{printToGoogleDrive}
</option> </option>
<if expr="chromeos"> <if expr="chromeos">
......
...@@ -12,9 +12,6 @@ Polymer({ ...@@ -12,9 +12,6 @@ Polymer({
appKioskMode: Boolean, appKioskMode: Boolean,
/** @type {!print_preview.CloudPrintState} */
cloudPrintState: Number,
dark: Boolean, dark: Boolean,
/** @type {!print_preview.Destination} */ /** @type {!print_preview.Destination} */
...@@ -26,10 +23,6 @@ Polymer({ ...@@ -26,10 +23,6 @@ Polymer({
/** @type {!Array<!print_preview.RecentDestination>} */ /** @type {!Array<!print_preview.RecentDestination>} */
recentDestinationList: Array, recentDestinationList: Array,
/** @private {boolean} */
showGoogleDrive_:
{type: Boolean, computed: 'computeShowGoogleDrive_(cloudPrintState)'},
}, },
/** @private {!IronMetaElement} */ /** @private {!IronMetaElement} */
...@@ -45,14 +38,6 @@ Polymer({ ...@@ -45,14 +38,6 @@ Polymer({
this.selectedValue = this.destination.key; this.selectedValue = this.destination.key;
}, },
/**
* @return {boolean} Whether to show the Google Drive option.
* @private
*/
computeShowGoogleDrive_: function() {
return this.cloudPrintState === print_preview.CloudPrintState.SIGNED_IN;
},
/** /**
* @return {string} Unique identifier for the Save as PDF destination * @return {string} Unique identifier for the Save as PDF destination
* @private * @private
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
<link rel="import" href="../data/destination_store.html"> <link rel="import" href="../data/destination_store.html">
<link rel="import" href="../data/invitation_store.html"> <link rel="import" href="../data/invitation_store.html">
<link rel="import" href="../data/state.html"> <link rel="import" href="../data/state.html">
<link rel="import" href="../data/user_info.html"> <link rel="import" href="../data/user_manager.html">
<link rel="import" href="destination_dialog.html"> <link rel="import" href="destination_dialog.html">
<link rel="import" href="destination_select.html"> <link rel="import" href="destination_select.html">
<link rel="import" href="print_preview_shared_css.html"> <link rel="import" href="print_preview_shared_css.html">
...@@ -58,10 +58,13 @@ ...@@ -58,10 +58,13 @@
min-height: 0; min-height: 0;
} }
</style> </style>
<print-preview-user-info id="userInfo" active-user="{{activeUser_}}" <print-preview-user-manager id="userManager" active-user="{{activeUser_}}"
app-kiosk-mode="[[appKioskMode]]"
cloud-print-disabled="{{cloudPrintDisabled_}}"
users="{{users_}}" destination-store="[[destinationStore_]]" users="{{users_}}" destination-store="[[destinationStore_]]"
invitation-store="[[invitationStore_]]"> invitation-store="[[invitationStore_]]"
</print-preview-user-info> should-reload-cookies="[[isDialogOpen_]]">
</print-preview-user-manager>
<print-preview-settings-section> <print-preview-settings-section>
<span slot="title">$i18n{destinationLabel}</span> <span slot="title">$i18n{destinationLabel}</span>
<div slot="controls"> <div slot="controls">
...@@ -71,8 +74,7 @@ ...@@ -71,8 +74,7 @@
<print-preview-destination-select id="destinationSelect" <print-preview-destination-select id="destinationSelect"
hidden$="[[!shouldHideSpinner_]]" hidden$="[[!shouldHideSpinner_]]"
active-user="[[activeUser_]]" active-user="[[activeUser_]]"
app-kiosk-mode="[[appKioskMode]]" app-kiosk-mode="[[appKioskMode]]" dark="[[dark]]"
cloud-print-state="[[cloudPrintState_]]" dark="[[dark]]"
destination="[[destination]]" destination="[[destination]]"
disabled="[[shouldDisableDropdown_( disabled="[[shouldDisableDropdown_(
destinationState, state, disabled)]]" destinationState, state, disabled)]]"
...@@ -92,7 +94,7 @@ ...@@ -92,7 +94,7 @@
<cr-lazy-render id="destinationDialog"> <cr-lazy-render id="destinationDialog">
<template> <template>
<print-preview-destination-dialog <print-preview-destination-dialog
cloud-print-state="[[cloudPrintState_]]" cloud-print-disabled="[[cloudPrintDisabled_]]"
destination-store="[[destinationStore_]]" destination-store="[[destinationStore_]]"
invitation-store="[[invitationStore_]]" invitation-store="[[invitationStore_]]"
recent-destination-list="[[recentDestinationList_]]" recent-destination-list="[[recentDestinationList_]]"
......
...@@ -72,11 +72,8 @@ Polymer({ ...@@ -72,11 +72,8 @@ Polymer({
observer: 'onActiveUserChanged_', observer: 'onActiveUserChanged_',
}, },
/** @private {!print_preview.CloudPrintState} */ /** @private {boolean} */
cloudPrintState_: { cloudPrintDisabled_: Boolean,
type: Number,
value: print_preview.CloudPrintState.DISABLED,
},
/** @private {?print_preview.DestinationStore} */ /** @private {?print_preview.DestinationStore} */
destinationStore_: { destinationStore_: {
...@@ -98,6 +95,12 @@ Polymer({ ...@@ -98,6 +95,12 @@ Polymer({
value: null, value: null,
}, },
/** @private {boolean} */
isDialogOpen_: {
type: Boolean,
value: false,
},
/** @private {boolean} */ /** @private {boolean} */
noDestinations_: { noDestinations_: {
type: Boolean, type: Boolean,
...@@ -164,18 +167,9 @@ Polymer({ ...@@ -164,18 +167,9 @@ Polymer({
/** @private */ /** @private */
onCloudPrintInterfaceSet_: function() { onCloudPrintInterfaceSet_: function() {
const cloudPrintInterface = assert(this.cloudPrintInterface); const cloudPrintInterface = assert(this.cloudPrintInterface);
[cloudprint.CloudPrintInterfaceEventType.SEARCH_FAILED, this.$.userManager.setCloudPrintInterface(cloudPrintInterface);
cloudprint.CloudPrintInterfaceEventType.PRINTER_FAILED,
].forEach(eventType => {
this.tracker_.add(
cloudPrintInterface.getEventTarget(), eventType,
this.checkCloudPrintStatus_.bind(this));
});
this.$.userInfo.setCloudPrintInterface(cloudPrintInterface);
this.destinationStore_.setCloudPrintInterface(cloudPrintInterface); this.destinationStore_.setCloudPrintInterface(cloudPrintInterface);
this.invitationStore_.setCloudPrintInterface(cloudPrintInterface); this.invitationStore_.setCloudPrintInterface(cloudPrintInterface);
assert(this.cloudPrintState_ === print_preview.CloudPrintState.DISABLED);
this.cloudPrintState_ = print_preview.CloudPrintState.ENABLED;
}, },
/** /**
...@@ -186,13 +180,7 @@ Polymer({ ...@@ -186,13 +180,7 @@ Polymer({
*/ */
init: function( init: function(
defaultPrinter, serializedDefaultDestinationRulesStr, userAccounts) { defaultPrinter, serializedDefaultDestinationRulesStr, userAccounts) {
if (!!userAccounts && userAccounts.length > 0) { this.$.userManager.initUserAccounts(userAccounts);
this.$.userInfo.updateActiveUser(userAccounts[0], false);
this.$.userInfo.updateUsers(userAccounts);
} else if (userAccounts) {
this.cloudPrintState_ = print_preview.CloudPrintState.NOT_SIGNED_IN;
}
this.destinationStore_.init( this.destinationStore_.init(
this.appKioskMode, defaultPrinter, serializedDefaultDestinationRulesStr, this.appKioskMode, defaultPrinter, serializedDefaultDestinationRulesStr,
/** @type {!Array<print_preview.RecentDestination>} */ /** @type {!Array<print_preview.RecentDestination>} */
...@@ -201,13 +189,10 @@ Polymer({ ...@@ -201,13 +189,10 @@ Polymer({
/** @private */ /** @private */
onActiveUserChanged_: function() { onActiveUserChanged_: function() {
if (!this.activeUser_) { if (!this.activeUser_ || !this.destination) {
return; return;
} }
assert(this.cloudPrintState_ !== print_preview.CloudPrintState.DISABLED);
this.cloudPrintState_ = print_preview.CloudPrintState.SIGNED_IN;
if (this.destinationState === print_preview.DestinationState.SELECTED && if (this.destinationState === print_preview.DestinationState.SELECTED &&
this.destination.origin === print_preview.DestinationOrigin.COOKIES) { this.destination.origin === print_preview.DestinationOrigin.COOKIES) {
// Adjust states if the destination is now ready to be printed to. // Adjust states if the destination is now ready to be printed to.
...@@ -225,7 +210,7 @@ Polymer({ ...@@ -225,7 +210,7 @@ Polymer({
return; return;
} }
const destination = this.destinationStore_.selectedDestination; const destination = this.destinationStore_.selectedDestination;
if (this.cloudPrintState_ === print_preview.CloudPrintState.SIGNED_IN || if (!!this.activeUser_ ||
destination.origin !== print_preview.DestinationOrigin.COOKIES) { destination.origin !== print_preview.DestinationOrigin.COOKIES) {
this.destinationState = print_preview.DestinationState.SET; this.destinationState = print_preview.DestinationState.SET;
} else { } else {
...@@ -280,25 +265,6 @@ Polymer({ ...@@ -280,25 +265,6 @@ Polymer({
} }
}, },
/**
* Updates the cloud print status to NOT_SIGNED_IN if there is an
* authentication error.
* @param {!CustomEvent<!cloudprint.CloudPrintInterfaceErrorEventDetail>}
* event Contains the error status
* @private
*/
checkCloudPrintStatus_: function(event) {
if (event.detail.status != 403 || this.appKioskMode) {
return;
}
// Should not have sent a message to Cloud Print if cloud print is
// disabled.
assert(this.cloudPrintState_ !== print_preview.CloudPrintState.DISABLED);
this.cloudPrintState_ = print_preview.CloudPrintState.NOT_SIGNED_IN;
console.warn('Google Cloud Print Error: HTTP status 403');
},
/** /**
* @param {!print_preview.RecentDestination} destination * @param {!print_preview.RecentDestination} destination
* @return {boolean} Whether the destination is Save as PDF or Save to Drive. * @return {boolean} Whether the destination is Save as PDF or Save to Drive.
...@@ -422,6 +388,7 @@ Polymer({ ...@@ -422,6 +388,7 @@ Polymer({
this.invitationStore_.startLoadingInvitations(this.activeUser_); this.invitationStore_.startLoadingInvitations(this.activeUser_);
} }
this.$.destinationDialog.get().show(); this.$.destinationDialog.get().show();
this.isDialogOpen_ = true;
} else { } else {
const success = this.destinationStore_.selectRecentDestinationByKey( const success = this.destinationStore_.selectRecentDestinationByKey(
value, this.displayedDestinations_); value, this.displayedDestinations_);
...@@ -437,7 +404,7 @@ Polymer({ ...@@ -437,7 +404,7 @@ Polymer({
* @private * @private
*/ */
onAccountChange_: function(e) { onAccountChange_: function(e) {
this.$.userInfo.updateActiveUser(e.detail, true); this.$.userManager.updateActiveUser(e.detail, true);
}, },
/** @private */ /** @private */
...@@ -446,6 +413,7 @@ Polymer({ ...@@ -446,6 +413,7 @@ Polymer({
// selecting a new destination. // selecting a new destination.
this.updateDestinationSelect_(); this.updateDestinationSelect_();
this.$.destinationSelect.focus(); this.$.destinationSelect.focus();
this.isDialogOpen_ = false;
}, },
/** @private */ /** @private */
......
...@@ -1108,7 +1108,13 @@ WebContents* PrintPreviewHandler::GetInitiator() const { ...@@ -1108,7 +1108,13 @@ WebContents* PrintPreviewHandler::GetInitiator() const {
void PrintPreviewHandler::OnAccountsInCookieUpdated( void PrintPreviewHandler::OnAccountsInCookieUpdated(
const identity::AccountsInCookieJarInfo& accounts_in_cookie_jar_info, const identity::AccountsInCookieJarInfo& accounts_in_cookie_jar_info,
const GoogleServiceAuthError& error) { const GoogleServiceAuthError& error) {
FireWebUIListener("reload-printer-list"); base::Value account_list(base::Value::Type::LIST);
const std::vector<gaia::ListedAccount>& accounts =
accounts_in_cookie_jar_info.signed_in_accounts;
for (const auto account : accounts) {
account_list.GetList().emplace_back(account.email);
}
FireWebUIListener("user-accounts-updated", std::move(account_list));
} }
void PrintPreviewHandler::OnPrintPreviewReady(int preview_uid, int request_id) { void PrintPreviewHandler::OnPrintPreviewReady(int preview_uid, int request_id) {
......
...@@ -9,7 +9,7 @@ cr.define('print_preview', function() { ...@@ -9,7 +9,7 @@ cr.define('print_preview', function() {
*/ */
class CloudPrintInterfaceStub extends TestBrowserProxy { class CloudPrintInterfaceStub extends TestBrowserProxy {
constructor() { constructor() {
super(['printer', 'submit']); super(['printer', 'search', 'submit']);
/** @private {!cr.EventTarget} */ /** @private {!cr.EventTarget} */
this.eventTarget_ = new cr.EventTarget(); this.eventTarget_ = new cr.EventTarget();
...@@ -48,6 +48,7 @@ cr.define('print_preview', function() { ...@@ -48,6 +48,7 @@ cr.define('print_preview', function() {
* @override * @override
*/ */
search(account) { search(account) {
this.methodCalled('search', account);
this.searchInProgress_ = true; this.searchInProgress_ = true;
const printers = []; const printers = [];
this.cloudPrintersMap_.forEach((value) => { this.cloudPrintersMap_.forEach((value) => {
......
...@@ -171,7 +171,7 @@ cr.define('destination_dialog_test', function() { ...@@ -171,7 +171,7 @@ cr.define('destination_dialog_test', function() {
const whenReset = test_util.eventToPromise( const whenReset = test_util.eventToPromise(
print_preview.DestinationStore.EventType.DESTINATIONS_RESET, print_preview.DestinationStore.EventType.DESTINATIONS_RESET,
destinationStore); destinationStore);
cr.webUIListenerCallback('reload-printer-list'); cr.webUIListenerCallback('user-accounts-updated', ['foo@chromium.org']);
return whenReset.then(() => { return whenReset.then(() => {
Polymer.dom.flush(); Polymer.dom.flush();
const printerItems = dialog.$.printList.shadowRoot.querySelectorAll( const printerItems = dialog.$.printList.shadowRoot.querySelectorAll(
...@@ -235,13 +235,13 @@ cr.define('destination_dialog_test', function() { ...@@ -235,13 +235,13 @@ cr.define('destination_dialog_test', function() {
// Check that both cloud print promo and dropdown are hidden when cloud // Check that both cloud print promo and dropdown are hidden when cloud
// print is disabled. // print is disabled.
dialog.cloudPrintState = print_preview.CloudPrintState.DISABLED; dialog.cloudPrintDisabled = true;
assertTrue(dialog.$.cloudprintPromo.hidden); assertTrue(dialog.$.cloudprintPromo.hidden);
assertTrue(dialog.$$('.user-info').hidden); assertTrue(dialog.$$('.user-info').hidden);
const userSelect = dialog.$$('.md-select'); const userSelect = dialog.$$('.md-select');
// Enable cloud print. // Enable cloud print.
dialog.cloudPrintState = print_preview.CloudPrintState.NOT_SIGNED_IN; dialog.cloudPrintDisabled = false;
assertSignedInState('', 0); assertSignedInState('', 0);
// 6 printers, no Google drive (since not signed in). // 6 printers, no Google drive (since not signed in).
...@@ -254,7 +254,6 @@ cr.define('destination_dialog_test', function() { ...@@ -254,7 +254,6 @@ cr.define('destination_dialog_test', function() {
.then(addAccount => { .then(addAccount => {
assertFalse(addAccount); assertFalse(addAccount);
nativeLayer.resetResolver('signIn'); nativeLayer.resetResolver('signIn');
dialog.cloudPrintState = print_preview.CloudPrintState.SIGNED_IN;
dialog.activeUser = user1; dialog.activeUser = user1;
dialog.users = [user1]; dialog.users = [user1];
Polymer.dom.flush(); Polymer.dom.flush();
...@@ -294,8 +293,8 @@ cr.define('destination_dialog_test', function() { ...@@ -294,8 +293,8 @@ cr.define('destination_dialog_test', function() {
// Check printers were temporarily cleared. // Check printers were temporarily cleared.
assertNumPrintersWithDriveAccount(0, ''); assertNumPrintersWithDriveAccount(0, '');
// This will all be done by app.js and user_info.js in response to // This will all be done by app.js and user_manager.js in response
// the account-change event. // to the account-change event.
destinationStore.setActiveUser(user2); destinationStore.setActiveUser(user2);
dialog.activeUser = user2; dialog.activeUser = user2;
const whenInserted = test_util.eventToPromise( const whenInserted = test_util.eventToPromise(
......
...@@ -44,6 +44,7 @@ cr.define('destination_select_test', function() { ...@@ -44,6 +44,7 @@ cr.define('destination_select_test', function() {
/** @override */ /** @override */
setup(function() { setup(function() {
initialSettings = print_preview_test_utils.getDefaultInitialSettings(); initialSettings = print_preview_test_utils.getDefaultInitialSettings();
initialSettings.userAccounts = [];
nativeLayer = new print_preview.NativeLayerStub(); nativeLayer = new print_preview.NativeLayerStub();
localDestinations = []; localDestinations = [];
destinations = print_preview_test_utils.getDestinations( destinations = print_preview_test_utils.getDestinations(
......
...@@ -78,6 +78,7 @@ cr.define('destination_settings_test', function() { ...@@ -78,6 +78,7 @@ cr.define('destination_settings_test', function() {
// Initial state: No destination store means that there is no destination // Initial state: No destination store means that there is no destination
// yet, so the dropdown is hidden. // yet, so the dropdown is hidden.
assertTrue(dropdown.hidden); assertTrue(dropdown.hidden);
destinationSettings.cloudPrintInterface = cloudPrintInterface;
// Set up the destination store, but no destination yet. Dropdown is still // Set up the destination store, but no destination yet. Dropdown is still
// hidden. // hidden.
...@@ -175,11 +176,9 @@ cr.define('destination_settings_test', function() { ...@@ -175,11 +176,9 @@ cr.define('destination_settings_test', function() {
/** Simulates a user signing in to Chrome. */ /** Simulates a user signing in to Chrome. */
function signIn() { function signIn() {
destinationSettings.$.userInfo.updateActiveUser(defaultUser, false);
destinationSettings.$.userInfo.updateUsers([defaultUser]);
cloudPrintInterface.setPrinter( cloudPrintInterface.setPrinter(
print_preview_test_utils.getGoogleDriveDestination(defaultUser)); print_preview_test_utils.getGoogleDriveDestination(defaultUser));
cr.webUIListenerCallback('reload-printer-list'); cr.webUIListenerCallback('user-accounts-updated', [defaultUser]);
Polymer.dom.flush(); Polymer.dom.flush();
} }
...@@ -570,10 +569,13 @@ cr.define('destination_settings_test', function() { ...@@ -570,10 +569,13 @@ cr.define('destination_settings_test', function() {
const dialog = const dialog =
destinationSettings.$$('print-preview-destination-dialog'); destinationSettings.$$('print-preview-destination-dialog');
assertTrue(dialog.isOpen()); assertTrue(dialog.isOpen());
const whenAdded = test_util.eventToPromise(
print_preview.DestinationStore.EventType.DESTINATIONS_INSERTED,
destinationSettings.destinationStore_);
// Simulate setting a new account. // Simulate setting a new account.
dialog.fire('account-change', account2); dialog.fire('account-change', account2);
Polymer.dom.flush(); Polymer.dom.flush();
return test_util.waitForRender(destinationSettings); return whenAdded;
}) })
.then(() => { .then(() => {
assertDropdownItems([ assertDropdownItems([
......
...@@ -198,7 +198,11 @@ cr.define('print_preview', function() { ...@@ -198,7 +198,11 @@ cr.define('print_preview', function() {
/** @override */ /** @override */
signIn(addAccount) { signIn(addAccount) {
this.methodCalled('signIn', addAccount); this.methodCalled('signIn', addAccount);
cr.webUIListenerCallback('reload-printer-list'); const accounts = ['foo@chromium.org'];
if (addAccount) {
accounts.push('bar@chromium.org');
}
cr.webUIListenerCallback('user-accounts-updated', accounts);
} }
/** /**
......
...@@ -37,7 +37,8 @@ cr.define('print_preview_app_test', function() { ...@@ -37,7 +37,8 @@ cr.define('print_preview_app_test', function() {
printerName: 'FooDevice', printerName: 'FooDevice',
serializedAppStateStr: null, serializedAppStateStr: null,
serializedDefaultDestinationSelectionRulesStr: null, serializedDefaultDestinationSelectionRulesStr: null,
cloudPrintURL: 'cloudprint URL' cloudPrintURL: 'cloudprint URL',
userAccounts: ['foo@chromium.org'],
}; };
/** @override */ /** @override */
......
...@@ -1592,3 +1592,26 @@ TEST_F('PrintPreviewPinSettingsTest', 'All', function() { ...@@ -1592,3 +1592,26 @@ TEST_F('PrintPreviewPinSettingsTest', 'All', function() {
mocha.run(); mocha.run();
}); });
GEN('#endif'); GEN('#endif');
PrintPreviewUserManagerTest = class extends PrintPreviewTest {
/** @override */
get browsePreload() {
return 'chrome://print/data/user_manager.html';
}
/** @override */
get extraLibraries() {
return super.extraLibraries.concat([
'../settings/test_util.js',
'../test_browser_proxy.js',
'cloud_print_interface_stub.js',
'native_layer_stub.js',
'print_preview_test_utils.js',
'user_manager_test.js',
]);
}
};
TEST_F('PrintPreviewUserManagerTest', 'All', function() {
mocha.run();
});
// Copyright 2019 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.
cr.define('user_manager_test', function() {
suite('UserManagerTest', function() {
/** @type {?PrintPreviewUserManagerElement} */
let userManager = null;
/** @type {?print_preview.DestinationStore} */
let destinationStore = null;
/** @type {?print_preview.NativeLayer} */
let nativeLayer = null;
/** @type {?print_preview.CloudPrintInterface} */
let cloudPrintInterface = null;
const account1 = 'foo@chromium.org';
const account2 = 'bar@chromium.org';
/** @override */
suiteSetup(function() {
print_preview_test_utils.setupTestListenerElement();
});
/** @override */
setup(function() {
PolymerTest.clearBody();
// Create data classes
nativeLayer = new print_preview.NativeLayerStub();
print_preview.NativeLayer.setInstance(nativeLayer);
cloudPrintInterface = new print_preview.CloudPrintInterfaceStub();
// Set up a cloud printer for each account, so that search will work.
cloudPrintInterface.setPrinter(
print_preview_test_utils.getGoogleDriveDestination(account1));
cloudPrintInterface.setPrinter(
print_preview_test_utils.getGoogleDriveDestination(account2));
userManager = document.createElement('print-preview-user-manager');
// Initialize destination store.
destinationStore = print_preview_test_utils.createDestinationStore();
destinationStore.setCloudPrintInterface(cloudPrintInterface);
const localDestinations = [];
const destinations = print_preview_test_utils.getDestinations(
nativeLayer, localDestinations);
destinationStore.init(
false /* isInAppKioskMode */, 'FooDevice' /* printerName */,
'' /* serializedDefaultDestinationSelectionRulesStr */, []);
nativeLayer.setLocalDestinations(localDestinations);
// Set up user manager
userManager.appKioskMode = false;
userManager.destinationStore = destinationStore;
userManager.invitationStore = new print_preview.InvitationStore();
userManager.shouldReloadCookies = false;
document.body.appendChild(userManager);
});
// Checks that initializing and updating user accounts works as expected.
test('update users', function() {
assertTrue(userManager.cloudPrintDisabled);
userManager.setCloudPrintInterface(cloudPrintInterface);
assertFalse(userManager.cloudPrintDisabled);
assertEquals(undefined, userManager.activeUser);
userManager.initUserAccounts([]);
assertEquals('', userManager.activeUser);
assertEquals(0, userManager.users.length);
assertEquals(0, cloudPrintInterface.getCallCount('search'));
// Simulate signing in and out of accounts. This should update the list of
// users and the active user and triggers a call to search since the
// destination store calls onDestinationsReload each time this event
// fires.
cr.webUIListenerCallback('user-accounts-updated', [account1]);
assertEquals(account1, userManager.activeUser);
assertEquals(1, userManager.users.length);
assertEquals(1, cloudPrintInterface.getCallCount('search'));
cr.webUIListenerCallback('user-accounts-updated', [account1, account2]);
assertEquals(account1, userManager.activeUser);
assertEquals(2, userManager.users.length);
assertEquals(2, cloudPrintInterface.getCallCount('search'));
cr.webUIListenerCallback('user-accounts-updated', [account2]);
assertEquals(account2, userManager.activeUser);
assertEquals(1, userManager.users.length);
assertEquals(3, cloudPrintInterface.getCallCount('search'));
cr.webUIListenerCallback('user-accounts-updated', []);
assertEquals('', userManager.activeUser);
assertEquals(0, userManager.users.length);
assertEquals(4, cloudPrintInterface.getCallCount('search'));
});
test('update active user', function() {
userManager.setCloudPrintInterface(cloudPrintInterface);
userManager.initUserAccounts([account1, account2]);
assertFalse(userManager.cloudPrintDisabled);
assertEquals(account1, userManager.activeUser);
assertEquals(2, userManager.users.length);
// Don't call search at startup.
assertEquals(0, cloudPrintInterface.getCallCount('search'));
// Changing the active user doesn't result in a search call if
// |shouldReloadCookies| is false, indicating the destinations aren't user
// visible.
userManager.updateActiveUser(account2);
assertEquals(account2, userManager.activeUser);
assertEquals(2, userManager.users.length);
assertEquals(0, cloudPrintInterface.getCallCount('search'));
// Changing the active user should result in a search call if
// |shouldReloadCookies| is true.
userManager.shouldReloadCookies = true;
userManager.updateActiveUser(account1);
return cloudPrintInterface.whenCalled('search').then(account => {
assertEquals(account1, account);
assertEquals(account1, userManager.activeUser);
assertEquals(2, userManager.users.length);
assertEquals(1, cloudPrintInterface.getCallCount('search'));
});
});
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment