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/interests/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
« no previous file with comments | « chrome/browser/interests/android/interests_service.h ('k') | chrome/browser/interests/interests_fetcher.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/interests/android/interests_service.cc
diff --git a/chrome/browser/interests/android/interests_service.cc b/chrome/browser/interests/android/interests_service.cc
new file mode 100644
index 0000000000000000000000000000000000000000..15c45e3c7f616c0a6aeacc7063cb6ff33466773a
--- /dev/null
+++ b/chrome/browser/interests/android/interests_service.cc
@@ -0,0 +1,99 @@
+// 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/interests/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::ConvertUTF8ToJavaString;
+using base::android::ScopedJavaGlobalRef;
+using base::android::ScopedJavaLocalRef;
+
+namespace {
+
+ScopedJavaLocalRef<jobjectArray> ConvertInterestsToJava(
+ JNIEnv* env,
+ scoped_ptr<std::vector<InterestsFetcher::Interest>> interests) {
+ if (!interests)
+ return ScopedJavaLocalRef<jobjectArray>();
+
+ ScopedJavaLocalRef<jobjectArray> j_interests =
+ Java_InterestsService_createInterestsArray(env, interests->size());
+
+ for (size_t i = 0; i != interests->size(); i++) {
+ const InterestsFetcher::Interest& interest = (*interests)[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.obj(), i, j_interest.obj());
+ }
+
+ return j_interests;
+}
+
+} // namespace
+
+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* fetcher_raw_ptr = fetcher.get();
+
+ InterestsFetcher::InterestsCallback callback = base::Bind(
+ &InterestsService::OnObtainedInterests,
+ weak_ptr_factory_.GetWeakPtr(),
+ base::Passed(fetcher.Pass()),
+ j_callback);
+
+ fetcher_raw_ptr->FetchInterests(callback);
+}
+
+// static
+bool InterestsService::Register(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+void InterestsService::OnObtainedInterests(
+ scoped_ptr<InterestsFetcher> fetcher,
+ const ScopedJavaGlobalRef<jobject>& j_callback,
+ scoped_ptr<std::vector<InterestsFetcher::Interest>> interests) {
+ JNIEnv* env = AttachCurrentThread();
+ ScopedJavaLocalRef<jobjectArray> j_interests =
+ ConvertInterestsToJava(env, interests.Pass());
+ Java_GetInterestsCallback_onInterestsAvailableCallback(env,
+ j_callback.obj(),
+ j_interests.obj());
+}
+
+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/interests/android/interests_service.h ('k') | chrome/browser/interests/interests_fetcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698