Chromium Code Reviews| 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( | |
|
Marc Treib
2016/04/15 15:21:41
I kinda preferred "Sites" over "URLs" (also below
| |
| 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) { |
| 264 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 265 std::unique_ptr<ScopedJavaGlobalRef<jobject>> j_callback( | 311 std::unique_ptr<ScopedJavaGlobalRef<jobject>> j_callback( |
| 266 new ScopedJavaGlobalRef<jobject>()); | 312 new ScopedJavaGlobalRef<jobject>()); |
| 267 j_callback->Reset(env, j_callback_obj); | 313 j_callback->Reset(env, j_callback_obj); |
| 314 auto callback = base::Bind(&CallJavaWithBitmap, base::Passed(&j_callback)); | |
| 315 GURL url(ConvertJavaStringToUTF8(env, j_url)); | |
| 316 GetURLThumbnail(url, callback); | |
| 317 } | |
| 268 | 318 |
| 269 GURL url(ConvertJavaStringToUTF8(env, j_url)); | 319 void MostVisitedSites::GetURLThumbnail( |
| 320 const GURL& url, | |
| 321 const ThumbnailCallback& callback) { | |
| 322 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 270 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); | 323 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); |
| 271 | 324 |
| 272 BrowserThread::PostTaskAndReplyWithResult( | 325 BrowserThread::PostTaskAndReplyWithResult( |
| 273 BrowserThread::DB, FROM_HERE, | 326 BrowserThread::DB, FROM_HERE, |
| 274 base::Bind(&MaybeFetchLocalThumbnail, url, top_sites), | 327 base::Bind(&MaybeFetchLocalThumbnail, url, top_sites), |
| 275 base::Bind(&MostVisitedSites::OnLocalThumbnailFetched, | 328 base::Bind(&MostVisitedSites::OnLocalThumbnailFetched, |
| 276 weak_ptr_factory_.GetWeakPtr(), url, | 329 weak_ptr_factory_.GetWeakPtr(), url, callback)); |
| 277 base::Passed(&j_callback))); | |
| 278 } | 330 } |
| 279 | 331 |
| 280 void MostVisitedSites::OnLocalThumbnailFetched( | 332 void MostVisitedSites::OnLocalThumbnailFetched( |
| 281 const GURL& url, | 333 const GURL& url, |
| 282 std::unique_ptr<ScopedJavaGlobalRef<jobject>> j_callback, | 334 const ThumbnailCallback& callback, |
| 283 std::unique_ptr<SkBitmap> bitmap) { | 335 std::unique_ptr<SkBitmap> bitmap) { |
| 284 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 336 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 285 if (!bitmap.get()) { | 337 if (!bitmap.get()) { |
| 286 // A thumbnail is not locally available for |url|. Make sure it is put in | 338 // 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. | 339 // the list to be fetched at the next visit to this site. |
| 288 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); | 340 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); |
| 289 if (top_sites) | 341 if (top_sites) |
| 290 top_sites->AddForcedURL(url, base::Time::Now()); | 342 top_sites->AddForcedURL(url, base::Time::Now()); |
| 291 // Also fetch a remote thumbnail if possible. PopularSites or the | 343 // Also fetch a remote thumbnail if possible. PopularSites or the |
| 292 // SuggestionsService can supply a thumbnail download URL. | 344 // SuggestionsService can supply a thumbnail download URL. |
| 293 SuggestionsService* suggestions_service = | 345 SuggestionsService* suggestions_service = |
| 294 SuggestionsServiceFactory::GetForProfile(profile_); | 346 SuggestionsServiceFactory::GetForProfile(profile_); |
| 295 if (popular_sites_) { | 347 if (popular_sites_) { |
| 296 const std::vector<PopularSites::Site>& sites = popular_sites_->sites(); | 348 const std::vector<PopularSites::Site>& sites = popular_sites_->sites(); |
| 297 auto it = std::find_if( | 349 auto it = std::find_if( |
| 298 sites.begin(), sites.end(), | 350 sites.begin(), sites.end(), |
| 299 [&url](const PopularSites::Site& site) { return site.url == url; }); | 351 [&url](const PopularSites::Site& site) { return site.url == url; }); |
| 300 if (it != sites.end() && it->thumbnail_url.is_valid()) { | 352 if (it != sites.end() && it->thumbnail_url.is_valid()) { |
| 301 return suggestions_service->GetPageThumbnailWithURL( | 353 return suggestions_service->GetPageThumbnailWithURL( |
| 302 url, it->thumbnail_url, | 354 url, it->thumbnail_url, |
| 303 base::Bind(&MostVisitedSites::OnObtainedThumbnail, | 355 base::Bind(&MostVisitedSites::OnObtainedThumbnail, |
| 304 weak_ptr_factory_.GetWeakPtr(), false, | 356 weak_ptr_factory_.GetWeakPtr(), false, callback)); |
| 305 base::Passed(&j_callback))); | |
| 306 } | 357 } |
| 307 } | 358 } |
| 308 if (mv_source_ == SUGGESTIONS_SERVICE) { | 359 if (mv_source_ == SUGGESTIONS_SERVICE) { |
| 309 return suggestions_service->GetPageThumbnail( | 360 return suggestions_service->GetPageThumbnail( |
| 310 url, base::Bind(&MostVisitedSites::OnObtainedThumbnail, | 361 url, base::Bind(&MostVisitedSites::OnObtainedThumbnail, |
| 311 weak_ptr_factory_.GetWeakPtr(), false, | 362 weak_ptr_factory_.GetWeakPtr(), false, callback)); |
| 312 base::Passed(&j_callback))); | |
| 313 } | 363 } |
| 314 } | 364 } |
| 315 OnObtainedThumbnail(true, std::move(j_callback), url, bitmap.get()); | 365 OnObtainedThumbnail(true, callback, url, bitmap.get()); |
| 316 } | 366 } |
| 317 | 367 |
| 318 void MostVisitedSites::OnObtainedThumbnail( | 368 void MostVisitedSites::OnObtainedThumbnail( |
| 319 bool is_local_thumbnail, | 369 bool is_local_thumbnail, |
| 320 std::unique_ptr<ScopedJavaGlobalRef<jobject>> j_callback, | 370 const ThumbnailCallback& callback, |
| 321 const GURL& url, | 371 const GURL& url, |
| 322 const SkBitmap* bitmap) { | 372 const SkBitmap* bitmap) { |
| 323 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 373 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 374 callback.Run(is_local_thumbnail, bitmap); | |
| 375 } | |
| 376 | |
| 377 static void CallJavaWithBitmap( | |
| 378 std::unique_ptr<ScopedJavaGlobalRef<jobject>> j_callback, | |
| 379 bool is_local_thumbnail, | |
| 380 const SkBitmap* bitmap) { | |
| 324 JNIEnv* env = AttachCurrentThread(); | 381 JNIEnv* env = AttachCurrentThread(); |
| 325 ScopedJavaLocalRef<jobject> j_bitmap; | 382 ScopedJavaLocalRef<jobject> j_bitmap; |
| 326 if (bitmap) | 383 if (bitmap) |
| 327 j_bitmap = gfx::ConvertToJavaBitmap(bitmap); | 384 j_bitmap = gfx::ConvertToJavaBitmap(bitmap); |
| 328 Java_ThumbnailCallback_onMostVisitedURLsThumbnailAvailable( | 385 Java_ThumbnailCallback_onMostVisitedURLsThumbnailAvailable( |
| 329 env, j_callback->obj(), j_bitmap.obj(), is_local_thumbnail); | 386 env, j_callback->obj(), j_bitmap.obj(), is_local_thumbnail); |
| 330 } | 387 } |
| 331 | 388 |
| 332 void MostVisitedSites::AddOrRemoveBlacklistedUrl( | 389 void MostVisitedSites::AddOrRemoveBlacklistedUrl( |
| 333 JNIEnv* env, | 390 JNIEnv* env, |
| 334 const JavaParamRef<jobject>& obj, | 391 const JavaParamRef<jobject>& obj, |
| 335 const JavaParamRef<jstring>& j_url, | 392 const JavaParamRef<jstring>& j_url, |
| 336 jboolean add_url) { | 393 jboolean add_url) { |
| 337 GURL url(ConvertJavaStringToUTF8(env, j_url)); | 394 GURL url(ConvertJavaStringToUTF8(env, j_url)); |
| 395 AddOrRemoveBlacklistedUrl(url, add_url); | |
| 396 } | |
| 338 | 397 |
| 398 void MostVisitedSites::AddOrRemoveBlacklistedUrl( | |
| 399 const GURL& url, bool add_url) { | |
| 339 // Always blacklist in the local TopSites. | 400 // Always blacklist in the local TopSites. |
| 340 scoped_refptr<TopSites> top_sites = TopSitesFactory::GetForProfile(profile_); | 401 scoped_refptr<TopSites> top_sites = TopSitesFactory::GetForProfile(profile_); |
| 341 if (top_sites) { | 402 if (top_sites) { |
| 342 if (add_url) | 403 if (add_url) |
| 343 top_sites->AddBlacklistedURL(url); | 404 top_sites->AddBlacklistedURL(url); |
| 344 else | 405 else |
| 345 top_sites->RemoveBlacklistedURL(url); | 406 top_sites->RemoveBlacklistedURL(url); |
| 346 } | 407 } |
| 347 | 408 |
| 348 // Only blacklist in the server-side suggestions service if it's active. | 409 // Only blacklist in the server-side suggestions service if it's active. |
| 349 if (mv_source_ == SUGGESTIONS_SERVICE) { | 410 if (mv_source_ == SUGGESTIONS_SERVICE) { |
| 350 SuggestionsService* suggestions_service = | 411 SuggestionsService* suggestions_service = |
| 351 SuggestionsServiceFactory::GetForProfile(profile_); | 412 SuggestionsServiceFactory::GetForProfile(profile_); |
| 352 if (add_url) | 413 if (add_url) |
| 353 suggestions_service->BlacklistURL(url); | 414 suggestions_service->BlacklistURL(url); |
| 354 else | 415 else |
| 355 suggestions_service->UndoBlacklistURL(url); | 416 suggestions_service->UndoBlacklistURL(url); |
| 356 } | 417 } |
| 357 } | 418 } |
| 358 | 419 |
| 359 void MostVisitedSites::RecordTileTypeMetrics( | 420 void MostVisitedSites::RecordTileTypeMetrics( |
| 360 JNIEnv* env, | 421 JNIEnv* env, |
| 361 const JavaParamRef<jobject>& obj, | 422 const JavaParamRef<jobject>& obj, |
| 362 const JavaParamRef<jintArray>& jtile_types) { | 423 const JavaParamRef<jintArray>& jtile_types) { |
| 363 std::vector<int> tile_types; | 424 std::vector<int> tile_types; |
| 364 base::android::JavaIntArrayToIntVector(env, jtile_types, &tile_types); | 425 base::android::JavaIntArrayToIntVector(env, jtile_types, &tile_types); |
| 365 DCHECK_EQ(current_suggestions_.size(), tile_types.size()); | 426 DCHECK_EQ(current_suggestions_.size(), tile_types.size()); |
| 427 RecordTileTypeMetrics(tile_types); | |
| 428 } | |
| 366 | 429 |
| 430 void MostVisitedSites::RecordTileTypeMetrics( | |
| 431 const std::vector<int>& tile_types) { | |
| 367 int counts_per_type[NUM_TILE_TYPES] = {0}; | 432 int counts_per_type[NUM_TILE_TYPES] = {0}; |
| 368 for (size_t i = 0; i < tile_types.size(); ++i) { | 433 for (size_t i = 0; i < tile_types.size(); ++i) { |
| 369 int tile_type = tile_types[i]; | 434 int tile_type = tile_types[i]; |
| 370 ++counts_per_type[tile_type]; | 435 ++counts_per_type[tile_type]; |
| 371 std::string histogram = base::StringPrintf( | 436 std::string histogram = base::StringPrintf( |
| 372 "NewTabPage.TileType.%s", | 437 "NewTabPage.TileType.%s", |
| 373 current_suggestions_[i]->GetSourceHistogramName().c_str()); | 438 current_suggestions_[i]->GetSourceHistogramName().c_str()); |
| 374 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES); | 439 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES); |
| 375 } | 440 } |
| 376 | 441 |
| 377 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsReal", | 442 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsReal", |
| 378 counts_per_type[ICON_REAL]); | 443 counts_per_type[ICON_REAL]); |
| 379 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsColor", | 444 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsColor", |
| 380 counts_per_type[ICON_COLOR]); | 445 counts_per_type[ICON_COLOR]); |
| 381 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsGray", | 446 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsGray", |
| 382 counts_per_type[ICON_DEFAULT]); | 447 counts_per_type[ICON_DEFAULT]); |
| 383 } | 448 } |
| 384 | 449 |
| 385 void MostVisitedSites::RecordOpenedMostVisitedItem( | 450 void MostVisitedSites::RecordOpenedMostVisitedItem( |
| 386 JNIEnv* env, | 451 JNIEnv* env, |
| 387 const JavaParamRef<jobject>& obj, | 452 const JavaParamRef<jobject>& obj, |
| 388 jint index, | 453 jint index, |
| 389 jint tile_type) { | 454 jint tile_type) { |
| 455 RecordOpenedMostVisitedItem(index, tile_type); | |
| 456 } | |
| 457 | |
| 458 void MostVisitedSites::RecordOpenedMostVisitedItem(int index, int tile_type) { | |
| 390 DCHECK_GE(index, 0); | 459 DCHECK_GE(index, 0); |
| 391 DCHECK_LT(index, static_cast<int>(current_suggestions_.size())); | 460 DCHECK_LT(index, static_cast<int>(current_suggestions_.size())); |
| 392 std::string histogram = base::StringPrintf( | 461 std::string histogram = base::StringPrintf( |
| 393 "NewTabPage.MostVisited.%s", | 462 "NewTabPage.MostVisited.%s", |
| 394 current_suggestions_[index]->GetSourceHistogramName().c_str()); | 463 current_suggestions_[index]->GetSourceHistogramName().c_str()); |
| 395 LogHistogramEvent(histogram, index, num_sites_); | 464 LogHistogramEvent(histogram, index, num_sites_); |
| 396 | 465 |
| 397 histogram = base::StringPrintf( | 466 histogram = base::StringPrintf( |
| 398 "NewTabPage.TileTypeClicked.%s", | 467 "NewTabPage.TileTypeClicked.%s", |
| 399 current_suggestions_[index]->GetSourceHistogramName().c_str()); | 468 current_suggestions_[index]->GetSourceHistogramName().c_str()); |
| (...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 797 | 866 |
| 798 void MostVisitedSites::NotifyMostVisitedURLsObserver() { | 867 void MostVisitedSites::NotifyMostVisitedURLsObserver() { |
| 799 size_t num_suggestions = current_suggestions_.size(); | 868 size_t num_suggestions = current_suggestions_.size(); |
| 800 if (received_most_visited_sites_ && received_popular_sites_ && | 869 if (received_most_visited_sites_ && received_popular_sites_ && |
| 801 !recorded_uma_) { | 870 !recorded_uma_) { |
| 802 RecordImpressionUMAMetrics(); | 871 RecordImpressionUMAMetrics(); |
| 803 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.NumberOfTiles", num_suggestions); | 872 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.NumberOfTiles", num_suggestions); |
| 804 recorded_uma_ = true; | 873 recorded_uma_ = true; |
| 805 } | 874 } |
| 806 | 875 |
| 807 if (observer_.is_null()) | 876 if (!observer_) |
| 808 return; | 877 return; |
| 809 | 878 |
| 810 std::vector<base::string16> titles; | 879 std::vector<base::string16> titles; |
| 811 std::vector<std::string> urls; | 880 std::vector<std::string> urls; |
| 812 std::vector<std::string> whitelist_icon_paths; | 881 std::vector<std::string> whitelist_icon_paths; |
| 813 titles.reserve(num_suggestions); | 882 titles.reserve(num_suggestions); |
| 814 urls.reserve(num_suggestions); | 883 urls.reserve(num_suggestions); |
| 815 for (const auto& suggestion : current_suggestions_) { | 884 for (const auto& suggestion : current_suggestions_) { |
| 816 titles.push_back(suggestion->title); | 885 titles.push_back(suggestion->title); |
| 817 urls.push_back(suggestion->url.spec()); | 886 urls.push_back(suggestion->url.spec()); |
| 818 whitelist_icon_paths.push_back(suggestion->whitelist_icon_path.value()); | 887 whitelist_icon_paths.push_back(suggestion->whitelist_icon_path.value()); |
| 819 } | 888 } |
| 820 JNIEnv* env = AttachCurrentThread(); | 889 |
| 821 DCHECK_EQ(titles.size(), urls.size()); | 890 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 } | 891 } |
| 827 | 892 |
| 828 void MostVisitedSites::OnPopularSitesAvailable(bool success) { | 893 void MostVisitedSites::OnPopularSitesAvailable(bool success) { |
| 829 received_popular_sites_ = true; | 894 received_popular_sites_ = true; |
| 830 | 895 |
| 831 if (!success) { | 896 if (!success) { |
| 832 LOG(WARNING) << "Download of popular sites failed"; | 897 LOG(WARNING) << "Download of popular sites failed"; |
| 833 return; | 898 return; |
| 834 } | 899 } |
| 835 | 900 |
| 836 if (observer_.is_null()) | 901 if (!observer_) |
| 837 return; | 902 return; |
| 838 | 903 |
| 839 std::vector<std::string> urls; | 904 std::vector<std::string> urls; |
| 840 std::vector<std::string> favicon_urls; | 905 std::vector<std::string> favicon_urls; |
| 841 std::vector<std::string> large_icon_urls; | 906 std::vector<std::string> large_icon_urls; |
| 842 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { | 907 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { |
| 843 urls.push_back(popular_site.url.spec()); | 908 urls.push_back(popular_site.url.spec()); |
| 844 favicon_urls.push_back(popular_site.favicon_url.spec()); | 909 favicon_urls.push_back(popular_site.favicon_url.spec()); |
| 845 large_icon_urls.push_back(popular_site.large_icon_url.spec()); | 910 large_icon_urls.push_back(popular_site.large_icon_url.spec()); |
| 846 } | 911 } |
| 847 JNIEnv* env = AttachCurrentThread(); | 912 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(); | 913 QueryMostVisitedURLs(); |
| 853 } | 914 } |
| 854 | 915 |
| 855 void MostVisitedSites::RecordImpressionUMAMetrics() { | 916 void MostVisitedSites::RecordImpressionUMAMetrics() { |
| 856 for (size_t i = 0; i < current_suggestions_.size(); i++) { | 917 for (size_t i = 0; i < current_suggestions_.size(); i++) { |
| 857 std::string histogram = base::StringPrintf( | 918 std::string histogram = base::StringPrintf( |
| 858 "NewTabPage.SuggestionsImpression.%s", | 919 "NewTabPage.SuggestionsImpression.%s", |
| 859 current_suggestions_[i]->GetSourceHistogramName().c_str()); | 920 current_suggestions_[i]->GetSourceHistogramName().c_str()); |
| 860 LogHistogramEvent(histogram, static_cast<int>(i), num_sites_); | 921 LogHistogramEvent(histogram, static_cast<int>(i), num_sites_); |
| 861 } | 922 } |
| 862 } | 923 } |
| 863 | 924 |
| 864 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} | 925 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} |
| 865 | 926 |
| 866 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, | 927 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, |
| 867 ChangeReason change_reason) { | 928 ChangeReason change_reason) { |
| 868 if (mv_source_ == TOP_SITES) { | 929 if (mv_source_ == TOP_SITES) { |
| 869 // The displayed suggestions are invalidated. | 930 // The displayed suggestions are invalidated. |
| 870 InitiateTopSitesQuery(); | 931 InitiateTopSitesQuery(); |
| 871 } | 932 } |
| 872 } | 933 } |
| 873 | 934 |
| 874 static jlong Init(JNIEnv* env, | 935 static jlong Init(JNIEnv* env, |
| 875 const JavaParamRef<jobject>& obj, | 936 const JavaParamRef<jobject>& obj, |
| 876 const JavaParamRef<jobject>& jprofile) { | 937 const JavaParamRef<jobject>& jprofile) { |
| 877 MostVisitedSites* most_visited_sites = | 938 MostVisitedSites* most_visited_sites = |
| 878 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); | 939 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); |
| 879 return reinterpret_cast<intptr_t>(most_visited_sites); | 940 return reinterpret_cast<intptr_t>(most_visited_sites); |
| 880 } | 941 } |
| OLD | NEW |