This repository was archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 476
Support for tracking external sources #10711
Merged
grigoryk
merged 2 commits into
mozilla-mobile:main
from
grigoryk:externalReferrerPlumbing
Aug 5, 2021
Merged
Changes from all commits
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 |
|---|---|---|
|
|
@@ -9,8 +9,11 @@ import android.util.JsonReader | |
| import android.util.JsonWriter | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import mozilla.components.browser.state.state.EngineState | ||
| import mozilla.components.browser.state.state.ExternalPackage | ||
| import mozilla.components.browser.state.state.LastMediaAccessState | ||
| import mozilla.components.browser.state.state.PackageCategory | ||
| import mozilla.components.browser.state.state.ReaderState | ||
| import mozilla.components.browser.state.state.SessionState | ||
| import mozilla.components.browser.state.state.TabSessionState | ||
| import mozilla.components.browser.state.state.createTab | ||
| import mozilla.components.concept.engine.Engine | ||
|
|
@@ -94,22 +97,24 @@ class BrowserStateWriterReaderTest { | |
| } | ||
|
|
||
| @Test | ||
| fun `Read tab with session source`() { | ||
| // We don't write tabs with session source anymore but need to be tolerant to | ||
| // session source being in the JSON to remain backward compatible. | ||
| fun `Read tab with deprecated session source`() { | ||
| // We don't persist session source of tabs unless it's an external source. | ||
| // However, in older versions we did persist other types of sources so need to be tolerant | ||
| // if these older cases are encountered in JSON to remain backward compatible. | ||
| val engineState = createFakeEngineState() | ||
| val engine = createFakeEngine(engineState) | ||
| val tab = createTab(url = "https://www.mozilla.org", title = "Mozilla") | ||
| val file = AtomicFile( | ||
| File.createTempFile(UUID.randomUUID().toString(), UUID.randomUUID().toString()) | ||
| ) | ||
| writeTabWithSource(tab, file) | ||
| writeTabWithDeprecatedSource(tab, file) | ||
|
|
||
| // When reading we don't care about the source either as we will just set | ||
| // it to RESTORED. So we just need to make sure we de-serialized successfully. | ||
| // When reading a tab that didn't have a source persisted, we just need to make sure | ||
| // it is deserialized correctly. In this case, source defaults to `Internal.Restored`. | ||
| val reader = BrowserStateReader() | ||
| val restoredTab = reader.readTab(engine, file) | ||
| assertNotNull(restoredTab!!) | ||
| assertEquals(SessionState.Source.Internal.None, restoredTab.source) | ||
|
|
||
| assertEquals("https://www.mozilla.org", restoredTab.url) | ||
| assertEquals("Mozilla", restoredTab.title) | ||
|
|
@@ -147,6 +152,105 @@ class BrowserStateWriterReaderTest { | |
| assertEquals(tab.content.url, restoredTab.historyMetadata!!.url) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Read and write tab with external custom tab source and full caller`() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once this is in Fenix it would be nice to add a new test case to |
||
| val engineState = createFakeEngineState() | ||
| val engine = createFakeEngine(engineState) | ||
|
|
||
| val tab = createTab( | ||
| url = "https://www.mozilla.org", | ||
| title = "Mozilla", | ||
| contextId = "work", | ||
| source = SessionState.Source.External.CustomTab( | ||
| caller = ExternalPackage("com.mozilla.test", PackageCategory.PRODUCTIVITY) | ||
| ) | ||
| ) | ||
|
|
||
| val writer = BrowserStateWriter() | ||
| val reader = BrowserStateReader() | ||
|
|
||
| val file = AtomicFile( | ||
| File.createTempFile(UUID.randomUUID().toString(), UUID.randomUUID().toString()) | ||
| ) | ||
|
|
||
| assertTrue(writer.writeTab(tab, file)) | ||
|
|
||
| val restoredTab = reader.readTab(engine, file) | ||
| assertNotNull(restoredTab!!) | ||
|
|
||
| assertNotNull(restoredTab.source) | ||
| assertTrue(restoredTab.source is SessionState.Source.External.CustomTab) | ||
| with(restoredTab.source as SessionState.Source.External.CustomTab) { | ||
| assertEquals("com.mozilla.test", this.caller!!.packageId) | ||
| assertEquals(PackageCategory.PRODUCTIVITY, this.caller!!.category) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `Read and write tab with external action view source and partial caller`() { | ||
| val engineState = createFakeEngineState() | ||
| val engine = createFakeEngine(engineState) | ||
|
|
||
| val tab = createTab( | ||
| url = "https://www.mozilla.org", | ||
| title = "Mozilla", | ||
| contextId = "work", | ||
| source = SessionState.Source.External.ActionView( | ||
| caller = ExternalPackage("com.mozilla.test", category = PackageCategory.UNKNOWN) | ||
| ) | ||
| ) | ||
|
|
||
| val writer = BrowserStateWriter() | ||
| val reader = BrowserStateReader() | ||
|
|
||
| val file = AtomicFile( | ||
| File.createTempFile(UUID.randomUUID().toString(), UUID.randomUUID().toString()) | ||
| ) | ||
|
|
||
| assertTrue(writer.writeTab(tab, file)) | ||
|
|
||
| val restoredTab = reader.readTab(engine, file) | ||
| assertNotNull(restoredTab!!) | ||
|
|
||
| assertNotNull(restoredTab.source) | ||
| assertTrue(restoredTab.source is SessionState.Source.External.ActionView) | ||
| with(restoredTab.source as SessionState.Source.External.ActionView) { | ||
| assertEquals("com.mozilla.test", this.caller!!.packageId) | ||
| assertEquals(PackageCategory.UNKNOWN, this.caller!!.category) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `Read and write tab with external action send source and missing caller`() { | ||
| val engineState = createFakeEngineState() | ||
| val engine = createFakeEngine(engineState) | ||
|
|
||
| val tab = createTab( | ||
| url = "https://www.mozilla.org", | ||
| title = "Mozilla", | ||
| contextId = "work", | ||
| source = SessionState.Source.External.ActionSend(caller = null) | ||
| ) | ||
|
|
||
| val writer = BrowserStateWriter() | ||
| val reader = BrowserStateReader() | ||
|
|
||
| val file = AtomicFile( | ||
| File.createTempFile(UUID.randomUUID().toString(), UUID.randomUUID().toString()) | ||
| ) | ||
|
|
||
| assertTrue(writer.writeTab(tab, file)) | ||
|
|
||
| val restoredTab = reader.readTab(engine, file) | ||
| assertNotNull(restoredTab!!) | ||
|
|
||
| assertNotNull(restoredTab.source) | ||
| assertTrue(restoredTab.source is SessionState.Source.External.ActionSend) | ||
| with(restoredTab.source as SessionState.Source.External.ActionSend) { | ||
| assertNull(this.caller) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `Read and write tab with LastMediaAccessState`() { | ||
| val engineState = createFakeEngineState() | ||
|
|
@@ -252,11 +356,11 @@ private fun createFakeEngine(engineState: EngineSessionState): Engine { | |
| return engine | ||
| } | ||
|
|
||
| private fun writeTabWithSource(tab: TabSessionState, file: AtomicFile) { | ||
| file.streamJSON { tabWithSource(tab) } | ||
| private fun writeTabWithDeprecatedSource(tab: TabSessionState, file: AtomicFile) { | ||
| file.streamJSON { tabWithDeprecatedSource(tab) } | ||
| } | ||
|
|
||
| private fun JsonWriter.tabWithSource( | ||
| private fun JsonWriter.tabWithDeprecatedSource( | ||
| tab: TabSessionState | ||
| ) { | ||
| beginObject() | ||
|
|
@@ -272,8 +376,8 @@ private fun JsonWriter.tabWithSource( | |
| name(Keys.SESSION_TITLE) | ||
| value(tab.content.title) | ||
|
|
||
| name(Keys.SESSION_SOURCE_KEY) | ||
| value(tab.source.name) | ||
| name(Keys.SESSION_DEPRECATED_SOURCE_KEY) | ||
| value(tab.source.toString()) | ||
|
|
||
| endObject() | ||
| } | ||
|
|
||
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
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.
This is a bit dangerous and we need to check the existing behaviors: We did not restore the source on purpose. There have been some parts of the code that look at new tabs with specific sources and log telemetry etc. If we now restore them they may show up as things like "new external app" to observers.
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.
A safer approach may be to introduce a new property - and potentially deprecate the existing one.
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.
@pocmo yeah we were concerned about this too, but I couldn't find a case where this would be a problem? Possible I missed it. Do you have any hints where to look? :)
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 was basically looking for readers of
SessionState.Sourcewhich make decisions based on it, but didn't find much other than what's covered in: https://github.com/mozilla-mobile/fenix/pull/20578/files?Uh oh!
There was an error while loading. Please reload this page.
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.
OK made another pass and couldn't find anything except: https://github.com/mozilla-mobile/fenix/pull/20578/files#r682729976 which we could handle, but not sure if worth it?
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.
OK, I think I would vote for going ahead and maybe handling the one case. Keeping both the old deprecated source and new one might cause other issues and extra work. Since we don't really know a case that could break maybe we should find out and worst case back out?
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.
Let's double check though in case I missed anything :).
Uh oh!
There was an error while loading. Please reload this page.
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.
@pocmo we found the old ticket that prompted us to not restore the source field: mozilla-mobile/fenix#9937
Basically to make sure the back button behaviour is accurate for tabs originally opened from a VIEW intent where back should close the app, but when restored it shouldn't. We can introduce a separate state (as part of Source) for this because technically
restoredis not a source, but additional information.