Chromium Code Reviews| 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..3c6cf3e2c0713f4b2c7bc61ccbaf97686514b1e3 |
| --- /dev/null |
| +++ b/chrome/browser/interests/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/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::ConvertJavaStringToUTF8; |
|
Bernhard Bauer
2015/10/08 09:04:41
This is unused.
PEConn
2015/10/08 09:40:55
Done.
|
| +using base::android::ConvertUTF8ToJavaString; |
| +using base::android::ScopedJavaGlobalRef; |
| +using base::android::ScopedJavaLocalRef; |
| +using base::android::ToJavaArrayOfStrings; |
|
Bernhard Bauer
2015/10/08 09:04:41
This is unused.
PEConn
2015/10/08 09:40:55
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) { |
| + |
|
Marc Treib
2015/10/08 08:26:51
nit: no empty line here
PEConn
2015/10/08 09:40:55
Done.
|
| + ScopedJavaGlobalRef<jobject> j_callback(env, j_callback_obj); |
| + |
| + scoped_ptr<InterestsFetcher> fetcher = |
| + InterestsFetcher::CreateFromProfile(profile_); |
| + InterestsFetcher* fetcherRawPtr = fetcher.get(); |
|
Bernhard Bauer
2015/10/08 09:04:41
Nit: |fetcher_raw_ptr|. I know, mixing Java and C+
PEConn
2015/10/08 09:40:55
Done.
|
| + |
| + InterestsFetcher::InterestsCallback callback = base::Bind( |
| + &InterestsService::OnObtainedInterests, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + base::Passed(fetcher.Pass()), |
| + j_callback); |
| + |
| + fetcherRawPtr->FetchInterests(callback); |
| +} |
| + |
| +// static |
| +bool InterestsService::Register(JNIEnv* env) { |
| + return RegisterNativesImpl(env); |
| +} |
| + |
| +jobjectArray InterestsService::ConvertInterestsToJava( |
|
Bernhard Bauer
2015/10/08 09:04:41
I think it would make sense to use a ScopedJavaLoc
PEConn
2015/10/08 09:40:55
Done.
|
| + JNIEnv* env, |
| + scoped_ptr<std::vector<InterestsFetcher::Interest>> interests) { |
| + if (!interests) |
| + return nullptr; |
| + |
| + jobjectArray j_interests = |
| + Java_InterestsService_createInterestsArray(env, interests->size()).obj(); |
| + |
| + for (size_t i = 0; i != interests->size(); i++) { |
| + InterestsFetcher::Interest& interest = (*interests)[i]; |
|
Bernhard Bauer
2015/10/08 09:04:41
Make this const?
PEConn
2015/10/08 09:40:55
Done.
|
| + 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( |
| + scoped_ptr<InterestsFetcher> fetcher, |
| + const ScopedJavaGlobalRef<jobject>& j_callback, |
| + scoped_ptr<std::vector<InterestsFetcher::Interest>> interests) { |
| + JNIEnv* env = AttachCurrentThread(); |
| + jobjectArray j_interests = ConvertInterestsToJava(env, 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); |
| +} |