-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add FXIOS-12301 [Context ID] Integrate with rust context id rotation component #27195
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
Draft
Cramsden
wants to merge
2
commits into
main
Choose a base branch
from
cr/FXIOS-12301-client-id-from-as
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+297
−128
Draft
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// 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 http://mozilla.org/MPL/2.0/ | ||
|
||
import Foundation | ||
import Glean | ||
import MozillaAppServices | ||
|
||
// Contextual identifier used for the sponsored tiles in top sites and the suggestions in the search view | ||
class ContextIDManager { | ||
private var contextIDComponent: ContextIdComponent? | ||
|
||
static let shared = ContextIDManager() | ||
|
||
static func setup(isGleanMetricsAllowed allowed: Bool, isTesting: Bool) { | ||
shared.contextIDComponent = shared.setupContextId(isGleanMetricsAllowed: allowed, isTesting: isTesting) | ||
} | ||
|
||
private struct ContextIDStorage { | ||
enum UserDefaultsKey: String { | ||
case keyContextId = "com.moz.contextId.key" | ||
case keyContextIdCreationTimestamp | ||
} | ||
|
||
static var contextID: String? { | ||
get { UserDefaults.standard.object(forKey: UserDefaultsKey.keyContextId.rawValue) as? String } | ||
set { UserDefaults.standard.set(newValue, forKey: UserDefaultsKey.keyContextId.rawValue) } | ||
} | ||
|
||
static var contextIdCreationTimeStamp: Int64? { | ||
get { UserDefaults.standard.object(forKey: UserDefaultsKey.keyContextId.rawValue) as? Int64 } | ||
set { UserDefaults.standard.set(newValue, forKey: UserDefaultsKey.keyContextId.rawValue) } | ||
} | ||
|
||
static func setContextID(_ contextID: String?, creationTimestamp: Int64?) { | ||
ContextIDStorage.contextID = contextID | ||
ContextIDStorage.contextIdCreationTimeStamp = creationTimestamp | ||
} | ||
} | ||
|
||
func getContextID() -> String? { | ||
do { | ||
// TODO: Set up rotation as a feature flag | ||
return try contextIDComponent?.request(rotationDaysInS: 0) | ||
} catch { | ||
// handle error | ||
return nil | ||
} | ||
} | ||
|
||
fileprivate func setContextID(_ contextID: String?, creationTimestamp: Int64?) { | ||
ContextIDStorage.setContextID(contextID, creationTimestamp: creationTimestamp) | ||
} | ||
|
||
private func setupContextId(isGleanMetricsAllowed allowed: Bool, isTesting: Bool) -> ContextIdComponent? { | ||
do { | ||
return try ContextIdComponent( | ||
initContextId: ContextIDStorage.contextID ?? "", | ||
creationTimestampS: ContextIDStorage.contextIdCreationTimeStamp ?? 0, | ||
runningInTestAutomation: isTesting, | ||
callback: ContextIDRotationHandler(isGleanMetricsAllowed: allowed) | ||
) | ||
} catch { | ||
// catch error | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
class ContextIDRotationHandler: ContextIdCallback { | ||
private let isGleanMetricsAllowed: Bool | ||
|
||
init(isGleanMetricsAllowed: Bool) { | ||
self.isGleanMetricsAllowed = isGleanMetricsAllowed | ||
} | ||
|
||
func persist(contextId: String, creationDate: Int64) { | ||
ContextIDManager.shared.setContextID(contextId, creationTimestamp: creationDate) | ||
guard isGleanMetricsAllowed else { return } | ||
guard let uuid = UUID(uuidString: contextId) else { | ||
// log error | ||
return | ||
} | ||
GleanMetrics.TopSites.contextId.set(uuid) | ||
} | ||
|
||
func rotated(oldContextId: String) { | ||
// NO-OP | ||
/* | ||
GleanPings.contextIdDeletionRequest.setEnabled(true); | ||
Glean.contextualServices.contextId.set(oldContextId); | ||
GleanPings.contextIdDeletionRequest.submit(); | ||
*/ | ||
} | ||
} |
38 changes: 0 additions & 38 deletions
38
firefox-ios/Client/Telemetry/TelemetryContextualIdentifier.swift
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
firefox-ios/firefox-ios-tests/Tests/ClientTests/ContextIDManagerTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// 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 http://mozilla.org/MPL/2.0/ | ||
|
||
@testable import Client | ||
|
||
import Glean | ||
import XCTest | ||
|
||
class ContextIDManagerTests: XCTestCase { | ||
override func setUp() { | ||
super.setUp() | ||
DependencyHelperMock().bootstrapDependencies() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 0 additions & 59 deletions
59
firefox-ios/firefox-ios-tests/Tests/ClientTests/TelemetryContextualIdentifierTests.swift
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see
GleanPings.contextIdDeletionRequest.submit();
commented in there. Will thatContextIDManager
support deleting the context id? I had some discussion with Mike Conley (summary noted in https://mozilla-hub.atlassian.net/browse/FXIOS-11783), and we're not deleting the localcontext_id
even when toggling off interaction data but we should. Happy to discuss in coffee chat to give more context (pun intended 😄 )