Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Add persistent private browsing alert for existing users
Browse files Browse the repository at this point in the history
Move AppState to the same location as delegates
Open context menus like Safari to allow opening URLs in new windows
Moved alert for existing users and updated text.
Updated icons with leo-sfsymbol
  • Loading branch information
Brandon-T committed Jul 17, 2023
1 parent cc1c728 commit 8959173
Show file tree
Hide file tree
Showing 28 changed files with 172 additions and 93 deletions.
4 changes: 4 additions & 0 deletions App/Client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
CA986FB4298C1254000C6DD8 /* BraveWidgetsModels in Frameworks */ = {isa = PBXBuildFile; productRef = CA986FB3298C1254000C6DD8 /* BraveWidgetsModels */; };
CAADEFE026E2707F0020DC4C /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAADEFDF26E2707F0020DC4C /* SceneDelegate.swift */; };
CAAE653D287C9FCF00FA44A3 /* CPTemplateApplicationSceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAAE653C287C9FCF00FA44A3 /* CPTemplateApplicationSceneDelegate.swift */; };
CABDE77F2A55DD1C00A388A4 /* AppState.swift in Sources */ = {isa = PBXBuildFile; fileRef = CABDE77E2A55DD1C00A388A4 /* AppState.swift */; };
E6231C011B90A44F005ABB0D /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = E6231C001B90A44F005ABB0D /* libz.tbd */; };
E6231C051B90A472005ABB0D /* libxml2.2.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = E6231C041B90A472005ABB0D /* libxml2.2.tbd */; };
F84B22041A0910F600AAB793 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F84B21E51A0910F600AAB793 /* AppDelegate.swift */; };
Expand Down Expand Up @@ -315,6 +316,7 @@
CA0391F7271E143F000EB13C /* SingleStatWidget.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SingleStatWidget.swift; sourceTree = "<group>"; };
CAADEFDF26E2707F0020DC4C /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
CAAE653C287C9FCF00FA44A3 /* CPTemplateApplicationSceneDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CPTemplateApplicationSceneDelegate.swift; sourceTree = "<group>"; };
CABDE77E2A55DD1C00A388A4 /* AppState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppState.swift; sourceTree = "<group>"; };
E6231C001B90A44F005ABB0D /* libz.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libz.tbd; path = usr/lib/libz.tbd; sourceTree = SDKROOT; };
E6231C041B90A472005ABB0D /* libxml2.2.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libxml2.2.tbd; path = usr/lib/libxml2.2.tbd; sourceTree = SDKROOT; };
E62AC15F1E956AFC00843532 /* Dev.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Dev.entitlements; sourceTree = "<group>"; };
Expand Down Expand Up @@ -606,6 +608,7 @@
children = (
CAAE653C287C9FCF00FA44A3 /* CPTemplateApplicationSceneDelegate.swift */,
F84B21E51A0910F600AAB793 /* AppDelegate.swift */,
CABDE77E2A55DD1C00A388A4 /* AppState.swift */,
CAADEFDF26E2707F0020DC4C /* SceneDelegate.swift */,
);
path = Delegates;
Expand Down Expand Up @@ -1040,6 +1043,7 @@
2F4A572429E608C8003454F8 /* BrowserIntents.intentdefinition in Sources */,
F84B22041A0910F600AAB793 /* AppDelegate.swift in Sources */,
27BEDCCC28AD37CE0073425E /* BraveWidgets.intentdefinition in Sources */,
CABDE77F2A55DD1C00A388A4 /* AppState.swift in Sources */,
CAADEFE026E2707F0020DC4C /* SceneDelegate.swift in Sources */,
CAAE653D287C9FCF00FA44A3 /* CPTemplateApplicationSceneDelegate.swift in Sources */,
);
Expand Down
3 changes: 3 additions & 0 deletions App/iOS/Delegates/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

if isFirstLaunch {
Preferences.PrivacyReports.ntpOnboardingCompleted.value = false
Preferences.Privacy.shouldShowPersistentPrivateBrowsingAlert.value = false
} else {
Preferences.Privacy.shouldShowPersistentPrivateBrowsingAlert.value = false
}

Preferences.General.isFirstLaunch.value = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import Foundation
import UIKit
import BraveCore
import Brave
import Data
import RuntimeWarnings
import Shared
Expand All @@ -15,17 +16,21 @@ import Storage
import BraveNews
import os.log

private let adsRewardsLog = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "ads-rewards")

/// Class that does startup initialization
/// Everything in this class can only be execute ONCE
/// IE: BraveCore initialization, BuildChannel, Migrations, etc.
public class AppState {
private let log = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "app-state")

public static let shared = AppState()

public let braveCore: BraveCoreMain
public let dau: DAU
public let migration: Migration
public let profile: Profile
public let rewards: BraveRewards
public let rewards: Brave.BraveRewards
public let newsFeedDataSource: FeedDataSource
private var didBecomeActive = false

Expand Down Expand Up @@ -63,7 +68,7 @@ public class AppState {
namespace: "TabManagerScreenshots",
quality: UIConstants.screenshotQuality)
} catch {
Logger.module.error("Failed to create an image store for files: \(self.profile.files.rootPath) and namespace: \"TabManagerScreenshots\": \(error.localizedDescription)")
log.error("Failed to create an image store for files: \(self.profile.files.rootPath) and namespace: \"TabManagerScreenshots\": \(error.localizedDescription)")
}
return nil
}()
Expand Down Expand Up @@ -193,14 +198,14 @@ public class AppState {
}
}

private static func migrateAdsConfirmations(for configruation: BraveRewards.Configuration) {
private static func migrateAdsConfirmations(for configuruation: Brave.BraveRewards.Configuration) {
// To ensure after a user launches 1.21 that their ads confirmations, viewed count and
// estimated payout remain correct.
//
// This hack is unfortunately neccessary due to a missed migration path when moving
// confirmations from ledger to ads, we must extract `confirmations.json` out of ledger's
// state file and save it as a new file under the ads directory.
let base = configruation.storageURL
let base = configuruation.storageURL
let ledgerStateContainer = base.appendingPathComponent("ledger/random_state.plist")
let adsConfirmations = base.appendingPathComponent("ads/confirmations.json")
let fm = FileManager.default
Expand Down
11 changes: 9 additions & 2 deletions App/iOS/Delegates/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ extension SceneDelegate {
// Store the scene's activities
let windowId: UUID
let isPrivate: Bool
var userActivity = userActivity
let urlToOpen: URL?

if let userActivity = userActivity {
// Restore the scene with the WindowID from the User-Activity
Expand All @@ -411,6 +411,7 @@ extension SceneDelegate {
windowId = UUID(uuidString: windowIdString) ?? UUID()
isPrivate = userActivity.userInfo?["isPrivate"] as? Bool == true
privateBrowsingManager.isPrivateBrowsing = isPrivate
urlToOpen = userActivity.userInfo?["OpenURL"] as? URL

// Create a new session window
SessionWindow.createWindow(isPrivate: isPrivate, isSelected: true, uuid: windowId)
Expand All @@ -427,6 +428,7 @@ extension SceneDelegate {
windowId = windowUUID
isPrivate = sceneIsPrivate
privateBrowsingManager.isPrivateBrowsing = sceneIsPrivate
urlToOpen = scene.session.userInfo?["OpenURL"] as? URL

scene.userActivity = BrowserState.userActivity(for: windowId, isPrivate: isPrivate)
} else {
Expand Down Expand Up @@ -461,6 +463,7 @@ extension SceneDelegate {

isPrivate = false
privateBrowsingManager.isPrivateBrowsing = false
urlToOpen = nil

scene.userActivity = BrowserState.userActivity(for: windowId, isPrivate: false)
scene.session.userInfo = ["WindowID": windowId.uuidString,
Expand All @@ -476,7 +479,6 @@ extension SceneDelegate {
rewards: rewards,
migration: migration,
crashedLastSession: crashedLastSession,
rewards: rewards,
newsFeedDataSource: newsFeedDataSource,
privateBrowsingManager: privateBrowsingManager
)
Expand Down Expand Up @@ -509,6 +511,11 @@ extension SceneDelegate {
currentTabSceneBrowser.moveTab(tabId: tabId, to: browserViewController)
}
}

if let urlToOpen = urlToOpen {
browserViewController.loadViewIfNeeded()
browserViewController.switchToTabForURLOrOpen(urlToOpen, isPrivileged: false)
}

return browserViewController
}
Expand Down
36 changes: 19 additions & 17 deletions Sources/Brave/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ public class BrowserViewController: UIViewController {
rewards: BraveRewards,
migration: Migration?,
crashedLastSession: Bool,
rewards: BraveRewards,
newsFeedDataSource: FeedDataSource,
privateBrowsingManager: PrivateBrowsingManager
) {
Expand All @@ -292,7 +291,6 @@ public class BrowserViewController: UIViewController {
self.rewards = rewards
self.migration = migration
self.crashedLastSession = crashedLastSession
self.rewards = rewards
self.privateBrowsingManager = privateBrowsingManager
self.feedDataSource = newsFeedDataSource
feedDataSource.historyAPI = braveCore.historyAPI
Expand Down Expand Up @@ -1952,6 +1950,25 @@ public class BrowserViewController: UIViewController {
}
}
}

func openInNewWindow(url: URL?, isPrivate: Bool) {
let activity = BrowserState.userActivity(for: UUID(), isPrivate: isPrivate)

if let url = url {
activity.addUserInfoEntries(from: ["OpenURL": url])
}

let options = UIScene.ActivationRequestOptions().then {
$0.requestingScene = view.window?.windowScene
}

UIApplication.shared.requestSceneSessionActivation(nil,
userActivity: activity,
options: options,
errorHandler: { error in
Logger.module.error("Error creating new window: \(error)")
})
}

func clearHistoryAndOpenNewTab() {
// When PB Only mode is enabled
Expand Down Expand Up @@ -2496,21 +2513,6 @@ extension BrowserViewController: TabsBarViewControllerDelegate {
func tabsBarDidSelectAddNewTab(_ isPrivate: Bool) {
openBlankNewTab(attemptLocationFieldFocus: false, isPrivate: isPrivate)
}

func tabsBarDidSelectAddNewWindow(_ isPrivate: Bool) {
let activity = BrowserState.userActivity(for: UUID(), isPrivate: false)

let options = UIScene.ActivationRequestOptions().then {
$0.requestingScene = view.window?.windowScene
}

UIApplication.shared.requestSceneSessionActivation(nil,
userActivity: activity,
options: options,
errorHandler: { error in
Logger.module.error("Error creating new window: \(error)")
})
}

func tabsBarDidSelectTab(_ tabsBarController: TabsBarViewController, _ tab: Tab) {
if tab == tabManager.selectedTab { return }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,6 @@ extension BrowserViewController: TabManagerDelegate {
newTabMenuChildren.append(openNewTab)
}
addTabMenuChildren.append(openNewTab)

addTabMenuChildren.append(UIAction(title: "New Window", image: UIImage(systemName: "window.horizontal.closed"), handler: UIAction.deferredActionHandler { [unowned self] _ in
self.tabsBarDidSelectAddNewWindow(privateBrowsingManager.isPrivateBrowsing)
}))

var bookmarkMenuChildren: [UIAction] = []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,6 @@ extension BrowserViewController: WKUIDelegate {
}

openNewTabAction.accessibilityLabel = "linkContextMenu.openInNewTab"

actions.append(openNewTabAction)
}

Expand All @@ -1001,8 +1000,31 @@ extension BrowserViewController: WKUIDelegate {
self.addTab(url: url, inPrivateMode: true, currentTab: currentTab)
}
openNewPrivateTabAction.accessibilityLabel = "linkContextMenu.openInNewPrivateTab"

actions.append(openNewPrivateTabAction)

if UIApplication.shared.supportsMultipleScenes {
if !tabType.isPrivate {
let openNewWindowAction = UIAction(
title: Strings.openInNewWindowTitle,
image: UIImage(braveSystemNamed: "leo.window")
) { _ in
self.openInNewWindow(url: url, isPrivate: false)
}

openNewWindowAction.accessibilityLabel = "linkContextMenu.openInNewWindow"
actions.append(openNewWindowAction)
}

let openNewPrivateWindowAction = UIAction(
title: Strings.openInNewPrivateWindowTitle,
image: UIImage(braveSystemNamed: "leo.window.tab-private")
) { _ in
self.openInNewWindow(url: url, isPrivate: true)
}

openNewPrivateWindowAction.accessibilityLabel = "linkContextMenu.openInNewPrivateWindow"
actions.append(openNewPrivateWindowAction)
}

let copyAction = UIAction(
title: Strings.copyLinkActionTitle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ extension PlaylistListViewController: UITableViewDelegate {
handler: { [weak self] (action, view, completionHandler) in
guard let self = self else { return }

let isPrivateBrowsing = self.privateBrowsingManager?.isPrivateBrowsing == true
let isPrivateBrowsing = self.isPrivateBrowsing
let style: UIAlertController.Style = UIDevice.current.userInterfaceIdiom == .pad ? .alert : .actionSheet

let alert = UIAlertController(
Expand Down Expand Up @@ -244,7 +244,7 @@ extension PlaylistListViewController: UITableViewDelegate {

// In Private-Browsing, we do not show "Open in New Tab",
// we only show "Open in Private Tab"
let isPrivateBrowsing = self.privateBrowsingManager?.isPrivateBrowsing == true
let isPrivateBrowsing = self.isPrivateBrowsing
if !isPrivateBrowsing {
actions.append(
UIAction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class PlaylistListViewController: UIViewController {

weak var delegate: PlaylistViewControllerDelegate?
private let playerView: VideoView
let privateBrowsingManager: PrivateBrowsingManager?
let isPrivateBrowsing: Bool

private var observers = Set<AnyCancellable>()
private var folderObserver: AnyCancellable?
Expand All @@ -74,9 +74,9 @@ class PlaylistListViewController: UIViewController {
$0.allowsSelectionDuringEditing = true
}

init(playerView: VideoView, privateBrowsingManager: PrivateBrowsingManager?) {
init(playerView: VideoView, isPrivateBrowsing: Bool) {
self.playerView = playerView
self.privateBrowsingManager = privateBrowsingManager
self.isPrivateBrowsing = isPrivateBrowsing
super.init(nibName: nil, bundle: nil)
}

Expand Down Expand Up @@ -641,7 +641,7 @@ extension PlaylistListViewController {
activityIndicator.isHidden = false

let selectedCell = tableView.cellForRow(at: indexPath) as? PlaylistCell
playerView.setVideoInfo(videoDomain: item.pageSrc, videoTitle: item.pageTitle, isPrivateBrowsing: privateBrowsingManager?.isPrivateBrowsing == true)
playerView.setVideoInfo(videoDomain: item.pageSrc, videoTitle: item.pageTitle, isPrivateBrowsing: isPrivateBrowsing)
PlaylistMediaStreamer.setNowPlayingMediaArtwork(image: selectedCell?.iconView.image)
completion?(item)
}
Expand Down Expand Up @@ -756,7 +756,7 @@ extension PlaylistListViewController {

if let url = URL(string: item.pageSrc) {
self.dismiss(animated: true, completion: nil)
let isPrivateBrowsing = self.privateBrowsingManager?.isPrivateBrowsing == true
let isPrivateBrowsing = self.isPrivateBrowsing
self.delegate?.openURLInNewTab(
url,
isPrivate: isPrivateBrowsing,
Expand Down Expand Up @@ -829,7 +829,7 @@ extension PlaylistListViewController {
let pageURL = URL(string: currentItem.pageSrc) {

self.dismiss(animated: true) {
let isPrivateBrowsing = self.privateBrowsingManager?.isPrivateBrowsing == true
let isPrivateBrowsing = self.isPrivateBrowsing
browser.tabManager.addTabAndSelect(
URLRequest(url: pageURL),
isPrivate: isPrivateBrowsing)
Expand Down
Loading

0 comments on commit 8959173

Please sign in to comment.