Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/android/interests_service.h" | |
| 6 | |
| 7 #include "base/android/jni_android.h" | |
| 8 #include "base/android/jni_array.h" | |
| 9 #include "base/android/jni_string.h" | |
| 10 #include "base/android/scoped_java_ref.h" | |
| 11 #include "chrome/browser/interests/interests_fetcher.h" | |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 13 #include "chrome/browser/profiles/profile_android.h" | |
| 14 #include "jni/InterestsService_jni.h" | |
| 15 | |
| 16 using base::android::AttachCurrentThread; | |
| 17 using base::android::ConvertJavaStringToUTF8; | |
| 18 using base::android::ScopedJavaGlobalRef; | |
| 19 using base::android::ScopedJavaLocalRef; | |
| 20 using base::android::ToJavaArrayOfStrings; | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 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.
| |
| 25 "org/chromium/chrome/browser/ntp/InterestsService$Interest"; | |
| 26 | |
| 27 } // 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.
| |
| 28 | |
| 29 InterestsService::InterestsService(Profile* profile) | |
| 30 : profile_(profile), weak_ptr_factory_(this) {} | |
| 31 | |
| 32 InterestsService::~InterestsService() {} | |
| 33 | |
| 34 void InterestsService::Destroy(JNIEnv* env, jobject obj) { | |
| 35 delete this; | |
| 36 } | |
| 37 | |
| 38 void InterestsService::GetInterests(JNIEnv* env, | |
| 39 jobject obj, | |
| 40 jobject j_callback_obj) { | |
| 41 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.
| |
| 42 new ScopedJavaGlobalRef<jobject>()); | |
| 43 | |
| 44 j_callback->Reset(env, j_callback_obj); | |
| 45 | |
| 46 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.
| |
| 47 InterestsFetcher::CreateFromProfile(profile_).release(); | |
| 48 | |
| 49 InterestsFetcher::InterestsCallback callback = base::Bind( | |
| 50 &InterestsService::OnObtainedInterests, weak_ptr_factory_.GetWeakPtr(), | |
| 51 base::Owned(fetcher), base::Passed(&j_callback)); | |
| 52 | |
| 53 fetcher->Start(callback); | |
| 54 } | |
| 55 | |
| 56 // static | |
| 57 bool InterestsService::Register(JNIEnv* env) { | |
| 58 return RegisterNativesImpl(env); | |
| 59 } | |
| 60 | |
| 61 jobjectArray InterestsService::ConvertInterestsToJava( | |
| 62 const std::vector<InterestsFetcher::Interest>& interests) { | |
| 63 JNIEnv* env = AttachCurrentThread(); | |
| 64 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.
| |
| 65 | |
| 66 jobjectArray j_interests = | |
| 67 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.
| |
| 68 | |
| 69 for (size_t i = 0; i != interests.size(); i++) { | |
| 70 ScopedJavaLocalRef<jobject> j_interest = | |
| 71 Java_InterestsService_createInterest( | |
| 72 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.
| |
| 73 env->NewStringUTF(interests[i].image_url.c_str()), | |
| 74 interests[i].relevance); | |
| 75 | |
| 76 env->SetObjectArrayElement(j_interests, i, j_interest.obj()); | |
| 77 } | |
| 78 | |
| 79 return j_interests; | |
| 80 } | |
| 81 | |
| 82 void InterestsService::OnObtainedInterests( | |
| 83 InterestsFetcher* fetcher, | |
| 84 scoped_ptr<ScopedJavaGlobalRef<jobject>> j_callback, | |
| 85 const std::vector<InterestsFetcher::Interest>& interests) { | |
| 86 JNIEnv* env = AttachCurrentThread(); | |
| 87 | |
| 88 jobjectArray j_interests = | |
| 89 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
| |
| 90 | |
| 91 Java_GetInterestsCallback_onInterestsAvailableCallback(env, j_callback->obj(), | |
| 92 j_interests); | |
| 93 } | |
| 94 | |
| 95 static jlong Init(JNIEnv* env, | |
| 96 const JavaParamRef<jobject>& jobj, | |
| 97 const JavaParamRef<jobject>& jprofile) { | |
| 98 InterestsService* interests_service = | |
| 99 new InterestsService(ProfileAndroid::FromProfileAndroid(jprofile)); | |
|
Bernhard Bauer
2015/09/29 14:04:04
The current version of the CL that this is based o
| |
| 100 return reinterpret_cast<intptr_t>(interests_service); | |
| 101 } | |
| OLD | NEW |