| 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/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
| 11 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
| 12 #include "base/android/scoped_java_ref.h" | 12 #include "base/android/scoped_java_ref.h" |
| 13 #include "chrome/browser/history/history_service_factory.h" |
| 13 #include "chrome/browser/ntp_snippets/ntp_snippets_service_factory.h" | 14 #include "chrome/browser/ntp_snippets/ntp_snippets_service_factory.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/profiles/profile_android.h" | 16 #include "chrome/browser/profiles/profile_android.h" |
| 16 #include "chrome/browser/profiles/profile_manager.h" | 17 #include "chrome/browser/profiles/profile_manager.h" |
| 18 #include "components/history/core/browser/history_service.h" |
| 17 #include "components/ntp_snippets/ntp_snippet.h" | 19 #include "components/ntp_snippets/ntp_snippet.h" |
| 18 #include "components/ntp_snippets/ntp_snippets_service.h" | 20 #include "components/ntp_snippets/ntp_snippets_service.h" |
| 19 #include "jni/SnippetsBridge_jni.h" | 21 #include "jni/SnippetsBridge_jni.h" |
| 20 | 22 |
| 21 using base::android::ConvertJavaStringToUTF8; | 23 using base::android::ConvertJavaStringToUTF8; |
| 22 using base::android::JavaParamRef; | 24 using base::android::JavaParamRef; |
| 23 using base::android::ToJavaArrayOfStrings; | 25 using base::android::ToJavaArrayOfStrings; |
| 24 using base::android::ToJavaLongArray; | 26 using base::android::ToJavaLongArray; |
| 25 | 27 |
| 28 namespace { |
| 29 |
| 30 void SnippetVisitedHistoryRequestCallback( |
| 31 base::android::ScopedJavaGlobalRef<jobject> callback, |
| 32 bool success, |
| 33 const history::URLRow& row, |
| 34 const history::VisitVector& visitVector) { |
| 35 bool visited = success && row.visit_count() != 0; |
| 36 |
| 37 JNIEnv* env = base::android::AttachCurrentThread(); |
| 38 Java_SnippetsBridge_runCallback(env, callback.obj(), |
| 39 static_cast<jboolean>(visited)); |
| 40 } |
| 41 |
| 42 } // namespace |
| 43 |
| 26 static jlong Init(JNIEnv* env, | 44 static jlong Init(JNIEnv* env, |
| 27 const JavaParamRef<jobject>& obj, | 45 const JavaParamRef<jobject>& obj, |
| 28 const JavaParamRef<jobject>& j_profile) { | 46 const JavaParamRef<jobject>& j_profile) { |
| 29 NTPSnippetsBridge* snippets_bridge = new NTPSnippetsBridge(env, j_profile); | 47 NTPSnippetsBridge* snippets_bridge = new NTPSnippetsBridge(env, j_profile); |
| 30 return reinterpret_cast<intptr_t>(snippets_bridge); | 48 return reinterpret_cast<intptr_t>(snippets_bridge); |
| 31 } | 49 } |
| 32 | 50 |
| 33 static void FetchSnippets(JNIEnv* env, | 51 static void FetchSnippets(JNIEnv* env, |
| 34 const JavaParamRef<jclass>& caller) { | 52 const JavaParamRef<jclass>& caller) { |
| 35 Profile* profile = ProfileManager::GetLastUsedProfile(); | 53 Profile* profile = ProfileManager::GetLastUsedProfile(); |
| 36 NTPSnippetsServiceFactory::GetForProfile(profile)->FetchSnippets(); | 54 NTPSnippetsServiceFactory::GetForProfile(profile)->FetchSnippets(); |
| 37 } | 55 } |
| 38 | 56 |
| 39 // Reschedules the fetching of snippets. Used to support different fetching | 57 // Reschedules the fetching of snippets. Used to support different fetching |
| 40 // intervals for different times of day. | 58 // intervals for different times of day. |
| 41 static void RescheduleFetching(JNIEnv* env, | 59 static void RescheduleFetching(JNIEnv* env, |
| 42 const JavaParamRef<jclass>& caller) { | 60 const JavaParamRef<jclass>& caller) { |
| 43 Profile* profile = ProfileManager::GetLastUsedProfile(); | 61 Profile* profile = ProfileManager::GetLastUsedProfile(); |
| 44 NTPSnippetsServiceFactory::GetForProfile(profile)->RescheduleFetching(); | 62 NTPSnippetsServiceFactory::GetForProfile(profile)->RescheduleFetching(); |
| 45 } | 63 } |
| 46 | 64 |
| 47 NTPSnippetsBridge::NTPSnippetsBridge(JNIEnv* env, | 65 NTPSnippetsBridge::NTPSnippetsBridge(JNIEnv* env, |
| 48 const JavaParamRef<jobject>& j_profile) | 66 const JavaParamRef<jobject>& j_profile) |
| 49 : snippet_service_observer_(this) { | 67 : snippet_service_observer_(this) { |
| 50 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); | 68 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); |
| 51 ntp_snippets_service_ = NTPSnippetsServiceFactory::GetForProfile(profile); | 69 ntp_snippets_service_ = NTPSnippetsServiceFactory::GetForProfile(profile); |
| 70 history_service_ = |
| 71 HistoryServiceFactory::GetForProfile(profile, |
| 72 ServiceAccessType::EXPLICIT_ACCESS); |
| 52 snippet_service_observer_.Add(ntp_snippets_service_); | 73 snippet_service_observer_.Add(ntp_snippets_service_); |
| 53 } | 74 } |
| 54 | 75 |
| 55 void NTPSnippetsBridge::SetObserver(JNIEnv* env, | 76 void NTPSnippetsBridge::SetObserver(JNIEnv* env, |
| 56 const JavaParamRef<jobject>& obj, | 77 const JavaParamRef<jobject>& obj, |
| 57 const JavaParamRef<jobject>& j_observer) { | 78 const JavaParamRef<jobject>& j_observer) { |
| 58 observer_.Reset(env, j_observer); | 79 observer_.Reset(env, j_observer); |
| 59 NTPSnippetsServiceLoaded(); | 80 NTPSnippetsServiceLoaded(); |
| 60 } | 81 } |
| 61 | 82 |
| 62 NTPSnippetsBridge::~NTPSnippetsBridge() {} | 83 NTPSnippetsBridge::~NTPSnippetsBridge() {} |
| 63 | 84 |
| 64 void NTPSnippetsBridge::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 85 void NTPSnippetsBridge::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 65 delete this; | 86 delete this; |
| 66 } | 87 } |
| 67 | 88 |
| 68 void NTPSnippetsBridge::DiscardSnippet(JNIEnv* env, | 89 void NTPSnippetsBridge::DiscardSnippet(JNIEnv* env, |
| 69 const JavaParamRef<jobject>& obj, | 90 const JavaParamRef<jobject>& obj, |
| 70 const JavaParamRef<jstring>& url) { | 91 const JavaParamRef<jstring>& url) { |
| 71 ntp_snippets_service_->DiscardSnippet( | 92 ntp_snippets_service_->DiscardSnippet( |
| 72 GURL(ConvertJavaStringToUTF8(env, url))); | 93 GURL(ConvertJavaStringToUTF8(env, url))); |
| 73 } | 94 } |
| 74 | 95 |
| 96 void NTPSnippetsBridge::SnippetVisited(JNIEnv* env, |
| 97 const JavaParamRef<jobject>& obj, |
| 98 const JavaParamRef<jobject>& jcallback, |
| 99 const JavaParamRef<jstring>& jurl) { |
| 100 base::android::ScopedJavaGlobalRef<jobject> callback(jcallback); |
| 101 |
| 102 history_service_->QueryURL( |
| 103 GURL(ConvertJavaStringToUTF8(env, jurl)), |
| 104 false, |
| 105 base::Bind(&SnippetVisitedHistoryRequestCallback, callback), |
| 106 &tracker_); |
| 107 } |
| 108 |
| 75 void NTPSnippetsBridge::NTPSnippetsServiceLoaded() { | 109 void NTPSnippetsBridge::NTPSnippetsServiceLoaded() { |
| 76 if (observer_.is_null()) | 110 if (observer_.is_null()) |
| 77 return; | 111 return; |
| 78 | 112 |
| 79 std::vector<std::string> titles; | 113 std::vector<std::string> titles; |
| 80 std::vector<std::string> urls; | 114 std::vector<std::string> urls; |
| 81 std::vector<std::string> thumbnail_urls; | 115 std::vector<std::string> thumbnail_urls; |
| 82 std::vector<std::string> snippets; | 116 std::vector<std::string> snippets; |
| 83 std::vector<int64_t> timestamps; | 117 std::vector<int64_t> timestamps; |
| 84 for (const ntp_snippets::NTPSnippet& snippet : *ntp_snippets_service_) { | 118 for (const ntp_snippets::NTPSnippet& snippet : *ntp_snippets_service_) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 100 | 134 |
| 101 void NTPSnippetsBridge::NTPSnippetsServiceShutdown() { | 135 void NTPSnippetsBridge::NTPSnippetsServiceShutdown() { |
| 102 observer_.Reset(); | 136 observer_.Reset(); |
| 103 snippet_service_observer_.Remove(ntp_snippets_service_); | 137 snippet_service_observer_.Remove(ntp_snippets_service_); |
| 104 } | 138 } |
| 105 | 139 |
| 106 // static | 140 // static |
| 107 bool NTPSnippetsBridge::Register(JNIEnv* env) { | 141 bool NTPSnippetsBridge::Register(JNIEnv* env) { |
| 108 return RegisterNativesImpl(env); | 142 return RegisterNativesImpl(env); |
| 109 } | 143 } |
| OLD | NEW |