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/callback_android.h" | 9 #include "base/android/callback_android.h" |
| 10 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 #include "ui/gfx/android/java_bitmap.h" | 23 #include "ui/gfx/android/java_bitmap.h" |
| 24 #include "ui/gfx/image/image.h" | 24 #include "ui/gfx/image/image.h" |
| 25 | 25 |
| 26 using base::android::AttachCurrentThread; | 26 using base::android::AttachCurrentThread; |
| 27 using base::android::ConvertJavaStringToUTF8; | 27 using base::android::ConvertJavaStringToUTF8; |
| 28 using base::android::ConvertUTF8ToJavaString; | 28 using base::android::ConvertUTF8ToJavaString; |
| 29 using base::android::ConvertUTF16ToJavaString; | 29 using base::android::ConvertUTF16ToJavaString; |
| 30 using base::android::JavaParamRef; | 30 using base::android::JavaParamRef; |
| 31 using base::android::ScopedJavaGlobalRef; | 31 using base::android::ScopedJavaGlobalRef; |
| 32 using base::android::ScopedJavaLocalRef; | 32 using base::android::ScopedJavaLocalRef; |
| 33 using base::android::ToJavaIntArray; | |
| 33 using ntp_snippets::Category; | 34 using ntp_snippets::Category; |
| 35 using ntp_snippets::CategoryInfo; | |
| 34 using ntp_snippets::CategoryStatus; | 36 using ntp_snippets::CategoryStatus; |
| 35 using ntp_snippets::KnownCategories; | 37 using ntp_snippets::KnownCategories; |
| 38 using ntp_snippets::ContentSuggestion; | |
| 36 | 39 |
| 37 namespace { | 40 namespace { |
| 38 | 41 |
| 39 void SnippetVisitedHistoryRequestCallback( | 42 void URLVisitedHistoryRequestCallback( |
| 40 base::android::ScopedJavaGlobalRef<jobject> callback, | 43 base::android::ScopedJavaGlobalRef<jobject> callback, |
| 41 bool success, | 44 bool success, |
| 42 const history::URLRow& row, | 45 const history::URLRow& row, |
| 43 const history::VisitVector& visitVector) { | 46 const history::VisitVector& visitVector) { |
| 44 bool visited = success && row.visit_count() != 0; | 47 bool visited = success && row.visit_count() != 0; |
| 45 base::android::RunCallbackAndroid(callback, visited); | 48 base::android::RunCallbackAndroid(callback, visited); |
| 46 } | 49 } |
| 47 | 50 |
| 48 } // namespace | 51 } // namespace |
| 49 | 52 |
| 50 static jlong Init(JNIEnv* env, | 53 static jlong Init(JNIEnv* env, |
| 51 const JavaParamRef<jobject>& obj, | 54 const JavaParamRef<jobject>& obj, |
| 52 const JavaParamRef<jobject>& j_profile) { | 55 const JavaParamRef<jobject>& j_profile) { |
| 53 NTPSnippetsBridge* snippets_bridge = new NTPSnippetsBridge(env, j_profile); | 56 NTPSnippetsBridge* snippets_bridge = new NTPSnippetsBridge(env, j_profile); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 } | 103 } |
| 101 | 104 |
| 102 void NTPSnippetsBridge::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 105 void NTPSnippetsBridge::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 103 delete this; | 106 delete this; |
| 104 } | 107 } |
| 105 | 108 |
| 106 void NTPSnippetsBridge::SetObserver(JNIEnv* env, | 109 void NTPSnippetsBridge::SetObserver(JNIEnv* env, |
| 107 const JavaParamRef<jobject>& obj, | 110 const JavaParamRef<jobject>& obj, |
| 108 const JavaParamRef<jobject>& j_observer) { | 111 const JavaParamRef<jobject>& j_observer) { |
| 109 observer_.Reset(env, j_observer); | 112 observer_.Reset(env, j_observer); |
| 110 OnNewSuggestions(); | |
|
dgn
2016/08/10 10:31:22
We currently pull snippets when setting the observ
Philipp Keck
2016/08/10 12:36:13
Replaced with this:
https://codereview.chromium.or
| |
| 111 } | 113 } |
| 112 | 114 |
| 113 void NTPSnippetsBridge::FetchImage(JNIEnv* env, | 115 ScopedJavaLocalRef<jintArray> NTPSnippetsBridge::GetCategories( |
| 114 const JavaParamRef<jobject>& obj, | 116 JNIEnv* env, |
| 115 const JavaParamRef<jstring>& snippet_id, | 117 const base::android::JavaParamRef<jobject>& obj) { |
| 116 const JavaParamRef<jobject>& j_callback) { | 118 std::vector<int> category_ids; |
| 117 base::android::ScopedJavaGlobalRef<jobject> callback(j_callback); | 119 for (Category category : content_suggestions_service_->GetCategories()) { |
| 118 content_suggestions_service_->FetchSuggestionImage( | 120 category_ids.push_back(category.id()); |
| 119 ConvertJavaStringToUTF8(env, snippet_id), | 121 } |
| 120 base::Bind(&NTPSnippetsBridge::OnImageFetched, | 122 return ToJavaIntArray(env, category_ids); |
| 121 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 122 } | |
| 123 | |
| 124 void NTPSnippetsBridge::DiscardSnippet(JNIEnv* env, | |
| 125 const JavaParamRef<jobject>& obj, | |
| 126 const JavaParamRef<jstring>& id) { | |
| 127 content_suggestions_service_->DismissSuggestion( | |
| 128 ConvertJavaStringToUTF8(env, id)); | |
| 129 } | |
| 130 | |
| 131 void NTPSnippetsBridge::SnippetVisited(JNIEnv* env, | |
| 132 const JavaParamRef<jobject>& obj, | |
| 133 const JavaParamRef<jobject>& jcallback, | |
| 134 const JavaParamRef<jstring>& jurl) { | |
| 135 base::android::ScopedJavaGlobalRef<jobject> callback(jcallback); | |
| 136 | |
| 137 history_service_->QueryURL( | |
| 138 GURL(ConvertJavaStringToUTF8(env, jurl)), | |
| 139 false, | |
| 140 base::Bind(&SnippetVisitedHistoryRequestCallback, callback), | |
| 141 &tracker_); | |
| 142 } | 123 } |
| 143 | 124 |
| 144 int NTPSnippetsBridge::GetCategoryStatus(JNIEnv* env, | 125 int NTPSnippetsBridge::GetCategoryStatus(JNIEnv* env, |
| 145 const JavaParamRef<jobject>& obj, | 126 const JavaParamRef<jobject>& obj, |
| 146 jint category) { | 127 jint category) { |
| 147 return static_cast<int>(content_suggestions_service_->GetCategoryStatus( | 128 return static_cast<int>(content_suggestions_service_->GetCategoryStatus( |
| 148 content_suggestions_service_->category_factory()->FromKnownCategory( | 129 content_suggestions_service_->category_factory()->FromIDValue(category))); |
| 149 KnownCategories::ARTICLES))); | 130 } |
| 131 | |
| 132 base::android::ScopedJavaLocalRef<jobject> NTPSnippetsBridge::GetCategoryInfo( | |
| 133 JNIEnv* env, | |
| 134 const base::android::JavaParamRef<jobject>& obj, | |
| 135 jint category) { | |
| 136 base::Optional<CategoryInfo> info = | |
| 137 content_suggestions_service_->GetCategoryInfo( | |
| 138 content_suggestions_service_->category_factory()->FromIDValue( | |
| 139 category)); | |
| 140 if (!info) | |
| 141 return base::android::ScopedJavaLocalRef<jobject>(env, nullptr); | |
| 142 return Java_SnippetsBridge_createSuggestionsCategoryInfo( | |
| 143 env, ConvertUTF16ToJavaString(env, info->title()).obj(), | |
| 144 static_cast<int>(info->card_layout())); | |
| 145 } | |
| 146 | |
| 147 ScopedJavaLocalRef<jobject> NTPSnippetsBridge::GetSuggestionsForCategory( | |
| 148 JNIEnv* env, | |
| 149 const base::android::JavaParamRef<jobject>& obj, | |
| 150 jint category) { | |
| 151 const std::vector<ContentSuggestion>& suggestions = | |
| 152 content_suggestions_service_->GetSuggestionsForCategory( | |
| 153 content_suggestions_service_->category_factory()->FromIDValue( | |
| 154 category)); | |
| 155 ScopedJavaLocalRef<jobject> result = | |
| 156 Java_SnippetsBridge_createSuggestionList(env); | |
| 157 for (const ContentSuggestion& suggestion : suggestions) { | |
| 158 Java_SnippetsBridge_addSuggestion( | |
| 159 env, result.obj(), ConvertUTF8ToJavaString(env, suggestion.id()).obj(), | |
| 160 ConvertUTF16ToJavaString(env, suggestion.title()).obj(), | |
| 161 ConvertUTF16ToJavaString(env, suggestion.publisher_name()).obj(), | |
| 162 ConvertUTF16ToJavaString(env, suggestion.snippet_text()).obj(), | |
| 163 ConvertUTF8ToJavaString(env, suggestion.url().spec()).obj(), | |
| 164 ConvertUTF8ToJavaString(env, suggestion.amp_url().spec()).obj(), | |
| 165 suggestion.publish_date().ToJavaTime(), suggestion.score()); | |
| 166 } | |
| 167 return result; | |
| 168 } | |
| 169 | |
| 170 void NTPSnippetsBridge::FetchSuggestionImage( | |
| 171 JNIEnv* env, | |
| 172 const JavaParamRef<jobject>& obj, | |
| 173 const JavaParamRef<jstring>& suggestion_id, | |
| 174 const JavaParamRef<jobject>& j_callback) { | |
| 175 base::android::ScopedJavaGlobalRef<jobject> callback(j_callback); | |
| 176 content_suggestions_service_->FetchSuggestionImage( | |
| 177 ConvertJavaStringToUTF8(env, suggestion_id), | |
| 178 base::Bind(&NTPSnippetsBridge::OnImageFetched, | |
| 179 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 180 } | |
| 181 | |
| 182 void NTPSnippetsBridge::DismissSuggestion( | |
| 183 JNIEnv* env, | |
| 184 const JavaParamRef<jobject>& obj, | |
| 185 const JavaParamRef<jstring>& suggestion_id) { | |
| 186 content_suggestions_service_->DismissSuggestion( | |
| 187 ConvertJavaStringToUTF8(env, suggestion_id)); | |
| 188 } | |
| 189 | |
| 190 void NTPSnippetsBridge::GetURLVisited(JNIEnv* env, | |
| 191 const JavaParamRef<jobject>& obj, | |
| 192 const JavaParamRef<jobject>& jcallback, | |
| 193 const JavaParamRef<jstring>& jurl) { | |
| 194 base::android::ScopedJavaGlobalRef<jobject> callback(jcallback); | |
| 195 | |
| 196 history_service_->QueryURL( | |
| 197 GURL(ConvertJavaStringToUTF8(env, jurl)), false, | |
| 198 base::Bind(&URLVisitedHistoryRequestCallback, callback), &tracker_); | |
| 150 } | 199 } |
| 151 | 200 |
| 152 NTPSnippetsBridge::~NTPSnippetsBridge() {} | 201 NTPSnippetsBridge::~NTPSnippetsBridge() {} |
| 153 | 202 |
| 154 void NTPSnippetsBridge::OnNewSuggestions() { | 203 void NTPSnippetsBridge::OnNewSuggestions(Category category) { |
| 155 if (observer_.is_null()) | 204 if (observer_.is_null()) |
| 156 return; | 205 return; |
| 157 | 206 |
| 158 // Show all suggestions from all categories, even though we currently display | |
| 159 // them in a single section on the UI. | |
| 160 // TODO(pke): This is only for debugging new sections and will be replaced | |
| 161 // with proper multi-section UI support. | |
| 162 JNIEnv* env = base::android::AttachCurrentThread(); | 207 JNIEnv* env = base::android::AttachCurrentThread(); |
| 163 ScopedJavaLocalRef<jobject> suggestions = | 208 Java_SnippetsBridge_onNewSuggestions(env, observer_.obj(), |
| 164 Java_SnippetsBridge_createSuggestionList(env); | 209 static_cast<int>(category.id())); |
| 165 for (Category category : | |
| 166 content_suggestions_service_->GetCategories()) { | |
| 167 if (content_suggestions_service_->GetCategoryStatus(category) != | |
| 168 CategoryStatus::AVAILABLE) { | |
| 169 continue; | |
| 170 } | |
| 171 for (const ntp_snippets::ContentSuggestion& suggestion : | |
| 172 content_suggestions_service_->GetSuggestionsForCategory(category)) { | |
| 173 Java_SnippetsBridge_addSuggestion( | |
| 174 env, suggestions.obj(), | |
| 175 ConvertUTF8ToJavaString(env, suggestion.id()).obj(), | |
| 176 ConvertUTF16ToJavaString(env, suggestion.title()).obj(), | |
| 177 ConvertUTF16ToJavaString(env, suggestion.publisher_name()).obj(), | |
| 178 ConvertUTF16ToJavaString(env, suggestion.snippet_text()).obj(), | |
| 179 ConvertUTF8ToJavaString(env, suggestion.url().spec()).obj(), | |
| 180 ConvertUTF8ToJavaString(env, suggestion.amp_url().spec()).obj(), | |
| 181 suggestion.publish_date().ToJavaTime(), suggestion.score()); | |
| 182 } | |
| 183 } | |
| 184 | |
| 185 // TODO(mvanouwerkerk): Do not hard code ARTICLES. | |
| 186 Java_SnippetsBridge_onSuggestionsAvailable( | |
| 187 env, observer_.obj(), | |
| 188 static_cast<int>( | |
| 189 content_suggestions_service_->category_factory()->FromKnownCategory( | |
| 190 KnownCategories::ARTICLES).id()), | |
| 191 suggestions.obj()); | |
| 192 } | 210 } |
| 193 | 211 |
| 194 void NTPSnippetsBridge::OnCategoryStatusChanged(Category category, | 212 void NTPSnippetsBridge::OnCategoryStatusChanged(Category category, |
| 195 CategoryStatus new_status) { | 213 CategoryStatus new_status) { |
| 196 // TODO(mvanouwerkerk): Do not hard code ARTICLES. | 214 if (observer_.is_null()) |
| 197 if (!category.IsKnownCategory(KnownCategories::ARTICLES)) | |
| 198 return; | 215 return; |
| 199 | 216 |
| 200 JNIEnv* env = base::android::AttachCurrentThread(); | 217 JNIEnv* env = base::android::AttachCurrentThread(); |
| 201 Java_SnippetsBridge_onCategoryStatusChanged(env, observer_.obj(), | 218 Java_SnippetsBridge_onCategoryStatusChanged(env, observer_.obj(), |
| 202 static_cast<int>(category.id()), | 219 static_cast<int>(category.id()), |
| 203 static_cast<int>(new_status)); | 220 static_cast<int>(new_status)); |
| 204 } | 221 } |
| 205 | 222 |
| 206 void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() { | 223 void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() { |
| 207 observer_.Reset(); | 224 observer_.Reset(); |
| 208 content_suggestions_service_observer_.Remove(content_suggestions_service_); | 225 content_suggestions_service_observer_.Remove(content_suggestions_service_); |
| 209 } | 226 } |
| 210 | 227 |
| 211 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback, | 228 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback, |
| 212 const std::string& snippet_id, | 229 const std::string& snippet_id, |
| 213 const gfx::Image& image) { | 230 const gfx::Image& image) { |
| 214 ScopedJavaLocalRef<jobject> j_bitmap; | 231 ScopedJavaLocalRef<jobject> j_bitmap; |
| 215 if (!image.IsEmpty()) | 232 if (!image.IsEmpty()) |
| 216 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); | 233 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); |
| 217 | 234 |
| 218 base::android::RunCallbackAndroid(callback, j_bitmap); | 235 base::android::RunCallbackAndroid(callback, j_bitmap); |
| 219 } | 236 } |
| 220 | 237 |
| 221 // static | 238 // static |
| 222 bool NTPSnippetsBridge::Register(JNIEnv* env) { | 239 bool NTPSnippetsBridge::Register(JNIEnv* env) { |
| 223 return RegisterNativesImpl(env); | 240 return RegisterNativesImpl(env); |
| 224 } | 241 } |
| OLD | NEW |