OLD | NEW |
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> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
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/android/scoped_java_ref.h" | 13 #include "base/android/scoped_java_ref.h" |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
16 #include "base/metrics/sparse_histogram.h" | 16 #include "base/metrics/sparse_histogram.h" |
17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
20 #include "base/time/time.h" | 20 #include "base/time/time.h" |
21 #include "chrome/browser/history/top_sites.h" | 21 #include "chrome/browser/history/top_sites.h" |
| 22 #include "chrome/browser/history/top_sites_factory.h" |
22 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
23 #include "chrome/browser/profiles/profile_android.h" | 24 #include "chrome/browser/profiles/profile_android.h" |
24 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | 25 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
25 #include "chrome/browser/search/suggestions/suggestions_source.h" | 26 #include "chrome/browser/search/suggestions/suggestions_source.h" |
26 #include "chrome/browser/sync/profile_sync_service.h" | 27 #include "chrome/browser/sync/profile_sync_service.h" |
27 #include "chrome/browser/sync/profile_sync_service_factory.h" | 28 #include "chrome/browser/sync/profile_sync_service_factory.h" |
28 #include "chrome/browser/thumbnails/thumbnail_list_source.h" | 29 #include "chrome/browser/thumbnails/thumbnail_list_source.h" |
29 #include "components/suggestions/suggestions_service.h" | 30 #include "components/suggestions/suggestions_service.h" |
30 #include "components/suggestions/suggestions_utils.h" | 31 #include "components/suggestions/suggestions_utils.h" |
31 #include "content/public/browser/browser_thread.h" | 32 #include "content/public/browser/browser_thread.h" |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 | 224 |
224 void MostVisitedSites::SetMostVisitedURLsObserver(JNIEnv* env, | 225 void MostVisitedSites::SetMostVisitedURLsObserver(JNIEnv* env, |
225 jobject obj, | 226 jobject obj, |
226 jobject j_observer, | 227 jobject j_observer, |
227 jint num_sites) { | 228 jint num_sites) { |
228 observer_.Reset(env, j_observer); | 229 observer_.Reset(env, j_observer); |
229 num_sites_ = num_sites; | 230 num_sites_ = num_sites; |
230 | 231 |
231 QueryMostVisitedURLs(); | 232 QueryMostVisitedURLs(); |
232 | 233 |
233 history::TopSites* top_sites = profile_->GetTopSites(); | 234 scoped_refptr<history::TopSites> top_sites = |
| 235 TopSitesFactory::GetForProfile(profile_); |
234 if (top_sites) { | 236 if (top_sites) { |
235 // TopSites updates itself after a delay. To ensure up-to-date results, | 237 // TopSites updates itself after a delay. To ensure up-to-date results, |
236 // force an update now. | 238 // force an update now. |
237 top_sites->SyncWithHistory(); | 239 top_sites->SyncWithHistory(); |
238 | 240 |
239 // Register as TopSitesObserver so that we can update ourselves when the | 241 // Register as TopSitesObserver so that we can update ourselves when the |
240 // TopSites changes. | 242 // TopSites changes. |
241 scoped_observer_.Add(top_sites); | 243 scoped_observer_.Add(top_sites.get()); |
242 } | 244 } |
243 } | 245 } |
244 | 246 |
245 // Called from the UI Thread. | 247 // Called from the UI Thread. |
246 void MostVisitedSites::GetURLThumbnail(JNIEnv* env, | 248 void MostVisitedSites::GetURLThumbnail(JNIEnv* env, |
247 jobject obj, | 249 jobject obj, |
248 jstring url, | 250 jstring url, |
249 jobject j_callback_obj) { | 251 jobject j_callback_obj) { |
250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 252 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
251 ScopedJavaGlobalRef<jobject>* j_callback = | 253 ScopedJavaGlobalRef<jobject>* j_callback = |
252 new ScopedJavaGlobalRef<jobject>(); | 254 new ScopedJavaGlobalRef<jobject>(); |
253 j_callback->Reset(env, j_callback_obj); | 255 j_callback->Reset(env, j_callback_obj); |
254 | 256 |
255 std::string url_string = ConvertJavaStringToUTF8(env, url); | 257 std::string url_string = ConvertJavaStringToUTF8(env, url); |
256 scoped_refptr<TopSites> top_sites(profile_->GetTopSites()); | 258 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); |
257 | 259 |
258 // If the Suggestions service is enabled and in use, create a callback to | 260 // If the Suggestions service is enabled and in use, create a callback to |
259 // fetch a server thumbnail from it, in case the local thumbnail is not found. | 261 // fetch a server thumbnail from it, in case the local thumbnail is not found. |
260 SuggestionsService* suggestions_service = | 262 SuggestionsService* suggestions_service = |
261 SuggestionsServiceFactory::GetForProfile(profile_); | 263 SuggestionsServiceFactory::GetForProfile(profile_); |
262 bool use_suggestions_service = suggestions_service && | 264 bool use_suggestions_service = suggestions_service && |
263 mv_source_ == SUGGESTIONS_SERVICE; | 265 mv_source_ == SUGGESTIONS_SERVICE; |
264 base::Closure lookup_failed_callback = use_suggestions_service ? | 266 base::Closure lookup_failed_callback = use_suggestions_service ? |
265 base::Bind(&MostVisitedSites::GetSuggestionsThumbnailOnUIThread, | 267 base::Bind(&MostVisitedSites::GetSuggestionsThumbnailOnUIThread, |
266 weak_ptr_factory_.GetWeakPtr(), | 268 weak_ptr_factory_.GetWeakPtr(), |
(...skipping 12 matching lines...) Expand all Loading... |
279 lookup_failed_callback)); | 281 lookup_failed_callback)); |
280 } | 282 } |
281 | 283 |
282 void MostVisitedSites::BlacklistUrl(JNIEnv* env, | 284 void MostVisitedSites::BlacklistUrl(JNIEnv* env, |
283 jobject obj, | 285 jobject obj, |
284 jstring j_url) { | 286 jstring j_url) { |
285 std::string url = ConvertJavaStringToUTF8(env, j_url); | 287 std::string url = ConvertJavaStringToUTF8(env, j_url); |
286 | 288 |
287 switch (mv_source_) { | 289 switch (mv_source_) { |
288 case TOP_SITES: { | 290 case TOP_SITES: { |
289 TopSites* top_sites = profile_->GetTopSites(); | 291 scoped_refptr<TopSites> top_sites = |
| 292 TopSitesFactory::GetForProfile(profile_); |
290 DCHECK(top_sites); | 293 DCHECK(top_sites); |
291 top_sites->AddBlacklistedURL(GURL(url)); | 294 top_sites->AddBlacklistedURL(GURL(url)); |
292 break; | 295 break; |
293 } | 296 } |
294 | 297 |
295 case SUGGESTIONS_SERVICE: { | 298 case SUGGESTIONS_SERVICE: { |
296 SuggestionsService* suggestions_service = | 299 SuggestionsService* suggestions_service = |
297 SuggestionsServiceFactory::GetForProfile(profile_); | 300 SuggestionsServiceFactory::GetForProfile(profile_); |
298 DCHECK(suggestions_service); | 301 DCHECK(suggestions_service); |
299 suggestions_service->BlacklistURL( | 302 suggestions_service->BlacklistURL( |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 base::Bind( | 359 base::Bind( |
357 &MostVisitedSites::OnSuggestionsProfileAvailable, | 360 &MostVisitedSites::OnSuggestionsProfileAvailable, |
358 weak_ptr_factory_.GetWeakPtr(), | 361 weak_ptr_factory_.GetWeakPtr(), |
359 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)))); | 362 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)))); |
360 } else { | 363 } else { |
361 InitiateTopSitesQuery(); | 364 InitiateTopSitesQuery(); |
362 } | 365 } |
363 } | 366 } |
364 | 367 |
365 void MostVisitedSites::InitiateTopSitesQuery() { | 368 void MostVisitedSites::InitiateTopSitesQuery() { |
366 TopSites* top_sites = profile_->GetTopSites(); | 369 scoped_refptr<TopSites> top_sites = TopSitesFactory::GetForProfile(profile_); |
367 if (!top_sites) | 370 if (!top_sites) |
368 return; | 371 return; |
369 | 372 |
370 top_sites->GetMostVisitedURLs( | 373 top_sites->GetMostVisitedURLs( |
371 base::Bind( | 374 base::Bind( |
372 &MostVisitedSites::OnMostVisitedURLsAvailable, | 375 &MostVisitedSites::OnMostVisitedURLsAvailable, |
373 weak_ptr_factory_.GetWeakPtr(), | 376 weak_ptr_factory_.GetWeakPtr(), |
374 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)), | 377 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)), |
375 num_sites_), | 378 num_sites_), |
376 false); | 379 false); |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 // The displayed suggestions are invalidated. | 526 // The displayed suggestions are invalidated. |
524 QueryMostVisitedURLs(); | 527 QueryMostVisitedURLs(); |
525 } | 528 } |
526 } | 529 } |
527 | 530 |
528 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { | 531 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { |
529 MostVisitedSites* most_visited_sites = | 532 MostVisitedSites* most_visited_sites = |
530 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); | 533 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); |
531 return reinterpret_cast<intptr_t>(most_visited_sites); | 534 return reinterpret_cast<intptr_t>(most_visited_sites); |
532 } | 535 } |
OLD | NEW |