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

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: review 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
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/ntp_snippets/ntp_snippets_service_factory.h" 16 #include "chrome/browser/ntp_snippets/ntp_snippets_service_factory.h"
17 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/profiles/profile_android.h" 18 #include "chrome/browser/profiles/profile_android.h"
19 #include "chrome/browser/profiles/profile_manager.h" 19 #include "chrome/browser/profiles/profile_manager.h"
20 #include "components/history/core/browser/history_service.h" 20 #include "components/history/core/browser/history_service.h"
21 #include "components/ntp_snippets/content_suggestion.h" 21 #include "components/ntp_snippets/content_suggestion.h"
22 #include "components/ntp_snippets/ntp_snippets_service.h" 22 #include "components/ntp_snippets/ntp_snippets_service.h"
23 #include "jni/SnippetsBridge_jni.h" 23 #include "jni/SnippetsBridge_jni.h"
24 #include "ui/gfx/android/java_bitmap.h" 24 #include "ui/gfx/android/java_bitmap.h"
25 #include "ui/gfx/image/image.h" 25 #include "ui/gfx/image/image.h"
26 26
27 using base::android::AttachCurrentThread; 27 using base::android::AttachCurrentThread;
28 using base::android::ConvertJavaStringToUTF8; 28 using base::android::ConvertJavaStringToUTF8;
29 using base::android::ConvertUTF8ToJavaString;
30 using base::android::ConvertUTF16ToJavaString;
29 using base::android::JavaParamRef; 31 using base::android::JavaParamRef;
30 using base::android::ToJavaArrayOfStrings;
31 using base::android::ToJavaLongArray;
32 using base::android::ToJavaFloatArray;
33 using base::android::ScopedJavaGlobalRef; 32 using base::android::ScopedJavaGlobalRef;
34 using base::android::ScopedJavaLocalRef; 33 using base::android::ScopedJavaLocalRef;
35 using ntp_snippets::Category; 34 using ntp_snippets::Category;
36 using ntp_snippets::CategoryStatus; 35 using ntp_snippets::CategoryStatus;
37 using ntp_snippets::KnownCategories; 36 using ntp_snippets::KnownCategories;
38 37
39 namespace { 38 namespace {
40 39
41 void SnippetVisitedHistoryRequestCallback( 40 void SnippetVisitedHistoryRequestCallback(
42 base::android::ScopedJavaGlobalRef<jobject> callback, 41 base::android::ScopedJavaGlobalRef<jobject> callback,
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 base::android::ScopedJavaGlobalRef<jobject> callback(jcallback); 119 base::android::ScopedJavaGlobalRef<jobject> callback(jcallback);
121 120
122 history_service_->QueryURL( 121 history_service_->QueryURL(
123 GURL(ConvertJavaStringToUTF8(env, jurl)), 122 GURL(ConvertJavaStringToUTF8(env, jurl)),
124 false, 123 false,
125 base::Bind(&SnippetVisitedHistoryRequestCallback, callback), 124 base::Bind(&SnippetVisitedHistoryRequestCallback, callback),
126 &tracker_); 125 &tracker_);
127 } 126 }
128 127
129 int NTPSnippetsBridge::GetCategoryStatus(JNIEnv* env, 128 int NTPSnippetsBridge::GetCategoryStatus(JNIEnv* env,
130 const JavaParamRef<jobject>& obj) { 129 const JavaParamRef<jobject>& obj,
130 jint category) {
131 return static_cast<int>(content_suggestions_service_->GetCategoryStatus( 131 return static_cast<int>(content_suggestions_service_->GetCategoryStatus(
132 content_suggestions_service_->category_factory()->FromKnownCategory( 132 content_suggestions_service_->category_factory()->FromKnownCategory(
133 KnownCategories::ARTICLES))); 133 KnownCategories::ARTICLES)));
134 } 134 }
135 135
136 NTPSnippetsBridge::~NTPSnippetsBridge() {} 136 NTPSnippetsBridge::~NTPSnippetsBridge() {}
137 137
138 void NTPSnippetsBridge::OnNewSuggestions() { 138 void NTPSnippetsBridge::OnNewSuggestions() {
139 if (observer_.is_null()) 139 if (observer_.is_null())
140 return; 140 return;
141 141
142 std::vector<std::string> ids;
143 std::vector<base::string16> titles;
144 // URL for the article. This will also be used to find the favicon for the
145 // article.
146 std::vector<std::string> urls;
147 // URL for the AMP version of the article if it exists. This will be used as
148 // the URL to direct the user to on tap.
149 std::vector<std::string> amp_urls;
150 std::vector<base::string16> snippet_texts;
151 std::vector<int64_t> timestamps;
152 std::vector<base::string16> publisher_names;
153 std::vector<float> scores;
154
155 // Show all suggestions from all categories, even though we currently display 142 // Show all suggestions from all categories, even though we currently display
156 // them in a single section on the UI. 143 // them in a single section on the UI.
157 // TODO(pke): This is only for debugging new sections and will be replaced 144 // TODO(pke): This is only for debugging new sections and will be replaced
158 // with proper multi-section UI support. 145 // with proper multi-section UI support.
159 for (Category category : content_suggestions_service_->GetCategories()) { 146 JNIEnv* env = base::android::AttachCurrentThread();
147 ScopedJavaLocalRef<jobject> suggestions =
148 Java_SnippetsBridge_createSuggestionList(env);
149 for (Category category :
150 content_suggestions_service_->GetCategories()) {
160 if (content_suggestions_service_->GetCategoryStatus(category) != 151 if (content_suggestions_service_->GetCategoryStatus(category) !=
161 CategoryStatus::AVAILABLE) { 152 CategoryStatus::AVAILABLE) {
162 continue; 153 continue;
163 } 154 }
164 for (const ntp_snippets::ContentSuggestion& suggestion : 155 for (const ntp_snippets::ContentSuggestion& suggestion :
165 content_suggestions_service_->GetSuggestionsForCategory(category)) { 156 content_suggestions_service_->GetSuggestionsForCategory(category)) {
166 ids.push_back(suggestion.id()); 157 Java_SnippetsBridge_addSuggestion(
167 titles.push_back(suggestion.title()); 158 env, suggestions.obj(),
168 // The url from source_info is a url for a site that is one of the 159 ConvertUTF8ToJavaString(env, suggestion.id()).obj(),
169 // HOST_RESTRICT parameters, so this is preferred. 160 ConvertUTF16ToJavaString(env, suggestion.title()).obj(),
PEConn 2016/08/02 15:45:15 So we have a mix of two different string represent
Bernhard Bauer 2016/08/03 16:21:16 Because overloads are discouraged by the style gui
170 urls.push_back(suggestion.url().spec()); 161 ConvertUTF16ToJavaString(env, suggestion.publisher_name()).obj(),
171 amp_urls.push_back(suggestion.amp_url().spec()); 162 ConvertUTF16ToJavaString(env, suggestion.snippet_text()).obj(),
172 snippet_texts.push_back(suggestion.snippet_text()); 163 ConvertUTF8ToJavaString(env, suggestion.url().spec()).obj(),
173 timestamps.push_back(suggestion.publish_date().ToJavaTime()); 164 ConvertUTF8ToJavaString(env, suggestion.amp_url().spec()).obj(),
174 publisher_names.push_back(suggestion.publisher_name()); 165 suggestion.publish_date().ToJavaTime(), suggestion.score());
175 scores.push_back(suggestion.score());
176 } 166 }
177 } 167 }
178 168
179 JNIEnv* env = base::android::AttachCurrentThread(); 169 // TODO(mvanouwerkerk): Do not hard code ARTICLES.
180 Java_SnippetsBridge_onSnippetsAvailable( 170 Java_SnippetsBridge_onSuggestionsAvailable(
181 env, observer_.obj(), ToJavaArrayOfStrings(env, ids).obj(), 171 env, observer_.obj(),
182 ToJavaArrayOfStrings(env, titles).obj(), 172 static_cast<int>(
183 ToJavaArrayOfStrings(env, urls).obj(), 173 content_suggestions_service_->category_factory()->FromKnownCategory(
184 ToJavaArrayOfStrings(env, amp_urls).obj(), 174 KnownCategories::ARTICLES).id()),
185 ToJavaArrayOfStrings(env, snippet_texts).obj(), 175 suggestions.obj());
186 ToJavaLongArray(env, timestamps).obj(),
187 ToJavaArrayOfStrings(env, publisher_names).obj(),
188 ToJavaFloatArray(env, scores).obj());
189 } 176 }
190 177
191 void NTPSnippetsBridge::OnCategoryStatusChanged(Category category, 178 void NTPSnippetsBridge::OnCategoryStatusChanged(Category category,
192 CategoryStatus new_status) { 179 CategoryStatus new_status) {
180 // TODO(mvanouwerkerk): Do not hard code ARTICLES.
193 if (!category.IsKnownCategory(KnownCategories::ARTICLES)) 181 if (!category.IsKnownCategory(KnownCategories::ARTICLES))
194 return; 182 return;
195 183
196 JNIEnv* env = base::android::AttachCurrentThread(); 184 JNIEnv* env = base::android::AttachCurrentThread();
197 Java_SnippetsBridge_onCategoryStatusChanged(env, observer_.obj(), 185 Java_SnippetsBridge_onCategoryStatusChanged(env, observer_.obj(),
186 static_cast<int>(category.id()),
198 static_cast<int>(new_status)); 187 static_cast<int>(new_status));
199 } 188 }
200 189
201 void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() { 190 void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() {
202 observer_.Reset(); 191 observer_.Reset();
203 content_suggestions_service_observer_.Remove(content_suggestions_service_); 192 content_suggestions_service_observer_.Remove(content_suggestions_service_);
204 } 193 }
205 194
206 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback, 195 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback,
207 const std::string& snippet_id, 196 const std::string& snippet_id,
208 const gfx::Image& image) { 197 const gfx::Image& image) {
209 ScopedJavaLocalRef<jobject> j_bitmap; 198 ScopedJavaLocalRef<jobject> j_bitmap;
210 if (!image.IsEmpty()) 199 if (!image.IsEmpty())
211 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); 200 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap());
212 201
213 base::android::RunCallbackAndroid(callback, j_bitmap); 202 base::android::RunCallbackAndroid(callback, j_bitmap);
214 } 203 }
215 204
216 // static 205 // static
217 bool NTPSnippetsBridge::Register(JNIEnv* env) { 206 bool NTPSnippetsBridge::Register(JNIEnv* env) {
218 return RegisterNativesImpl(env); 207 return RegisterNativesImpl(env);
219 } 208 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698