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/chrome_notification_types.h" | 21 #include "chrome/browser/chrome_notification_types.h" |
22 #include "chrome/browser/history/top_sites.h" | 22 #include "chrome/browser/history/top_sites_provider.h" |
| 23 #include "chrome/browser/history/top_sites_service_factory.h" |
23 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
24 #include "chrome/browser/profiles/profile_android.h" | 25 #include "chrome/browser/profiles/profile_android.h" |
25 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | 26 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
26 #include "chrome/browser/search/suggestions/suggestions_source.h" | 27 #include "chrome/browser/search/suggestions/suggestions_source.h" |
27 #include "chrome/browser/sync/profile_sync_service.h" | 28 #include "chrome/browser/sync/profile_sync_service.h" |
28 #include "chrome/browser/sync/profile_sync_service_factory.h" | 29 #include "chrome/browser/sync/profile_sync_service_factory.h" |
29 #include "chrome/browser/thumbnails/thumbnail_list_source.h" | 30 #include "chrome/browser/thumbnails/thumbnail_list_source.h" |
30 #include "components/suggestions/suggestions_service.h" | 31 #include "components/suggestions/suggestions_service.h" |
31 #include "components/suggestions/suggestions_utils.h" | 32 #include "components/suggestions/suggestions_utils.h" |
32 #include "content/public/browser/browser_thread.h" | 33 #include "content/public/browser/browser_thread.h" |
33 #include "content/public/browser/notification_source.h" | 34 #include "content/public/browser/notification_source.h" |
34 #include "content/public/browser/url_data_source.h" | 35 #include "content/public/browser/url_data_source.h" |
35 #include "jni/MostVisitedSites_jni.h" | 36 #include "jni/MostVisitedSites_jni.h" |
36 #include "third_party/skia/include/core/SkBitmap.h" | 37 #include "third_party/skia/include/core/SkBitmap.h" |
37 #include "ui/gfx/android/java_bitmap.h" | 38 #include "ui/gfx/android/java_bitmap.h" |
38 #include "ui/gfx/codec/jpeg_codec.h" | 39 #include "ui/gfx/codec/jpeg_codec.h" |
39 | 40 |
40 using base::android::AttachCurrentThread; | 41 using base::android::AttachCurrentThread; |
41 using base::android::ConvertUTF8ToJavaString; | 42 using base::android::ConvertUTF8ToJavaString; |
42 using base::android::ConvertJavaStringToUTF8; | 43 using base::android::ConvertJavaStringToUTF8; |
43 using base::android::ScopedJavaGlobalRef; | 44 using base::android::ScopedJavaGlobalRef; |
44 using base::android::ToJavaArrayOfStrings; | 45 using base::android::ToJavaArrayOfStrings; |
45 using base::android::CheckException; | 46 using base::android::CheckException; |
46 using content::BrowserThread; | 47 using content::BrowserThread; |
47 using history::TopSites; | 48 using history::TopSitesProvider; |
48 using suggestions::ChromeSuggestion; | 49 using suggestions::ChromeSuggestion; |
49 using suggestions::SuggestionsProfile; | 50 using suggestions::SuggestionsProfile; |
50 using suggestions::SuggestionsService; | 51 using suggestions::SuggestionsService; |
51 using suggestions::SuggestionsServiceFactory; | 52 using suggestions::SuggestionsServiceFactory; |
52 using suggestions::SyncState; | 53 using suggestions::SyncState; |
53 | 54 |
54 namespace { | 55 namespace { |
55 | 56 |
56 // Total number of tiles displayed. | 57 // Total number of tiles displayed. |
57 const char kNumTilesHistogramName[] = "NewTabPage.NumberOfTiles"; | 58 const char kNumTilesHistogramName[] = "NewTabPage.NumberOfTiles"; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 } | 100 } |
100 } | 101 } |
101 | 102 |
102 SkBitmap ExtractThumbnail(const base::RefCountedMemory& image_data) { | 103 SkBitmap ExtractThumbnail(const base::RefCountedMemory& image_data) { |
103 scoped_ptr<SkBitmap> image(gfx::JPEGCodec::Decode( | 104 scoped_ptr<SkBitmap> image(gfx::JPEGCodec::Decode( |
104 image_data.front(), | 105 image_data.front(), |
105 image_data.size())); | 106 image_data.size())); |
106 return image.get() ? *image : SkBitmap(); | 107 return image.get() ? *image : SkBitmap(); |
107 } | 108 } |
108 | 109 |
109 void AddForcedURLOnUIThread(scoped_refptr<history::TopSites> top_sites, | 110 void AddForcedURLOnUIThread( |
110 const GURL& url) { | 111 scoped_refptr<history::TopSitesProvider> top_sites_provider, |
| 112 const GURL& url) { |
111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
112 top_sites->AddForcedURL(url, base::Time::Now()); | 114 top_sites_provider->AddForcedURL(url, base::Time::Now()); |
113 } | 115 } |
114 | 116 |
115 // Runs on the DB thread. | 117 // Runs on the DB thread. |
116 void GetUrlThumbnailTask( | 118 void GetUrlThumbnailTask( |
117 std::string url_string, | 119 std::string url_string, |
118 scoped_refptr<TopSites> top_sites, | 120 scoped_refptr<TopSitesProvider> top_sites_provider, |
119 ScopedJavaGlobalRef<jobject>* j_callback, | 121 ScopedJavaGlobalRef<jobject>* j_callback, |
120 MostVisitedSites::LookupSuccessCallback lookup_success_ui_callback, | 122 MostVisitedSites::LookupSuccessCallback lookup_success_ui_callback, |
121 base::Closure lookup_failed_ui_callback) { | 123 base::Closure lookup_failed_ui_callback) { |
122 JNIEnv* env = AttachCurrentThread(); | 124 JNIEnv* env = AttachCurrentThread(); |
123 | 125 |
124 ScopedJavaGlobalRef<jobject>* j_bitmap_ref = | 126 ScopedJavaGlobalRef<jobject>* j_bitmap_ref = |
125 new ScopedJavaGlobalRef<jobject>(); | 127 new ScopedJavaGlobalRef<jobject>(); |
126 | 128 |
127 GURL gurl(url_string); | 129 GURL gurl(url_string); |
128 | 130 |
129 scoped_refptr<base::RefCountedMemory> data; | 131 scoped_refptr<base::RefCountedMemory> data; |
130 if (top_sites->GetPageThumbnail(gurl, false, &data)) { | 132 if (top_sites_provider->GetPageThumbnail(gurl, false, &data)) { |
131 SkBitmap thumbnail_bitmap = ExtractThumbnail(*data.get()); | 133 SkBitmap thumbnail_bitmap = ExtractThumbnail(*data.get()); |
132 if (!thumbnail_bitmap.empty()) { | 134 if (!thumbnail_bitmap.empty()) { |
133 j_bitmap_ref->Reset( | 135 j_bitmap_ref->Reset( |
134 env, | 136 env, |
135 gfx::ConvertToJavaBitmap(&thumbnail_bitmap).obj()); | 137 gfx::ConvertToJavaBitmap(&thumbnail_bitmap).obj()); |
136 } | 138 } |
137 } else { | 139 } else { |
138 // A thumbnail is not locally available for |gurl|. Make sure it is put in | 140 // A thumbnail is not locally available for |gurl|. Make sure it is put in |
139 // the list to be fetched at the next visit to this site. | 141 // the list to be fetched at the next visit to this site. |
140 BrowserThread::PostTask( | 142 BrowserThread::PostTask( |
141 BrowserThread::UI, FROM_HERE, | 143 BrowserThread::UI, FROM_HERE, |
142 base::Bind(AddForcedURLOnUIThread, top_sites, gurl)); | 144 base::Bind(AddForcedURLOnUIThread, top_sites_provider, gurl)); |
143 | 145 |
144 // If appropriate, return on the UI thread to execute the proper callback. | 146 // If appropriate, return on the UI thread to execute the proper callback. |
145 if (!lookup_failed_ui_callback.is_null()) { | 147 if (!lookup_failed_ui_callback.is_null()) { |
146 BrowserThread::PostTask( | 148 BrowserThread::PostTask( |
147 BrowserThread::UI, FROM_HERE, lookup_failed_ui_callback); | 149 BrowserThread::UI, FROM_HERE, lookup_failed_ui_callback); |
148 delete j_bitmap_ref; | 150 delete j_bitmap_ref; |
149 return; | 151 return; |
150 } | 152 } |
151 } | 153 } |
152 | 154 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 | 227 |
226 void MostVisitedSites::SetMostVisitedURLsObserver(JNIEnv* env, | 228 void MostVisitedSites::SetMostVisitedURLsObserver(JNIEnv* env, |
227 jobject obj, | 229 jobject obj, |
228 jobject j_observer, | 230 jobject j_observer, |
229 jint num_sites) { | 231 jint num_sites) { |
230 observer_.Reset(env, j_observer); | 232 observer_.Reset(env, j_observer); |
231 num_sites_ = num_sites; | 233 num_sites_ = num_sites; |
232 | 234 |
233 QueryMostVisitedURLs(); | 235 QueryMostVisitedURLs(); |
234 | 236 |
235 history::TopSites* top_sites = profile_->GetTopSites(); | 237 history::TopSitesProvider* top_sites_provider = |
236 if (top_sites) { | 238 TopSitesServiceFactory::GetForProfile(profile_); |
| 239 if (top_sites_provider) { |
237 // TopSites updates itself after a delay. To ensure up-to-date results, | 240 // TopSites updates itself after a delay. To ensure up-to-date results, |
238 // force an update now. | 241 // force an update now. |
239 top_sites->SyncWithHistory(); | 242 top_sites_provider->SyncWithHistory(); |
240 | 243 |
241 // Register for notification when TopSites changes so that we can update | 244 // Register for notification when TopSites changes so that we can update |
242 // ourself. | 245 // ourself. |
243 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED, | 246 registrar_.Add( |
244 content::Source<history::TopSites>(top_sites)); | 247 this, chrome::NOTIFICATION_TOP_SITES_CHANGED, |
| 248 content::Source<history::TopSitesProvider>(top_sites_provider)); |
245 } | 249 } |
246 } | 250 } |
247 | 251 |
248 // Called from the UI Thread. | 252 // Called from the UI Thread. |
249 void MostVisitedSites::GetURLThumbnail(JNIEnv* env, | 253 void MostVisitedSites::GetURLThumbnail(JNIEnv* env, |
250 jobject obj, | 254 jobject obj, |
251 jstring url, | 255 jstring url, |
252 jobject j_callback_obj) { | 256 jobject j_callback_obj) { |
253 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 257 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
254 ScopedJavaGlobalRef<jobject>* j_callback = | 258 ScopedJavaGlobalRef<jobject>* j_callback = |
255 new ScopedJavaGlobalRef<jobject>(); | 259 new ScopedJavaGlobalRef<jobject>(); |
256 j_callback->Reset(env, j_callback_obj); | 260 j_callback->Reset(env, j_callback_obj); |
257 | 261 |
258 std::string url_string = ConvertJavaStringToUTF8(env, url); | 262 std::string url_string = ConvertJavaStringToUTF8(env, url); |
259 scoped_refptr<TopSites> top_sites(profile_->GetTopSites()); | 263 scoped_refptr<TopSitesProvider> top_sites_provider( |
| 264 TopSitesServiceFactory::GetForProfile(profile_)); |
260 | 265 |
261 // If the Suggestions service is enabled and in use, create a callback to | 266 // If the Suggestions service is enabled and in use, create a callback to |
262 // fetch a server thumbnail from it, in case the local thumbnail is not found. | 267 // fetch a server thumbnail from it, in case the local thumbnail is not found. |
263 SuggestionsService* suggestions_service = | 268 SuggestionsService* suggestions_service = |
264 SuggestionsServiceFactory::GetForProfile(profile_); | 269 SuggestionsServiceFactory::GetForProfile(profile_); |
265 bool use_suggestions_service = suggestions_service && | 270 bool use_suggestions_service = suggestions_service && |
266 mv_source_ == SUGGESTIONS_SERVICE; | 271 mv_source_ == SUGGESTIONS_SERVICE; |
267 base::Closure lookup_failed_callback = use_suggestions_service ? | 272 base::Closure lookup_failed_callback = use_suggestions_service ? |
268 base::Bind(&MostVisitedSites::GetSuggestionsThumbnailOnUIThread, | 273 base::Bind(&MostVisitedSites::GetSuggestionsThumbnailOnUIThread, |
269 weak_ptr_factory_.GetWeakPtr(), | 274 weak_ptr_factory_.GetWeakPtr(), |
270 suggestions_service, url_string, | 275 suggestions_service, url_string, |
271 base::Owned(new ScopedJavaGlobalRef<jobject>(*j_callback))) : | 276 base::Owned(new ScopedJavaGlobalRef<jobject>(*j_callback))) : |
272 base::Closure(); | 277 base::Closure(); |
273 LookupSuccessCallback lookup_success_callback = | 278 LookupSuccessCallback lookup_success_callback = |
274 base::Bind(&MostVisitedSites::OnObtainedThumbnail, | 279 base::Bind(&MostVisitedSites::OnObtainedThumbnail, |
275 weak_ptr_factory_.GetWeakPtr()); | 280 weak_ptr_factory_.GetWeakPtr()); |
276 | 281 |
277 BrowserThread::PostTask( | 282 BrowserThread::PostTask( |
278 BrowserThread::DB, FROM_HERE, | 283 BrowserThread::DB, FROM_HERE, |
279 base::Bind( | 284 base::Bind(&GetUrlThumbnailTask, url_string, top_sites_provider, |
280 &GetUrlThumbnailTask, url_string, top_sites, | 285 base::Owned(j_callback), lookup_success_callback, |
281 base::Owned(j_callback), lookup_success_callback, | 286 lookup_failed_callback)); |
282 lookup_failed_callback)); | |
283 } | 287 } |
284 | 288 |
285 void MostVisitedSites::BlacklistUrl(JNIEnv* env, | 289 void MostVisitedSites::BlacklistUrl(JNIEnv* env, |
286 jobject obj, | 290 jobject obj, |
287 jstring j_url) { | 291 jstring j_url) { |
288 std::string url = ConvertJavaStringToUTF8(env, j_url); | 292 std::string url = ConvertJavaStringToUTF8(env, j_url); |
289 | 293 |
290 switch (mv_source_) { | 294 switch (mv_source_) { |
291 case TOP_SITES: { | 295 case TOP_SITES: { |
292 TopSites* top_sites = profile_->GetTopSites(); | 296 TopSitesProvider* top_sites_provider = |
293 DCHECK(top_sites); | 297 TopSitesServiceFactory::GetForProfile(profile_); |
294 top_sites->AddBlacklistedURL(GURL(url)); | 298 DCHECK(top_sites_provider); |
| 299 top_sites_provider->AddBlacklistedURL(GURL(url)); |
295 break; | 300 break; |
296 } | 301 } |
297 | 302 |
298 case SUGGESTIONS_SERVICE: { | 303 case SUGGESTIONS_SERVICE: { |
299 SuggestionsService* suggestions_service = | 304 SuggestionsService* suggestions_service = |
300 SuggestionsServiceFactory::GetForProfile(profile_); | 305 SuggestionsServiceFactory::GetForProfile(profile_); |
301 DCHECK(suggestions_service); | 306 DCHECK(suggestions_service); |
302 suggestions_service->BlacklistURL( | 307 suggestions_service->BlacklistURL( |
303 GURL(url), | 308 GURL(url), |
304 base::Bind( | 309 base::Bind( |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 base::Bind( | 375 base::Bind( |
371 &MostVisitedSites::OnSuggestionsProfileAvailable, | 376 &MostVisitedSites::OnSuggestionsProfileAvailable, |
372 weak_ptr_factory_.GetWeakPtr(), | 377 weak_ptr_factory_.GetWeakPtr(), |
373 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)))); | 378 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)))); |
374 } else { | 379 } else { |
375 InitiateTopSitesQuery(); | 380 InitiateTopSitesQuery(); |
376 } | 381 } |
377 } | 382 } |
378 | 383 |
379 void MostVisitedSites::InitiateTopSitesQuery() { | 384 void MostVisitedSites::InitiateTopSitesQuery() { |
380 TopSites* top_sites = profile_->GetTopSites(); | 385 TopSitesProvider* top_sites_provider = |
381 if (!top_sites) | 386 TopSitesServiceFactory::GetForProfile(profile_); |
| 387 if (!top_sites_provider) |
382 return; | 388 return; |
383 | 389 |
384 top_sites->GetMostVisitedURLs( | 390 top_sites_provider->GetMostVisitedURLs( |
385 base::Bind( | 391 base::Bind(&MostVisitedSites::OnMostVisitedURLsAvailable, |
386 &MostVisitedSites::OnMostVisitedURLsAvailable, | 392 weak_ptr_factory_.GetWeakPtr(), |
387 weak_ptr_factory_.GetWeakPtr(), | 393 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)), |
388 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)), | 394 num_sites_), |
389 num_sites_), | |
390 false); | 395 false); |
391 } | 396 } |
392 | 397 |
393 void MostVisitedSites::OnMostVisitedURLsAvailable( | 398 void MostVisitedSites::OnMostVisitedURLsAvailable( |
394 ScopedJavaGlobalRef<jobject>* j_observer, | 399 ScopedJavaGlobalRef<jobject>* j_observer, |
395 int num_sites, | 400 int num_sites, |
396 const history::MostVisitedURLList& visited_list) { | 401 const history::MostVisitedURLList& visited_list) { |
397 std::vector<base::string16> titles; | 402 std::vector<base::string16> titles; |
398 std::vector<std::string> urls; | 403 std::vector<std::string> urls; |
399 ExtractMostVisitedTitlesAndURLs(visited_list, &titles, &urls, num_sites); | 404 ExtractMostVisitedTitlesAndURLs(visited_list, &titles, &urls, num_sites); |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 num_empty_thumbs_ = 0; | 532 num_empty_thumbs_ = 0; |
528 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumServerTilesHistogramName, num_server_thumbs_); | 533 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumServerTilesHistogramName, num_server_thumbs_); |
529 num_server_thumbs_ = 0; | 534 num_server_thumbs_ = 0; |
530 } | 535 } |
531 | 536 |
532 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { | 537 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { |
533 MostVisitedSites* most_visited_sites = | 538 MostVisitedSites* most_visited_sites = |
534 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); | 539 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); |
535 return reinterpret_cast<intptr_t>(most_visited_sites); | 540 return reinterpret_cast<intptr_t>(most_visited_sites); |
536 } | 541 } |
OLD | NEW |