aboutsummaryrefslogtreecommitdiffstats
path: root/examples/webenginewidgets/simplebrowser/browser.py
blob: a124ea084df093064246a2f66efc6df0570cd6dd (plain)
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
# Copyright (C) 2023 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

from PySide6.QtWebEngineCore import (qWebEngineChromiumVersion,
                                     QWebEngineProfile, QWebEngineSettings)
from PySide6.QtCore import QObject, Qt, Slot

from downloadmanagerwidget import DownloadManagerWidget
from browserwindow import BrowserWindow


class Browser(QObject):

    def __init__(self, parent=None):
        super().__init__(parent)
        self._windows = []
        self._download_manager_widget = DownloadManagerWidget()
        self._profile = None

        # Quit application if the download manager window is the only
        # remaining window
        self._download_manager_widget.setAttribute(Qt.WA_QuitOnClose, False)

        dp = QWebEngineProfile.defaultProfile()
        dp.downloadRequested.connect(self._download_manager_widget.download_requested)

    def create_hidden_window(self, offTheRecord=False):
        if not offTheRecord and not self._profile:
            name = "simplebrowser." + qWebEngineChromiumVersion()
            self._profile = QWebEngineProfile(name)
            s = self._profile.settings()
            s.setAttribute(QWebEngineSettings.PluginsEnabled, True)
            s.setAttribute(QWebEngineSettings.DnsPrefetchEnabled, True)
            s.setAttribute(QWebEngineSettings.LocalContentCanAccessRemoteUrls, True)
            s.setAttribute(QWebEngineSettings.LocalContentCanAccessFileUrls, False)
            self._profile.downloadRequested.connect(
                self._download_manager_widget.download_requested)

        profile = QWebEngineProfile.defaultProfile() if offTheRecord else self._profile
        main_window = BrowserWindow(self, profile, False)
        self._windows.append(main_window)
        main_window.about_to_close.connect(self._remove_window)
        return main_window

    def create_window(self, offTheRecord=False):
        main_window = self.create_hidden_window(offTheRecord)
        main_window.show()
        return main_window

    def create_dev_tools_window(self):
        profile = (self._profile if self._profile
                   else QWebEngineProfile.defaultProfile())
        main_window = BrowserWindow(self, profile, True)
        self._windows.append(main_window)
        main_window.about_to_close.connect(self._remove_window)
        main_window.show()
        return main_window

    def windows(self):
        return self._windows

    def download_manager_widget(self):
        return self._download_manager_widget

    @Slot()
    def _remove_window(self):
        w = self.sender()
        if w in self._windows:
            del self._windows[self._windows.index(w)]