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

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

Issue 1884203002: NTP tiles: split methods into C++ and Java parts. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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/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 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 MostVisitedSites::~MostVisitedSites() { 203 MostVisitedSites::~MostVisitedSites() {
204 SupervisedUserService* supervised_user_service = 204 SupervisedUserService* supervised_user_service =
205 SupervisedUserServiceFactory::GetForProfile(profile_); 205 SupervisedUserServiceFactory::GetForProfile(profile_);
206 supervised_user_service->RemoveObserver(this); 206 supervised_user_service->RemoveObserver(this);
207 } 207 }
208 208
209 void MostVisitedSites::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { 209 void MostVisitedSites::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) {
210 delete this; 210 delete this;
211 } 211 }
212 212
213 namespace {
Marc Treib 2016/04/14 16:57:36 This should be merged into the anonymous namespace
sfiera 2016/04/15 13:35:59 Done.
214
215 class JavaObserverBridge : public MostVisitedSitesObserver {
216 public:
217 JavaObserverBridge(
218 JNIEnv* env, const base::android::JavaParamRef<jobject>& obj)
Marc Treib 2016/04/14 16:57:35 nit: "base::android::" isn't required here.
sfiera 2016/04/15 13:35:59 Done.
219 : observer_(env, obj) {}
220
221 void NotifyMostVisitedURLsObserver(
Marc Treib 2016/04/14 16:57:36 I think this should be called "OnMostVisitedSitesA
sfiera 2016/04/15 13:35:59 Done. (also below, OnPopularSitesAvailable => OnPo
222 const std::vector<base::string16>& titles,
223 const std::vector<std::string>& urls,
224 const std::vector<std::string>& whitelist_icon_paths) override {
225 JNIEnv* env = AttachCurrentThread();
226 DCHECK_EQ(titles.size(), urls.size());
227 Java_MostVisitedURLsObserver_onMostVisitedURLsAvailable(
228 env, observer_.obj(), ToJavaArrayOfStrings(env, titles).obj(),
229 ToJavaArrayOfStrings(env, urls).obj(),
230 ToJavaArrayOfStrings(env, whitelist_icon_paths).obj());
231 }
232
233 void OnPopularSitesAvailable(
234 const std::vector<std::string>& urls,
235 const std::vector<std::string>& favicon_urls,
236 const std::vector<std::string>& large_icon_urls) override {
237 JNIEnv* env = AttachCurrentThread();
238 Java_MostVisitedURLsObserver_onPopularURLsAvailable(
239 env, observer_.obj(), ToJavaArrayOfStrings(env, urls).obj(),
240 ToJavaArrayOfStrings(env, favicon_urls).obj(),
241 ToJavaArrayOfStrings(env, large_icon_urls).obj());
242 }
243
244 private:
245 base::android::ScopedJavaGlobalRef<jobject> observer_;
Marc Treib 2016/04/14 16:57:36 Also here: no "base::android::" required.
sfiera 2016/04/15 13:35:59 Done.
246
247 DISALLOW_COPY_AND_ASSIGN(JavaObserverBridge);
248 };
249
250 } // namespace
251
213 void MostVisitedSites::SetMostVisitedURLsObserver( 252 void MostVisitedSites::SetMostVisitedURLsObserver(
214 JNIEnv* env, 253 JNIEnv* env,
215 const JavaParamRef<jobject>& obj, 254 const JavaParamRef<jobject>& obj,
216 const JavaParamRef<jobject>& j_observer, 255 const JavaParamRef<jobject>& j_observer,
217 jint num_sites) { 256 jint num_sites) {
218 observer_.Reset(env, j_observer); 257 SetMostVisitedURLsObserver(
258 std::unique_ptr<MostVisitedSitesObserver>(
259 new JavaObserverBridge(env, j_observer)),
260 num_sites);
261 }
262
263 void MostVisitedSites::SetMostVisitedURLsObserver(
264 std::unique_ptr<MostVisitedSitesObserver> observer, int num_sites) {
265 observer_ = std::move(observer);
219 num_sites_ = num_sites; 266 num_sites_ = num_sites;
220 267
221 if (ShouldShowPopularSites() && 268 if (ShouldShowPopularSites() &&
222 NeedPopularSites(profile_->GetPrefs(), num_sites_)) { 269 NeedPopularSites(profile_->GetPrefs(), num_sites_)) {
223 popular_sites_.reset(new PopularSites( 270 popular_sites_.reset(new PopularSites(
224 profile_, 271 profile_,
225 GetPopularSitesCountry(), 272 GetPopularSitesCountry(),
226 GetPopularSitesVersion(), 273 GetPopularSitesVersion(),
227 false, 274 false,
228 base::Bind(&MostVisitedSites::OnPopularSitesAvailable, 275 base::Bind(&MostVisitedSites::OnPopularSitesAvailable,
(...skipping 20 matching lines...) Expand all
249 base::Unretained(this))); 296 base::Unretained(this)));
250 297
251 // Immediately get the current suggestions from the cache. If the cache is 298 // Immediately get the current suggestions from the cache. If the cache is
252 // empty, this will fall back to TopSites. 299 // empty, this will fall back to TopSites.
253 OnSuggestionsProfileAvailable( 300 OnSuggestionsProfileAvailable(
254 suggestions_service->GetSuggestionsDataFromCache()); 301 suggestions_service->GetSuggestionsDataFromCache());
255 // Also start a request for fresh suggestions. 302 // Also start a request for fresh suggestions.
256 suggestions_service->FetchSuggestionsData(); 303 suggestions_service->FetchSuggestionsData();
257 } 304 }
258 305
306 static void CallJavaWithBitmap(
Marc Treib 2016/04/14 16:57:36 This should go into the anonymous namespace above.
sfiera 2016/04/15 13:35:59 Any reason? It's static already.
Marc Treib 2016/04/15 15:21:41 Ah, the code style indeed allows either. But putti
sfiera 2016/04/15 16:09:06 There's actually no need to forward-declare it eit
Marc Treib 2016/04/15 16:30:03 Acknowledged. Please do eventually move it into th
307 std::unique_ptr<ScopedJavaGlobalRef<jobject>> j_callback,
308 bool is_local_thumbnail,
309 const SkBitmap* bitmap);
310
259 void MostVisitedSites::GetURLThumbnail( 311 void MostVisitedSites::GetURLThumbnail(
260 JNIEnv* env, 312 JNIEnv* env,
261 const JavaParamRef<jobject>& obj, 313 const JavaParamRef<jobject>& obj,
262 const JavaParamRef<jstring>& j_url, 314 const JavaParamRef<jstring>& j_url,
263 const JavaParamRef<jobject>& j_callback_obj) { 315 const JavaParamRef<jobject>& j_callback_obj) {
264 DCHECK_CURRENTLY_ON(BrowserThread::UI);
265 std::unique_ptr<ScopedJavaGlobalRef<jobject>> j_callback( 316 std::unique_ptr<ScopedJavaGlobalRef<jobject>> j_callback(
266 new ScopedJavaGlobalRef<jobject>()); 317 new ScopedJavaGlobalRef<jobject>());
267 j_callback->Reset(env, j_callback_obj); 318 j_callback->Reset(env, j_callback_obj);
Marc Treib 2016/04/14 16:57:36 Not your doing, but do you see any reason why we c
sfiera 2016/04/15 13:35:59 That's not exactly what's happening. We're calling
Marc Treib 2016/04/15 15:21:41 Ah, good point, I misread that. ScopedJavaGlobalRe
sfiera 2016/04/15 16:09:06 Acknowledged.
319 auto callback = base::Bind(&CallJavaWithBitmap, base::Passed(&j_callback));
320 GURL url(ConvertJavaStringToUTF8(env, j_url));
321 GetURLThumbnail(url, callback);
322 }
268 323
269 GURL url(ConvertJavaStringToUTF8(env, j_url)); 324 void MostVisitedSites::GetURLThumbnail(
325 const GURL& url,
326 const base::Callback<void(bool, const SkBitmap*)>& callback) {
327 DCHECK_CURRENTLY_ON(BrowserThread::UI);
270 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); 328 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_));
271 329
272 BrowserThread::PostTaskAndReplyWithResult( 330 BrowserThread::PostTaskAndReplyWithResult(
273 BrowserThread::DB, FROM_HERE, 331 BrowserThread::DB, FROM_HERE,
274 base::Bind(&MaybeFetchLocalThumbnail, url, top_sites), 332 base::Bind(&MaybeFetchLocalThumbnail, url, top_sites),
275 base::Bind(&MostVisitedSites::OnLocalThumbnailFetched, 333 base::Bind(&MostVisitedSites::OnLocalThumbnailFetched,
276 weak_ptr_factory_.GetWeakPtr(), url, 334 weak_ptr_factory_.GetWeakPtr(), url, callback));
277 base::Passed(&j_callback)));
278 } 335 }
279 336
280 void MostVisitedSites::OnLocalThumbnailFetched( 337 void MostVisitedSites::OnLocalThumbnailFetched(
281 const GURL& url, 338 const GURL& url,
282 std::unique_ptr<ScopedJavaGlobalRef<jobject>> j_callback, 339 const base::Callback<void(bool, const SkBitmap*)>& callback,
283 std::unique_ptr<SkBitmap> bitmap) { 340 std::unique_ptr<SkBitmap> bitmap) {
284 DCHECK_CURRENTLY_ON(BrowserThread::UI); 341 DCHECK_CURRENTLY_ON(BrowserThread::UI);
285 if (!bitmap.get()) { 342 if (!bitmap.get()) {
286 // A thumbnail is not locally available for |url|. Make sure it is put in 343 // 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. 344 // the list to be fetched at the next visit to this site.
288 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); 345 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_));
289 if (top_sites) 346 if (top_sites)
290 top_sites->AddForcedURL(url, base::Time::Now()); 347 top_sites->AddForcedURL(url, base::Time::Now());
291 // Also fetch a remote thumbnail if possible. PopularSites or the 348 // Also fetch a remote thumbnail if possible. PopularSites or the
292 // SuggestionsService can supply a thumbnail download URL. 349 // SuggestionsService can supply a thumbnail download URL.
293 SuggestionsService* suggestions_service = 350 SuggestionsService* suggestions_service =
294 SuggestionsServiceFactory::GetForProfile(profile_); 351 SuggestionsServiceFactory::GetForProfile(profile_);
295 if (popular_sites_) { 352 if (popular_sites_) {
296 const std::vector<PopularSites::Site>& sites = popular_sites_->sites(); 353 const std::vector<PopularSites::Site>& sites = popular_sites_->sites();
297 auto it = std::find_if( 354 auto it = std::find_if(
298 sites.begin(), sites.end(), 355 sites.begin(), sites.end(),
299 [&url](const PopularSites::Site& site) { return site.url == url; }); 356 [&url](const PopularSites::Site& site) { return site.url == url; });
300 if (it != sites.end() && it->thumbnail_url.is_valid()) { 357 if (it != sites.end() && it->thumbnail_url.is_valid()) {
301 return suggestions_service->GetPageThumbnailWithURL( 358 return suggestions_service->GetPageThumbnailWithURL(
302 url, it->thumbnail_url, 359 url, it->thumbnail_url,
303 base::Bind(&MostVisitedSites::OnObtainedThumbnail, 360 base::Bind(&MostVisitedSites::OnObtainedThumbnail,
304 weak_ptr_factory_.GetWeakPtr(), false, 361 weak_ptr_factory_.GetWeakPtr(), false, callback));
305 base::Passed(&j_callback)));
306 } 362 }
307 } 363 }
308 if (mv_source_ == SUGGESTIONS_SERVICE) { 364 if (mv_source_ == SUGGESTIONS_SERVICE) {
309 return suggestions_service->GetPageThumbnail( 365 return suggestions_service->GetPageThumbnail(
310 url, base::Bind(&MostVisitedSites::OnObtainedThumbnail, 366 url, base::Bind(&MostVisitedSites::OnObtainedThumbnail,
311 weak_ptr_factory_.GetWeakPtr(), false, 367 weak_ptr_factory_.GetWeakPtr(), false, callback));
312 base::Passed(&j_callback)));
313 } 368 }
314 } 369 }
315 OnObtainedThumbnail(true, std::move(j_callback), url, bitmap.get()); 370 OnObtainedThumbnail(true, callback, url, bitmap.get());
316 } 371 }
317 372
318 void MostVisitedSites::OnObtainedThumbnail( 373 void MostVisitedSites::OnObtainedThumbnail(
319 bool is_local_thumbnail, 374 bool is_local_thumbnail,
320 std::unique_ptr<ScopedJavaGlobalRef<jobject>> j_callback, 375 const base::Callback<void(bool, const SkBitmap*)>& callback,
321 const GURL& url, 376 const GURL& url,
322 const SkBitmap* bitmap) { 377 const SkBitmap* bitmap) {
323 DCHECK_CURRENTLY_ON(BrowserThread::UI); 378 DCHECK_CURRENTLY_ON(BrowserThread::UI);
379 callback.Run(is_local_thumbnail, bitmap);
380 }
381
382 static void CallJavaWithBitmap(
383 std::unique_ptr<ScopedJavaGlobalRef<jobject>> j_callback,
384 bool is_local_thumbnail,
385 const SkBitmap* bitmap) {
324 JNIEnv* env = AttachCurrentThread(); 386 JNIEnv* env = AttachCurrentThread();
325 ScopedJavaLocalRef<jobject> j_bitmap; 387 ScopedJavaLocalRef<jobject> j_bitmap;
326 if (bitmap) 388 if (bitmap)
327 j_bitmap = gfx::ConvertToJavaBitmap(bitmap); 389 j_bitmap = gfx::ConvertToJavaBitmap(bitmap);
328 Java_ThumbnailCallback_onMostVisitedURLsThumbnailAvailable( 390 Java_ThumbnailCallback_onMostVisitedURLsThumbnailAvailable(
329 env, j_callback->obj(), j_bitmap.obj(), is_local_thumbnail); 391 env, j_callback->obj(), j_bitmap.obj(), is_local_thumbnail);
330 } 392 }
331 393
332 void MostVisitedSites::AddOrRemoveBlacklistedUrl( 394 void MostVisitedSites::AddOrRemoveBlacklistedUrl(
333 JNIEnv* env, 395 JNIEnv* env,
334 const JavaParamRef<jobject>& obj, 396 const JavaParamRef<jobject>& obj,
335 const JavaParamRef<jstring>& j_url, 397 const JavaParamRef<jstring>& j_url,
336 jboolean add_url) { 398 jboolean add_url) {
337 GURL url(ConvertJavaStringToUTF8(env, j_url)); 399 GURL url(ConvertJavaStringToUTF8(env, j_url));
400 AddOrRemoveBlacklistedUrl(url, add_url);
401 }
338 402
403 void MostVisitedSites::AddOrRemoveBlacklistedUrl(
404 const GURL& url, bool add_url) {
339 // Always blacklist in the local TopSites. 405 // Always blacklist in the local TopSites.
340 scoped_refptr<TopSites> top_sites = TopSitesFactory::GetForProfile(profile_); 406 scoped_refptr<TopSites> top_sites = TopSitesFactory::GetForProfile(profile_);
341 if (top_sites) { 407 if (top_sites) {
342 if (add_url) 408 if (add_url)
343 top_sites->AddBlacklistedURL(url); 409 top_sites->AddBlacklistedURL(url);
344 else 410 else
345 top_sites->RemoveBlacklistedURL(url); 411 top_sites->RemoveBlacklistedURL(url);
346 } 412 }
347 413
348 // Only blacklist in the server-side suggestions service if it's active. 414 // Only blacklist in the server-side suggestions service if it's active.
349 if (mv_source_ == SUGGESTIONS_SERVICE) { 415 if (mv_source_ == SUGGESTIONS_SERVICE) {
350 SuggestionsService* suggestions_service = 416 SuggestionsService* suggestions_service =
351 SuggestionsServiceFactory::GetForProfile(profile_); 417 SuggestionsServiceFactory::GetForProfile(profile_);
352 if (add_url) 418 if (add_url)
353 suggestions_service->BlacklistURL(url); 419 suggestions_service->BlacklistURL(url);
354 else 420 else
355 suggestions_service->UndoBlacklistURL(url); 421 suggestions_service->UndoBlacklistURL(url);
356 } 422 }
357 } 423 }
358 424
359 void MostVisitedSites::RecordTileTypeMetrics( 425 void MostVisitedSites::RecordTileTypeMetrics(
360 JNIEnv* env, 426 JNIEnv* env,
361 const JavaParamRef<jobject>& obj, 427 const JavaParamRef<jobject>& obj,
362 const JavaParamRef<jintArray>& jtile_types) { 428 const JavaParamRef<jintArray>& jtile_types) {
363 std::vector<int> tile_types; 429 std::vector<int> tile_types;
364 base::android::JavaIntArrayToIntVector(env, jtile_types, &tile_types); 430 base::android::JavaIntArrayToIntVector(env, jtile_types, &tile_types);
365 DCHECK_EQ(current_suggestions_.size(), tile_types.size()); 431 DCHECK_EQ(current_suggestions_.size(), tile_types.size());
432 RecordTileTypeMetrics(tile_types);
433 }
366 434
435 void MostVisitedSites::RecordTileTypeMetrics(
436 const std::vector<int>& tile_types) {
367 int counts_per_type[NUM_TILE_TYPES] = {0}; 437 int counts_per_type[NUM_TILE_TYPES] = {0};
368 for (size_t i = 0; i < tile_types.size(); ++i) { 438 for (size_t i = 0; i < tile_types.size(); ++i) {
369 int tile_type = tile_types[i]; 439 int tile_type = tile_types[i];
370 ++counts_per_type[tile_type]; 440 ++counts_per_type[tile_type];
371 std::string histogram = base::StringPrintf( 441 std::string histogram = base::StringPrintf(
372 "NewTabPage.TileType.%s", 442 "NewTabPage.TileType.%s",
373 current_suggestions_[i]->GetSourceHistogramName().c_str()); 443 current_suggestions_[i]->GetSourceHistogramName().c_str());
374 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES); 444 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES);
375 } 445 }
376 446
377 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsReal", 447 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsReal",
378 counts_per_type[ICON_REAL]); 448 counts_per_type[ICON_REAL]);
379 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsColor", 449 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsColor",
380 counts_per_type[ICON_COLOR]); 450 counts_per_type[ICON_COLOR]);
381 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsGray", 451 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsGray",
382 counts_per_type[ICON_DEFAULT]); 452 counts_per_type[ICON_DEFAULT]);
383 } 453 }
384 454
385 void MostVisitedSites::RecordOpenedMostVisitedItem( 455 void MostVisitedSites::RecordOpenedMostVisitedItem(
386 JNIEnv* env, 456 JNIEnv* env,
387 const JavaParamRef<jobject>& obj, 457 const JavaParamRef<jobject>& obj,
388 jint index, 458 jint index,
389 jint tile_type) { 459 jint tile_type) {
460 RecordOpenedMostVisitedItem(index, tile_type);
461 }
462
463 void MostVisitedSites::RecordOpenedMostVisitedItem(int index, int tile_type) {
390 DCHECK_GE(index, 0); 464 DCHECK_GE(index, 0);
391 DCHECK_LT(index, static_cast<int>(current_suggestions_.size())); 465 DCHECK_LT(index, static_cast<int>(current_suggestions_.size()));
392 std::string histogram = base::StringPrintf( 466 std::string histogram = base::StringPrintf(
393 "NewTabPage.MostVisited.%s", 467 "NewTabPage.MostVisited.%s",
394 current_suggestions_[index]->GetSourceHistogramName().c_str()); 468 current_suggestions_[index]->GetSourceHistogramName().c_str());
395 LogHistogramEvent(histogram, index, num_sites_); 469 LogHistogramEvent(histogram, index, num_sites_);
396 470
397 histogram = base::StringPrintf( 471 histogram = base::StringPrintf(
398 "NewTabPage.TileTypeClicked.%s", 472 "NewTabPage.TileTypeClicked.%s",
399 current_suggestions_[index]->GetSourceHistogramName().c_str()); 473 current_suggestions_[index]->GetSourceHistogramName().c_str());
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after
797 871
798 void MostVisitedSites::NotifyMostVisitedURLsObserver() { 872 void MostVisitedSites::NotifyMostVisitedURLsObserver() {
799 size_t num_suggestions = current_suggestions_.size(); 873 size_t num_suggestions = current_suggestions_.size();
800 if (received_most_visited_sites_ && received_popular_sites_ && 874 if (received_most_visited_sites_ && received_popular_sites_ &&
801 !recorded_uma_) { 875 !recorded_uma_) {
802 RecordImpressionUMAMetrics(); 876 RecordImpressionUMAMetrics();
803 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.NumberOfTiles", num_suggestions); 877 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.NumberOfTiles", num_suggestions);
804 recorded_uma_ = true; 878 recorded_uma_ = true;
805 } 879 }
806 880
807 if (observer_.is_null()) 881 if (!observer_.get())
Marc Treib 2016/04/14 16:57:35 nit: I think ".get()" isn't required.
sfiera 2016/04/15 13:36:00 Correct you are.
808 return; 882 return;
809 883
810 std::vector<base::string16> titles; 884 std::vector<base::string16> titles;
811 std::vector<std::string> urls; 885 std::vector<std::string> urls;
812 std::vector<std::string> whitelist_icon_paths; 886 std::vector<std::string> whitelist_icon_paths;
813 titles.reserve(num_suggestions); 887 titles.reserve(num_suggestions);
814 urls.reserve(num_suggestions); 888 urls.reserve(num_suggestions);
815 for (const auto& suggestion : current_suggestions_) { 889 for (const auto& suggestion : current_suggestions_) {
816 titles.push_back(suggestion->title); 890 titles.push_back(suggestion->title);
817 urls.push_back(suggestion->url.spec()); 891 urls.push_back(suggestion->url.spec());
818 whitelist_icon_paths.push_back(suggestion->whitelist_icon_path.value()); 892 whitelist_icon_paths.push_back(suggestion->whitelist_icon_path.value());
819 } 893 }
820 JNIEnv* env = AttachCurrentThread(); 894
821 DCHECK_EQ(titles.size(), urls.size()); 895 observer_->NotifyMostVisitedURLsObserver(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 } 896 }
827 897
828 void MostVisitedSites::OnPopularSitesAvailable(bool success) { 898 void MostVisitedSites::OnPopularSitesAvailable(bool success) {
829 received_popular_sites_ = true; 899 received_popular_sites_ = true;
830 900
831 if (!success) { 901 if (!success) {
832 LOG(WARNING) << "Download of popular sites failed"; 902 LOG(WARNING) << "Download of popular sites failed";
833 return; 903 return;
834 } 904 }
835 905
836 if (observer_.is_null()) 906 if (!observer_.get())
837 return; 907 return;
838 908
839 std::vector<std::string> urls; 909 std::vector<std::string> urls;
840 std::vector<std::string> favicon_urls; 910 std::vector<std::string> favicon_urls;
841 std::vector<std::string> large_icon_urls; 911 std::vector<std::string> large_icon_urls;
842 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { 912 for (const PopularSites::Site& popular_site : popular_sites_->sites()) {
843 urls.push_back(popular_site.url.spec()); 913 urls.push_back(popular_site.url.spec());
844 favicon_urls.push_back(popular_site.favicon_url.spec()); 914 favicon_urls.push_back(popular_site.favicon_url.spec());
845 large_icon_urls.push_back(popular_site.large_icon_url.spec()); 915 large_icon_urls.push_back(popular_site.large_icon_url.spec());
846 } 916 }
847 JNIEnv* env = AttachCurrentThread(); 917 observer_->OnPopularSitesAvailable(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(); 918 QueryMostVisitedURLs();
853 } 919 }
854 920
855 void MostVisitedSites::RecordImpressionUMAMetrics() { 921 void MostVisitedSites::RecordImpressionUMAMetrics() {
856 for (size_t i = 0; i < current_suggestions_.size(); i++) { 922 for (size_t i = 0; i < current_suggestions_.size(); i++) {
857 std::string histogram = base::StringPrintf( 923 std::string histogram = base::StringPrintf(
858 "NewTabPage.SuggestionsImpression.%s", 924 "NewTabPage.SuggestionsImpression.%s",
859 current_suggestions_[i]->GetSourceHistogramName().c_str()); 925 current_suggestions_[i]->GetSourceHistogramName().c_str());
860 LogHistogramEvent(histogram, static_cast<int>(i), num_sites_); 926 LogHistogramEvent(histogram, static_cast<int>(i), num_sites_);
861 } 927 }
862 } 928 }
863 929
864 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} 930 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {}
865 931
866 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, 932 void MostVisitedSites::TopSitesChanged(TopSites* top_sites,
867 ChangeReason change_reason) { 933 ChangeReason change_reason) {
868 if (mv_source_ == TOP_SITES) { 934 if (mv_source_ == TOP_SITES) {
869 // The displayed suggestions are invalidated. 935 // The displayed suggestions are invalidated.
870 InitiateTopSitesQuery(); 936 InitiateTopSitesQuery();
871 } 937 }
872 } 938 }
873 939
874 static jlong Init(JNIEnv* env, 940 static jlong Init(JNIEnv* env,
875 const JavaParamRef<jobject>& obj, 941 const JavaParamRef<jobject>& obj,
876 const JavaParamRef<jobject>& jprofile) { 942 const JavaParamRef<jobject>& jprofile) {
877 MostVisitedSites* most_visited_sites = 943 MostVisitedSites* most_visited_sites =
878 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); 944 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile));
879 return reinterpret_cast<intptr_t>(most_visited_sites); 945 return reinterpret_cast<intptr_t>(most_visited_sites);
880 } 946 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698