| 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/ntp/most_visited_sites.h" | 5 #include "chrome/browser/android/ntp/most_visited_sites.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 } | 155 } |
| 156 // The whole grid is already filled with personal suggestions, no point in | 156 // The whole grid is already filled with personal suggestions, no point in |
| 157 // bothering with popular ones. | 157 // bothering with popular ones. |
| 158 return false; | 158 return false; |
| 159 } | 159 } |
| 160 | 160 |
| 161 bool AreURLsEquivalent(const GURL& url1, const GURL& url2) { | 161 bool AreURLsEquivalent(const GURL& url1, const GURL& url2) { |
| 162 return url1.host() == url2.host() && url1.path() == url2.path(); | 162 return url1.host() == url2.host() && url1.path() == url2.path(); |
| 163 } | 163 } |
| 164 | 164 |
| 165 class JavaObserverBridge : public MostVisitedSitesObserver { |
| 166 public: |
| 167 JavaObserverBridge(JNIEnv* env, const JavaParamRef<jobject>& obj) |
| 168 : observer_(env, obj) {} |
| 169 |
| 170 void OnMostVisitedURLsAvailable( |
| 171 const std::vector<base::string16>& titles, |
| 172 const std::vector<std::string>& urls, |
| 173 const std::vector<std::string>& whitelist_icon_paths) override { |
| 174 JNIEnv* env = AttachCurrentThread(); |
| 175 DCHECK_EQ(titles.size(), urls.size()); |
| 176 Java_MostVisitedURLsObserver_onMostVisitedURLsAvailable( |
| 177 env, observer_.obj(), ToJavaArrayOfStrings(env, titles).obj(), |
| 178 ToJavaArrayOfStrings(env, urls).obj(), |
| 179 ToJavaArrayOfStrings(env, whitelist_icon_paths).obj()); |
| 180 } |
| 181 |
| 182 void OnPopularURLsAvailable( |
| 183 const std::vector<std::string>& urls, |
| 184 const std::vector<std::string>& favicon_urls, |
| 185 const std::vector<std::string>& large_icon_urls) override { |
| 186 JNIEnv* env = AttachCurrentThread(); |
| 187 Java_MostVisitedURLsObserver_onPopularURLsAvailable( |
| 188 env, observer_.obj(), ToJavaArrayOfStrings(env, urls).obj(), |
| 189 ToJavaArrayOfStrings(env, favicon_urls).obj(), |
| 190 ToJavaArrayOfStrings(env, large_icon_urls).obj()); |
| 191 } |
| 192 |
| 193 private: |
| 194 ScopedJavaGlobalRef<jobject> observer_; |
| 195 |
| 196 DISALLOW_COPY_AND_ASSIGN(JavaObserverBridge); |
| 197 }; |
| 198 |
| 165 } // namespace | 199 } // namespace |
| 166 | 200 |
| 167 MostVisitedSites::Suggestion::Suggestion() : provider_index(-1) {} | 201 MostVisitedSites::Suggestion::Suggestion() : provider_index(-1) {} |
| 168 | 202 |
| 169 MostVisitedSites::Suggestion::~Suggestion() {} | 203 MostVisitedSites::Suggestion::~Suggestion() {} |
| 170 | 204 |
| 171 std::string MostVisitedSites::Suggestion::GetSourceHistogramName() const { | 205 std::string MostVisitedSites::Suggestion::GetSourceHistogramName() const { |
| 172 switch (source) { | 206 switch (source) { |
| 173 case MostVisitedSites::TOP_SITES: | 207 case MostVisitedSites::TOP_SITES: |
| 174 return kHistogramClientName; | 208 return kHistogramClientName; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 | 242 |
| 209 void MostVisitedSites::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 243 void MostVisitedSites::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 210 delete this; | 244 delete this; |
| 211 } | 245 } |
| 212 | 246 |
| 213 void MostVisitedSites::SetMostVisitedURLsObserver( | 247 void MostVisitedSites::SetMostVisitedURLsObserver( |
| 214 JNIEnv* env, | 248 JNIEnv* env, |
| 215 const JavaParamRef<jobject>& obj, | 249 const JavaParamRef<jobject>& obj, |
| 216 const JavaParamRef<jobject>& j_observer, | 250 const JavaParamRef<jobject>& j_observer, |
| 217 jint num_sites) { | 251 jint num_sites) { |
| 218 observer_.Reset(env, j_observer); | 252 SetMostVisitedURLsObserver( |
| 253 std::unique_ptr<MostVisitedSitesObserver>( |
| 254 new JavaObserverBridge(env, j_observer)), |
| 255 num_sites); |
| 256 } |
| 257 |
| 258 void MostVisitedSites::SetMostVisitedURLsObserver( |
| 259 std::unique_ptr<MostVisitedSitesObserver> observer, int num_sites) { |
| 260 observer_ = std::move(observer); |
| 219 num_sites_ = num_sites; | 261 num_sites_ = num_sites; |
| 220 | 262 |
| 221 if (ShouldShowPopularSites() && | 263 if (ShouldShowPopularSites() && |
| 222 NeedPopularSites(profile_->GetPrefs(), num_sites_)) { | 264 NeedPopularSites(profile_->GetPrefs(), num_sites_)) { |
| 223 popular_sites_.reset(new PopularSites( | 265 popular_sites_.reset(new PopularSites( |
| 224 profile_, | 266 profile_, |
| 225 GetPopularSitesCountry(), | 267 GetPopularSitesCountry(), |
| 226 GetPopularSitesVersion(), | 268 GetPopularSitesVersion(), |
| 227 false, | 269 false, |
| 228 base::Bind(&MostVisitedSites::OnPopularSitesAvailable, | 270 base::Bind(&MostVisitedSites::OnPopularSitesAvailable, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 249 base::Unretained(this))); | 291 base::Unretained(this))); |
| 250 | 292 |
| 251 // Immediately get the current suggestions from the cache. If the cache is | 293 // Immediately get the current suggestions from the cache. If the cache is |
| 252 // empty, this will fall back to TopSites. | 294 // empty, this will fall back to TopSites. |
| 253 OnSuggestionsProfileAvailable( | 295 OnSuggestionsProfileAvailable( |
| 254 suggestions_service->GetSuggestionsDataFromCache()); | 296 suggestions_service->GetSuggestionsDataFromCache()); |
| 255 // Also start a request for fresh suggestions. | 297 // Also start a request for fresh suggestions. |
| 256 suggestions_service->FetchSuggestionsData(); | 298 suggestions_service->FetchSuggestionsData(); |
| 257 } | 299 } |
| 258 | 300 |
| 301 static void CallJavaWithBitmap( |
| 302 std::unique_ptr<ScopedJavaGlobalRef<jobject>> j_callback, |
| 303 bool is_local_thumbnail, |
| 304 const SkBitmap* bitmap); |
| 305 |
| 259 void MostVisitedSites::GetURLThumbnail( | 306 void MostVisitedSites::GetURLThumbnail( |
| 260 JNIEnv* env, | 307 JNIEnv* env, |
| 261 const JavaParamRef<jobject>& obj, | 308 const JavaParamRef<jobject>& obj, |
| 262 const JavaParamRef<jstring>& j_url, | 309 const JavaParamRef<jstring>& j_url, |
| 263 const JavaParamRef<jobject>& j_callback_obj) { | 310 const JavaParamRef<jobject>& j_callback_obj) { |
| 311 std::unique_ptr<ScopedJavaGlobalRef<jobject>> j_callback( |
| 312 new ScopedJavaGlobalRef<jobject>(env, j_callback_obj)); |
| 313 auto callback = base::Bind(&CallJavaWithBitmap, base::Passed(&j_callback)); |
| 314 GURL url(ConvertJavaStringToUTF8(env, j_url)); |
| 315 GetURLThumbnail(url, callback); |
| 316 } |
| 317 |
| 318 void MostVisitedSites::GetURLThumbnail( |
| 319 const GURL& url, |
| 320 const ThumbnailCallback& callback) { |
| 264 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 321 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 265 std::unique_ptr<ScopedJavaGlobalRef<jobject>> j_callback( | |
| 266 new ScopedJavaGlobalRef<jobject>()); | |
| 267 j_callback->Reset(env, j_callback_obj); | |
| 268 | |
| 269 GURL url(ConvertJavaStringToUTF8(env, j_url)); | |
| 270 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); | 322 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); |
| 271 | 323 |
| 272 BrowserThread::PostTaskAndReplyWithResult( | 324 BrowserThread::PostTaskAndReplyWithResult( |
| 273 BrowserThread::DB, FROM_HERE, | 325 BrowserThread::DB, FROM_HERE, |
| 274 base::Bind(&MaybeFetchLocalThumbnail, url, top_sites), | 326 base::Bind(&MaybeFetchLocalThumbnail, url, top_sites), |
| 275 base::Bind(&MostVisitedSites::OnLocalThumbnailFetched, | 327 base::Bind(&MostVisitedSites::OnLocalThumbnailFetched, |
| 276 weak_ptr_factory_.GetWeakPtr(), url, | 328 weak_ptr_factory_.GetWeakPtr(), url, callback)); |
| 277 base::Passed(&j_callback))); | |
| 278 } | 329 } |
| 279 | 330 |
| 280 void MostVisitedSites::OnLocalThumbnailFetched( | 331 void MostVisitedSites::OnLocalThumbnailFetched( |
| 281 const GURL& url, | 332 const GURL& url, |
| 282 std::unique_ptr<ScopedJavaGlobalRef<jobject>> j_callback, | 333 const ThumbnailCallback& callback, |
| 283 std::unique_ptr<SkBitmap> bitmap) { | 334 std::unique_ptr<SkBitmap> bitmap) { |
| 284 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 335 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 285 if (!bitmap.get()) { | 336 if (!bitmap.get()) { |
| 286 // A thumbnail is not locally available for |url|. Make sure it is put in | 337 // A thumbnail is not locally available for |url|. Make sure it is put in |
| 287 // the list to be fetched at the next visit to this site. | 338 // the list to be fetched at the next visit to this site. |
| 288 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); | 339 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); |
| 289 if (top_sites) | 340 if (top_sites) |
| 290 top_sites->AddForcedURL(url, base::Time::Now()); | 341 top_sites->AddForcedURL(url, base::Time::Now()); |
| 291 // Also fetch a remote thumbnail if possible. PopularSites or the | 342 // Also fetch a remote thumbnail if possible. PopularSites or the |
| 292 // SuggestionsService can supply a thumbnail download URL. | 343 // SuggestionsService can supply a thumbnail download URL. |
| 293 SuggestionsService* suggestions_service = | 344 SuggestionsService* suggestions_service = |
| 294 SuggestionsServiceFactory::GetForProfile(profile_); | 345 SuggestionsServiceFactory::GetForProfile(profile_); |
| 295 if (popular_sites_) { | 346 if (popular_sites_) { |
| 296 const std::vector<PopularSites::Site>& sites = popular_sites_->sites(); | 347 const std::vector<PopularSites::Site>& sites = popular_sites_->sites(); |
| 297 auto it = std::find_if( | 348 auto it = std::find_if( |
| 298 sites.begin(), sites.end(), | 349 sites.begin(), sites.end(), |
| 299 [&url](const PopularSites::Site& site) { return site.url == url; }); | 350 [&url](const PopularSites::Site& site) { return site.url == url; }); |
| 300 if (it != sites.end() && it->thumbnail_url.is_valid()) { | 351 if (it != sites.end() && it->thumbnail_url.is_valid()) { |
| 301 return suggestions_service->GetPageThumbnailWithURL( | 352 return suggestions_service->GetPageThumbnailWithURL( |
| 302 url, it->thumbnail_url, | 353 url, it->thumbnail_url, |
| 303 base::Bind(&MostVisitedSites::OnObtainedThumbnail, | 354 base::Bind(&MostVisitedSites::OnObtainedThumbnail, |
| 304 weak_ptr_factory_.GetWeakPtr(), false, | 355 weak_ptr_factory_.GetWeakPtr(), false, callback)); |
| 305 base::Passed(&j_callback))); | |
| 306 } | 356 } |
| 307 } | 357 } |
| 308 if (mv_source_ == SUGGESTIONS_SERVICE) { | 358 if (mv_source_ == SUGGESTIONS_SERVICE) { |
| 309 return suggestions_service->GetPageThumbnail( | 359 return suggestions_service->GetPageThumbnail( |
| 310 url, base::Bind(&MostVisitedSites::OnObtainedThumbnail, | 360 url, base::Bind(&MostVisitedSites::OnObtainedThumbnail, |
| 311 weak_ptr_factory_.GetWeakPtr(), false, | 361 weak_ptr_factory_.GetWeakPtr(), false, callback)); |
| 312 base::Passed(&j_callback))); | |
| 313 } | 362 } |
| 314 } | 363 } |
| 315 OnObtainedThumbnail(true, std::move(j_callback), url, bitmap.get()); | 364 OnObtainedThumbnail(true, callback, url, bitmap.get()); |
| 316 } | 365 } |
| 317 | 366 |
| 318 void MostVisitedSites::OnObtainedThumbnail( | 367 void MostVisitedSites::OnObtainedThumbnail( |
| 319 bool is_local_thumbnail, | 368 bool is_local_thumbnail, |
| 320 std::unique_ptr<ScopedJavaGlobalRef<jobject>> j_callback, | 369 const ThumbnailCallback& callback, |
| 321 const GURL& url, | 370 const GURL& url, |
| 322 const SkBitmap* bitmap) { | 371 const SkBitmap* bitmap) { |
| 323 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 372 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 373 callback.Run(is_local_thumbnail, bitmap); |
| 374 } |
| 375 |
| 376 static void CallJavaWithBitmap( |
| 377 std::unique_ptr<ScopedJavaGlobalRef<jobject>> j_callback, |
| 378 bool is_local_thumbnail, |
| 379 const SkBitmap* bitmap) { |
| 324 JNIEnv* env = AttachCurrentThread(); | 380 JNIEnv* env = AttachCurrentThread(); |
| 325 ScopedJavaLocalRef<jobject> j_bitmap; | 381 ScopedJavaLocalRef<jobject> j_bitmap; |
| 326 if (bitmap) | 382 if (bitmap) |
| 327 j_bitmap = gfx::ConvertToJavaBitmap(bitmap); | 383 j_bitmap = gfx::ConvertToJavaBitmap(bitmap); |
| 328 Java_ThumbnailCallback_onMostVisitedURLsThumbnailAvailable( | 384 Java_ThumbnailCallback_onMostVisitedURLsThumbnailAvailable( |
| 329 env, j_callback->obj(), j_bitmap.obj(), is_local_thumbnail); | 385 env, j_callback->obj(), j_bitmap.obj(), is_local_thumbnail); |
| 330 } | 386 } |
| 331 | 387 |
| 332 void MostVisitedSites::AddOrRemoveBlacklistedUrl( | 388 void MostVisitedSites::AddOrRemoveBlacklistedUrl( |
| 333 JNIEnv* env, | 389 JNIEnv* env, |
| 334 const JavaParamRef<jobject>& obj, | 390 const JavaParamRef<jobject>& obj, |
| 335 const JavaParamRef<jstring>& j_url, | 391 const JavaParamRef<jstring>& j_url, |
| 336 jboolean add_url) { | 392 jboolean add_url) { |
| 337 GURL url(ConvertJavaStringToUTF8(env, j_url)); | 393 GURL url(ConvertJavaStringToUTF8(env, j_url)); |
| 394 AddOrRemoveBlacklistedUrl(url, add_url); |
| 395 } |
| 338 | 396 |
| 397 void MostVisitedSites::AddOrRemoveBlacklistedUrl( |
| 398 const GURL& url, bool add_url) { |
| 339 // Always blacklist in the local TopSites. | 399 // Always blacklist in the local TopSites. |
| 340 scoped_refptr<TopSites> top_sites = TopSitesFactory::GetForProfile(profile_); | 400 scoped_refptr<TopSites> top_sites = TopSitesFactory::GetForProfile(profile_); |
| 341 if (top_sites) { | 401 if (top_sites) { |
| 342 if (add_url) | 402 if (add_url) |
| 343 top_sites->AddBlacklistedURL(url); | 403 top_sites->AddBlacklistedURL(url); |
| 344 else | 404 else |
| 345 top_sites->RemoveBlacklistedURL(url); | 405 top_sites->RemoveBlacklistedURL(url); |
| 346 } | 406 } |
| 347 | 407 |
| 348 // Only blacklist in the server-side suggestions service if it's active. | 408 // Only blacklist in the server-side suggestions service if it's active. |
| 349 if (mv_source_ == SUGGESTIONS_SERVICE) { | 409 if (mv_source_ == SUGGESTIONS_SERVICE) { |
| 350 SuggestionsService* suggestions_service = | 410 SuggestionsService* suggestions_service = |
| 351 SuggestionsServiceFactory::GetForProfile(profile_); | 411 SuggestionsServiceFactory::GetForProfile(profile_); |
| 352 if (add_url) | 412 if (add_url) |
| 353 suggestions_service->BlacklistURL(url); | 413 suggestions_service->BlacklistURL(url); |
| 354 else | 414 else |
| 355 suggestions_service->UndoBlacklistURL(url); | 415 suggestions_service->UndoBlacklistURL(url); |
| 356 } | 416 } |
| 357 } | 417 } |
| 358 | 418 |
| 359 void MostVisitedSites::RecordTileTypeMetrics( | 419 void MostVisitedSites::RecordTileTypeMetrics( |
| 360 JNIEnv* env, | 420 JNIEnv* env, |
| 361 const JavaParamRef<jobject>& obj, | 421 const JavaParamRef<jobject>& obj, |
| 362 const JavaParamRef<jintArray>& jtile_types) { | 422 const JavaParamRef<jintArray>& jtile_types) { |
| 363 std::vector<int> tile_types; | 423 std::vector<int> tile_types; |
| 364 base::android::JavaIntArrayToIntVector(env, jtile_types, &tile_types); | 424 base::android::JavaIntArrayToIntVector(env, jtile_types, &tile_types); |
| 365 DCHECK_EQ(current_suggestions_.size(), tile_types.size()); | 425 DCHECK_EQ(current_suggestions_.size(), tile_types.size()); |
| 426 RecordTileTypeMetrics(tile_types); |
| 427 } |
| 366 | 428 |
| 429 void MostVisitedSites::RecordTileTypeMetrics( |
| 430 const std::vector<int>& tile_types) { |
| 367 int counts_per_type[NUM_TILE_TYPES] = {0}; | 431 int counts_per_type[NUM_TILE_TYPES] = {0}; |
| 368 for (size_t i = 0; i < tile_types.size(); ++i) { | 432 for (size_t i = 0; i < tile_types.size(); ++i) { |
| 369 int tile_type = tile_types[i]; | 433 int tile_type = tile_types[i]; |
| 370 ++counts_per_type[tile_type]; | 434 ++counts_per_type[tile_type]; |
| 371 std::string histogram = base::StringPrintf( | 435 std::string histogram = base::StringPrintf( |
| 372 "NewTabPage.TileType.%s", | 436 "NewTabPage.TileType.%s", |
| 373 current_suggestions_[i]->GetSourceHistogramName().c_str()); | 437 current_suggestions_[i]->GetSourceHistogramName().c_str()); |
| 374 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES); | 438 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES); |
| 375 } | 439 } |
| 376 | 440 |
| 377 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsReal", | 441 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsReal", |
| 378 counts_per_type[ICON_REAL]); | 442 counts_per_type[ICON_REAL]); |
| 379 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsColor", | 443 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsColor", |
| 380 counts_per_type[ICON_COLOR]); | 444 counts_per_type[ICON_COLOR]); |
| 381 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsGray", | 445 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsGray", |
| 382 counts_per_type[ICON_DEFAULT]); | 446 counts_per_type[ICON_DEFAULT]); |
| 383 } | 447 } |
| 384 | 448 |
| 385 void MostVisitedSites::RecordOpenedMostVisitedItem( | 449 void MostVisitedSites::RecordOpenedMostVisitedItem( |
| 386 JNIEnv* env, | 450 JNIEnv* env, |
| 387 const JavaParamRef<jobject>& obj, | 451 const JavaParamRef<jobject>& obj, |
| 388 jint index, | 452 jint index, |
| 389 jint tile_type) { | 453 jint tile_type) { |
| 454 RecordOpenedMostVisitedItem(index, tile_type); |
| 455 } |
| 456 |
| 457 void MostVisitedSites::RecordOpenedMostVisitedItem(int index, int tile_type) { |
| 390 DCHECK_GE(index, 0); | 458 DCHECK_GE(index, 0); |
| 391 DCHECK_LT(index, static_cast<int>(current_suggestions_.size())); | 459 DCHECK_LT(index, static_cast<int>(current_suggestions_.size())); |
| 392 std::string histogram = base::StringPrintf( | 460 std::string histogram = base::StringPrintf( |
| 393 "NewTabPage.MostVisited.%s", | 461 "NewTabPage.MostVisited.%s", |
| 394 current_suggestions_[index]->GetSourceHistogramName().c_str()); | 462 current_suggestions_[index]->GetSourceHistogramName().c_str()); |
| 395 LogHistogramEvent(histogram, index, num_sites_); | 463 LogHistogramEvent(histogram, index, num_sites_); |
| 396 | 464 |
| 397 histogram = base::StringPrintf( | 465 histogram = base::StringPrintf( |
| 398 "NewTabPage.TileTypeClicked.%s", | 466 "NewTabPage.TileTypeClicked.%s", |
| 399 current_suggestions_[index]->GetSourceHistogramName().c_str()); | 467 current_suggestions_[index]->GetSourceHistogramName().c_str()); |
| (...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 797 | 865 |
| 798 void MostVisitedSites::NotifyMostVisitedURLsObserver() { | 866 void MostVisitedSites::NotifyMostVisitedURLsObserver() { |
| 799 size_t num_suggestions = current_suggestions_.size(); | 867 size_t num_suggestions = current_suggestions_.size(); |
| 800 if (received_most_visited_sites_ && received_popular_sites_ && | 868 if (received_most_visited_sites_ && received_popular_sites_ && |
| 801 !recorded_uma_) { | 869 !recorded_uma_) { |
| 802 RecordImpressionUMAMetrics(); | 870 RecordImpressionUMAMetrics(); |
| 803 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.NumberOfTiles", num_suggestions); | 871 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.NumberOfTiles", num_suggestions); |
| 804 recorded_uma_ = true; | 872 recorded_uma_ = true; |
| 805 } | 873 } |
| 806 | 874 |
| 807 if (observer_.is_null()) | 875 if (!observer_) |
| 808 return; | 876 return; |
| 809 | 877 |
| 810 std::vector<base::string16> titles; | 878 std::vector<base::string16> titles; |
| 811 std::vector<std::string> urls; | 879 std::vector<std::string> urls; |
| 812 std::vector<std::string> whitelist_icon_paths; | 880 std::vector<std::string> whitelist_icon_paths; |
| 813 titles.reserve(num_suggestions); | 881 titles.reserve(num_suggestions); |
| 814 urls.reserve(num_suggestions); | 882 urls.reserve(num_suggestions); |
| 815 for (const auto& suggestion : current_suggestions_) { | 883 for (const auto& suggestion : current_suggestions_) { |
| 816 titles.push_back(suggestion->title); | 884 titles.push_back(suggestion->title); |
| 817 urls.push_back(suggestion->url.spec()); | 885 urls.push_back(suggestion->url.spec()); |
| 818 whitelist_icon_paths.push_back(suggestion->whitelist_icon_path.value()); | 886 whitelist_icon_paths.push_back(suggestion->whitelist_icon_path.value()); |
| 819 } | 887 } |
| 820 JNIEnv* env = AttachCurrentThread(); | 888 |
| 821 DCHECK_EQ(titles.size(), urls.size()); | 889 observer_->OnMostVisitedURLsAvailable(titles, urls, whitelist_icon_paths); |
| 822 Java_MostVisitedURLsObserver_onMostVisitedURLsAvailable( | |
| 823 env, observer_.obj(), ToJavaArrayOfStrings(env, titles).obj(), | |
| 824 ToJavaArrayOfStrings(env, urls).obj(), | |
| 825 ToJavaArrayOfStrings(env, whitelist_icon_paths).obj()); | |
| 826 } | 890 } |
| 827 | 891 |
| 828 void MostVisitedSites::OnPopularSitesAvailable(bool success) { | 892 void MostVisitedSites::OnPopularSitesAvailable(bool success) { |
| 829 received_popular_sites_ = true; | 893 received_popular_sites_ = true; |
| 830 | 894 |
| 831 if (!success) { | 895 if (!success) { |
| 832 LOG(WARNING) << "Download of popular sites failed"; | 896 LOG(WARNING) << "Download of popular sites failed"; |
| 833 return; | 897 return; |
| 834 } | 898 } |
| 835 | 899 |
| 836 if (observer_.is_null()) | 900 if (!observer_) |
| 837 return; | 901 return; |
| 838 | 902 |
| 839 std::vector<std::string> urls; | 903 std::vector<std::string> urls; |
| 840 std::vector<std::string> favicon_urls; | 904 std::vector<std::string> favicon_urls; |
| 841 std::vector<std::string> large_icon_urls; | 905 std::vector<std::string> large_icon_urls; |
| 842 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { | 906 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { |
| 843 urls.push_back(popular_site.url.spec()); | 907 urls.push_back(popular_site.url.spec()); |
| 844 favicon_urls.push_back(popular_site.favicon_url.spec()); | 908 favicon_urls.push_back(popular_site.favicon_url.spec()); |
| 845 large_icon_urls.push_back(popular_site.large_icon_url.spec()); | 909 large_icon_urls.push_back(popular_site.large_icon_url.spec()); |
| 846 } | 910 } |
| 847 JNIEnv* env = AttachCurrentThread(); | 911 observer_->OnPopularURLsAvailable(urls, favicon_urls, large_icon_urls); |
| 848 Java_MostVisitedURLsObserver_onPopularURLsAvailable( | |
| 849 env, observer_.obj(), ToJavaArrayOfStrings(env, urls).obj(), | |
| 850 ToJavaArrayOfStrings(env, favicon_urls).obj(), | |
| 851 ToJavaArrayOfStrings(env, large_icon_urls).obj()); | |
| 852 QueryMostVisitedURLs(); | 912 QueryMostVisitedURLs(); |
| 853 } | 913 } |
| 854 | 914 |
| 855 void MostVisitedSites::RecordImpressionUMAMetrics() { | 915 void MostVisitedSites::RecordImpressionUMAMetrics() { |
| 856 for (size_t i = 0; i < current_suggestions_.size(); i++) { | 916 for (size_t i = 0; i < current_suggestions_.size(); i++) { |
| 857 std::string histogram = base::StringPrintf( | 917 std::string histogram = base::StringPrintf( |
| 858 "NewTabPage.SuggestionsImpression.%s", | 918 "NewTabPage.SuggestionsImpression.%s", |
| 859 current_suggestions_[i]->GetSourceHistogramName().c_str()); | 919 current_suggestions_[i]->GetSourceHistogramName().c_str()); |
| 860 LogHistogramEvent(histogram, static_cast<int>(i), num_sites_); | 920 LogHistogramEvent(histogram, static_cast<int>(i), num_sites_); |
| 861 } | 921 } |
| 862 } | 922 } |
| 863 | 923 |
| 864 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} | 924 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} |
| 865 | 925 |
| 866 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, | 926 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, |
| 867 ChangeReason change_reason) { | 927 ChangeReason change_reason) { |
| 868 if (mv_source_ == TOP_SITES) { | 928 if (mv_source_ == TOP_SITES) { |
| 869 // The displayed suggestions are invalidated. | 929 // The displayed suggestions are invalidated. |
| 870 InitiateTopSitesQuery(); | 930 InitiateTopSitesQuery(); |
| 871 } | 931 } |
| 872 } | 932 } |
| 873 | 933 |
| 874 static jlong Init(JNIEnv* env, | 934 static jlong Init(JNIEnv* env, |
| 875 const JavaParamRef<jobject>& obj, | 935 const JavaParamRef<jobject>& obj, |
| 876 const JavaParamRef<jobject>& jprofile) { | 936 const JavaParamRef<jobject>& jprofile) { |
| 877 MostVisitedSites* most_visited_sites = | 937 MostVisitedSites* most_visited_sites = |
| 878 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); | 938 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); |
| 879 return reinterpret_cast<intptr_t>(most_visited_sites); | 939 return reinterpret_cast<intptr_t>(most_visited_sites); |
| 880 } | 940 } |
| OLD | NEW |