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::ConvertUTF8ToJavaString; | |
| 19 using base::android::ScopedJavaGlobalRef; | |
| 20 using base::android::ScopedJavaLocalRef; | |
| 21 using base::android::ToJavaArrayOfStrings; | |
| 22 | |
| 23 const char kInterestJavaClass[] = | |
|
Bernhard Bauer
2015/10/07 15:26:37
This is now unused.
PEConn
2015/10/07 18:27:20
Done.
| |
| 24 "org/chromium/chrome/browser/ntp/InterestsService$Interest"; | |
| 25 | |
| 26 InterestsService::InterestsService(Profile* profile) | |
| 27 : profile_(profile), weak_ptr_factory_(this) {} | |
| 28 | |
| 29 InterestsService::~InterestsService() {} | |
| 30 | |
| 31 void InterestsService::Destroy(JNIEnv* env, jobject obj) { | |
| 32 delete this; | |
| 33 } | |
| 34 | |
| 35 void InterestsService::GetInterests(JNIEnv* env, | |
| 36 jobject obj, | |
| 37 jobject j_callback_obj) { | |
| 38 | |
| 39 ScopedJavaGlobalRef<jobject> j_callback(env, j_callback_obj); | |
| 40 | |
| 41 scoped_ptr<InterestsFetcher> fetcher = | |
| 42 InterestsFetcher::CreateFromProfile(profile_); | |
| 43 | |
| 44 InterestsFetcher::InterestsCallback callback = base::Bind( | |
| 45 &InterestsService::OnObtainedInterests, | |
| 46 weak_ptr_factory_.GetWeakPtr(), | |
| 47 base::Owned(fetcher.get()), | |
|
Marc Treib
2015/10/07 15:03:01
Can you use base::Passed(fetcher), and remove the
PEConn
2015/10/07 18:27:20
Done.
| |
| 48 j_callback); | |
| 49 | |
| 50 fetcher->FetchInterests(callback); | |
| 51 | |
| 52 // The callback owns InterestsFetcher now. | |
| 53 ignore_result(fetcher.release()); | |
| 54 } | |
| 55 | |
| 56 // static | |
| 57 bool InterestsService::Register(JNIEnv* env) { | |
| 58 return RegisterNativesImpl(env); | |
| 59 } | |
| 60 | |
| 61 jobjectArray InterestsService::ConvertInterestsToJava( | |
| 62 scoped_ptr<std::vector<InterestsFetcher::Interest>> interests) { | |
| 63 if (!interests) | |
| 64 return nullptr; | |
| 65 | |
| 66 JNIEnv* env = AttachCurrentThread(); | |
|
Bernhard Bauer
2015/10/07 15:26:37
What I meant in my previous comment was to pass |e
PEConn
2015/10/07 18:27:20
Done.
| |
| 67 | |
| 68 jobjectArray j_interests = | |
| 69 Java_InterestsService_createInterestsArray(env, interests->size()).obj(); | |
| 70 | |
| 71 for (size_t i = 0; i != interests->size(); i++) { | |
| 72 auto& interest = interests->at(i); | |
|
Bernhard Bauer
2015/10/07 15:26:37
Isn't this type just InterestsFetcher::Interest? I
PEConn
2015/10/07 18:27:20
Done. Good point about at throwing an exception. I
| |
| 73 ScopedJavaLocalRef<jobject> j_interest = | |
| 74 Java_InterestsService_createInterest( | |
| 75 env, | |
| 76 ConvertUTF8ToJavaString(env, interest.name).obj(), | |
| 77 ConvertUTF8ToJavaString(env, interest.image_url.spec()).obj(), | |
| 78 interest.relevance); | |
| 79 | |
| 80 env->SetObjectArrayElement(j_interests, i, j_interest.obj()); | |
| 81 } | |
| 82 | |
| 83 return j_interests; | |
| 84 } | |
| 85 | |
| 86 void InterestsService::OnObtainedInterests( | |
| 87 InterestsFetcher* fetcher, | |
| 88 const ScopedJavaGlobalRef<jobject>& j_callback, | |
| 89 scoped_ptr<std::vector<InterestsFetcher::Interest>> interests) { | |
| 90 | |
|
Marc Treib
2015/10/07 15:03:01
nit: remove empty line
PEConn
2015/10/07 18:27:20
Done.
| |
| 91 JNIEnv* env = AttachCurrentThread(); | |
| 92 jobjectArray j_interests = ConvertInterestsToJava(interests.Pass()); | |
| 93 Java_GetInterestsCallback_onInterestsAvailableCallback(env, | |
| 94 j_callback.obj(), | |
| 95 j_interests); | |
| 96 } | |
| 97 | |
| 98 static jlong Init(JNIEnv* env, | |
| 99 const JavaParamRef<jobject>& jobj, | |
| 100 const JavaParamRef<jobject>& jprofile) { | |
| 101 InterestsService* interests_service = | |
| 102 new InterestsService(ProfileAndroid::FromProfileAndroid(jprofile)); | |
| 103 return reinterpret_cast<intptr_t>(interests_service); | |
| 104 } | |
| OLD | NEW |