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

Commit

Permalink
Fix #337: Implement Private Browsing Only mode
Browse files Browse the repository at this point in the history
  • Loading branch information
kylehickinson authored and Joel Reis committed Oct 24, 2018
1 parent e72060f commit e1d2df2
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 19 deletions.
3 changes: 3 additions & 0 deletions Client/Application/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UIViewControllerRestorati

// Add restoration class, the factory that will return the ViewController we
// will restore with.

// Make sure current private browsing flag respects the private browsing only user preference
PrivateBrowsingManager.shared.isPrivateBrowsing = Preferences.Privacy.privateBrowsingOnly.value

browserViewController = BrowserViewController(profile: self.profile!, tabManager: self.tabManager)
browserViewController.edgesForExtendedLayout = []
Expand Down
14 changes: 10 additions & 4 deletions Client/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class BrowserViewController: UIViewController {
downloadQueue.delegate = self

// Observe some user preferences
Preferences.Privacy.privateBrowsingOnly.observe(from: self)
Preferences.Privacy.cookieAcceptPolicy.observe(from: self)
Preferences.General.tabBarVisibility.observe(from: self)
Preferences.Shields.fingerprintingProtection.observe(from: self)
Expand Down Expand Up @@ -2631,7 +2632,7 @@ extension BrowserViewController: TabTrayDelegate {
extension BrowserViewController: Themeable {

func applyTheme(_ theme: Theme) {
let ui: [Themeable?] = [urlBar, toolbar, readerModeBar, tabsBar]
let ui: [Themeable?] = [urlBar, toolbar, readerModeBar, tabsBar, favoritesViewController]
ui.forEach { $0?.applyTheme(theme) }
statusBarOverlay.backgroundColor = urlBar.backgroundColor
setNeedsStatusBarAppearanceUpdate()
Expand Down Expand Up @@ -2755,10 +2756,15 @@ extension BrowserViewController: PreferencesObserver {
case Preferences.General.tabBarVisibility.key:
updateTabsBarVisibility()
case Preferences.Privacy.privateBrowsingOnly.key:
if Preferences.Privacy.privateBrowsingOnly.value {
switchToPrivacyMode(isPrivate: true)
// TODO: Add more logic to `switchToPrivacyMode` once specific Brave Private Browsing logic is added back
let isPrivate = Preferences.Privacy.privateBrowsingOnly.value
switchToPrivacyMode(isPrivate: isPrivate)
PrivateBrowsingManager.shared.isPrivateBrowsing = isPrivate
if let firstTab = tabManager.tabs(withType: isPrivate ? .private : .regular).first {
tabManager.selectTab(firstTab)
} else {
tabManager.addTabAndSelect(nil, isPrivate: isPrivate)
}
updateTabsBarVisibility()
case Preferences.Shields.fingerprintingProtection.key:
// TODO: Update fingerprinting protection based on `Preferences.Shields.fingerprintingProtection` once fingerprinting protection is added back
break
Expand Down
15 changes: 7 additions & 8 deletions Client/Frontend/Browser/HomePanel/FavoritesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protocol TopSitesDelegate: class {
func didTapDuckDuckGoCallout()
}

class FavoritesViewController: UIViewController {
class FavoritesViewController: UIViewController, Themeable {
private struct UI {
static let statsHeight: CGFloat = 110.0
static let statsBottomMargin: CGFloat = 5
Expand Down Expand Up @@ -198,16 +198,15 @@ class FavoritesViewController: UIViewController {

// MARK: - Private browsing modde
@objc func privateBrowsingModeChanged() {
let isPrivateBrowsing = PrivateBrowsingManager.shared.isPrivateBrowsing

// TODO: This entire blockshould be abstracted
// to make code in this class DRY (duplicates from elsewhere)
view.backgroundColor = PrivateBrowsingManager.shared.isPrivateBrowsing ? UX.HomePanel.BackgroundColorPBM : UX.HomePanel.BackgroundColor
braveShieldStatsView.timeStatView.color = isPrivateBrowsing ? UX.GreyA : UX.GreyJ
collection.reloadData()
updateDuckDuckGoVisibility()
}

func applyTheme(_ theme: Theme) {
let isPrivate = theme == .private
view.backgroundColor = isPrivate ? UX.HomePanel.BackgroundColorPBM : UX.HomePanel.BackgroundColor
braveShieldStatsView.timeStatView.color = isPrivate ? UX.GreyA : UX.GreyJ
}

@objc func showPrivateTabInfo() {
let url = URL(string: "https://github.com/brave/browser-laptop/wiki/What-a-Private-Tab-actually-does")!
DispatchQueue.main.async {
Expand Down
6 changes: 4 additions & 2 deletions Client/Frontend/Browser/TabManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -857,10 +857,12 @@ extension TabManager {
}
isRestoring = false

let isPrivateBrowsingOnly = Preferences.Privacy.privateBrowsingOnly.value

// Always make sure there is a single normal tab.
let tabs = self.tabs(withType: .regular)
let tabs = self.tabs(withType: isPrivateBrowsingOnly ? .private : .regular)
if tabs.isEmpty {
let tab = addTab()
let tab = addTab(isPrivate: isPrivateBrowsingOnly)
if selectedTab == nil {
selectTab(tab)
}
Expand Down
16 changes: 16 additions & 0 deletions Client/Frontend/Browser/TabTrayController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ class TabTrayController: UIViewController, Themeable {
super.init(nibName: nil, bundle: nil)

tabManager.addDelegate(self)

Preferences.Privacy.privateBrowsingOnly.observe(from: self)
}

convenience init(tabManager: TabManager, profile: Profile, tabTrayDelegate: TabTrayDelegate) {
Expand Down Expand Up @@ -320,6 +322,8 @@ class TabTrayController: UIViewController, Themeable {

view.addSubview(collectionView)
view.addSubview(toolbar)

updatePrivateModeButtonVisibility()

makeConstraints()

Expand Down Expand Up @@ -550,6 +554,18 @@ class TabTrayController: UIViewController, Themeable {
tabManager.removeTabsWithUndoToast(tabsToDisplay)
self.collectionView.reloadData()
}

func updatePrivateModeButtonVisibility() {
toolbar.privateModeButton.isHidden = Preferences.Privacy.privateBrowsingOnly.value
}
}

extension TabTrayController: PreferencesObserver {
func preferencesDidChange(for key: String) {
if key == Preferences.Privacy.privateBrowsingOnly.key {
updatePrivateModeButtonVisibility()
}
}
}

// MARK: - App Notifications
Expand Down
13 changes: 8 additions & 5 deletions Client/Frontend/Widgets/Theme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ enum Theme: String {
/// - parameter tab: An object representing a Tab.
/// - returns: A Tab theme.
static func of(_ tab: Tab?) -> Theme {
switch TabType.of(tab) {
case .regular:
return .regular
case .private:
return .private
if let tab = tab {
switch TabType.of(tab) {
case .regular:
return .regular
case .private:
return .private
}
}
return PrivateBrowsingManager.shared.isPrivateBrowsing ? .private : .regular
}

}

0 comments on commit e1d2df2

Please sign in to comment.