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

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: 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;
29 using base::android::JavaParamRef; 30 using base::android::JavaParamRef;
30 using base::android::ToJavaArrayOfStrings;
31 using base::android::ToJavaLongArray;
32 using base::android::ToJavaFloatArray;
33 using base::android::ScopedJavaGlobalRef; 31 using base::android::ScopedJavaGlobalRef;
34 using base::android::ScopedJavaLocalRef; 32 using base::android::ScopedJavaLocalRef;
35 using ntp_snippets::ContentSuggestionsCategory; 33 using ntp_snippets::ContentSuggestionsCategory;
36 using ntp_snippets::ContentSuggestionsCategoryStatus; 34 using ntp_snippets::ContentSuggestionsCategoryStatus;
37 35
38 namespace { 36 namespace {
39 37
40 void SnippetVisitedHistoryRequestCallback( 38 void SnippetVisitedHistoryRequestCallback(
41 base::android::ScopedJavaGlobalRef<jobject> callback, 39 base::android::ScopedJavaGlobalRef<jobject> callback,
42 bool success, 40 bool success,
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 base::android::ScopedJavaGlobalRef<jobject> callback(jcallback); 117 base::android::ScopedJavaGlobalRef<jobject> callback(jcallback);
120 118
121 history_service_->QueryURL( 119 history_service_->QueryURL(
122 GURL(ConvertJavaStringToUTF8(env, jurl)), 120 GURL(ConvertJavaStringToUTF8(env, jurl)),
123 false, 121 false,
124 base::Bind(&SnippetVisitedHistoryRequestCallback, callback), 122 base::Bind(&SnippetVisitedHistoryRequestCallback, callback),
125 &tracker_); 123 &tracker_);
126 } 124 }
127 125
128 int NTPSnippetsBridge::GetCategoryStatus(JNIEnv* env, 126 int NTPSnippetsBridge::GetCategoryStatus(JNIEnv* env,
129 const JavaParamRef<jobject>& obj) { 127 const JavaParamRef<jobject>& obj,
128 jint category) {
130 return static_cast<int>(content_suggestions_service_->GetCategoryStatus( 129 return static_cast<int>(content_suggestions_service_->GetCategoryStatus(
131 ContentSuggestionsCategory::ARTICLES)); 130 static_cast<ContentSuggestionsCategory>(category)));
132 } 131 }
133 132
134 NTPSnippetsBridge::~NTPSnippetsBridge() {} 133 NTPSnippetsBridge::~NTPSnippetsBridge() {}
135 134
136 void NTPSnippetsBridge::OnNewSuggestions() { 135 void NTPSnippetsBridge::OnNewSuggestions() {
137 if (observer_.is_null()) 136 if (observer_.is_null())
138 return; 137 return;
139 138
140 std::vector<std::string> ids;
141 std::vector<std::string> titles;
142 // URL for the article. This will also be used to find the favicon for the
143 // article.
144 std::vector<std::string> urls;
145 // URL for the AMP version of the article if it exists. This will be used as
146 // the URL to direct the user to on tap.
147 std::vector<std::string> amp_urls;
148 std::vector<std::string> snippets;
149 std::vector<int64_t> timestamps;
150 std::vector<std::string> publishers;
151 std::vector<float> scores;
152
153 // Show all suggestions from all categories, even though we currently display 139 // Show all suggestions from all categories, even though we currently display
154 // them in a single section on the UI. 140 // them in a single section on the UI.
155 // TODO(pke): This is only for debugging new sections and will be replaced 141 // TODO(pke): This is only for debugging new sections and will be replaced
156 // with proper multi-section UI support. 142 // with proper multi-section UI support.
143 JNIEnv* env = base::android::AttachCurrentThread();
144 ScopedJavaLocalRef<jobject> suggestions =
145 Java_SnippetsBridge_createSuggestionList(env);
157 for (ContentSuggestionsCategory category : 146 for (ContentSuggestionsCategory category :
158 content_suggestions_service_->GetCategories()) { 147 content_suggestions_service_->GetCategories()) {
159 if (content_suggestions_service_->GetCategoryStatus(category) != 148 if (content_suggestions_service_->GetCategoryStatus(category) !=
160 ContentSuggestionsCategoryStatus::AVAILABLE) { 149 ContentSuggestionsCategoryStatus::AVAILABLE) {
161 continue; 150 continue;
162 } 151 }
163 for (const ntp_snippets::ContentSuggestion& suggestion : 152 for (const ntp_snippets::ContentSuggestion& suggestion :
164 content_suggestions_service_->GetSuggestionsForCategory(category)) { 153 content_suggestions_service_->GetSuggestionsForCategory(category)) {
165 ids.push_back(suggestion.id()); 154 Java_SnippetsBridge_addSuggestion(
PEConn 2016/08/01 16:26:51 I do like this approach but is there any overhead
Bernhard Bauer 2016/08/02 13:03:27 My gut feeling would be a lot of the time would be
166 titles.push_back(suggestion.title()); 155 env, suggestions.obj(),
167 // The url from source_info is a url for a site that is one of the 156 ConvertUTF8ToJavaString(env, suggestion.id()).obj(),
168 // HOST_RESTRICT parameters, so this is preferred. 157 ConvertUTF8ToJavaString(env, suggestion.title()).obj(),
169 urls.push_back(suggestion.url().spec()); 158 ConvertUTF8ToJavaString(env, suggestion.publisher_name()).obj(),
170 amp_urls.push_back(suggestion.amp_url().spec()); 159 ConvertUTF8ToJavaString(env, suggestion.snippet_text()).obj(),
171 snippets.push_back(suggestion.snippet_text()); 160 ConvertUTF8ToJavaString(env, suggestion.url().spec()).obj(),
172 timestamps.push_back(suggestion.publish_date().ToJavaTime()); 161 ConvertUTF8ToJavaString(env, suggestion.amp_url().spec()).obj(),
173 publishers.push_back(suggestion.publisher_name()); 162 suggestion.publish_date().ToJavaTime(), suggestion.score());
174 scores.push_back(suggestion.score());
175 } 163 }
176 } 164 }
177 165
178 JNIEnv* env = base::android::AttachCurrentThread(); 166 // TODO(mvanouwerkerk): Do not hard code ARTICLES.
179 Java_SnippetsBridge_onSnippetsAvailable( 167 Java_SnippetsBridge_onSuggestionsAvailable(
180 env, observer_.obj(), ToJavaArrayOfStrings(env, ids).obj(), 168 env, observer_.obj(),
181 ToJavaArrayOfStrings(env, titles).obj(), 169 static_cast<int>(ContentSuggestionsCategory::ARTICLES),
182 ToJavaArrayOfStrings(env, urls).obj(), 170 suggestions.obj());
183 ToJavaArrayOfStrings(env, amp_urls).obj(),
184 ToJavaArrayOfStrings(env, snippets).obj(),
185 ToJavaLongArray(env, timestamps).obj(),
186 ToJavaArrayOfStrings(env, publishers).obj(),
187 ToJavaFloatArray(env, scores).obj());
188 } 171 }
189 172
190 void NTPSnippetsBridge::OnCategoryStatusChanged( 173 void NTPSnippetsBridge::OnCategoryStatusChanged(
191 ContentSuggestionsCategory category, 174 ContentSuggestionsCategory category,
192 ContentSuggestionsCategoryStatus new_status) { 175 ContentSuggestionsCategoryStatus new_status) {
176 // TODO(mvanouwerkerk): Do not hard code ARTICLES.
193 if (category != ContentSuggestionsCategory::ARTICLES) 177 if (category != ContentSuggestionsCategory::ARTICLES)
194 return; 178 return;
195 179
196 JNIEnv* env = base::android::AttachCurrentThread(); 180 JNIEnv* env = base::android::AttachCurrentThread();
197 Java_SnippetsBridge_onCategoryStatusChanged(env, observer_.obj(), 181 Java_SnippetsBridge_onCategoryStatusChanged(env, observer_.obj(),
182 static_cast<int>(category),
198 static_cast<int>(new_status)); 183 static_cast<int>(new_status));
199 } 184 }
200 185
201 void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() { 186 void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() {
202 observer_.Reset(); 187 observer_.Reset();
203 content_suggestions_service_observer_.Remove(content_suggestions_service_); 188 content_suggestions_service_observer_.Remove(content_suggestions_service_);
204 } 189 }
205 190
206 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback, 191 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback,
207 const std::string& snippet_id, 192 const std::string& snippet_id,
208 const gfx::Image& image) { 193 const gfx::Image& image) {
209 ScopedJavaLocalRef<jobject> j_bitmap; 194 ScopedJavaLocalRef<jobject> j_bitmap;
210 if (!image.IsEmpty()) 195 if (!image.IsEmpty())
211 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); 196 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap());
212 197
213 base::android::RunCallbackAndroid(callback, j_bitmap); 198 base::android::RunCallbackAndroid(callback, j_bitmap);
214 } 199 }
215 200
216 // static 201 // static
217 bool NTPSnippetsBridge::Register(JNIEnv* env) { 202 bool NTPSnippetsBridge::Register(JNIEnv* env) {
218 return RegisterNativesImpl(env); 203 return RegisterNativesImpl(env);
219 } 204 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698