123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- /**
- * This file is part of Threema Web.
- *
- * Threema Web is free software: you can redistribute it and/or modify it
- * under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or (at
- * your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
- * General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
- */
- import {Logger} from 'ts-log';
- import {BrowserInfo} from '../helpers/browser_info';
- import {LogService} from './log';
- import BrowserName = threema.BrowserName;
- export class BrowserService {
- private browser: BrowserInfo;
- private $window: ng.IWindowService;
- private readonly log: Logger;
- private supportsExtendedLocaleCompareCache: boolean;
- public static $inject = ['$window', 'LogService'];
- constructor($window: ng.IWindowService, logService: LogService) {
- this.$window = $window;
- this.log = logService.getLogger('Browser-S');
- }
- public getBrowser(): BrowserInfo {
- if (this.browser === undefined) {
- const browser = {
- chrome: false,
- chromeIos: false,
- firefox: false,
- firefoxIos: false,
- ie: false,
- edge: false,
- opera: false,
- safari: false,
- };
- const uagent = this.$window.navigator.userAgent.toLowerCase();
- browser.chrome = /webkit/.test(uagent) && /chrome/.test(uagent) && !/edg/.test(uagent);
- browser.chromeIos = /mozilla/.test(uagent) && /crios/.test(uagent);
- browser.firefox = /mozilla/.test(uagent) && /firefox/.test(uagent);
- browser.firefoxIos = /mozilla/.test(uagent) && /fxios/.test(uagent);
- browser.ie = (/msie/.test(uagent) || /trident/.test(uagent)) && !/edge/.test(uagent);
- browser.edge = /edg/.test(uagent);
- browser.safari = /safari/.test(uagent) && /applewebkit/.test(uagent)
- && !/chrome/.test(uagent) && !/fxios/.test(uagent) && !/crios/.test(uagent);
- browser.opera = /mozilla/.test(uagent) && /applewebkit/.test(uagent)
- && /chrome/.test(uagent) && /safari/.test(uagent) && /opr/.test(uagent);
- if (browser.opera && browser.chrome) {
- browser.chrome = false;
- }
- let version = null;
- for (const x in browser) {
- if (browser[x]) {
- let b;
- if (x === 'ie') {
- b = 'msie';
- } else if (x === 'edge') {
- b = 'edge?';
- } else if (x === 'opera') {
- b = 'opr';
- } else if (x === 'firefoxIos') {
- b = 'fxios';
- } else if (x === 'chromeIos') {
- b = 'crios';
- } else if (x === 'safari') {
- b = 'version';
- } else {
- b = x;
- }
- let match = uagent.match(new RegExp('(' + b + ')( |\/)([0-9]+)'));
- let versionString;
- if (match) {
- versionString = match[3];
- } else {
- match = uagent.match(new RegExp('rv:([0-9]+)'));
- versionString = match ? match[1] : '';
- }
- const versionInt: number = parseInt(versionString, 10);
- version = isNaN(versionInt) ? undefined : versionInt;
- break;
- }
- }
- if (browser.chrome) {
- this.browser = new BrowserInfo(uagent, BrowserName.Chrome, version);
- }
- if (browser.chromeIos) {
- this.browser = new BrowserInfo(uagent, BrowserName.ChromeIos, version, true);
- }
- if (browser.firefox) {
- this.browser = new BrowserInfo(uagent, BrowserName.Firefox, version);
- }
- if (browser.firefoxIos) {
- this.browser = new BrowserInfo(uagent, BrowserName.FirefoxIos, version, true);
- }
- if (browser.ie) {
- this.browser = new BrowserInfo(uagent, BrowserName.InternetExplorer, version);
- }
- if (browser.edge) {
- this.browser = new BrowserInfo(uagent, BrowserName.Edge, version);
- }
- if (browser.safari) {
- const mobile = /mobile/.test(uagent);
- this.browser = new BrowserInfo(uagent, BrowserName.Safari, version, mobile);
- }
- if (browser.opera) {
- this.browser = new BrowserInfo(uagent, BrowserName.Opera, version);
- }
- }
- return this.browser;
- }
- /**
- * Return whether the current browser supports the WebRTC task or not.
- */
- public supportsWebrtcTask() {
- if (this.browser === undefined) {
- this.getBrowser();
- }
- return this.browser.supportsWebrtcTask();
- }
- /**
- * Return whether the browser supports extended `string.localeCompare` options.
- */
- public supportsExtendedLocaleCompare() {
- if (this.supportsExtendedLocaleCompareCache !== undefined) {
- return this.supportsExtendedLocaleCompareCache;
- }
- function getSupport(): boolean {
- try {
- 'foo'.localeCompare('bar', 'i');
- } catch (e) {
- return e.name === 'RangeError';
- }
- return false;
- }
- const support = getSupport();
- this.supportsExtendedLocaleCompareCache = support;
- this.log.debug(`Browser ${support ? 'supports' : 'does not support'} extended locale compare options`);
- return support;
- }
- }
|