Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1492)

Unified Diff: chrome/browser/engagement/site_engagement_service_android.cc

Issue 2553013002: Expose the Site Engagement Service to Java. (Closed)
Patch Set: LocalRef to GlobalRef Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/engagement/site_engagement_service_android.cc
diff --git a/chrome/browser/engagement/site_engagement_service_android.cc b/chrome/browser/engagement/site_engagement_service_android.cc
new file mode 100644
index 0000000000000000000000000000000000000000..9d71bf952941d0a91ea7d7bda6e5c98ce9444a54
--- /dev/null
+++ b/chrome/browser/engagement/site_engagement_service_android.cc
@@ -0,0 +1,81 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/engagement/site_engagement_service_android.h"
+
+#include "base/android/jni_android.h"
+#include "base/android/jni_string.h"
+#include "base/memory/ptr_util.h"
+#include "chrome/browser/profiles/profile_android.h"
+#include "jni/SiteEngagementService_jni.h"
+#include "url/gurl.h"
+
+using base::android::JavaParamRef;
+
+// static
+bool SiteEngagementServiceAndroid::Register(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+// static
+const base::android::ScopedJavaGlobalRef<jobject>&
+SiteEngagementServiceAndroid::GetOrCreate(JNIEnv* env,
+ SiteEngagementService* service) {
+ SiteEngagementServiceAndroid* android_service = service->GetAndroidService();
+ if (!android_service) {
+ service->SetAndroidService(
+ base::MakeUnique<SiteEngagementServiceAndroid>(env, service));
+ android_service = service->GetAndroidService();
+ }
+
+ return android_service->java_service_;
+}
+
+SiteEngagementServiceAndroid::SiteEngagementServiceAndroid(
+ JNIEnv* env,
+ SiteEngagementService* service)
+ : service_(service) {
+ java_service_.Reset(Java_SiteEngagementService_create(
+ env, reinterpret_cast<uintptr_t>(this)));
+}
+
+SiteEngagementServiceAndroid::~SiteEngagementServiceAndroid() {
+ Java_SiteEngagementService_onNativeDestroyed(
+ base::android::AttachCurrentThread(), java_service_);
+ java_service_.Reset();
+}
+
+double SiteEngagementServiceAndroid::GetScore(
+ JNIEnv* env,
+ const JavaParamRef<jobject>& caller,
+ const JavaParamRef<jstring>& jurl) const {
+ if (!jurl)
+ return 0;
+
+ return service_->GetScore(
+ GURL(base::android::ConvertJavaStringToUTF16(env, jurl)));
+}
+
+void SiteEngagementServiceAndroid::ResetScoreForURL(
+ JNIEnv* env,
+ const JavaParamRef<jobject>& caller,
+ const JavaParamRef<jstring>& jurl,
+ double score) {
+ if (jurl) {
+ service_->ResetScoreForURL(
+ GURL(base::android::ConvertJavaStringToUTF16(env, jurl)), score);
+ }
+}
+
+base::android::ScopedJavaLocalRef<jobject> SiteEngagementServiceForProfile(
benwells 2016/12/09 04:31:32 Out of interest, how does this get hooked up to na
dominickn 2016/12/09 04:44:17 The JNI bindings generator generates the declarati
benwells 2016/12/09 04:57:36 Cool, thanks for the explanation!
+ JNIEnv* env,
+ const JavaParamRef<jclass>& clazz,
+ const JavaParamRef<jobject>& jprofile) {
+ Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
+ SiteEngagementService* service = SiteEngagementService::Get(profile);
+ DCHECK(service);
+
+ return base::android::ScopedJavaLocalRef<jobject>(
+ SiteEngagementServiceAndroid::GetOrCreate(env, service));
+}

Powered by Google App Engine
This is Rietveld 408576698