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[] = | |
| 25 "org/chromium/chrome/browser/ntp/InterestsService$Interest"; | |
| 26 | |
| 27 } // end namespace | |
| 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 jstring j_token, | |
| 41 jobject j_callback_obj) { | |
| 42 scoped_ptr<ScopedJavaGlobalRef<jobject>> j_callback( | |
| 43 new ScopedJavaGlobalRef<jobject>()); | |
| 44 | |
| 45 j_callback->Reset(env, j_callback_obj); | |
| 46 std::string token = ConvertJavaStringToUTF8(env, j_token); | |
| 47 | |
| 48 InterestsFetcher* fetcher = | |
| 49 new InterestsFetcher(profile_->GetRequestContext(), token); | |
| 50 | |
| 51 InterestsFetcher::InterestsCallback callback = base::Bind( | |
| 52 &InterestsService::OnObtainedInterests, weak_ptr_factory_.GetWeakPtr(), | |
| 53 base::Owned(fetcher), base::Passed(&j_callback)); | |
| 54 | |
| 55 fetcher->Start(callback); | |
| 56 } | |
| 57 | |
| 58 // static | |
| 59 bool InterestsService::Register(JNIEnv* env) { | |
| 60 return RegisterNativesImpl(env); | |
| 61 } | |
| 62 | |
| 63 jobjectArray InterestsService::ConvertInterestsToJava( | |
| 64 const std::vector<InterestsFetcher::Interest>& interests) { | |
| 65 JNIEnv* env = AttachCurrentThread(); | |
| 66 jclass interest_class = env->FindClass(kInterestJavaClass); | |
| 67 | |
| 68 jobjectArray j_interests = | |
| 69 env->NewObjectArray(interests.size(), interest_class, 0); | |
| 70 | |
| 71 for (size_t i = 0; i != interests.size(); i++) { | |
| 72 ScopedJavaLocalRef<jobject> j_interest = | |
| 73 Java_InterestsService_createInterest( | |
| 74 env, env->NewStringUTF(interests[i].name.c_str()), | |
| 75 env->NewStringUTF(interests[i].image_url.c_str()), | |
| 76 interests[i].relevance); | |
| 77 | |
| 78 env->SetObjectArrayElement(j_interests, i, j_interest.obj()); | |
| 79 } | |
| 80 | |
| 81 return j_interests; | |
| 82 } | |
| 83 | |
| 84 void InterestsService::OnObtainedInterests( | |
| 85 InterestsFetcher* fetcher, | |
| 86 scoped_ptr<ScopedJavaGlobalRef<jobject>> j_callback, | |
| 87 const std::vector<InterestsFetcher::Interest>& interests) { | |
| 88 JNIEnv* env = AttachCurrentThread(); | |
| 89 | |
| 90 jobjectArray j_interests = | |
| 91 InterestsService::ConvertInterestsToJava(interests); | |
| 92 | |
| 93 Java_GetInterestsCallback_onInterestsAvailableCallback(env, j_callback->obj(), | |
| 94 j_interests); | |
|
Marc Treib
2015/09/11 16:02:14
nit: misaligned
tache
2015/09/18 09:41:21
Done.
| |
| 95 } | |
| 96 | |
| 97 static jlong Init(JNIEnv* env, | |
| 98 const JavaParamRef<jobject>& jobj, | |
| 99 const JavaParamRef<jobject>& jprofile) { | |
| 100 InterestsService* interests_service = | |
| 101 new InterestsService(ProfileAndroid::FromProfileAndroid(jprofile)); | |
| 102 return reinterpret_cast<intptr_t>(interests_service); | |
| 103 } | |
| OLD | NEW |