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

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

Issue 1334233003: Add the JNI code in order to let Java use the InterestsFetcher. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ntp-interests-retriever
Patch Set: Fix some of the comments 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
« no previous file with comments | « chrome/browser/android/interests_service.h ('k') | chrome/browser/android/interests_service_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..a6e1d173ad4354519ce6fde616faa942858b919f
--- /dev/null
+++ b/chrome/browser/android/interests_service.cc
@@ -0,0 +1,97 @@
+// 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::ScopedJavaGlobalRef;
+using base::android::ScopedJavaLocalRef;
+using base::android::ToJavaArrayOfStrings;
+
+const char kInterestJavaClass[] =
+ "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) {
+ scoped_ptr<ScopedJavaGlobalRef<jobject>> j_callback(
+ new ScopedJavaGlobalRef<jobject>());
+
+ j_callback->Reset(env, j_callback_obj);
+
+ InterestsFetcher* fetcher =
+ InterestsFetcher::CreateFromProfile(profile_).release();
Marc Treib 2015/10/06 13:33:57 You should be able to actually keep the scoped_ptr
+
+ InterestsFetcher::InterestsCallback callback = base::Bind(
+ &InterestsService::OnObtainedInterests, weak_ptr_factory_.GetWeakPtr(),
+ base::Owned(fetcher), base::Unretained(env), base::Passed(&j_callback));
+
+ fetcher->Start(callback);
+}
+
+// static
+bool InterestsService::Register(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+jobjectArray InterestsService::ConvertInterestsToJava(
+ const std::vector<InterestsFetcher::Interest>& interests) {
+ JNIEnv* env = AttachCurrentThread();
+ jclass interest_class = env->FindClass(kInterestJavaClass);
+
+ jobjectArray j_interests =
+ env->NewObjectArray(interests.size(), interest_class, nullptr);
+
+ for (size_t i = 0; i != interests.size(); i++) {
+ ScopedJavaLocalRef<jobject> j_interest =
+ Java_InterestsService_createInterest(
+ env, base::android::ConvertUTF8ToJavaString(
+ env, interests[i].name).obj(),
+ base::android::ConvertUTF8ToJavaString(
+ env, interests[i].image_url.spec()).obj(),
+ interests[i].relevance);
+
+ env->SetObjectArrayElement(j_interests, i, j_interest.obj());
+ }
+
+ return j_interests;
+}
+
+void InterestsService::OnObtainedInterests(
+ InterestsFetcher* fetcher,
+ JNIEnv* env,
+ scoped_ptr<ScopedJavaGlobalRef<jobject>> j_callback,
+ const std::vector<InterestsFetcher::Interest>& interests) {
+ jobjectArray j_interests = ConvertInterestsToJava(interests);
+
+ 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);
+}
« no previous file with comments | « chrome/browser/android/interests_service.h ('k') | chrome/browser/android/interests_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698