<commit_before>import * as fs from 'fs';
import {app, BrowserWindow, shell, dialog, Menu} from 'electron';
import windowState = require('electron-window-state');
import * as menubar from 'menubar';
import {Config, Account} from './config';
import {partitionForAccount} from './account_switcher';
import log from './log';
import {IS_DEBUG, IS_DARWIN, IS_WINDOWS, IS_LINUX, APP_ICON, PRELOAD_JS, USER_CSS, trayIcon} from './common';

const ELECTRON_ISSUE_9230 = IS_WINDOWS || IS_LINUX;

export default class Window {
    static create(account: Account, config: Config, mb: Menubar.MenubarApp | null = null) {
        return (config.normal_window ? startNormalWindow(account, config) : startMenuBar(account, config, mb))
            .then(win => {
                win.browser.webContents.on('dom-ready', () => {
                    applyUserCss(win.browser, config);
                    win.browser.webContents.setZoomFactor(config.zoom_factor);
                    log.debug('Send config to renderer procress');
                    win.browser.webContents.send('mstdn:config', config, account);
                });
                return win;
            });
    }

    constructor(
        public browser: Electron.BrowserWindow,
        public state: any /*XXX: ElectronWindowState.WindowState */,
        public account: Account,
        public menubar: Menubar.MenubarApp | null,
    ) {
        if (!IS_DARWIN) {
            // Users can still access menu bar with pressing Alt key.
            browser.setMenu(Menu.getApplicationMenu());
        }

        browser.webContents.on('will-navigate', (e, url) => {
            if (!url.startsWith(`https://${this.account.host}`)) {
                e.preventDefault();
                shell.openExternal(url);
            }
            log.debug('Opened URL with external browser (will-navigate)', url);
        });
        browser.webContents.on('new-window', (e, url) => {
            if (ELECTRON_ISSUE_9230) {
                // XXX:
                // On Windows or Linux, rel="noopener" lets app crash on preventing the event.
                // Issue: https://github.com/electron/electron/issues/9230
                return;
            }
            e.preventDefault();
            shell.openExternal(url);
            log.debug('Opened URL with external browser (new-window)', url);
        });

        browser.webContents.session.setPermissionRequestHandler((contents, permission, callback) => {
            const url = contents.getURL();
            const grantedByDefault =
                url.startsWith(`https://${this.account.host}`) &&
                permission !== 'geolocation' &&
                permission !== 'media';

            if (grantedByDefault) {
                // Granted
                log.debug('Permission was granted', permission);
                callback(true);
                return;
            }

            log.debug('Create dialog for user permission', permission);
            dialog.showMessageBox({
                type: 'question',
                buttons: ['Accept', 'Reject'],
                message: `Permission '${permission}' is requested by ${url}`,
                detail: "Please choose one of 'Accept' or 'Reject'",
            }, (buttonIndex: number) => {
                const granted = buttonIndex === 0;
                callback(granted);
            });
        });
    }

    open(url: string) {
        log.debug('Open URL:', url);
        this.browser.webContents.once('did-get-redirect-request', (e: Event, _: string, newUrl: string) => {
            log.debug('Redirecting to ' + newUrl + '. Will navigate to login page for user using single user mode');
            e.preventDefault();
            this.browser.loadURL(`https://${this.account.host}/auth/sign_in`);
        });
        this.browser.loadURL(url);
    }

    close() {
        log.debug('Closing window:', this.account);
        this.state.saveState();
        this.state.unmanage();
        this.browser.webContents.removeAllListeners();
        this.browser.removeAllListeners();
        if (this.menubar) {
            // Note:
            // menubar.windowClear() won't be called because all listners was removed
            delete this.menubar.window;
        }
        this.browser.close();<commit_msg>do not navigate to login page if destination of redirect is expected page<commit_after>import * as fs from 'fs';
import {app, BrowserWindow, shell, dialog, Menu} from 'electron';
import windowState = require('electron-window-state');
import * as menubar from 'menubar';
import {Config, Account} from './config';
import {partitionForAccount} from './account_switcher';
import log from './log';
import {IS_DEBUG, IS_DARWIN, IS_WINDOWS, IS_LINUX, APP_ICON, PRELOAD_JS, USER_CSS, trayIcon} from './common';

const ELECTRON_ISSUE_9230 = IS_WINDOWS || IS_LINUX;

export default class Window {
    static create(account: Account, config: Config, mb: Menubar.MenubarApp | null = null) {
        return (config.normal_window ? startNormalWindow(account, config) : startMenuBar(account, config, mb))
            .then(win => {
                win.browser.webContents.on('dom-ready', () => {
                    applyUserCss(win.browser, config);
                    win.browser.webContents.setZoomFactor(config.zoom_factor);
                    log.debug('Send config to renderer procress');
                    win.browser.webContents.send('mstdn:config', config, account);
                });
                return win;
            });
    }

    constructor(
        public browser: Electron.BrowserWindow,
        public state: any /*XXX: ElectronWindowState.WindowState */,
        public account: Account,
        public menubar: Menubar.MenubarApp | null,
    ) {
        if (!IS_DARWIN) {
            // Users can still access menu bar with pressing Alt key.
            browser.setMenu(Menu.getApplicationMenu());
        }

        browser.webContents.on('will-navigate', (e, url) => {
            if (!url.startsWith(`https://${this.account.host}`)) {
                e.preventDefault();
                shell.openExternal(url);
            }
            log.debug('Opened URL with external browser (will-navigate)', url);
        });
        browser.webContents.on('new-window', (e, url) => {
            if (ELECTRON_ISSUE_9230) {
                // XXX:
                // On Windows or Linux, rel="noopener" lets app crash on preventing the event.
                // Issue: https://github.com/electron/electron/issues/9230
                return;
            }
            e.preventDefault();
            shell.openExternal(url);
            log.debug('Opened URL with external browser (new-window)', url);
        });

        browser.webContents.session.setPermissionRequestHandler((contents, permission, callback) => {
            const url = contents.getURL();
            const grantedByDefault =
                url.startsWith(`https://${this.account.host}`) &&
                permission !== 'geolocation' &&
                permission !== 'media';

            if (grantedByDefault) {
                // Granted
                log.debug('Permission was granted', permission);
                callback(true);
                return;
            }

            log.debug('Create dialog for user permission', permission);
            dialog.showMessageBox({
                type: 'question',
                buttons: ['Accept', 'Reject'],
                message: `Permission '${permission}' is requested by ${url}`,
                detail: "Please choose one of 'Accept' or 'Reject'",
            }, (buttonIndex: number) => {
                const granted = buttonIndex === 0;
                callback(granted);
            });
        });
    }

    open(url: string) {
        log.debug('Open URL:', url);
        this.browser.webContents.once('did-get-redirect-request', (e: Event, _: string, newUrl: string) => {
            if (url === newUrl) {
                return;
            }
            log.debug('Redirecting to ' + newUrl + '. Will navigate to login page for user using single user mode');
            e.preventDefault();
            this.browser.loadURL(`https://${this.account.host}/auth/sign_in`);
        });
        this.browser.loadURL(url);
    }

    close() {
        log.debug('Closing window:', this.account);
        this.state.saveState();
        this.state.unmanage();
        this.browser.webContents.removeAllListeners();
        this.browser.removeAllListeners();
        if (this.menubar) {
            // Note:
            // menubar.windowClear() won't be called because all listners was removed
            delete this.menubar.window;
        }
        this.browser.close();
{'fraction_non_alphanumeric': 0.12660550458715597, 'fraction_numerical': 0.002981651376146789, 'mean_word_length': 1.9562711864406779, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4461421', 'n_tokens_mistral': 2369, 'n_tokens_neox': 2275, 'n_words': 543}