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