Commit 1342282d authored by Evan Stade's avatar Evan Stade Committed by Chromium LUCI CQ

Prepare SiteEngagementServiceAndroid for componentization.

Change Profile to BrowserContext/BrowserContextHandle.

Note that SiteEngagementServiceTest has Chrome dependencies
and can't move to //components.

Bug: 1147274
Change-Id: Icaf2a007bc427d5f5276189c9161169a63f51f86
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2601454
Commit-Queue: Evan Stade <estade@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Cr-Commit-Position: refs/heads/master@{#840847}
parent e83399b1
......@@ -115,7 +115,7 @@ public class TabContext {
}
public double getSiteEngagementScore() {
return SiteEngagementService.getForProfile(Profile.getLastUsedRegularProfile())
return SiteEngagementService.getForBrowserContext(Profile.getLastUsedRegularProfile())
.getScore(visibleUrl);
}
......
......@@ -230,7 +230,7 @@ public final class PeriodicBackgroundSyncTest {
private void resetEngagementForUrl(final String url, final double engagement) {
TestThreadUtils.runOnUiThreadBlocking(() -> {
// TODO (https://crbug.com/1063807): Add incognito mode tests.
SiteEngagementService.getForProfile(Profile.getLastUsedRegularProfile())
SiteEngagementService.getForBrowserContext(Profile.getLastUsedRegularProfile())
.resetBaseScoreForUrl(url, engagement);
});
}
......
......@@ -116,7 +116,8 @@ public class NotificationPlatformBridgeTest {
@Override
public Double call() {
// TODO (https://crbug.com/1063807): Add incognito mode tests.
return SiteEngagementService.getForProfile(Profile.getLastUsedRegularProfile())
return SiteEngagementService
.getForBrowserContext(Profile.getLastUsedRegularProfile())
.getScore(mPermissionTestRule.getOrigin());
}
});
......
......@@ -624,7 +624,7 @@ public class PortalsTest {
final String url = WebappTestPage.getServiceWorkerUrlWithAction(
mTestServer, "call_stashed_prompt_on_click");
TestThreadUtils.runOnUiThreadBlocking(() -> {
SiteEngagementService.getForProfile(Profile.getLastUsedRegularProfile())
SiteEngagementService.getForBrowserContext(Profile.getLastUsedRegularProfile())
.resetBaseScoreForUrl(url, 10);
});
......
......@@ -278,7 +278,7 @@ public class AppBannerManagerTest {
private void resetEngagementForUrl(final String url, final double engagement) {
ThreadUtils.runOnUiThreadBlocking(() -> {
// TODO (https://crbug.com/1063807): Add incognito mode tests.
SiteEngagementService.getForProfile(Profile.getLastUsedRegularProfile())
SiteEngagementService.getForBrowserContext(Profile.getLastUsedRegularProfile())
.resetBaseScoreForUrl(url, engagement);
});
}
......
......@@ -8,10 +8,10 @@ import org.chromium.base.ThreadUtils;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.components.embedder_support.browser_context.BrowserContextHandle;
/**
* Provides access to the Site Engagement Service for a profile.
* Provides access to the Site Engagement Service for a browser context.
*
* Site engagement measures the level of engagement that a user has with an origin. This class
* allows Java to retrieve and modify engagement scores for URLs.
......@@ -22,12 +22,13 @@ public class SiteEngagementService {
private long mNativePointer;
/**
* Returns a SiteEngagementService for the provided profile.
* Returns a SiteEngagementService for the provided browser context.
* Must be called on the UI thread.
*/
public static SiteEngagementService getForProfile(Profile profile) {
public static SiteEngagementService getForBrowserContext(BrowserContextHandle browserContext) {
assert ThreadUtils.runningOnUiThread();
return SiteEngagementServiceJni.get().siteEngagementServiceForProfile(profile);
return SiteEngagementServiceJni.get().siteEngagementServiceForBrowserContext(
browserContext);
}
/**
......@@ -64,7 +65,7 @@ public class SiteEngagementService {
return new SiteEngagementService(nativePointer);
}
/** This object may only be created via the static getForProfile method. */
/** This object may only be created via the static getForBrowserContext method. */
private SiteEngagementService(long nativePointer) {
mNativePointer = nativePointer;
}
......@@ -76,7 +77,8 @@ public class SiteEngagementService {
@NativeMethods
interface Natives {
SiteEngagementService siteEngagementServiceForProfile(Profile profile);
SiteEngagementService siteEngagementServiceForBrowserContext(
BrowserContextHandle browserContext);
void setParamValuesForTesting();
double getScore(
long nativeSiteEngagementServiceAndroid, SiteEngagementService caller, String url);
......
......@@ -40,7 +40,7 @@ public class SiteEngagementServiceTest {
public void run() {
final String url = "https://www.example.com";
SiteEngagementService service =
SiteEngagementService.getForProfile(Profile.fromWebContents(
SiteEngagementService.getForBrowserContext(Profile.fromWebContents(
mActivityTestRule.getActivity().getActivityTab().getWebContents()));
Assert.assertEquals(0.0, service.getScore(url), 0);
......@@ -68,14 +68,14 @@ public class SiteEngagementServiceTest {
mActivityTestRule.getActivity().getActivityTab().getWebContents());
Assert.assertEquals(
0.0, SiteEngagementService.getForProfile(profile).getScore(url), 0);
SiteEngagementService.getForProfile(profile).resetBaseScoreForUrl(url, 5.0);
0.0, SiteEngagementService.getForBrowserContext(profile).getScore(url), 0);
SiteEngagementService.getForBrowserContext(profile).resetBaseScoreForUrl(url, 5.0);
Assert.assertEquals(
5.0, SiteEngagementService.getForProfile(profile).getScore(url), 0);
5.0, SiteEngagementService.getForBrowserContext(profile).getScore(url), 0);
SiteEngagementService.getForProfile(profile).resetBaseScoreForUrl(url, 2.0);
SiteEngagementService.getForBrowserContext(profile).resetBaseScoreForUrl(url, 2.0);
Assert.assertEquals(
2.0, SiteEngagementService.getForProfile(profile).getScore(url), 0);
2.0, SiteEngagementService.getForBrowserContext(profile).getScore(url), 0);
}
});
}
......
......@@ -7,7 +7,8 @@
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "chrome/browser/engagement/android/jni_headers/SiteEngagementService_jni.h"
#include "chrome/browser/profiles/profile_android.h"
#include "chrome/browser/engagement/site_engagement_service.h"
#include "components/embedder_support/android/browser_context/browser_context_handle.h"
#include "components/site_engagement/content/site_engagement_score.h"
#include "url/gurl.h"
......@@ -70,11 +71,11 @@ void JNI_SiteEngagementService_SetParamValuesForTesting(JNIEnv* env) {
}
base::android::ScopedJavaLocalRef<jobject>
JNI_SiteEngagementService_SiteEngagementServiceForProfile(
JNI_SiteEngagementService_SiteEngagementServiceForBrowserContext(
JNIEnv* env,
const JavaParamRef<jobject>& jprofile) {
Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
SiteEngagementService* service = SiteEngagementService::Get(profile);
const base::android::JavaParamRef<jobject>& jhandle) {
SiteEngagementService* service = SiteEngagementService::Get(
browser_context::BrowserContextFromJavaHandle(jhandle));
DCHECK(service);
return base::android::ScopedJavaLocalRef<jobject>(
......
......@@ -6,14 +6,14 @@
#define CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_ANDROID_H_
#include "base/android/scoped_java_ref.h"
#include "base/macros.h"
#include "chrome/browser/engagement/site_engagement_service.h"
namespace site_engagement {
class SiteEngagementService;
// Wrapper class to expose the Site Engagement Service to Java. This object is
// owned by the |service_| which it wraps, and is lazily created when
// a Java-side SiteEngagementService is constructed. Once created, all future
// owned by the |service_| which it wraps, and is lazily created when a
// Java-side SiteEngagementService is constructed. Once created, all future
// Java-side requests for a SiteEngagementService will use the same native
// object.
//
......@@ -27,6 +27,9 @@ class SiteEngagementServiceAndroid {
SiteEngagementService* service);
SiteEngagementServiceAndroid(JNIEnv* env, SiteEngagementService* service);
SiteEngagementServiceAndroid(const SiteEngagementServiceAndroid&) = delete;
SiteEngagementServiceAndroid& operator=(
const SiteEngagementServiceAndroid& other) = delete;
~SiteEngagementServiceAndroid();
......@@ -42,8 +45,6 @@ class SiteEngagementServiceAndroid {
private:
base::android::ScopedJavaGlobalRef<jobject> java_service_;
SiteEngagementService* service_;
DISALLOW_COPY_AND_ASSIGN(SiteEngagementServiceAndroid);
};
} // namespace site_engagement
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment