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