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 const char kInterestJavaClass[] = | |
23 "org/chromium/chrome/browser/ntp/InterestsService$Interest"; | |
24 | |
25 InterestsService::InterestsService(Profile* profile) | |
26 : profile_(profile), weak_ptr_factory_(this) {} | |
27 | |
28 InterestsService::~InterestsService() {} | |
29 | |
30 void InterestsService::Destroy(JNIEnv* env, jobject obj) { | |
31 delete this; | |
32 } | |
33 | |
34 void InterestsService::GetInterests(JNIEnv* env, | |
35 jobject obj, | |
36 jobject j_callback_obj) { | |
37 scoped_ptr<ScopedJavaGlobalRef<jobject>> j_callback( | |
38 new ScopedJavaGlobalRef<jobject>()); | |
39 | |
40 j_callback->Reset(env, j_callback_obj); | |
41 | |
42 InterestsFetcher* fetcher = | |
43 InterestsFetcher::CreateFromProfile(profile_).release(); | |
Marc Treib
2015/10/06 13:33:57
You should be able to actually keep the scoped_ptr
| |
44 | |
45 InterestsFetcher::InterestsCallback callback = base::Bind( | |
46 &InterestsService::OnObtainedInterests, weak_ptr_factory_.GetWeakPtr(), | |
47 base::Owned(fetcher), base::Unretained(env), base::Passed(&j_callback)); | |
48 | |
49 fetcher->Start(callback); | |
50 } | |
51 | |
52 // static | |
53 bool InterestsService::Register(JNIEnv* env) { | |
54 return RegisterNativesImpl(env); | |
55 } | |
56 | |
57 jobjectArray InterestsService::ConvertInterestsToJava( | |
58 const std::vector<InterestsFetcher::Interest>& interests) { | |
59 JNIEnv* env = AttachCurrentThread(); | |
60 jclass interest_class = env->FindClass(kInterestJavaClass); | |
61 | |
62 jobjectArray j_interests = | |
63 env->NewObjectArray(interests.size(), interest_class, nullptr); | |
64 | |
65 for (size_t i = 0; i != interests.size(); i++) { | |
66 ScopedJavaLocalRef<jobject> j_interest = | |
67 Java_InterestsService_createInterest( | |
68 env, base::android::ConvertUTF8ToJavaString( | |
69 env, interests[i].name).obj(), | |
70 base::android::ConvertUTF8ToJavaString( | |
71 env, interests[i].image_url.spec()).obj(), | |
72 interests[i].relevance); | |
73 | |
74 env->SetObjectArrayElement(j_interests, i, j_interest.obj()); | |
75 } | |
76 | |
77 return j_interests; | |
78 } | |
79 | |
80 void InterestsService::OnObtainedInterests( | |
81 InterestsFetcher* fetcher, | |
82 JNIEnv* env, | |
83 scoped_ptr<ScopedJavaGlobalRef<jobject>> j_callback, | |
84 const std::vector<InterestsFetcher::Interest>& interests) { | |
85 jobjectArray j_interests = ConvertInterestsToJava(interests); | |
86 | |
87 Java_GetInterestsCallback_onInterestsAvailableCallback(env, j_callback->obj(), | |
88 j_interests); | |
89 } | |
90 | |
91 static jlong Init(JNIEnv* env, | |
92 const JavaParamRef<jobject>& jobj, | |
93 const JavaParamRef<jobject>& jprofile) { | |
94 InterestsService* interests_service = | |
95 new InterestsService(ProfileAndroid::FromProfileAndroid(jprofile)); | |
96 return reinterpret_cast<intptr_t>(interests_service); | |
97 } | |
OLD | NEW |