Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugzilla 1646756: close last priv tab clears the WKWebViewConfiguration #6827

Merged
merged 2 commits into from Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions Client/Frontend/Browser/TabManager.swift
Expand Up @@ -63,9 +63,10 @@ class TabManager: NSObject {

fileprivate let navDelegate: TabManagerNavDelegate

public static func makeWebViewConfig(isPrivate: Bool, blockPopups: Bool) -> WKWebViewConfiguration {
public static func makeWebViewConfig(isPrivate: Bool, prefs: Prefs?) -> WKWebViewConfiguration {
let configuration = WKWebViewConfiguration()
configuration.processPool = WKProcessPool()
let blockPopups = prefs?.boolForKey("blockPopups") ?? true
nbhasin2 marked this conversation as resolved.
Show resolved Hide resolved
configuration.preferences.javaScriptCanOpenWindowsAutomatically = !blockPopups
// We do this to go against the configuration of the <meta name="viewport">
// tag to behave the same way as Safari :-(
Expand All @@ -79,14 +80,13 @@ class TabManager: NSObject {

// A WKWebViewConfiguration used for normal tabs
lazy fileprivate var configuration: WKWebViewConfiguration = {
let blockPopups = profile.prefs.boolForKey("blockPopups") ?? true
return TabManager.makeWebViewConfig(isPrivate: false, blockPopups: blockPopups)
return TabManager.makeWebViewConfig(isPrivate: false, prefs: profile.prefs)
}()

// A WKWebViewConfiguration used for private mode tabs
lazy fileprivate var privateConfiguration: WKWebViewConfiguration = {
let blockPopups = profile.prefs.boolForKey("blockPopups") ?? true
return TabManager.makeWebViewConfig(isPrivate: true, blockPopups: blockPopups)
return TabManager.makeWebViewConfig(isPrivate: true, prefs: profile.prefs)
}()

var selectedIndex: Int { return _selectedIndex }
Expand Down Expand Up @@ -441,6 +441,10 @@ class TabManager: NSObject {
tabs.remove(at: removalIndex)
assert(count == prevCount - 1, "Make sure the tab count was actually removed")

if (tab.isPrivate && privateTabs.count < 1) {
privateConfiguration = TabManager.makeWebViewConfig(isPrivate: true, prefs: profile.prefs)
}

tab.closeAndRemovePrivateBrowsingData()

if notify {
Expand Down Expand Up @@ -474,6 +478,8 @@ class TabManager: NSObject {
}
privateTabs.forEach { $0.closeAndRemovePrivateBrowsingData() }
tabs = normalTabs

privateConfiguration = TabManager.makeWebViewConfig(isPrivate: true, prefs: profile.prefs)
}

func removeTabsWithUndoToast(_ tabs: [Tab]) {
Expand Down
4 changes: 3 additions & 1 deletion Client/Frontend/Settings/SettingsContentViewController.swift
Expand Up @@ -107,7 +107,9 @@ class SettingsContentViewController: UIViewController, WKNavigationDelegate {
}

func makeWebView() -> WKWebView {
let config = TabManager.makeWebViewConfig(isPrivate: true, blockPopups: true)
let config = TabManager.makeWebViewConfig(isPrivate: true, prefs: nil)
config.preferences.javaScriptCanOpenWindowsAutomatically = false

let webView = WKWebView(
frame: CGRect(width: 1, height: 1),
configuration: config
Expand Down
25 changes: 25 additions & 0 deletions XCUITests/PrivateBrowsingTest.swift
Expand Up @@ -7,6 +7,7 @@ import XCTest
let url1 = "example.com"
let url2 = path(forTestPage: "test-mozilla-org.html")
let url3 = path(forTestPage: "test-example.html")
let urlIndexedDB = path(forTestPage: "test-indexeddb-private.html")

let url1And3Label = "Example Domain"
let url2Label = "Internet for people, not profit — Mozilla"
Expand Down Expand Up @@ -140,6 +141,30 @@ class PrivateBrowsingTest: BaseTestCase {
checkOpenTabsAfterClosingPrivateMode()
}

/* Loads a page that checks if an db file exists already. It uses indexedDB on both the main document, and in a web worker.
The loaded page has two staticTexts that get set when the db is correctly created (because the db didn't exist in the cache)
https://bugzilla.mozilla.org/show_bug.cgi?id=1646756
*/
func testClearIndexedDB() {
enableClosePrivateBrowsingOptionWhenLeaving()

func checkIndexedDBIsCreated() {
navigator.openURL(urlIndexedDB)
waitUntilPageLoad()
XCTAssertTrue(app.webViews.staticTexts["DB_CREATED_PAGE"].exists)
XCTAssertTrue(app.webViews.staticTexts["DB_CREATED_WORKER"].exists)
}

navigator.toggleOn(userState.isPrivate, withAction: Action.TogglePrivateMode)
checkIndexedDBIsCreated()

navigator.toggleOff(userState.isPrivate, withAction: Action.TogglePrivateMode)
checkIndexedDBIsCreated()

navigator.toggleOn(userState.isPrivate, withAction: Action.TogglePrivateMode)
checkIndexedDBIsCreated()
}

func testPrivateBrowserPanelView() {
// If no private tabs are open, there should be a initial screen with label Private Browsing
navigator.toggleOn(userState.isPrivate, withAction: Action.TogglePrivateMode)
Expand Down
2 changes: 1 addition & 1 deletion XCUITests/WebPagesForTesting.swift
Expand Up @@ -17,7 +17,7 @@ func registerHandlersForTestMethods(server: GCDWebServer) {
return GCDWebServerDataResponse(html: "<html><body>\(textNodes)</body></html>")
}

["test-window-opener", "test-password", "test-password-submit", "test-password-2", "test-password-submit-2", "empty-login-form", "empty-login-form-submit", "test-example", "test-example-link", "test-mozilla-book", "test-mozilla-org", "test-popup-blocker",
["test-indexeddb-private", "test-window-opener", "test-password", "test-password-submit", "test-password-2", "test-password-submit-2", "empty-login-form", "empty-login-form-submit", "test-example", "test-example-link", "test-mozilla-book", "test-mozilla-org", "test-popup-blocker",
"manifesto-en", "manifesto-es", "manifesto-zh-CN", "manifesto-ar", "test-user-agent"].forEach {
addHTMLFixture(name: $0, server: server)
}
Expand Down
37 changes: 37 additions & 0 deletions test-fixtures/test-indexeddb-private.html
@@ -0,0 +1,37 @@
<head>
</head>

<body>
<div id='pagediv' >REPLACE_ME_PAGE</div>
<div id='workerdiv' >REPLACE_ME_WORKER</div>
</body>

<script>
var request = window.indexedDB.open("MyTestDatabase", 1);
request.onupgradeneeded = function(event) {
var db = event.target.result;
var objectStore = db.createObjectStore("sample", { keyPath: "foo" });
document.getElementById('pagediv').innerHTML = 'DB_CREATED_PAGE'
}

///////////////////////
//////////////////////

var worker_fn = function(e) {
var request = indexedDB.open("workerdb", 1);
request.onupgradeneeded = function(event) {
var db = event.target.result;
var objectStore = db.createObjectStore("sample", { keyPath: "foo" });
self.postMessage('msg from worker');
}
};

var blob = new Blob(["onmessage ="+worker_fn.toString()], { type: "text/javascript" });
var worker = new Worker(window.URL.createObjectURL(blob));
worker.onmessage = function(e) {
document.getElementById('workerdiv').innerHTML = 'DB_CREATED_WORKER'
};
worker.postMessage("start");
</script>