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
|
/*
* Copyright 2013-2017 Canonical Ltd.
*
* This file is part of morph-browser.
*
* morph-browser is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* morph-browser 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 2.5
import QtQuick.Window 2.2
import Qt.labs.settings 1.0
import Lomiri.Components 1.3
import "."
import ".."
import webbrowsercommon.private 0.1
import webbrowserapp.private 0.1
QtObject {
id: webbrowserapp
function init(urls, newSession, incognito) {
i18n.domain = "morph-browser"
if (!newSession && settings.restoreSession && ! (incognito || settings.incognitoOnStart)) {
session.restore();
}
if (allWindows.length == 0) {
windowFactory.createObject(null, {"incognito": (incognito || settings.incognitoOnStart)}).show();
}
var window = allWindows[allWindows.length - 1];
for (var i in urls) {
window.addTab(urls[i]).load();
window.tabsModel.currentIndex = window.tabsModel.count - 1;
}
if (window.tabsModel.count === 0) {
window.addTab(settings.homepage).load();
window.tabsModel.currentIndex = 0;
}
for (var w in allWindows) {
allWindows[w].tabsModel.currentTab.load();
}
// FIXME: do this asynchronously
BookmarksModel.databasePath = dataLocation + "/bookmarks.sqlite";
HistoryModel.databasePath = dataLocation + "/history.sqlite";
DownloadsModel.databasePath = dataLocation + "/downloads.sqlite";
DomainPermissionsModel.databasePath = dataLocation + "/domainpermissions.sqlite";
DomainPermissionsModel.whiteListMode = settings.domainWhiteListMode;
DomainSettingsModel.defaultZoomFactor = settings.zoomFactor;
DomainSettingsModel.databasePath = dataLocation + "/domainsettings.sqlite";
UserAgentsModel.databasePath = DomainSettingsModel.databasePath;
// create path for pages printed to PDF
FileOperations.mkpath(Qt.resolvedUrl(cacheLocation) + "/pdf_tmp");
}
// Array of all windows, sorted chronologically (most recently active last)
readonly property var allWindows: []
function getLastActiveWindow(incognito) {
for (var i = allWindows.length - 1; i >= 0; --i) {
var window = allWindows[i]
if (window.incognito === incognito) {
return window
}
}
return null
}
function openUrls(urls, newWindow, incognito) {
var window = getLastActiveWindow(incognito)
if (!window || newWindow) {
window = windowFactory.createObject(null, {"incognito": incognito})
}
for (var i in urls) {
window.addTab(urls[i]).load()
}
if (window.tabsModel.count === 0) {
window.addTab().load()
}
window.tabsModel.currentIndex = window.tabsModel.count - 1
window.show()
window.requestActivate()
}
property var windowFactory: Component {
BrowserWindow {
id: window
color: "#111111"
property alias incognito: browser.incognito
readonly property alias model: browser.tabsModel
readonly property var tabsModel: browser.tabsModel
currentWebview: browser.currentWebview
title: {
if (browser.title) {
// TRANSLATORS: %1 refers to the current page’s title
return i18n.tr("%1 - Morph Web Browser").arg(browser.title)
} else {
return i18n.tr("Morph Web Browser")
}
}
onActiveChanged: {
if (active) {
var index = allWindows.indexOf(this)
if (index > -1) {
allWindows.push(allWindows.splice(index, 1)[0])
}
}
}
onClosing: {
if (allWindows.length == 1) {
if (tabsModel.count > 0) {
session.save()
} else {
session.clear()
}
}
if (incognito && (allWindows.length > 1)) {
// If the last incognito window is being closed,
// prune incognito entries from the downloads model
var incognitoWindows = 0
for (var w in allWindows) {
var window = allWindows[w]
if ((window !== this) && window.incognito) {
++incognitoWindows
}
}
if (incognitoWindows == 0) {
DownloadsModel.pruneIncognitoDownloads()
}
}
if (allWindows.length > 1)
{
for (var win in allWindows) {
if (this === allWindows[win]) {
var tabs = allWindows[win].tabsModel
for (var t = tabs.count - 1; t >= 0; --t) {
//console.log("remove tab with url " + tabs.get(t).url)
tabs.removeTab(t)
}
allWindows.splice(win, 1)
return
}
}
}
destroy()
}
Shortcut {
sequence: StandardKey.Quit
onActivated: Qt.quit()
}
function toggleApplicationLevelFullscreen() {
setFullscreen(visibility !== Window.FullScreen)
}
Shortcut {
sequence: StandardKey.FullScreen
onActivated: window.toggleApplicationLevelFullscreen()
}
Shortcut {
sequence: "F11"
onActivated: window.toggleApplicationLevelFullscreen()
}
Shortcut {
sequence: "Ctrl+N"
onActivated: browser.newWindowRequested(false)
}
Shortcut {
sequence: "Ctrl+Shift+N"
onActivated: browser.newWindowRequested(true)
}
Component.onCompleted: allWindows.push(this)
Browser {
id: browser
anchors.fill: parent
thisWindow: window
settings: webbrowserapp.settings
windowFactory: webbrowserapp.windowFactory
onNewWindowRequested: {
var window = windowFactory.createObject(
null,
{
"incognito": incognito,
"height": parent.height,
"width": parent.width,
}
)
window.addTab()
window.tabsModel.currentIndex = 0
window.tabsModel.currentTab.load()
window.show()
}
onOpenLinkInNewWindowRequested: {
var window = windowFactory.createObject(
null,
{
"incognito": incognito,
"height": parent.height,
"width": parent.width,
}
)
window.addTab(url)
window.tabsModel.currentIndex = window.tabsModel.count - 1
window.tabsModel.currentTab.load()
window.show()
window.requestActivate()
}
// Not handled as a window-level shortcut as it would take
// precedence over key events in web content.
Keys.onEscapePressed: {
// ESC to exit fullscreen, regardless of whether it was
// requested by the page or toggled on by the user.
window.setFullscreen(false)
}
}
Connections {
target: window.tabsModel
onCountChanged: {
if ((window.tabsModel.count === 0) && browser.wide) {
window.close()
}
}
}
Connections {
target: window.incognito ? null : window.tabsModel
onCurrentIndexChanged: delayedSessionSaver.restart()
onCountChanged: delayedSessionSaver.restart()
}
Connections {
target: (session.restoring || !window.visible || browser.wide) ? null : window.tabsModel
onCurrentIndexChanged: {
// In narrow mode, the tabslist is a stack:
// the current tab is always at the top.
window.tabsModel.move(window.tabsModel.currentIndex, 0)
}
}
function serializeTabState(tab) {
return browser.serializeTabState(tab)
}
function restoreTabState(state) {
return browser.restoreTabState(state)
}
function addTab(url) {
var tab = browser.createTab({"initialUrl": url || ""})
tabsModel.add(tab)
return tab
}
}
}
property var settings: Settings {
property url homepage: ""
property string searchEngine: "duckduckgo"
property bool restoreSession: true
property bool setDesktopMode: false
property bool autoFitToWidthEnabled: false
property real zoomFactor: 1.0
property int newTabDefaultSection: 0
property string defaultAudioDevice: ""
property string defaultVideoDevice: ""
property bool domainWhiteListMode: false
property bool incognitoOnStart: false
function restoreDefaults() {
homepage = ""
searchEngine = "duckduckgo";
restoreSession = true;
setDesktopMode = false;
autoFitToWidthEnabled = false;
zoomFactor = 1.0;
newTabDefaultSection = 0;
defaultAudioDevice = "";
defaultVideoDevice = "";
domainWhiteListMode = false;
incognitoOnStart = false;
}
function resetDomainPermissions() {
DomainPermissionsModel.deleteAndResetDataBase();
}
function resetDomainSettings() {
DomainSettingsModel.deleteAndResetDataBase();
// it is a common database with DomainSettingsModel, so it is only for reset here
UserAgentsModel.deleteAndResetDataBase();
}
}
// Handle runtime requests to open urls as defined
// by the freedesktop application dbus interface's open
// method for DBUS application activation:
// http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#dbus
// The dispatch on the org.freedesktop.Application if is done per appId at the
// url-dispatcher/upstart level.
property var openUrlsHandler: Connections {
target: UriHandler
onOpened: webbrowserapp.openUrls(uris, false, settings.incognitoOnStart)
}
property var session: SessionStorage {
dataFile: dataLocation + "/session.json"
function save() {
if (!locked || restoring) {
return
}
var windows = []
for (var w in allWindows) {
var window = allWindows[w]
if (window.incognito) {
continue
}
windows.push(serializeWindowState(window))
}
if (windows.length > 0) {
store(JSON.stringify({windows: windows}))
}
}
property bool restoring: false
function restore() {
restoring = true
_doRestore()
restoring = false
}
function _doRestore() {
if (!locked) {
return
}
var state = null
try {
state = JSON.parse(retrieve())
} catch (e) {
return
}
if (state) {
var windows = state.windows
if (windows) {
for (var w in windows) {
restoreWindowState(windows[w])
}
} else if (state.tabs) {
// One-off code path: when launching the app for the first time
// after the upgrade that adds support for multiple windows, the
// saved session contains a list of tabs, not windows.
restoreWindowState(state)
}
if (allWindows.length > 0) {
var window = allWindows[allWindows.length - 1]
window.requestActivate()
window.raise()
}
}
}
function serializeWindowState(window) {
var tabs = []
for (var i = 0; i < window.tabsModel.count; ++i) {
tabs.push(window.serializeTabState(window.tabsModel.get(i)))
}
return {tabs: tabs, currentIndex: window.tabsModel.currentIndex,
width: window.width, height: window.height}
}
function restoreWindowState(state) {
var windowProperties = {}
if (state.width) {
windowProperties["width"] = state.width
}
if (state.height) {
windowProperties["height"] = state.height
}
var window = windowFactory.createObject(null, windowProperties)
for (var i in state.tabs) {
window.tabsModel.add(window.restoreTabState(state.tabs[i]))
}
window.tabsModel.currentIndex = state.currentIndex
window.show()
}
function clear() {
if (!locked) {
return
}
store("")
}
}
property var delayedSessionSaver: Timer {
interval: 500
onTriggered: session.save()
}
property var periodicSessionSaver: Timer {
// Save session periodically to mitigate state loss when the application crashes
interval: 60000 // every minute
repeat: true
running: true
onTriggered: delayedSessionSaver.restart()
}
property var applicationMonitor: Connections {
target: Qt.application
onStateChanged: {
if (Qt.application.state !== Qt.ApplicationActive) {
session.save()
}
}
onAboutToQuit: {
if (allWindows.length > 0) {
session.save()
}
}
}
property var memoryPressureMonitor: Connections {
target: MemInfo
onFreeChanged: {
var freeMemRatio = (MemInfo.total > 0) ? (MemInfo.free / MemInfo.total) : 1.0
var lowMemoryThresholdPercent = 0.2;
// Under that threshold, available memory is considered "low", and the
// browser is going to try and free up memory from unused tabs. This
// value was chosen empirically, it is subject to change to better
// reflect what a system under memory pressure might look like.
if (freeMemRatio < lowMemoryThresholdPercent) {
// Unload an inactive tab to (hopefully) free up some memory
function getCandidate(model) {
// Naive implementation that only takes into account the
// last time a tab was current. In the future we might
// want to take into account other parameters such as
// whether the tab is currently playing audio/video.
var candidate = null
for (var i = 0; i < model.count; ++i) {
var tab = model.get(i)
if (tab.current || !tab.webview) {
continue
}
if (!candidate || (candidate.lastCurrent > tab.lastCurrent)) {
candidate = tab
}
}
return candidate
}
for (var w in allWindows) {
var candidate = getCandidate(allWindows[w].tabsModel)
if (candidate) {
if (allWindows[w].incognito) {
console.warn("Unloading a background incognito tab to free up some memory")
} else {
console.warn("Unloading background tab (%1) to free up some memory".arg(candidate.url))
}
candidate.unload()
return
}
}
console.warn("System low on memory, but unable to pick a tab to unload")
}
}
}
property var historyModelMonitor: Connections {
target: HistoryModel
onLoaded: {
var doNotCleanUrls = []
for (var x in allWindows) {
var tabs = allWindows[x].tabsModel
for (var t = 0; t < tabs.count; ++t) {
doNotCleanUrls.push(tabs.get(t).url)
}
}
PreviewManager.cleanUnusedPreviews(doNotCleanUrls)
}
}
//Component.onCompleted: console.info("morph-browser using oxide %1 (chromium %2)".arg(Oxide.version).arg(Oxide.chromiumVersion))
}
|