OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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/ntp_snippets_bridge.h" | |
6 | |
7 #include <jni.h> | |
8 | |
9 #include "base/android/jni_android.h" | |
10 #include "base/android/jni_array.h" | |
11 #include "base/android/scoped_java_ref.h" | |
12 #include "chrome/browser/ntp_snippets/ntp_snippets_service_factory.h" | |
13 #include "chrome/browser/profiles/profile.h" | |
14 #include "chrome/browser/profiles/profile_android.h" | |
15 #include "components/ntp_snippets/ntp_snippet.h" | |
16 #include "components/ntp_snippets/ntp_snippets_service.h" | |
17 #include "jni/SnippetsBridge_jni.h" | |
18 | |
19 using base::android::JavaParamRef; | |
20 using base::android::ToJavaArrayOfStrings; | |
21 using ntp_snippets::NTPSnippetsService; | |
22 using ntp_snippets::NTPSnippetsServiceObserver; | |
23 | |
24 static jlong Init(JNIEnv* env, | |
25 const JavaParamRef<jobject>& obj, | |
26 const JavaParamRef<jobject>& j_profile, | |
27 const JavaParamRef<jobject>& j_observer) { | |
28 NTPSnippetsBridge* snippets_bridge = | |
29 new NTPSnippetsBridge(env, j_profile, j_observer); | |
30 return reinterpret_cast<intptr_t>(snippets_bridge); | |
31 } | |
32 | |
33 NTPSnippetsBridge::NTPSnippetsBridge(JNIEnv* env, | |
34 jobject j_profile, | |
35 jobject j_observer) | |
36 : snippet_service_observer_(this) { | |
37 observer_.Reset(env, j_observer); | |
38 | |
39 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); | |
40 if (profile) { | |
Bernhard Bauer
2016/02/10 18:36:20
Actually, could we remove this? I would rather cra
May
2016/02/11 15:25:24
Done.
| |
41 ntp_snippets_service_ = NTPSnippetsServiceFactory::GetForProfile(profile); | |
42 snippet_service_observer_.Add(ntp_snippets_service_); | |
43 if (ntp_snippets_service_->is_loaded()) | |
44 NTPSnippetsServiceLoaded(ntp_snippets_service_); | |
45 } | |
46 } | |
47 | |
48 NTPSnippetsBridge::~NTPSnippetsBridge() {} | |
49 | |
50 void NTPSnippetsBridge::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { | |
51 delete this; | |
52 } | |
53 | |
54 void NTPSnippetsBridge::NTPSnippetsServiceLoaded(NTPSnippetsService* service) { | |
55 if (observer_.is_null()) | |
56 return; | |
57 | |
58 std::vector<std::string> titles; | |
59 std::vector<std::string> urls; | |
60 std::vector<std::string> thumbnail_urls; | |
61 std::vector<std::string> snippets; | |
62 for (const ntp_snippets::NTPSnippet& snippet : *service) { | |
63 titles.push_back(snippet.title()); | |
64 urls.push_back(snippet.url().spec()); | |
65 thumbnail_urls.push_back(snippet.salient_image_url().spec()); | |
66 snippets.push_back(snippet.snippet()); | |
67 } | |
68 | |
69 JNIEnv* env = base::android::AttachCurrentThread(); | |
70 Java_SnippetsObserver_onSnippetsAvailable( | |
71 env, observer_.obj(), ToJavaArrayOfStrings(env, titles).obj(), | |
72 ToJavaArrayOfStrings(env, urls).obj(), | |
73 ToJavaArrayOfStrings(env, thumbnail_urls).obj(), | |
74 ToJavaArrayOfStrings(env, snippets).obj()); | |
75 } | |
76 | |
77 void NTPSnippetsBridge::NTPSnippetsServiceShutdown( | |
78 NTPSnippetsService* service) { | |
79 observer_.Reset(); | |
80 snippet_service_observer_.Remove(ntp_snippets_service_); | |
81 } | |
82 | |
83 // static | |
84 bool NTPSnippetsBridge::Register(JNIEnv* env) { | |
85 return RegisterNativesImpl(env); | |
86 } | |
OLD | NEW |