mirror of
https://github.com/bitwarden/browser.git
synced 2024-12-24 16:49:26 +01:00
web request background. fixes to setIcon.
This commit is contained in:
parent
985c02c5e1
commit
c2e5945be5
@ -2,6 +2,8 @@ import { CipherType } from '../enums/cipherType.enum';
|
|||||||
|
|
||||||
import { Cipher } from '../models/domain/cipher';
|
import { Cipher } from '../models/domain/cipher';
|
||||||
|
|
||||||
|
import WebRequestBackground from './webRequest.background';
|
||||||
|
|
||||||
import ApiService from '../services/api.service';
|
import ApiService from '../services/api.service';
|
||||||
import AppIdService from '../services/appId.service';
|
import AppIdService from '../services/appId.service';
|
||||||
import AutofillService from '../services/autofill.service';
|
import AutofillService from '../services/autofill.service';
|
||||||
@ -41,6 +43,8 @@ export default class MainBackground {
|
|||||||
totpService: TotpService;
|
totpService: TotpService;
|
||||||
autofillService: AutofillService;
|
autofillService: AutofillService;
|
||||||
|
|
||||||
|
private webRequestBackground: WebRequestBackground;
|
||||||
|
|
||||||
private sidebarAction: any;
|
private sidebarAction: any;
|
||||||
private buildingContextMenu: boolean;
|
private buildingContextMenu: boolean;
|
||||||
private onUpdatedRan: boolean;
|
private onUpdatedRan: boolean;
|
||||||
@ -51,7 +55,6 @@ export default class MainBackground {
|
|||||||
private loginsToAdd: any[] = [];
|
private loginsToAdd: any[] = [];
|
||||||
private syncTimeout: number;
|
private syncTimeout: number;
|
||||||
private autofillTimeout: number;
|
private autofillTimeout: number;
|
||||||
private pendingAuthRequests: any[] = [];
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
// Services
|
// Services
|
||||||
@ -82,6 +85,9 @@ export default class MainBackground {
|
|||||||
// Other fields
|
// Other fields
|
||||||
this.sidebarAction = (typeof opr !== 'undefined') && opr.sidebarAction ?
|
this.sidebarAction = (typeof opr !== 'undefined') && opr.sidebarAction ?
|
||||||
opr.sidebarAction : (window as any).chrome.sidebarAction;
|
opr.sidebarAction : (window as any).chrome.sidebarAction;
|
||||||
|
|
||||||
|
// Background
|
||||||
|
this.webRequestBackground = new WebRequestBackground(this.utilsService, this.cipherService);
|
||||||
}
|
}
|
||||||
|
|
||||||
async bootstrap() {
|
async bootstrap() {
|
||||||
@ -264,80 +270,15 @@ export default class MainBackground {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (chrome.webRequest && chrome.webRequest.onAuthRequired) {
|
|
||||||
(window as any).chrome.webRequest.onAuthRequired.addListener((details: any, callback: any) => {
|
|
||||||
if (!details.url || this.pendingAuthRequests.indexOf(details.requestId) !== -1) {
|
|
||||||
if (callback) {
|
|
||||||
callback();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const domain = UtilsService.getDomain(details.url);
|
|
||||||
if (domain == null) {
|
|
||||||
if (callback) {
|
|
||||||
callback();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.pendingAuthRequests.push(details.requestId);
|
|
||||||
|
|
||||||
if (this.utilsService.isFirefox()) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
this.cipherService.getAllDecryptedForDomain(domain).then((ciphers) => {
|
|
||||||
if (ciphers == null || ciphers.length !== 1) {
|
|
||||||
reject();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve({
|
|
||||||
authCredentials: {
|
|
||||||
username: ciphers[0].login.username,
|
|
||||||
password: ciphers[0].login.password,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}, () => {
|
|
||||||
reject();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
this.cipherService.getAllDecryptedForDomain(domain).then((ciphers) => {
|
|
||||||
if (ciphers == null || ciphers.length !== 1) {
|
|
||||||
callback();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
callback({
|
|
||||||
authCredentials: {
|
|
||||||
username: ciphers[0].login.username,
|
|
||||||
password: ciphers[0].login.password,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}, () => {
|
|
||||||
callback();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, { urls: ['http://*/*', 'https://*/*'] }, [this.utilsService.isFirefox() ? 'blocking' : 'asyncBlocking']);
|
|
||||||
|
|
||||||
chrome.webRequest.onCompleted.addListener(this.completeAuthRequest, { urls: ['http://*/*'] });
|
|
||||||
chrome.webRequest.onErrorOccurred.addListener(this.completeAuthRequest, { urls: ['http://*/*'] });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Bootstrap
|
// Bootstrap
|
||||||
|
await this.webRequestBackground.init();
|
||||||
|
|
||||||
await this.environmentService.setUrlsFromStorage();
|
await this.environmentService.setUrlsFromStorage();
|
||||||
this.setIcon();
|
this.setIcon();
|
||||||
this.cleanupLoginsToAdd();
|
this.cleanupLoginsToAdd();
|
||||||
this.fullSync(true);
|
this.fullSync(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private completeAuthRequest(details: any) {
|
|
||||||
const i = this.pendingAuthRequests.indexOf(details.requestId);
|
|
||||||
if (i > -1) {
|
|
||||||
this.pendingAuthRequests.splice(i, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async buildContextMenu() {
|
private async buildContextMenu() {
|
||||||
if (!chrome.contextMenus || this.buildingContextMenu) {
|
if (!chrome.contextMenus || this.buildingContextMenu) {
|
||||||
return;
|
return;
|
||||||
@ -492,8 +433,10 @@ export default class MainBackground {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const title = cipher.name + (cipher.login.username && cipher.login.username !== '' ?
|
let title = cipher.name;
|
||||||
' (' + cipher.login.username + ')' : '');
|
if (cipher.login.username && cipher.login.username !== '') {
|
||||||
|
title += (' (' + cipher.login.username + ')');
|
||||||
|
}
|
||||||
await this.loadContextMenuOptions(title, cipher.id, cipher);
|
await this.loadContextMenuOptions(title, cipher.id, cipher);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -831,21 +774,25 @@ export default class MainBackground {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private actionSetIcon(theAction: any, suffix: string): Promise<any> {
|
private async actionSetIcon(theAction: any, suffix: string): Promise<any> {
|
||||||
if (!theAction || !theAction.setIcon) {
|
if (!theAction || !theAction.setIcon) {
|
||||||
return Promise.resolve();
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Promise((resolve) => {
|
const options = {
|
||||||
theAction.setIcon({
|
path: {
|
||||||
path: {
|
19: 'images/icon19' + suffix + '.png',
|
||||||
19: 'images/icon19' + suffix + '.png',
|
38: 'images/icon38' + suffix + '.png',
|
||||||
38: 'images/icon38' + suffix + '.png',
|
},
|
||||||
},
|
};
|
||||||
}, () => {
|
|
||||||
resolve();
|
if (this.utilsService.isFirefox()) {
|
||||||
|
await theAction.setIcon(options);
|
||||||
|
} else {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
theAction.setIcon(options, () => resolve());
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private actionSetBadgeBackgroundColor(action: any) {
|
private actionSetBadgeBackgroundColor(action: any) {
|
||||||
|
77
src/background/webRequest.background.ts
Normal file
77
src/background/webRequest.background.ts
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
import CipherService from '../services/cipher.service';
|
||||||
|
import UtilsService from '../services/utils.service';
|
||||||
|
|
||||||
|
export default class WebRequestBackground {
|
||||||
|
private pendingAuthRequests: any[] = [];
|
||||||
|
private webRequest: any;
|
||||||
|
private isFirefox: boolean;
|
||||||
|
|
||||||
|
constructor(utilsService: UtilsService, private cipherService: CipherService) {
|
||||||
|
this.webRequest = (window as any).chrome.webRequest;
|
||||||
|
this.isFirefox = utilsService.isFirefox();
|
||||||
|
}
|
||||||
|
|
||||||
|
async init() {
|
||||||
|
if (!this.webRequest || !this.webRequest.onAuthRequired) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.webRequest.onAuthRequired.addListener(async (details: any, callback: any) => {
|
||||||
|
if (!details.url || this.pendingAuthRequests.indexOf(details.requestId) !== -1) {
|
||||||
|
if (callback) {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const domain = UtilsService.getDomain(details.url);
|
||||||
|
if (domain == null) {
|
||||||
|
if (callback) {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.pendingAuthRequests.push(details.requestId);
|
||||||
|
|
||||||
|
if (this.isFirefox) {
|
||||||
|
return new Promise(async (resolve, reject) => {
|
||||||
|
await this.resolveAuthCredentials(domain, resolve, reject);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
await this.resolveAuthCredentials(domain, callback, callback);
|
||||||
|
}
|
||||||
|
}, { urls: ['http://*/*', 'https://*/*'] }, [this.isFirefox ? 'blocking' : 'asyncBlocking']);
|
||||||
|
|
||||||
|
this.webRequest.onCompleted.addListener(
|
||||||
|
(details: any) => this.completeAuthRequest(details), { urls: ['http://*/*'] });
|
||||||
|
this.webRequest.onErrorOccurred.addListener(
|
||||||
|
(details: any) => this.completeAuthRequest(details), { urls: ['http://*/*'] });
|
||||||
|
}
|
||||||
|
|
||||||
|
private async resolveAuthCredentials(domain: string, success: Function, error: Function) {
|
||||||
|
try {
|
||||||
|
const ciphers = await this.cipherService.getAllDecryptedForDomain(domain);
|
||||||
|
if (ciphers == null || ciphers.length !== 1) {
|
||||||
|
error();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
success({
|
||||||
|
authCredentials: {
|
||||||
|
username: ciphers[0].login.username,
|
||||||
|
password: ciphers[0].login.password,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
error();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private completeAuthRequest(details: any) {
|
||||||
|
const i = this.pendingAuthRequests.indexOf(details.requestId);
|
||||||
|
if (i > -1) {
|
||||||
|
this.pendingAuthRequests.splice(i, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user