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

Commit

Permalink
Fix #8674: Add default browser P3A
Browse files Browse the repository at this point in the history
  • Loading branch information
kylehickinson committed Jan 19, 2024
1 parent bd215d9 commit fb8f9bb
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Sources/Brave/Frontend/Browser/BrowserViewController/BVC+P3A.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@ import os.log

extension BrowserViewController {

func recordDefaultBrowserLikelyhoodP3A(openedHTTPLink: Bool = false) {
let isInstalledInThePastWeek: Bool = {
guard let installDate = Preferences.DAU.installationDate.value else {
// Pref is removed after 14 days
return false
}
return Date.now.timeIntervalSince(installDate) <= 7.days
}()
enum Answer: Int, CaseIterable {
case notEnoughInfo = 0
case no = 1
case yes = 2
}
let isLikelyDefault = DefaultBrowserHelper.isBraveLikelyDefaultBrowser()
let answer: Answer = {
if !openedHTTPLink, isInstalledInThePastWeek {
// Hasn't been at least 7 days
return .notEnoughInfo
}
return isLikelyDefault ? .yes : .no
}()
UmaHistogramEnumeration("Brave.IOS.DefaultBrowser", sample: answer)
}

func maybeRecordInitialShieldsP3A() {
if Preferences.Shields.initialP3AStateReported.value { return }
defer { Preferences.Shields.initialP3AStateReported.value = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ public class BrowserViewController: UIViewController {
recordGeneralBottomBarLocationP3A()
PlaylistP3A.recordHistogram()
recordAdsUsageType()
recordDefaultBrowserLikelyhoodP3A()

// Revised Review Handling
AppReviewManager.shared.handleAppReview(for: .revisedCrossPlatform, using: self)
Expand Down Expand Up @@ -3153,6 +3154,9 @@ extension BrowserViewController {
// in case an external url is triggered
if case .url(let navigatedURL, _) = path {
if navigatedURL?.isWebPage(includeDataURIs: false) == true {
Preferences.General.lastHTTPURLOpenedDate.value = .now
recordDefaultBrowserLikelyhoodP3A(openedHTTPLink: true)

Preferences.General.defaultBrowserCalloutDismissed.value = true
Preferences.DefaultBrowserIntro.defaultBrowserNotificationScheduled.value = true

Expand Down
3 changes: 3 additions & 0 deletions Sources/Brave/Frontend/ClientPreferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ extension Preferences {
static var defaultPageZoomLevel = Option<Double>(key: "general.default-page-zoom-level", default: 1.0)

static let isUsingBottomBar = Option<Bool>(key: "general.bottom-bar", default: false)

/// The last time the app opened and handed a http or https url
static let lastHTTPURLOpenedDate = Option<Date?>(key: "general.last-url-opened-date", default: nil)
}

final public class Search {
Expand Down
15 changes: 15 additions & 0 deletions Sources/Brave/Helpers/DefaultBrowserHelper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2024 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

import Foundation
import Preferences

enum DefaultBrowserHelper {
/// Whether or not its likely that Brave is the users default browser based on a 14 day usage period
static func isBraveLikelyDefaultBrowser() -> Bool {
guard let date = Preferences.General.lastHTTPURLOpenedDate.value else { return false }
return date >= Date.now.addingTimeInterval(-14.days)
}
}
42 changes: 42 additions & 0 deletions Tests/ClientTests/DefaultBrowserHelperTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2024 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

import Foundation
@testable import Brave
import XCTest
import Preferences

class DefaultBrowserHelperTests: XCTestCase {
override func setUp() async throws {
Preferences.General.lastHTTPURLOpenedDate.reset()
}

func testUserRecentlyOpenedURL() {
let date = Date()
Preferences.General.lastHTTPURLOpenedDate.value = date
let isLikelyDefault = DefaultBrowserHelper.isBraveLikelyDefaultBrowser()
XCTAssertTrue(isLikelyDefault)
}

func testUserNeverOpenedURL() {
// User never opened a link
let isLikelyDefault = DefaultBrowserHelper.isBraveLikelyDefaultBrowser()
XCTAssertFalse(isLikelyDefault)
}

func testUserOpenedURL8DaysAgo() {
let date = Date()
Preferences.General.lastHTTPURLOpenedDate.value = date.addingTimeInterval(-8.days)
let isLikelyDefault = DefaultBrowserHelper.isBraveLikelyDefaultBrowser()
XCTAssertTrue(isLikelyDefault)
}

func testUserOpenedURL30DaysAgo() {
let date = Date()
Preferences.General.lastHTTPURLOpenedDate.value = date.addingTimeInterval(-30.days)
let isLikelyDefault = DefaultBrowserHelper.isBraveLikelyDefaultBrowser()
XCTAssertFalse(isLikelyDefault)
}
}

0 comments on commit fb8f9bb

Please sign in to comment.