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/interests/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; | |
Bernhard Bauer
2015/10/08 09:04:41
This is unused.
PEConn
2015/10/08 09:40:55
Done.
| |
18 using base::android::ConvertUTF8ToJavaString; | |
19 using base::android::ScopedJavaGlobalRef; | |
20 using base::android::ScopedJavaLocalRef; | |
21 using base::android::ToJavaArrayOfStrings; | |
Bernhard Bauer
2015/10/08 09:04:41
This is unused.
PEConn
2015/10/08 09:40:55
Done.
| |
22 | |
23 InterestsService::InterestsService(Profile* profile) | |
24 : profile_(profile), weak_ptr_factory_(this) {} | |
25 | |
26 InterestsService::~InterestsService() {} | |
27 | |
28 void InterestsService::Destroy(JNIEnv* env, jobject obj) { | |
29 delete this; | |
30 } | |
31 | |
32 void InterestsService::GetInterests(JNIEnv* env, | |
33 jobject obj, | |
34 jobject j_callback_obj) { | |
35 | |
Marc Treib
2015/10/08 08:26:51
nit: no empty line here
PEConn
2015/10/08 09:40:55
Done.
| |
36 ScopedJavaGlobalRef<jobject> j_callback(env, j_callback_obj); | |
37 | |
38 scoped_ptr<InterestsFetcher> fetcher = | |
39 InterestsFetcher::CreateFromProfile(profile_); | |
40 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.
| |
41 | |
42 InterestsFetcher::InterestsCallback callback = base::Bind( | |
43 &InterestsService::OnObtainedInterests, | |
44 weak_ptr_factory_.GetWeakPtr(), | |
45 base::Passed(fetcher.Pass()), | |
46 j_callback); | |
47 | |
48 fetcherRawPtr->FetchInterests(callback); | |
49 } | |
50 | |
51 // static | |
52 bool InterestsService::Register(JNIEnv* env) { | |
53 return RegisterNativesImpl(env); | |
54 } | |
55 | |
56 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.
| |
57 JNIEnv* env, | |
58 scoped_ptr<std::vector<InterestsFetcher::Interest>> interests) { | |
59 if (!interests) | |
60 return nullptr; | |
61 | |
62 jobjectArray j_interests = | |
63 Java_InterestsService_createInterestsArray(env, interests->size()).obj(); | |
64 | |
65 for (size_t i = 0; i != interests->size(); i++) { | |
66 InterestsFetcher::Interest& interest = (*interests)[i]; | |
Bernhard Bauer
2015/10/08 09:04:41
Make this const?
PEConn
2015/10/08 09:40:55
Done.
| |
67 ScopedJavaLocalRef<jobject> j_interest = | |
68 Java_InterestsService_createInterest( | |
69 env, | |
70 ConvertUTF8ToJavaString(env, interest.name).obj(), | |
71 ConvertUTF8ToJavaString(env, interest.image_url.spec()).obj(), | |
72 interest.relevance); | |
73 | |
74 env->SetObjectArrayElement(j_interests, i, j_interest.obj()); | |
75 } | |
76 | |
77 return j_interests; | |
78 } | |
79 | |
80 void InterestsService::OnObtainedInterests( | |
81 scoped_ptr<InterestsFetcher> fetcher, | |
82 const ScopedJavaGlobalRef<jobject>& j_callback, | |
83 scoped_ptr<std::vector<InterestsFetcher::Interest>> interests) { | |
84 JNIEnv* env = AttachCurrentThread(); | |
85 jobjectArray j_interests = ConvertInterestsToJava(env, interests.Pass()); | |
86 Java_GetInterestsCallback_onInterestsAvailableCallback(env, | |
87 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 |