1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'chrome://resources/cr_elements/cr_button/cr_button.js';
import 'chrome://resources/cr_elements/cr_icon_button/cr_icon_button.js';
import 'chrome://resources/cr_elements/cr_icons.css.js';
import 'chrome://resources/cr_elements/cr_link_row/cr_link_row.js';
import 'chrome://resources/cr_elements/cr_shared_style.css.js';
import 'chrome://resources/cr_elements/cr_spinner_style.css.js';
import 'chrome://resources/cr_elements/icons.html.js';
import './shared_style.css.js';
import {getInstance as getAnnouncerInstance} from 'chrome://resources/cr_elements/cr_a11y_announcer/cr_a11y_announcer.js';
import type {CrButtonElement} from 'chrome://resources/cr_elements/cr_button/cr_button.js';
import type {CrIconButtonElement} from 'chrome://resources/cr_elements/cr_icon_button/cr_icon_button.js';
import type {CrLinkRowElement} from 'chrome://resources/cr_elements/cr_link_row/cr_link_row.js';
import {I18nMixin} from 'chrome://resources/cr_elements/i18n_mixin.js';
import {assert, assertNotReached} from 'chrome://resources/js/assert.js';
import {focusWithoutInk} from 'chrome://resources/js/focus_without_ink.js';
import {PluralStringProxyImpl} from 'chrome://resources/js/plural_string_proxy.js';
import {PolymerElement} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import {getTemplate} from './checkup_section.html.js';
import type {FocusConfig} from './focus_config.js';
import type {CredentialsChangedListener, PasswordCheckStatusChangedListener} from './password_manager_proxy.js';
import {PasswordCheckInteraction, PasswordManagerImpl} from './password_manager_proxy.js';
import type {Route} from './router.js';
import {CheckupSubpage, Page, RouteObserverMixin, Router, UrlParam} from './router.js';
const CheckState = chrome.passwordsPrivate.PasswordCheckState;
export interface CheckupSectionElement {
$: {
checkupResult: HTMLElement,
checkupStatusLabel: HTMLElement,
checkupStatusSubLabel: HTMLElement,
refreshButton: CrIconButtonElement,
retryButton: CrButtonElement,
spinner: HTMLElement,
compromisedRow: CrLinkRowElement,
reusedRow: CrLinkRowElement,
weakRow: CrLinkRowElement,
};
}
const CheckupSectionElementBase = RouteObserverMixin(I18nMixin(PolymerElement));
export class CheckupSectionElement extends CheckupSectionElementBase {
static get is() {
return 'checkup-section';
}
static get template() {
return getTemplate();
}
static get properties() {
return {
focusConfig: {
type: Object,
observer: 'focusConfigChanged_',
},
/**
* The number of checked passwords as a formatted string.
*/
checkedPasswordsText_: String,
/**
* The number of compromised passwords as a formatted string.
*/
compromisedPasswordsText_: String,
/**
* The number of weak passwords as a formatted string.
*/
reusedPasswordsText_: String,
/**
* The number of weak passwords as a formatted string.
*/
weakPasswordsText_: String,
/**
* Suggested action to take upon compromised passwords discovery.
*/
compromisedPasswordsSuggestion_: String,
/**
* The status indicates progress and affects banner, title and icon.
*/
status_: {
type: Object,
observer: 'onStatusChanged_',
},
compromisedPasswords_: {
type: Array,
observer: 'onCompromisedPasswordsChanged_',
},
reusedPasswords_: {
type: Array,
observer: 'onReusedPasswordsChanged_',
},
weakPasswords_: {
type: Array,
observer: 'onWeakPasswordsChanged_',
},
isCheckRunning_: {
type: Boolean,
computed: 'computeIsCheckRunning_(status_)',
},
isCheckSuccessful_: {
type: Boolean,
computed: 'computeIsCheckSuccessful_(status_)',
},
bannerImage_: {
type: Array,
value: 'checkup_result_banner_error',
computed: 'computeBannerImage_(status_, compromisedPasswords_, ' +
'reusedPasswords_, weakPasswords_)',
},
passwordCount_: {
type: Number,
value: 0,
observer: 'updateCheckedPasswordsText_',
},
};
}
declare focusConfig: FocusConfig;
declare private checkedPasswordsText_: string;
declare private compromisedPasswordsText_: string;
declare private reusedPasswordsText_: string;
declare private weakPasswordsText_: string;
declare private compromisedPasswordsSuggestion_: string;
declare private status_: chrome.passwordsPrivate.PasswordCheckStatus;
declare private compromisedPasswords_:
chrome.passwordsPrivate.PasswordUiEntry[];
declare private weakPasswords_: chrome.passwordsPrivate.PasswordUiEntry[];
declare private isCheckRunning_: boolean;
declare private isCheckSuccessful_: boolean;
declare private bannerImage_: string;
declare private reusedPasswords_: chrome.passwordsPrivate.PasswordUiEntry[];
private didCheckAutomatically_: boolean = false;
declare private passwordCount_: number;
private statusChangedListener_: PasswordCheckStatusChangedListener|null =
null;
private insecureCredentialsChangedListener_: CredentialsChangedListener|null =
null;
private setSavedPasswordsListener_: CredentialsChangedListener|null = null;
override connectedCallback() {
super.connectedCallback();
this.statusChangedListener_ = status => {
this.status_ = status;
};
this.insecureCredentialsChangedListener_ = insecureCredentials => {
this.compromisedPasswords_ = insecureCredentials.filter(cred => {
return !cred.compromisedInfo!.isMuted &&
cred.compromisedInfo!.compromiseTypes.some(type => {
return (
type === chrome.passwordsPrivate.CompromiseType.LEAKED ||
type === chrome.passwordsPrivate.CompromiseType.PHISHED);
});
});
this.reusedPasswords_ = insecureCredentials.filter(cred => {
return cred.compromisedInfo!.compromiseTypes.some(type => {
return type === chrome.passwordsPrivate.CompromiseType.REUSED;
});
});
this.weakPasswords_ = insecureCredentials.filter(cred => {
return cred.compromisedInfo!.compromiseTypes.some(type => {
return type === chrome.passwordsPrivate.CompromiseType.WEAK;
});
});
};
this.setSavedPasswordsListener_ = passwordList => {
this.passwordCount_ =
passwordList
.filter(entry => !entry.federationText && !entry.isPasskey)
.length;
};
PasswordManagerImpl.getInstance().getPasswordCheckStatus().then(
this.statusChangedListener_);
PasswordManagerImpl.getInstance().addPasswordCheckStatusListener(
this.statusChangedListener_);
PasswordManagerImpl.getInstance().getInsecureCredentials().then(
this.insecureCredentialsChangedListener_);
PasswordManagerImpl.getInstance().addInsecureCredentialsListener(
this.insecureCredentialsChangedListener_);
PasswordManagerImpl.getInstance().getSavedPasswordList().then(
this.setSavedPasswordsListener_);
PasswordManagerImpl.getInstance().addSavedPasswordListChangedListener(
this.setSavedPasswordsListener_);
}
override disconnectedCallback() {
super.disconnectedCallback();
assert(this.statusChangedListener_);
PasswordManagerImpl.getInstance().removePasswordCheckStatusListener(
this.statusChangedListener_);
this.statusChangedListener_ = null;
assert(this.insecureCredentialsChangedListener_);
PasswordManagerImpl.getInstance().removeInsecureCredentialsListener(
this.insecureCredentialsChangedListener_);
this.insecureCredentialsChangedListener_ = null;
assert(this.setSavedPasswordsListener_);
PasswordManagerImpl.getInstance().removeSavedPasswordListChangedListener(
this.setSavedPasswordsListener_);
this.setSavedPasswordsListener_ = null;
}
override currentRouteChanged(route: Route): void {
const param = route.queryParameters.get(UrlParam.START_CHECK) || '';
if (param === 'true' && !this.didCheckAutomatically_) {
this.didCheckAutomatically_ = true;
PasswordManagerImpl.getInstance().startBulkPasswordCheck().catch(
() => {});
PasswordManagerImpl.getInstance().recordPasswordCheckInteraction(
PasswordCheckInteraction.START_CHECK_AUTOMATICALLY);
}
if (route.page === Page.CHECKUP) {
PasswordManagerImpl.getInstance()
.dismissSafetyHubPasswordMenuNotification();
}
}
private async onStatusChanged_(
newStatus: chrome.passwordsPrivate.PasswordCheckStatus,
oldStatus: chrome.passwordsPrivate.PasswordCheckStatus) {
// if state is unchanged - nothing to do.
if (oldStatus !== undefined && oldStatus.state === newStatus.state) {
return;
}
await this.updateCheckedPasswordsText_();
if (newStatus.state === CheckState.NO_PASSWORDS) {
return;
}
// Announce password check result and focus retry/refresh button when
// password check is finished.
if (!!oldStatus && oldStatus.state === CheckState.RUNNING &&
newStatus.state !== CheckState.RUNNING) {
let stateText: string;
if (this.compromisedPasswords_.length > 0) {
stateText = this.i18n('checkupResultRed');
} else if (this.hasAnyIssues_()) {
stateText = this.i18n('checkupResultYellow');
} else {
stateText = this.i18n('checkupResultGreen');
}
getAnnouncerInstance().announce(
[this.checkedPasswordsText_, stateText].join('. '));
focusWithoutInk(
this.showRetryButton_() ? this.$.retryButton : this.$.refreshButton);
} else if (
!!oldStatus && oldStatus.state !== CheckState.RUNNING &&
newStatus.state === CheckState.RUNNING) {
// Announce password checkup has started.
getAnnouncerInstance().announce('Password check started');
}
}
private async updateCheckedPasswordsText_() {
if (!this.status_) {
return;
}
switch (this.status_.state) {
case CheckState.IDLE:
case CheckState.OFFLINE:
case CheckState.SIGNED_OUT:
case CheckState.QUOTA_LIMIT:
case CheckState.OTHER_ERROR:
case CheckState.NO_PASSWORDS:
this.checkedPasswordsText_ =
await PluralStringProxyImpl.getInstance().getPluralString(
'checkedPasswords', this.passwordCount_);
return;
case CheckState.CANCELED:
this.checkedPasswordsText_ = this.i18n('checkupCanceled');
return;
case CheckState.RUNNING:
this.checkedPasswordsText_ =
await PluralStringProxyImpl.getInstance().getPluralString(
'checkingPasswords', this.status_.totalNumberOfPasswords || 0);
return;
default:
assertNotReached(
'Can\'t find a title for state: ' + this.status_.state);
}
}
private async onCompromisedPasswordsChanged_() {
this.compromisedPasswordsText_ =
await PluralStringProxyImpl.getInstance().getPluralString(
'compromisedPasswords', this.compromisedPasswords_.length);
this.compromisedPasswordsSuggestion_ =
await PluralStringProxyImpl.getInstance().getPluralString(
'compromisedPasswordsTitle', this.compromisedPasswords_.length);
}
private async onReusedPasswordsChanged_() {
this.reusedPasswordsText_ =
await PluralStringProxyImpl.getInstance().getPluralString(
'reusedPasswords', this.reusedPasswords_.length);
}
private async onWeakPasswordsChanged_() {
this.weakPasswordsText_ =
await PluralStringProxyImpl.getInstance().getPluralString(
'weakPasswords', this.weakPasswords_.length);
}
/**
* @return true iff a check is running right according to the given |status|.
*/
private computeIsCheckRunning_(): boolean {
return this.status_.state === CheckState.RUNNING;
}
private computeIsCheckSuccessful_(): boolean {
return this.status_.state === CheckState.IDLE;
}
private didCompromiseCheckFail_(): boolean {
return [
CheckState.OFFLINE,
CheckState.SIGNED_OUT,
CheckState.QUOTA_LIMIT,
CheckState.OTHER_ERROR,
].includes(this.status_.state);
}
private showRetryButton_(): boolean {
return !this.computeIsCheckRunning_() && !this.computeIsCheckSuccessful_();
}
private showCheckButton_(): boolean {
return this.status_.state !== CheckState.NO_PASSWORDS;
}
/**
* Starts/Restarts bulk password check.
*/
private onPasswordCheckButtonClick_() {
PasswordManagerImpl.getInstance().startBulkPasswordCheck().catch(() => {});
PasswordManagerImpl.getInstance().recordPasswordCheckInteraction(
PasswordCheckInteraction.START_CHECK_MANUALLY);
}
private computeBannerImage_(): string {
if (!this.status_) {
return 'checkup_result_banner_error';
}
if (this.computeIsCheckRunning_() ||
this.status_.state === CheckState.NO_PASSWORDS) {
return 'checkup_result_banner_running';
}
if (this.computeIsCheckSuccessful_()) {
return this.hasAnyIssues_() ? 'checkup_result_banner_compromised' :
'checkup_result_banner_ok';
}
return 'checkup_result_banner_error';
}
private getIcon_(
issues: chrome.passwordsPrivate.PasswordUiEntry[],
checkForError: boolean): string {
if (checkForError && this.status_ && this.didCompromiseCheckFail_()) {
return 'cr:error';
}
return !!issues && issues.length ? 'cr:error' : 'cr:check-circle';
}
private hasAnyIssues_(): boolean {
if (!this.compromisedPasswords_ || !this.reusedPasswords_ ||
!this.weakPasswords_) {
return false;
}
return !!this.compromisedPasswords_.length ||
!!this.reusedPasswords_.length || !!this.weakPasswords_.length;
}
private hasIssues_(issues: chrome.passwordsPrivate.PasswordUiEntry[]):
boolean {
return !!issues.length;
}
private getCompromisedSectionLabel_(): string {
if (this.status_ && this.didCompromiseCheckFail_()) {
// In case of an error, don't show "No compromised passwords" title since
// this might be a lie.
return !this.compromisedPasswords_ || !this.compromisedPasswords_.length ?
this.i18n('compromisedRowWithError') :
this.compromisedPasswordsText_;
}
return this.compromisedPasswordsText_;
}
private getCompromisedSectionSublabel_(): string {
if (!this.status_ || !this.compromisedPasswords_) {
return '';
}
const brandingName = this.i18n('localPasswordManager');
switch (this.status_.state) {
case CheckState.IDLE:
case CheckState.NO_PASSWORDS:
case CheckState.RUNNING:
case CheckState.CANCELED:
return this.compromisedPasswords_.length ?
this.compromisedPasswordsSuggestion_ :
this.i18n('compromisedPasswordsEmpty');
case CheckState.OFFLINE:
return this.i18n('checkupErrorOffline', brandingName);
case CheckState.SIGNED_OUT:
return this.i18n('checkupErrorSignedOut', brandingName);
case CheckState.QUOTA_LIMIT:
return this.i18n('checkupErrorQuota', brandingName);
case CheckState.OTHER_ERROR:
return this.i18n('checkupErrorGeneric', brandingName);
default:
assertNotReached(
'Can\'t find a title for state: ' + this.status_.state);
}
}
private getReusedSectionSublabel_(): string {
return this.reusedPasswords_.length ? this.i18n('reusedPasswordsTitle') :
this.i18n('reusedPasswordsEmpty');
}
private getWeakSectionSublabel_(): string {
return this.weakPasswords_.length ? this.i18n('weakPasswordsTitle') :
this.i18n('weakPasswordsEmpty');
}
private onCompromisedClick_() {
if (!this.compromisedPasswords_.length) {
return;
}
Router.getInstance().navigateTo(
Page.CHECKUP_DETAILS, CheckupSubpage.COMPROMISED);
}
private onReusedClick_() {
if (!this.reusedPasswords_.length) {
return;
}
Router.getInstance().navigateTo(
Page.CHECKUP_DETAILS, CheckupSubpage.REUSED);
}
private onWeakClick_() {
if (!this.weakPasswords_.length) {
return;
}
Router.getInstance().navigateTo(Page.CHECKUP_DETAILS, CheckupSubpage.WEAK);
}
private showCheckupSublabel_(): boolean {
return this.computeIsCheckRunning_();
}
private getCheckupSublabelValue_(): string {
assert(this.status_);
if (!this.computeIsCheckRunning_()) {
return this.status_.state === CheckState.NO_PASSWORDS ?
this.i18n(
'checkupErrorNoPasswords', this.i18n('localPasswordManager')) :
this.status_.elapsedTimeSinceLastCheck || '';
}
return this.i18n(
'checkupProgress', this.status_.alreadyProcessed || 0,
this.status_.totalNumberOfPasswords || 0);
}
private showCheckupResult_(): boolean {
assert(this.status_);
if (this.computeIsCheckRunning_()) {
return false;
}
return this.status_.state !== CheckState.NO_PASSWORDS;
}
private focusConfigChanged_(_newConfig: FocusConfig, oldConfig: FocusConfig) {
// focusConfig is set only once on the parent, so this observer should
// only fire once.
assert(!oldConfig);
this.focusConfig.set(Page.CHECKUP_DETAILS, () => {
const previousRoute = Router.getInstance().previousRoute;
switch (previousRoute?.details as unknown as CheckupSubpage) {
case CheckupSubpage.COMPROMISED:
focusWithoutInk(this.$.compromisedRow);
break;
case CheckupSubpage.REUSED:
focusWithoutInk(this.$.reusedRow);
break;
case CheckupSubpage.WEAK:
focusWithoutInk(this.$.weakRow);
break;
}
});
}
}
declare global {
interface HTMLElementTagNameMap {
'checkup-section': CheckupSectionElement;
}
}
customElements.define(CheckupSectionElement.is, CheckupSectionElement);
|