Skip to content

Commit

Permalink
Bug 1806740 - Add API to expose hasRuleForBrowsingContext r=geckoview…
Browse files Browse the repository at this point in the history
…-reviewers,amejiamarmol,calu

Differential Revision: https://phabricator.services.mozilla.com/D167890
  • Loading branch information
owlishDeveloper committed Feb 1, 2023
1 parent 227114a commit 65bed0f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions mobile/android/geckoview/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,7 @@ package org.mozilla.geckoview {
method @AnyThread public void goForward();
method @AnyThread public void goForward(boolean);
method @AnyThread public void gotoHistoryIndex(int);
method @AnyThread @NonNull public GeckoResult<Boolean> hasCookieBannerRuleForBrowsingContextTree();
method @UiThread public boolean isOpen();
method @AnyThread public void load(@NonNull GeckoSession.Loader);
method @AnyThread public void loadUri(@NonNull String);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.json.JSONObject
import org.junit.Assume.assumeThat
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.geckoview.ContentBlocking.CookieBannerMode
import org.mozilla.geckoview.GeckoResult
import org.mozilla.geckoview.GeckoSession
import org.mozilla.geckoview.GeckoSession.ContentDelegate
Expand Down Expand Up @@ -2062,4 +2063,55 @@ class GeckoSessionTestRuleTest : BaseSessionTest(noErrorCollector = true) {

sessionRule.waitForResult(result)
}

@Test fun checkCookieBannerRuleForSession() {
// set preferences. We have a cookie rule for example.com
val testRules = "[{\"id\":\"87815b2d-a840-4155-8713-f8a26d1f483a\",\"click\":{},\"cookies\":{\"optOut\":[{\"name\":\"foo\", \"value\":\"bar\"}]}, \"domains\":[\"example.com\"]}]"
sessionRule.setPrefsUntilTestEnd(
mapOf(
"cookiebanners.service.mode" to CookieBannerMode.COOKIE_BANNER_MODE_REJECT,
"cookiebanners.listService.testSkipRemoteSettings" to true,
"cookiebanners.listService.testRules" to testRules
)
)
var prefs = sessionRule.getPrefs(
"cookiebanners.service.mode",
"cookiebanners.listService.testSkipRemoteSettings",
"cookiebanners.listService.testRules"
)
assertThat("Cookie banner service mode should be correct", prefs[0] as Int, equalTo(1))
assertThat("Cookie banner remote settings should be skipped", prefs[1] as Boolean, equalTo(true))
assertThat("Cookie banner rule should be set", prefs[2] as String, equalTo(testRules))

// session 1 - load url for which there is no rule
mainSession.loadUri(HELLO_HTML_PATH)
sessionRule.waitForPageStop()
val response1 = mainSession.hasCookieBannerRuleForBrowsingContextTree()
sessionRule.waitForResult(response1).let {
assertThat("There should be no rule", it, equalTo(false))
}

// session 1 - load url for which there is a rule
mainSession.loadUri("https://example.com")
sessionRule.waitForPageStop()
val response2 = mainSession.hasCookieBannerRuleForBrowsingContextTree()
sessionRule.waitForResult(response2).let {
assertThat("There should be a rule", it, equalTo(true))
}

// session 2 load url for which there is no rule
val session2 = sessionRule.createOpenSession()
session2.loadUri(HELLO_HTML_PATH)
sessionRule.waitForPageStop()
val response3 = session2.hasCookieBannerRuleForBrowsingContextTree()
sessionRule.waitForResult(response3).let {
assertThat("There should be no rule", it, equalTo(false))
}

// API shoul return the correct result for the page we have loaded in session 1
val response4 = mainSession.hasCookieBannerRuleForBrowsingContextTree()
sessionRule.waitForResult(response4).let {
assertThat("There should be a rule the second time", it, equalTo(true))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2327,6 +2327,17 @@ protected FinderResult() {
return mFinder;
}

/**
* Checks whether we have a rule for this session. Uses the browsing context or any of its
* children, calls nsICookieBannerService.hasRuleForBrowsingContextTree
*
* @return {@link GeckoResult} with boolean
*/
@AnyThread
public @NonNull GeckoResult<Boolean> hasCookieBannerRuleForBrowsingContextTree() {
return mEventDispatcher.queryBoolean("GeckoView:HasCookieBannerRuleForBrowsingContextTree");
}

/**
* Set this GeckoSession as active or inactive, which represents if the session is currently
* visible or not. Setting a GeckoSession to inactive will significantly reduce its memory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ exclude: true
- ⚠️ Deprecated [`CookieBannerMode.COOKIE_BANNER_MODE_DETECT_ONLY`][111.1]
- Added [`GeckoView.ActivityContextDelegate`][111.3], `setActivityContextDelegate`, and `getActivityContextDelegate` to `GeckoView`
- Added [`GeckoSession.PrintDelegate`][111.4], a [`PrintDocumentAdapter`][111.5], getters and setters for the `PrintDelegate`, and [`printPageContent`] to print [`session content`][111.6]
- Added [`GeckoSession.hasCookieBannerRuleForBrowsingContextTree`][111.7] to expose Gecko API nsICookieBannerService::hasRuleForBrowsingContextTree see ([bug 1806740]({{bugzilla}}1806740))

[111.1]: {{javadoc_uri}}/ContentBlocking.CookieBannerMode.html#COOKIE_BANNER_MODE_DETECT_ONLY
[111.2]: {{javadoc_uri}}/ContentBlocking.Settings.Builder.html#cookieBannerHandlingDetectOnlyMode(boolean)
[111.3]: {{javadoc_uri}}/GeckoView.ActivityContextDelegate.html
[111.4]: {{javadoc_uri}}/GeckoSession.PrintDelegate.html
[111.5]: {{javadoc_uri}}/GeckoViewPrintDocumentAdapter.html
[111.6]: {{javadoc_uri}}/GeckoSession.html#printPageContent--
[111.7]: {{javadoc_uri}}/GeckoSession.html#hasCookieBannerRuleForBrowsingContextTree()

## v110
- Added [`GeckoSession.ContentDelegate.onCookieBannerDetected`][110.1] and [`GeckoSession.ContentDelegate.onCookieBannerHandled`][110.2]
Expand Down Expand Up @@ -1309,4 +1311,4 @@ to allow adding gecko profiler markers.
[65.24]: {{javadoc_uri}}/CrashReporter.html#sendCrashReport(android.content.Context,android.os.Bundle,java.lang.String)
[65.25]: {{javadoc_uri}}/GeckoResult.html

[api-version]: d77f7067d9a176270b885a48ccb315dedc6bceb8
[api-version]: d4e2dda372380fde58318d9485207cd21fc4af91
11 changes: 11 additions & 0 deletions mobile/android/modules/geckoview/GeckoViewContent.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class GeckoViewContent extends GeckoViewModule {
"GeckoView:ClearMatches",
"GeckoView:DisplayMatches",
"GeckoView:FindInPage",
"GeckoView:HasCookieBannerRuleForBrowsingContextTree",
"GeckoView:RestoreState",
"GeckoView:ContainsFormData",
"GeckoView:ScrollBy",
Expand Down Expand Up @@ -192,6 +193,9 @@ class GeckoViewContent extends GeckoViewModule {
case "GeckoView:ContainsFormData":
this._containsFormData(aCallback);
break;
case "GeckoView:HasCookieBannerRuleForBrowsingContextTree":
this._hasCookieBannerRuleForBrowsingContextTree(aCallback);
break;
}
}

Expand Down Expand Up @@ -302,6 +306,13 @@ class GeckoViewContent extends GeckoViewModule {
aCallback.onSuccess(await this.actor.containsFormData());
}

async _hasCookieBannerRuleForBrowsingContextTree(aCallback) {
const { browsingContext } = this.actor;
aCallback.onSuccess(
Services.cookieBanners.hasRuleForBrowsingContextTree(browsingContext)
);
}

_findInPage(aData, aCallback) {
debug`findInPage: data=${aData} callback=${aCallback && "non-null"}`;

Expand Down

0 comments on commit 65bed0f

Please sign in to comment.