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

Unified Diff: chrome/browser/android/interests_service.cc

Issue 1396443002: Add the JNI code in order to let Java use the InterestsFetcher. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 months 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/android/interests_service.cc
diff --git a/chrome/browser/android/interests_service.cc b/chrome/browser/android/interests_service.cc
new file mode 100644
index 0000000000000000000000000000000000000000..c111070085e7cd3b000646f751322dde06583f4f
--- /dev/null
+++ b/chrome/browser/android/interests_service.cc
@@ -0,0 +1,104 @@
+// Copyright 2015 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/android/interests_service.h"
+
+#include "base/android/jni_android.h"
+#include "base/android/jni_array.h"
+#include "base/android/jni_string.h"
+#include "base/android/scoped_java_ref.h"
+#include "chrome/browser/interests/interests_fetcher.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/profiles/profile_android.h"
+#include "jni/InterestsService_jni.h"
+
+using base::android::AttachCurrentThread;
+using base::android::ConvertJavaStringToUTF8;
+using base::android::ConvertUTF8ToJavaString;
+using base::android::ScopedJavaGlobalRef;
+using base::android::ScopedJavaLocalRef;
+using base::android::ToJavaArrayOfStrings;
+
+const char kInterestJavaClass[] =
Bernhard Bauer 2015/10/07 15:26:37 This is now unused.
PEConn 2015/10/07 18:27:20 Done.
+ "org/chromium/chrome/browser/ntp/InterestsService$Interest";
+
+InterestsService::InterestsService(Profile* profile)
+ : profile_(profile), weak_ptr_factory_(this) {}
+
+InterestsService::~InterestsService() {}
+
+void InterestsService::Destroy(JNIEnv* env, jobject obj) {
+ delete this;
+}
+
+void InterestsService::GetInterests(JNIEnv* env,
+ jobject obj,
+ jobject j_callback_obj) {
+
+ ScopedJavaGlobalRef<jobject> j_callback(env, j_callback_obj);
+
+ scoped_ptr<InterestsFetcher> fetcher =
+ InterestsFetcher::CreateFromProfile(profile_);
+
+ InterestsFetcher::InterestsCallback callback = base::Bind(
+ &InterestsService::OnObtainedInterests,
+ weak_ptr_factory_.GetWeakPtr(),
+ base::Owned(fetcher.get()),
Marc Treib 2015/10/07 15:03:01 Can you use base::Passed(fetcher), and remove the
PEConn 2015/10/07 18:27:20 Done.
+ j_callback);
+
+ fetcher->FetchInterests(callback);
+
+ // The callback owns InterestsFetcher now.
+ ignore_result(fetcher.release());
+}
+
+// static
+bool InterestsService::Register(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+jobjectArray InterestsService::ConvertInterestsToJava(
+ scoped_ptr<std::vector<InterestsFetcher::Interest>> interests) {
+ if (!interests)
+ return nullptr;
+
+ JNIEnv* env = AttachCurrentThread();
Bernhard Bauer 2015/10/07 15:26:37 What I meant in my previous comment was to pass |e
PEConn 2015/10/07 18:27:20 Done.
+
+ jobjectArray j_interests =
+ Java_InterestsService_createInterestsArray(env, interests->size()).obj();
+
+ for (size_t i = 0; i != interests->size(); i++) {
+ auto& interest = interests->at(i);
Bernhard Bauer 2015/10/07 15:26:37 Isn't this type just InterestsFetcher::Interest? I
PEConn 2015/10/07 18:27:20 Done. Good point about at throwing an exception. I
+ ScopedJavaLocalRef<jobject> j_interest =
+ Java_InterestsService_createInterest(
+ env,
+ ConvertUTF8ToJavaString(env, interest.name).obj(),
+ ConvertUTF8ToJavaString(env, interest.image_url.spec()).obj(),
+ interest.relevance);
+
+ env->SetObjectArrayElement(j_interests, i, j_interest.obj());
+ }
+
+ return j_interests;
+}
+
+void InterestsService::OnObtainedInterests(
+ InterestsFetcher* fetcher,
+ const ScopedJavaGlobalRef<jobject>& j_callback,
+ scoped_ptr<std::vector<InterestsFetcher::Interest>> interests) {
+
Marc Treib 2015/10/07 15:03:01 nit: remove empty line
PEConn 2015/10/07 18:27:20 Done.
+ JNIEnv* env = AttachCurrentThread();
+ jobjectArray j_interests = ConvertInterestsToJava(interests.Pass());
+ Java_GetInterestsCallback_onInterestsAvailableCallback(env,
+ j_callback.obj(),
+ j_interests);
+}
+
+static jlong Init(JNIEnv* env,
+ const JavaParamRef<jobject>& jobj,
+ const JavaParamRef<jobject>& jprofile) {
+ InterestsService* interests_service =
+ new InterestsService(ProfileAndroid::FromProfileAndroid(jprofile));
+ return reinterpret_cast<intptr_t>(interests_service);
+}

Powered by Google App Engine
This is Rietveld 408576698