OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/android/ntp/ntp_snippets_bridge.h" | 5 #include "chrome/browser/android/ntp/ntp_snippets_bridge.h" |
6 | 6 |
7 #include <jni.h> | 7 #include <jni.h> |
8 | 8 |
| 9 #include "base/android/callback_android.h" |
9 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
10 #include "base/android/jni_array.h" | 11 #include "base/android/jni_array.h" |
11 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
12 #include "base/callback.h" | 13 #include "base/callback.h" |
13 #include "chrome/browser/history/history_service_factory.h" | 14 #include "chrome/browser/history/history_service_factory.h" |
14 #include "chrome/browser/ntp_snippets/ntp_snippets_service_factory.h" | 15 #include "chrome/browser/ntp_snippets/ntp_snippets_service_factory.h" |
15 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/profiles/profile_android.h" | 17 #include "chrome/browser/profiles/profile_android.h" |
17 #include "chrome/browser/profiles/profile_manager.h" | 18 #include "chrome/browser/profiles/profile_manager.h" |
18 #include "components/history/core/browser/history_service.h" | 19 #include "components/history/core/browser/history_service.h" |
(...skipping 12 matching lines...) Expand all Loading... |
31 using base::android::ScopedJavaLocalRef; | 32 using base::android::ScopedJavaLocalRef; |
32 | 33 |
33 namespace { | 34 namespace { |
34 | 35 |
35 void SnippetVisitedHistoryRequestCallback( | 36 void SnippetVisitedHistoryRequestCallback( |
36 base::android::ScopedJavaGlobalRef<jobject> callback, | 37 base::android::ScopedJavaGlobalRef<jobject> callback, |
37 bool success, | 38 bool success, |
38 const history::URLRow& row, | 39 const history::URLRow& row, |
39 const history::VisitVector& visitVector) { | 40 const history::VisitVector& visitVector) { |
40 bool visited = success && row.visit_count() != 0; | 41 bool visited = success && row.visit_count() != 0; |
41 | 42 base::android::RunCallbackAndroid(callback, visited); |
42 JNIEnv* env = base::android::AttachCurrentThread(); | |
43 Java_SnippetsBridge_runCallback(env, callback.obj(), | |
44 static_cast<jboolean>(visited)); | |
45 } | 43 } |
46 | 44 |
47 } // namespace | 45 } // namespace |
48 | 46 |
49 static jlong Init(JNIEnv* env, | 47 static jlong Init(JNIEnv* env, |
50 const JavaParamRef<jobject>& obj, | 48 const JavaParamRef<jobject>& obj, |
51 const JavaParamRef<jobject>& j_profile) { | 49 const JavaParamRef<jobject>& j_profile) { |
52 NTPSnippetsBridge* snippets_bridge = new NTPSnippetsBridge(env, j_profile); | 50 NTPSnippetsBridge* snippets_bridge = new NTPSnippetsBridge(env, j_profile); |
53 return reinterpret_cast<intptr_t>(snippets_bridge); | 51 return reinterpret_cast<intptr_t>(snippets_bridge); |
54 } | 52 } |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 void NTPSnippetsBridge::NTPSnippetsServiceDisabled() { | 169 void NTPSnippetsBridge::NTPSnippetsServiceDisabled() { |
172 // The user signed out or disabled sync. Since snippets rely on those, we | 170 // The user signed out or disabled sync. Since snippets rely on those, we |
173 // clear them to be consistent with the initially signed out state. | 171 // clear them to be consistent with the initially signed out state. |
174 JNIEnv* env = base::android::AttachCurrentThread(); | 172 JNIEnv* env = base::android::AttachCurrentThread(); |
175 Java_SnippetsBridge_onSnippetsDisabled(env, observer_.obj()); | 173 Java_SnippetsBridge_onSnippetsDisabled(env, observer_.obj()); |
176 } | 174 } |
177 | 175 |
178 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback, | 176 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback, |
179 const std::string& snippet_id, | 177 const std::string& snippet_id, |
180 const gfx::Image& image) { | 178 const gfx::Image& image) { |
181 JNIEnv* env = AttachCurrentThread(); | |
182 | |
183 ScopedJavaLocalRef<jobject> j_bitmap; | 179 ScopedJavaLocalRef<jobject> j_bitmap; |
184 if (!image.IsEmpty()) | 180 if (!image.IsEmpty()) |
185 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); | 181 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); |
186 | 182 |
187 Java_FetchSnippetImageCallback_onSnippetImageAvailable(env, callback.obj(), | 183 base::android::RunCallbackAndroid(callback, j_bitmap); |
188 j_bitmap.obj()); | |
189 } | 184 } |
190 | 185 |
191 // static | 186 // static |
192 bool NTPSnippetsBridge::Register(JNIEnv* env) { | 187 bool NTPSnippetsBridge::Register(JNIEnv* env) { |
193 return RegisterNativesImpl(env); | 188 return RegisterNativesImpl(env); |
194 } | 189 } |
OLD | NEW |