Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4434)

Unified Diff: chrome/browser/android/ntp/ntp_snippets_bridge.cc

Issue 2158883002: Change NTPSnippetsBridge to read from ContentSuggestionsService (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@offlinepagesprovider
Patch Set: Marc's comments Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/android/ntp/ntp_snippets_bridge.cc
diff --git a/chrome/browser/android/ntp/ntp_snippets_bridge.cc b/chrome/browser/android/ntp/ntp_snippets_bridge.cc
index 373de14c8c375514d5a2045b5272095207e5892c..49ef541d84e94329742a05da8c3ed716ffec5c6f 100644
--- a/chrome/browser/android/ntp/ntp_snippets_bridge.cc
+++ b/chrome/browser/android/ntp/ntp_snippets_bridge.cc
@@ -12,13 +12,13 @@
#include "base/android/jni_string.h"
#include "base/callback.h"
#include "chrome/browser/history/history_service_factory.h"
+#include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h"
#include "chrome/browser/ntp_snippets/ntp_snippets_service_factory.h"
Marc Treib 2016/07/20 09:17:38 Not needed anymore
Philipp Keck 2016/07/20 13:20:02 It's still needed in the static methods FetchSnipp
Marc Treib 2016/07/20 13:41:11 Ah, right. Hm, we could pipe both of these through
Philipp Keck 2016/07/20 14:51:15 For now, I'd consider it an implementation detail
Marc Treib 2016/07/20 14:56:02 For the scheduling I agree (and it's really just o
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_android.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "components/history/core/browser/history_service.h"
-#include "components/ntp_snippets/ntp_snippet.h"
-#include "components/ntp_snippets/ntp_snippets_service.h"
+#include "components/ntp_snippets/content_suggestion.h"
#include "jni/SnippetsBridge_jni.h"
#include "ui/gfx/android/java_bitmap.h"
#include "ui/gfx/image/image.h"
@@ -31,6 +31,8 @@ using base::android::ToJavaLongArray;
using base::android::ToJavaFloatArray;
using base::android::ScopedJavaGlobalRef;
using base::android::ScopedJavaLocalRef;
+using ntp_snippets::ContentSuggestionsCategory;
+using ntp_snippets::ContentSuggestionsCategoryStatus;
namespace {
@@ -68,13 +70,14 @@ static void RescheduleFetching(JNIEnv* env,
NTPSnippetsBridge::NTPSnippetsBridge(JNIEnv* env,
const JavaParamRef<jobject>& j_profile)
- : snippet_service_observer_(this), weak_ptr_factory_(this) {
+ : content_suggestions_service_observer_(this), weak_ptr_factory_(this) {
Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile);
- ntp_snippets_service_ = NTPSnippetsServiceFactory::GetForProfile(profile);
+ content_suggestions_service_ =
+ ContentSuggestionsServiceFactory::GetForProfile(profile);
history_service_ =
HistoryServiceFactory::GetForProfile(profile,
ServiceAccessType::EXPLICIT_ACCESS);
- snippet_service_observer_.Add(ntp_snippets_service_);
+ content_suggestions_service_observer_.Add(content_suggestions_service_);
}
void NTPSnippetsBridge::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) {
@@ -85,7 +88,7 @@ void NTPSnippetsBridge::SetObserver(JNIEnv* env,
const JavaParamRef<jobject>& obj,
const JavaParamRef<jobject>& j_observer) {
observer_.Reset(env, j_observer);
- NTPSnippetsServiceLoaded();
+ OnNewSuggestions();
}
void NTPSnippetsBridge::FetchImage(JNIEnv* env,
@@ -93,7 +96,7 @@ void NTPSnippetsBridge::FetchImage(JNIEnv* env,
const JavaParamRef<jstring>& snippet_id,
const JavaParamRef<jobject>& j_callback) {
base::android::ScopedJavaGlobalRef<jobject> callback(j_callback);
- ntp_snippets_service_->FetchSuggestionImage(
+ content_suggestions_service_->FetchSuggestionImage(
ConvertJavaStringToUTF8(env, snippet_id),
base::Bind(&NTPSnippetsBridge::OnImageFetched,
weak_ptr_factory_.GetWeakPtr(), callback));
@@ -102,7 +105,8 @@ void NTPSnippetsBridge::FetchImage(JNIEnv* env,
void NTPSnippetsBridge::DiscardSnippet(JNIEnv* env,
const JavaParamRef<jobject>& obj,
const JavaParamRef<jstring>& id) {
- ntp_snippets_service_->DiscardSuggestion(ConvertJavaStringToUTF8(env, id));
+ content_suggestions_service_->DiscardSuggestion(
+ ConvertJavaStringToUTF8(env, id));
}
void NTPSnippetsBridge::SnippetVisited(JNIEnv* env,
@@ -118,14 +122,15 @@ void NTPSnippetsBridge::SnippetVisited(JNIEnv* env,
&tracker_);
}
-int NTPSnippetsBridge::GetDisabledReason(JNIEnv* env,
+int NTPSnippetsBridge::GetCategoryStatus(JNIEnv* env,
const JavaParamRef<jobject>& obj) {
- return static_cast<int>(ntp_snippets_service_->disabled_reason());
+ return static_cast<int>(content_suggestions_service_->GetCategoryStatus(
+ ContentSuggestionsCategory::ARTICLES));
}
NTPSnippetsBridge::~NTPSnippetsBridge() {}
-void NTPSnippetsBridge::NTPSnippetsServiceLoaded() {
+void NTPSnippetsBridge::OnNewSuggestions() {
if (observer_.is_null())
return;
@@ -137,24 +142,23 @@ void NTPSnippetsBridge::NTPSnippetsServiceLoaded() {
// URL for the AMP version of the article if it exists. This will be used as
// the URL to direct the user to on tap.
std::vector<std::string> amp_urls;
- std::vector<std::string> thumbnail_urls;
std::vector<std::string> snippets;
std::vector<int64_t> timestamps;
std::vector<std::string> publishers;
std::vector<float> scores;
- for (const std::unique_ptr<ntp_snippets::NTPSnippet>& snippet :
- ntp_snippets_service_->snippets()) {
- ids.push_back(snippet->id());
- titles.push_back(snippet->title());
+ for (const ntp_snippets::ContentSuggestion& suggestion :
+ content_suggestions_service_->GetSuggestionsForCategory(
+ ContentSuggestionsCategory::ARTICLES)) {
+ ids.push_back(suggestion.id());
+ titles.push_back(suggestion.title());
// The url from source_info is a url for a site that is one of the
// HOST_RESTRICT parameters, so this is preferred.
- urls.push_back(snippet->best_source().url.spec());
- amp_urls.push_back(snippet->best_source().amp_url.spec());
- thumbnail_urls.push_back(snippet->salient_image_url().spec());
- snippets.push_back(snippet->snippet());
- timestamps.push_back(snippet->publish_date().ToJavaTime());
- publishers.push_back(snippet->best_source().publisher_name);
- scores.push_back(snippet->score());
+ urls.push_back(suggestion.url().spec());
+ amp_urls.push_back(suggestion.amp_url().spec());
+ snippets.push_back(suggestion.snippet_text());
+ timestamps.push_back(suggestion.publish_date().ToJavaTime());
+ publishers.push_back(suggestion.publisher_name());
+ scores.push_back(suggestion.score());
}
JNIEnv* env = base::android::AttachCurrentThread();
@@ -163,25 +167,26 @@ void NTPSnippetsBridge::NTPSnippetsServiceLoaded() {
ToJavaArrayOfStrings(env, titles).obj(),
ToJavaArrayOfStrings(env, urls).obj(),
ToJavaArrayOfStrings(env, amp_urls).obj(),
- ToJavaArrayOfStrings(env, thumbnail_urls).obj(),
ToJavaArrayOfStrings(env, snippets).obj(),
ToJavaLongArray(env, timestamps).obj(),
ToJavaArrayOfStrings(env, publishers).obj(),
ToJavaFloatArray(env, scores).obj());
}
-void NTPSnippetsBridge::NTPSnippetsServiceShutdown() {
- observer_.Reset();
- snippet_service_observer_.Remove(ntp_snippets_service_);
-}
+void NTPSnippetsBridge::OnCategoryStatusChanged(
+ ContentSuggestionsCategory category,
+ ContentSuggestionsCategoryStatus new_status) {
+ if (category != ContentSuggestionsCategory::ARTICLES)
+ return;
-void NTPSnippetsBridge::NTPSnippetsServiceDisabledReasonChanged(
- ntp_snippets::DisabledReason disabled_reason) {
- // The user signed out or disabled sync. Since snippets rely on those, we
- // clear them to be consistent with the initially signed out state.
JNIEnv* env = base::android::AttachCurrentThread();
- Java_SnippetsBridge_onDisabledReasonChanged(
- env, observer_.obj(), static_cast<int>(disabled_reason));
+ Java_SnippetsBridge_onCategoryStatusChanged(env, observer_.obj(),
+ static_cast<int>(new_status));
+}
+
+void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() {
+ observer_.Reset();
+ content_suggestions_service_observer_.Remove(content_suggestions_service_);
}
void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback,

Powered by Google App Engine
This is Rietveld 408576698