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

Side by Side Diff: chrome/browser/android/most_visited_sites.cc

Issue 298703009: SuggestionsService blacklist handling. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address first round of comments. Avoid QueryMostVisited loop. Created 6 years, 7 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/most_visited_sites.h" 5 #include "chrome/browser/android/most_visited_sites.h"
6 6
7 #include <string>
8 #include <vector>
9
7 #include "base/android/jni_android.h" 10 #include "base/android/jni_android.h"
8 #include "base/android/jni_array.h" 11 #include "base/android/jni_array.h"
9 #include "base/android/jni_string.h" 12 #include "base/android/jni_string.h"
10 #include "base/android/scoped_java_ref.h" 13 #include "base/android/scoped_java_ref.h"
11 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h" 15 #include "base/time/time.h"
13 #include "chrome/browser/chrome_notification_types.h" 16 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/history/history_types.h"
15 #include "chrome/browser/history/top_sites.h" 17 #include "chrome/browser/history/top_sites.h"
16 #include "chrome/browser/profiles/profile.h" 18 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/profiles/profile_android.h" 19 #include "chrome/browser/profiles/profile_android.h"
18 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" 20 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h"
19 #include "chrome/browser/search/suggestions/suggestions_service.h" 21 #include "chrome/browser/search/suggestions/suggestions_service.h"
20 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" 22 #include "chrome/browser/search/suggestions/suggestions_service_factory.h"
21 #include "chrome/browser/search/suggestions/suggestions_source.h" 23 #include "chrome/browser/search/suggestions/suggestions_source.h"
22 #include "chrome/browser/thumbnails/thumbnail_list_source.h" 24 #include "chrome/browser/thumbnails/thumbnail_list_source.h"
23 #include "content/public/browser/browser_thread.h" 25 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/notification_source.h" 26 #include "content/public/browser/notification_source.h"
(...skipping 28 matching lines...) Expand all
53 const history::MostVisitedURL& visited = visited_list[i]; 55 const history::MostVisitedURL& visited = visited_list[i];
54 56
55 if (visited.url.is_empty()) 57 if (visited.url.is_empty())
56 break; // This is the signal that there are no more real visited sites. 58 break; // This is the signal that there are no more real visited sites.
57 59
58 titles->push_back(visited.title); 60 titles->push_back(visited.title);
59 urls->push_back(visited.url.spec()); 61 urls->push_back(visited.url.spec());
60 } 62 }
61 } 63 }
62 64
63 void OnMostVisitedURLsAvailable(
64 ScopedJavaGlobalRef<jobject>* j_observer,
65 int num_sites,
66 const history::MostVisitedURLList& visited_list) {
67 std::vector<base::string16> titles;
68 std::vector<std::string> urls;
69 ExtractMostVisitedTitlesAndURLs(visited_list, &titles, &urls, num_sites);
70
71 JNIEnv* env = AttachCurrentThread();
72 Java_MostVisitedURLsObserver_onMostVisitedURLsAvailable(
73 env,
74 j_observer->obj(),
75 ToJavaArrayOfStrings(env, titles).obj(),
76 ToJavaArrayOfStrings(env, urls).obj());
77 }
78
79 SkBitmap ExtractThumbnail(const base::RefCountedMemory& image_data) { 65 SkBitmap ExtractThumbnail(const base::RefCountedMemory& image_data) {
80 scoped_ptr<SkBitmap> image(gfx::JPEGCodec::Decode( 66 scoped_ptr<SkBitmap> image(gfx::JPEGCodec::Decode(
81 image_data.front(), 67 image_data.front(),
82 image_data.size())); 68 image_data.size()));
83 return image.get() ? *image : SkBitmap(); 69 return image.get() ? *image : SkBitmap();
84 } 70 }
85 71
86 void OnObtainedThumbnail( 72 void OnObtainedThumbnail(
87 ScopedJavaGlobalRef<jobject>* bitmap, 73 ScopedJavaGlobalRef<jobject>* bitmap,
88 ScopedJavaGlobalRef<jobject>* j_callback) { 74 ScopedJavaGlobalRef<jobject>* j_callback) {
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 142
157 void MostVisitedSites::SetMostVisitedURLsObserver(JNIEnv* env, 143 void MostVisitedSites::SetMostVisitedURLsObserver(JNIEnv* env,
158 jobject obj, 144 jobject obj,
159 jobject j_observer, 145 jobject j_observer,
160 jint num_sites) { 146 jint num_sites) {
161 observer_.Reset(env, j_observer); 147 observer_.Reset(env, j_observer);
162 num_sites_ = num_sites; 148 num_sites_ = num_sites;
163 149
164 QueryMostVisitedURLs(); 150 QueryMostVisitedURLs();
165 151
166 history::TopSites* top_sites = profile_->GetTopSites(); 152 history::TopSites* top_sites = profile_->GetTopSites();
Ted C 2014/05/27 23:59:09 should we not be doing this block if we are using
manzagop (departed) 2014/05/29 21:40:31 The TopSites notification actually does get used i
Ted C 2014/05/30 16:37:13 Ah, I missed the InitiateTopSitesQuery call in the
manzagop (departed) 2014/06/02 13:18:56 Acknowledged.
167 if (top_sites) { 153 if (top_sites) {
168 // TopSites updates itself after a delay. To ensure up-to-date results, 154 // TopSites updates itself after a delay. To ensure up-to-date results,
169 // force an update now. 155 // force an update now.
170 top_sites->SyncWithHistory(); 156 top_sites->SyncWithHistory();
171 157
172 // Register for notification when TopSites changes so that we can update 158 // Register for notification when TopSites changes so that we can update
173 // ourself. 159 // ourself.
174 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED, 160 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED,
175 content::Source<history::TopSites>(top_sites)); 161 content::Source<history::TopSites>(top_sites));
176 } 162 }
(...skipping 13 matching lines...) Expand all
190 BrowserThread::PostTask( 176 BrowserThread::PostTask(
191 BrowserThread::DB, FROM_HERE, base::Bind( 177 BrowserThread::DB, FROM_HERE, base::Bind(
192 &GetUrlThumbnailTask, 178 &GetUrlThumbnailTask,
193 url_string, 179 url_string,
194 top_sites, base::Owned(j_callback))); 180 top_sites, base::Owned(j_callback)));
195 } 181 }
196 182
197 void MostVisitedSites::BlacklistUrl(JNIEnv* env, 183 void MostVisitedSites::BlacklistUrl(JNIEnv* env,
198 jobject obj, 184 jobject obj,
199 jstring j_url) { 185 jstring j_url) {
200 TopSites* top_sites = profile_->GetTopSites(); 186 std::string url = ConvertJavaStringToUTF8(env, j_url);
201 if (!top_sites)
202 return;
203 187
204 std::string url_string = ConvertJavaStringToUTF8(env, j_url); 188 switch (mv_source_) {
205 top_sites->AddBlacklistedURL(GURL(url_string)); 189 case TOP_SITES: {
190 TopSites* top_sites = profile_->GetTopSites();
191 if (top_sites) {
Ted C 2014/05/27 23:59:09 braces aren't needed...and should we be dchecking
manzagop (departed) 2014/05/29 21:40:31 Removed braces. (Btw is it frowned upon in chromiu
Ted C 2014/05/30 16:37:13 It's more about consistency. I think most places
manzagop (departed) 2014/06/02 13:18:56 Got it.
192 top_sites->AddBlacklistedURL(GURL(url));
193 }
194 break;
195 }
196
197 case SUGGESTIONS_SERVICE: {
198 SuggestionsService* suggestions_service =
199 SuggestionsServiceFactory::GetForProfile(profile_);
200 if (suggestions_service) {
201 suggestions_service->AddBlacklistedURL(
202 GURL(url),
203 base::Bind(
204 &MostVisitedSites::OnSuggestionsProfileAvailable,
Ted C 2014/05/27 23:59:09 +2 indent on these lines
manzagop (departed) 2014/05/29 21:40:31 Done.
205 weak_ptr_factory_.GetWeakPtr(),
206 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_))));
207 }
208 break;
209 }
210 }
206 } 211 }
207 212
208 void MostVisitedSites::Observe(int type, 213 void MostVisitedSites::Observe(int type,
209 const content::NotificationSource& source, 214 const content::NotificationSource& source,
210 const content::NotificationDetails& details) { 215 const content::NotificationDetails& details) {
211 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); 216 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED);
212 217
213 // Most visited urls changed, query again. 218 if (mv_source_ == TOP_SITES) {
214 QueryMostVisitedURLs(); 219 // The displayed suggestions are invalidated.
220 QueryMostVisitedURLs();
221 }
215 } 222 }
216 223
217 // static 224 // static
218 bool MostVisitedSites::Register(JNIEnv* env) { 225 bool MostVisitedSites::Register(JNIEnv* env) {
219 return RegisterNativesImpl(env); 226 return RegisterNativesImpl(env);
220 } 227 }
221 228
222 void MostVisitedSites::QueryMostVisitedURLs() { 229 void MostVisitedSites::QueryMostVisitedURLs() {
223 SuggestionsServiceFactory* suggestions_service_factory =
224 SuggestionsServiceFactory::GetInstance();
225 SuggestionsService* suggestions_service = 230 SuggestionsService* suggestions_service =
226 suggestions_service_factory->GetForProfile(profile_); 231 SuggestionsServiceFactory::GetForProfile(profile_);
Ted C 2014/05/27 23:59:09 The data source seems to be entirely determined of
manzagop (departed) 2014/05/29 21:40:31 The source may change in case of fallback (eg beco
227 if (suggestions_service) { 232 if (suggestions_service) {
228 // Suggestions service is enabled, initiate a query. 233 // Suggestions service is enabled, initiate a query.
229 suggestions_service->FetchSuggestionsData( 234 suggestions_service->FetchSuggestionsData(
230 base::Bind( 235 base::Bind(
231 &MostVisitedSites::OnSuggestionsProfileAvailable, 236 &MostVisitedSites::OnSuggestionsProfileAvailable,
232 weak_ptr_factory_.GetWeakPtr(), 237 weak_ptr_factory_.GetWeakPtr(),
233 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)))); 238 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_))));
234 } else { 239 } else {
235 InitiateTopSitesQuery(); 240 InitiateTopSitesQuery();
236 } 241 }
237 } 242 }
238 243
239 void MostVisitedSites::InitiateTopSitesQuery() { 244 void MostVisitedSites::InitiateTopSitesQuery() {
240 TopSites* top_sites = profile_->GetTopSites(); 245 TopSites* top_sites = profile_->GetTopSites();
241 if (!top_sites) 246 if (!top_sites)
242 return; 247 return;
243 248
244 top_sites->GetMostVisitedURLs( 249 top_sites->GetMostVisitedURLs(
245 base::Bind( 250 base::Bind(
246 &OnMostVisitedURLsAvailable, 251 &MostVisitedSites::OnMostVisitedURLsAvailable,
252 weak_ptr_factory_.GetWeakPtr(),
247 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)), 253 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)),
248 num_sites_), 254 num_sites_),
249 false); 255 false);
250 } 256 }
251 257
258 void MostVisitedSites::OnMostVisitedURLsAvailable(
259 ScopedJavaGlobalRef<jobject>* j_observer,
260 int num_sites,
261 const history::MostVisitedURLList& visited_list) {
262 std::vector<base::string16> titles;
263 std::vector<std::string> urls;
264 ExtractMostVisitedTitlesAndURLs(visited_list, &titles, &urls, num_sites);
265
266 mv_source_ = TOP_SITES;
Ted C 2014/05/27 23:59:09 if you set the source in the constructor, then thi
manzagop (departed) 2014/05/29 21:40:31 As mentioned above, this is because of the fallbac
267
268 JNIEnv* env = AttachCurrentThread();
269 Java_MostVisitedURLsObserver_onMostVisitedURLsAvailable(
270 env,
271 j_observer->obj(),
272 ToJavaArrayOfStrings(env, titles).obj(),
273 ToJavaArrayOfStrings(env, urls).obj());
274 }
275
252 void MostVisitedSites::OnSuggestionsProfileAvailable( 276 void MostVisitedSites::OnSuggestionsProfileAvailable(
253 ScopedJavaGlobalRef<jobject>* j_observer, 277 ScopedJavaGlobalRef<jobject>* j_observer,
254 const SuggestionsProfile& suggestions_profile) { 278 const SuggestionsProfile& suggestions_profile) {
255 size_t size = suggestions_profile.suggestions_size(); 279 size_t size = suggestions_profile.suggestions_size();
256 if (size == 0) { 280 if (size == 0) {
257 // No suggestions data available, initiate Top Sites query. 281 // No suggestions data available, initiate Top Sites query.
258 InitiateTopSitesQuery(); 282 InitiateTopSitesQuery();
259 return; 283 return;
260 } 284 }
261 285
262 std::vector<base::string16> titles; 286 std::vector<base::string16> titles;
263 std::vector<std::string> urls; 287 std::vector<std::string> urls;
264 for (size_t i = 0; i < size; ++i) { 288 for (size_t i = 0; i < size; ++i) {
265 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i); 289 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i);
266 titles.push_back(base::UTF8ToUTF16(suggestion.title())); 290 titles.push_back(base::UTF8ToUTF16(suggestion.title()));
267 urls.push_back(suggestion.url()); 291 urls.push_back(suggestion.url());
268 } 292 }
269 293
294 mv_source_ = SUGGESTIONS_SERVICE;
295
270 JNIEnv* env = AttachCurrentThread(); 296 JNIEnv* env = AttachCurrentThread();
271 Java_MostVisitedURLsObserver_onMostVisitedURLsAvailable( 297 Java_MostVisitedURLsObserver_onMostVisitedURLsAvailable(
272 env, 298 env,
273 j_observer->obj(), 299 j_observer->obj(),
274 ToJavaArrayOfStrings(env, titles).obj(), 300 ToJavaArrayOfStrings(env, titles).obj(),
275 ToJavaArrayOfStrings(env, urls).obj()); 301 ToJavaArrayOfStrings(env, urls).obj());
276 } 302 }
277 303
278 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { 304 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) {
279 MostVisitedSites* most_visited_sites = 305 MostVisitedSites* most_visited_sites =
280 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); 306 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile));
281 return reinterpret_cast<intptr_t>(most_visited_sites); 307 return reinterpret_cast<intptr_t>(most_visited_sites);
282 } 308 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698