Chromium Code Reviews| 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..f3d45df04f480380009d0b816dab634576a8bdf5 |
| --- /dev/null |
| +++ b/chrome/browser/android/interests_service.cc |
| @@ -0,0 +1,101 @@ |
| +// 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; |
| + |
| +namespace { |
| + |
| +const char kInterestJavaClass[] = |
|
Bernhard Bauer
2015/09/29 14:04:04
Const variables actually get internal linkage auto
PEConn
2015/10/07 13:55:21
Done.
|
| + "org/chromium/chrome/browser/ntp/InterestsService$Interest"; |
| + |
| +} // end namespace |
|
Bernhard Bauer
2015/09/29 14:04:04
If you do keep the namespace, remove the "end ".
PEConn
2015/10/07 13:55:21
Done.
|
| + |
| +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( |
|
Bernhard Bauer
2015/09/29 14:04:05
You don't actually need a scoped_ptr for a ScopedJ
PEConn
2015/10/07 13:55:21
Done.
|
| + new ScopedJavaGlobalRef<jobject>()); |
| + |
| + j_callback->Reset(env, j_callback_obj); |
| + |
| + InterestsFetcher* fetcher = |
|
Bernhard Bauer
2015/09/29 14:04:04
I would make this a scoped_ptr and only release()
PEConn
2015/10/07 13:55:21
Done.
|
| + InterestsFetcher::CreateFromProfile(profile_).release(); |
| + |
| + InterestsFetcher::InterestsCallback callback = base::Bind( |
| + &InterestsService::OnObtainedInterests, weak_ptr_factory_.GetWeakPtr(), |
| + base::Owned(fetcher), 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); |
|
Bernhard Bauer
2015/09/29 14:04:05
It's not great to have to manually resolve this cl
PEConn
2015/10/07 13:55:21
Done.
|
| + |
| + jobjectArray j_interests = |
| + env->NewObjectArray(interests.size(), interest_class, 0); |
|
Bernhard Bauer
2015/09/29 14:04:04
I would use nullptr rather than 0.
PEConn
2015/10/07 13:55:21
Done.
|
| + |
| + for (size_t i = 0; i != interests.size(); i++) { |
| + ScopedJavaLocalRef<jobject> j_interest = |
| + Java_InterestsService_createInterest( |
| + env, env->NewStringUTF(interests[i].name.c_str()), |
|
Bernhard Bauer
2015/09/29 14:04:05
Please use base::android::ConvertUTF8ToJavaString(
PEConn
2015/10/07 13:55:21
Done.
|
| + env->NewStringUTF(interests[i].image_url.c_str()), |
| + interests[i].relevance); |
| + |
| + env->SetObjectArrayElement(j_interests, i, j_interest.obj()); |
| + } |
| + |
| + return j_interests; |
| +} |
| + |
| +void InterestsService::OnObtainedInterests( |
| + InterestsFetcher* fetcher, |
| + scoped_ptr<ScopedJavaGlobalRef<jobject>> j_callback, |
| + const std::vector<InterestsFetcher::Interest>& interests) { |
| + JNIEnv* env = AttachCurrentThread(); |
| + |
| + jobjectArray j_interests = |
| + InterestsService::ConvertInterestsToJava(interests); |
|
Bernhard Bauer
2015/09/29 14:04:05
You could pass the JNIEnv to this method. Also, yo
PEConn
2015/10/07 13:55:21
Not Done. This would require passing the JNIEnv th
|
| + |
| + 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)); |
|
Bernhard Bauer
2015/09/29 14:04:04
The current version of the CL that this is based o
|
| + return reinterpret_cast<intptr_t>(interests_service); |
| +} |