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

Side by Side Diff: chrome/browser/android/ntp/ntp_snippets_bridge.cc

Issue 2196273002: Zine: support multiple sections in the ui (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 4 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 unified diff | Download patch
« no previous file with comments | « chrome/browser/android/ntp/ntp_snippets_bridge.h ('k') | components/ntp_snippets/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "base/android/jni_array.h" 11 #include "base/android/jni_array.h"
12 #include "base/android/jni_string.h" 12 #include "base/android/jni_string.h"
13 #include "base/callback.h" 13 #include "base/callback.h"
14 #include "chrome/browser/history/history_service_factory.h" 14 #include "chrome/browser/history/history_service_factory.h"
15 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" 15 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h"
16 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/profiles/profile_android.h" 17 #include "chrome/browser/profiles/profile_android.h"
18 #include "chrome/browser/profiles/profile_manager.h" 18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "components/history/core/browser/history_service.h" 19 #include "components/history/core/browser/history_service.h"
20 #include "components/ntp_snippets/content_suggestion.h" 20 #include "components/ntp_snippets/content_suggestion.h"
21 #include "components/ntp_snippets/ntp_snippets_service.h" 21 #include "components/ntp_snippets/ntp_snippets_service.h"
22 #include "jni/SnippetsBridge_jni.h" 22 #include "jni/SnippetsBridge_jni.h"
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;
29 using base::android::ConvertUTF16ToJavaString;
28 using base::android::JavaParamRef; 30 using base::android::JavaParamRef;
29 using base::android::ToJavaArrayOfStrings;
30 using base::android::ToJavaLongArray;
31 using base::android::ToJavaFloatArray;
32 using base::android::ScopedJavaGlobalRef; 31 using base::android::ScopedJavaGlobalRef;
33 using base::android::ScopedJavaLocalRef; 32 using base::android::ScopedJavaLocalRef;
34 using ntp_snippets::Category; 33 using ntp_snippets::Category;
35 using ntp_snippets::CategoryStatus; 34 using ntp_snippets::CategoryStatus;
36 using ntp_snippets::KnownCategories; 35 using ntp_snippets::KnownCategories;
37 36
38 namespace { 37 namespace {
39 38
40 void SnippetVisitedHistoryRequestCallback( 39 void SnippetVisitedHistoryRequestCallback(
41 base::android::ScopedJavaGlobalRef<jobject> callback, 40 base::android::ScopedJavaGlobalRef<jobject> callback,
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 base::android::ScopedJavaGlobalRef<jobject> callback(jcallback); 121 base::android::ScopedJavaGlobalRef<jobject> callback(jcallback);
123 122
124 history_service_->QueryURL( 123 history_service_->QueryURL(
125 GURL(ConvertJavaStringToUTF8(env, jurl)), 124 GURL(ConvertJavaStringToUTF8(env, jurl)),
126 false, 125 false,
127 base::Bind(&SnippetVisitedHistoryRequestCallback, callback), 126 base::Bind(&SnippetVisitedHistoryRequestCallback, callback),
128 &tracker_); 127 &tracker_);
129 } 128 }
130 129
131 int NTPSnippetsBridge::GetCategoryStatus(JNIEnv* env, 130 int NTPSnippetsBridge::GetCategoryStatus(JNIEnv* env,
132 const JavaParamRef<jobject>& obj) { 131 const JavaParamRef<jobject>& obj,
132 jint category) {
133 return static_cast<int>(content_suggestions_service_->GetCategoryStatus( 133 return static_cast<int>(content_suggestions_service_->GetCategoryStatus(
134 content_suggestions_service_->category_factory()->FromKnownCategory( 134 content_suggestions_service_->category_factory()->FromKnownCategory(
135 KnownCategories::ARTICLES))); 135 KnownCategories::ARTICLES)));
136 } 136 }
137 137
138 NTPSnippetsBridge::~NTPSnippetsBridge() {} 138 NTPSnippetsBridge::~NTPSnippetsBridge() {}
139 139
140 void NTPSnippetsBridge::OnNewSuggestions() { 140 void NTPSnippetsBridge::OnNewSuggestions() {
141 if (observer_.is_null()) 141 if (observer_.is_null())
142 return; 142 return;
143 143
144 std::vector<std::string> ids;
145 std::vector<base::string16> titles;
146 // URL for the article. This will also be used to find the favicon for the
147 // article.
148 std::vector<std::string> urls;
149 // URL for the AMP version of the article if it exists. This will be used as
150 // the URL to direct the user to on tap.
151 std::vector<std::string> amp_urls;
152 std::vector<base::string16> snippet_texts;
153 std::vector<int64_t> timestamps;
154 std::vector<base::string16> publisher_names;
155 std::vector<float> scores;
156
157 // Show all suggestions from all categories, even though we currently display 144 // Show all suggestions from all categories, even though we currently display
158 // them in a single section on the UI. 145 // them in a single section on the UI.
159 // TODO(pke): This is only for debugging new sections and will be replaced 146 // TODO(pke): This is only for debugging new sections and will be replaced
160 // with proper multi-section UI support. 147 // with proper multi-section UI support.
161 for (Category category : content_suggestions_service_->GetCategories()) { 148 JNIEnv* env = base::android::AttachCurrentThread();
149 ScopedJavaLocalRef<jobject> suggestions =
150 Java_SnippetsBridge_createSuggestionList(env);
151 for (Category category :
152 content_suggestions_service_->GetCategories()) {
162 if (content_suggestions_service_->GetCategoryStatus(category) != 153 if (content_suggestions_service_->GetCategoryStatus(category) !=
163 CategoryStatus::AVAILABLE) { 154 CategoryStatus::AVAILABLE) {
164 continue; 155 continue;
165 } 156 }
166 for (const ntp_snippets::ContentSuggestion& suggestion : 157 for (const ntp_snippets::ContentSuggestion& suggestion :
167 content_suggestions_service_->GetSuggestionsForCategory(category)) { 158 content_suggestions_service_->GetSuggestionsForCategory(category)) {
168 ids.push_back(suggestion.id()); 159 Java_SnippetsBridge_addSuggestion(
169 titles.push_back(suggestion.title()); 160 env, suggestions.obj(),
170 // The url from source_info is a url for a site that is one of the 161 ConvertUTF8ToJavaString(env, suggestion.id()).obj(),
171 // HOST_RESTRICT parameters, so this is preferred. 162 ConvertUTF16ToJavaString(env, suggestion.title()).obj(),
172 urls.push_back(suggestion.url().spec()); 163 ConvertUTF16ToJavaString(env, suggestion.publisher_name()).obj(),
173 amp_urls.push_back(suggestion.amp_url().spec()); 164 ConvertUTF16ToJavaString(env, suggestion.snippet_text()).obj(),
174 snippet_texts.push_back(suggestion.snippet_text()); 165 ConvertUTF8ToJavaString(env, suggestion.url().spec()).obj(),
175 timestamps.push_back(suggestion.publish_date().ToJavaTime()); 166 ConvertUTF8ToJavaString(env, suggestion.amp_url().spec()).obj(),
176 publisher_names.push_back(suggestion.publisher_name()); 167 suggestion.publish_date().ToJavaTime(), suggestion.score());
177 scores.push_back(suggestion.score());
178 } 168 }
179 } 169 }
180 170
181 JNIEnv* env = base::android::AttachCurrentThread(); 171 // TODO(mvanouwerkerk): Do not hard code ARTICLES.
182 Java_SnippetsBridge_onSnippetsAvailable( 172 Java_SnippetsBridge_onSuggestionsAvailable(
183 env, observer_.obj(), ToJavaArrayOfStrings(env, ids).obj(), 173 env, observer_.obj(),
184 ToJavaArrayOfStrings(env, titles).obj(), 174 static_cast<int>(
185 ToJavaArrayOfStrings(env, urls).obj(), 175 content_suggestions_service_->category_factory()->FromKnownCategory(
186 ToJavaArrayOfStrings(env, amp_urls).obj(), 176 KnownCategories::ARTICLES).id()),
187 ToJavaArrayOfStrings(env, snippet_texts).obj(), 177 suggestions.obj());
188 ToJavaLongArray(env, timestamps).obj(),
189 ToJavaArrayOfStrings(env, publisher_names).obj(),
190 ToJavaFloatArray(env, scores).obj());
191 } 178 }
192 179
193 void NTPSnippetsBridge::OnCategoryStatusChanged(Category category, 180 void NTPSnippetsBridge::OnCategoryStatusChanged(Category category,
194 CategoryStatus new_status) { 181 CategoryStatus new_status) {
182 // TODO(mvanouwerkerk): Do not hard code ARTICLES.
195 if (!category.IsKnownCategory(KnownCategories::ARTICLES)) 183 if (!category.IsKnownCategory(KnownCategories::ARTICLES))
196 return; 184 return;
197 185
198 JNIEnv* env = base::android::AttachCurrentThread(); 186 JNIEnv* env = base::android::AttachCurrentThread();
199 Java_SnippetsBridge_onCategoryStatusChanged(env, observer_.obj(), 187 Java_SnippetsBridge_onCategoryStatusChanged(env, observer_.obj(),
188 static_cast<int>(category.id()),
200 static_cast<int>(new_status)); 189 static_cast<int>(new_status));
201 } 190 }
202 191
203 void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() { 192 void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() {
204 observer_.Reset(); 193 observer_.Reset();
205 content_suggestions_service_observer_.Remove(content_suggestions_service_); 194 content_suggestions_service_observer_.Remove(content_suggestions_service_);
206 } 195 }
207 196
208 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback, 197 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback,
209 const std::string& snippet_id, 198 const std::string& snippet_id,
210 const gfx::Image& image) { 199 const gfx::Image& image) {
211 ScopedJavaLocalRef<jobject> j_bitmap; 200 ScopedJavaLocalRef<jobject> j_bitmap;
212 if (!image.IsEmpty()) 201 if (!image.IsEmpty())
213 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); 202 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap());
214 203
215 base::android::RunCallbackAndroid(callback, j_bitmap); 204 base::android::RunCallbackAndroid(callback, j_bitmap);
216 } 205 }
217 206
218 // static 207 // static
219 bool NTPSnippetsBridge::Register(JNIEnv* env) { 208 bool NTPSnippetsBridge::Register(JNIEnv* env) {
220 return RegisterNativesImpl(env); 209 return RegisterNativesImpl(env);
221 } 210 }
OLDNEW
« no previous file with comments | « chrome/browser/android/ntp/ntp_snippets_bridge.h ('k') | components/ntp_snippets/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698