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
3 // found in the LICENSE file.
5 #include "chrome/browser/android/most_visited_sites.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_array.h"
9 #include "base/android/jni_string.h"
10 #include "base/android/scoped_java_ref.h"
11 #include "base/callback.h"
12 #include "base/command_line.h"
13 #include "base/metrics/histogram.h"
14 #include "base/metrics/sparse_histogram.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/time/time.h"
19 #include "chrome/browser/android/popular_sites.h"
20 #include "chrome/browser/history/top_sites_factory.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/profiles/profile_android.h"
23 #include "chrome/browser/search/suggestions/suggestions_service_factory.h"
24 #include "chrome/browser/search/suggestions/suggestions_source.h"
25 #include "chrome/browser/sync/profile_sync_service.h"
26 #include "chrome/browser/sync/profile_sync_service_factory.h"
27 #include "chrome/browser/thumbnails/thumbnail_list_source.h"
28 #include "chrome/common/chrome_switches.h"
29 #include "components/history/core/browser/top_sites.h"
30 #include "components/suggestions/suggestions_service.h"
31 #include "components/suggestions/suggestions_utils.h"
32 #include "content/public/browser/browser_thread.h"
33 #include "content/public/browser/url_data_source.h"
34 #include "jni/MostVisitedSites_jni.h"
35 #include "third_party/skia/include/core/SkBitmap.h"
36 #include "ui/gfx/android/java_bitmap.h"
37 #include "ui/gfx/codec/jpeg_codec.h"
40 using base::android::AttachCurrentThread
;
41 using base::android::ConvertJavaStringToUTF8
;
42 using base::android::ScopedJavaGlobalRef
;
43 using base::android::ScopedJavaLocalRef
;
44 using base::android::ToJavaArrayOfStrings
;
45 using content::BrowserThread
;
46 using history::TopSites
;
47 using suggestions::ChromeSuggestion
;
48 using suggestions::SuggestionsProfile
;
49 using suggestions::SuggestionsService
;
50 using suggestions::SuggestionsServiceFactory
;
51 using suggestions::SyncState
;
55 // Total number of tiles displayed.
56 const char kNumTilesHistogramName
[] = "NewTabPage.NumberOfTiles";
57 // Tracking thumbnails.
58 const char kNumLocalThumbnailTilesHistogramName
[] =
59 "NewTabPage.NumberOfThumbnailTiles";
60 const char kNumEmptyTilesHistogramName
[] = "NewTabPage.NumberOfGrayTiles";
61 const char kNumServerTilesHistogramName
[] = "NewTabPage.NumberOfExternalTiles";
63 // Format for tile clicks histogram.
64 const char kOpenedItemHistogramFormat
[] = "NewTabPage.MostVisited.%s";
65 // Format for tile impressions histogram.
66 const char kImpressionHistogramFormat
[] = "NewTabPage.SuggestionsImpression.%s";
67 // Identifiers for the various tile sources.
68 const char kHistogramClientName
[] = "client";
69 const char kHistogramServerName
[] = "server";
70 const char kHistogramServerFormat
[] = "server%d";
71 const char kHistogramPopularName
[] = "popular";
73 const char kPopularSitesFieldTrialName
[] = "NTPPopularSites";
75 scoped_ptr
<SkBitmap
> MaybeFetchLocalThumbnail(
77 const scoped_refptr
<TopSites
>& top_sites
) {
78 DCHECK_CURRENTLY_ON(BrowserThread::DB
);
79 scoped_refptr
<base::RefCountedMemory
> image
;
80 scoped_ptr
<SkBitmap
> bitmap
;
81 if (top_sites
&& top_sites
->GetPageThumbnail(url
, false, &image
))
82 bitmap
.reset(gfx::JPEGCodec::Decode(image
->front(), image
->size()));
86 // Log an event for a given |histogram| at a given element |position|. This
87 // routine exists because regular histogram macros are cached thus can't be used
88 // if the name of the histogram will change at a given call site.
89 void LogHistogramEvent(const std::string
& histogram
,
92 base::HistogramBase
* counter
= base::LinearHistogram::FactoryGet(
97 base::Histogram::kUmaTargetedHistogramFlag
);
99 counter
->Add(position
);
102 // Return the current SyncState for use with the SuggestionsService.
103 SyncState
GetSyncState(Profile
* profile
) {
104 ProfileSyncService
* sync
=
105 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile
);
107 return SyncState::SYNC_OR_HISTORY_SYNC_DISABLED
;
108 return suggestions::GetSyncState(
109 sync
->CanSyncStart(),
110 sync
->IsSyncActive() && sync
->ConfigurationDone(),
111 sync
->GetActiveDataTypes().Has(syncer::HISTORY_DELETE_DIRECTIVES
));
114 bool ShouldShowPopularSites() {
115 // Note: It's important to query the field trial state first, to ensure that
116 // UMA reports the correct group.
117 const std::string group_name
=
118 base::FieldTrialList::FindFullName(kPopularSitesFieldTrialName
);
119 base::CommandLine
* cmd_line
= base::CommandLine::ForCurrentProcess();
120 if (cmd_line
->HasSwitch(switches::kDisableNTPPopularSites
))
122 if (cmd_line
->HasSwitch(switches::kEnableNTPPopularSites
))
124 return group_name
== "Enabled";
127 std::string
GetPopularSitesFilename() {
128 return variations::GetVariationParamValue(kPopularSitesFieldTrialName
,
134 MostVisitedSites::MostVisitedSites(Profile
* profile
)
135 : profile_(profile
), num_sites_(0), received_most_visited_sites_(false),
136 received_popular_sites_(false), recorded_uma_(false),
137 num_local_thumbs_(0), num_server_thumbs_(0), num_empty_thumbs_(0),
138 scoped_observer_(this), weak_ptr_factory_(this) {
139 // Register the debugging page for the Suggestions Service and the thumbnails
141 content::URLDataSource::Add(profile_
,
142 new suggestions::SuggestionsSource(profile_
));
143 content::URLDataSource::Add(profile_
, new ThumbnailListSource(profile_
));
145 // Register this class as an observer to the sync service. It is important to
146 // be notified of changes in the sync state such as initialization, sync
147 // being enabled or disabled, etc.
148 ProfileSyncService
* profile_sync_service
=
149 ProfileSyncServiceFactory::GetForProfile(profile_
);
150 if (profile_sync_service
)
151 profile_sync_service
->AddObserver(this);
153 if (ShouldShowPopularSites()) {
154 popular_sites_
.reset(new PopularSites(
155 GetPopularSitesFilename(),
156 profile_
->GetRequestContext(),
157 base::Bind(&MostVisitedSites::OnPopularSitesAvailable
,
158 base::Unretained(this))));
160 received_popular_sites_
= true;
164 MostVisitedSites::~MostVisitedSites() {
165 ProfileSyncService
* profile_sync_service
=
166 ProfileSyncServiceFactory::GetForProfile(profile_
);
167 if (profile_sync_service
&& profile_sync_service
->HasObserver(this))
168 profile_sync_service
->RemoveObserver(this);
171 void MostVisitedSites::Destroy(JNIEnv
* env
, jobject obj
) {
175 void MostVisitedSites::OnLoadingComplete(JNIEnv
* env
, jobject obj
) {
176 RecordThumbnailUMAMetrics();
179 void MostVisitedSites::SetMostVisitedURLsObserver(JNIEnv
* env
,
183 observer_
.Reset(env
, j_observer
);
184 num_sites_
= num_sites
;
186 QueryMostVisitedURLs();
188 scoped_refptr
<history::TopSites
> top_sites
=
189 TopSitesFactory::GetForProfile(profile_
);
191 // TopSites updates itself after a delay. To ensure up-to-date results,
192 // force an update now.
193 top_sites
->SyncWithHistory();
195 // Register as TopSitesObserver so that we can update ourselves when the
197 scoped_observer_
.Add(top_sites
.get());
201 void MostVisitedSites::GetURLThumbnail(JNIEnv
* env
,
204 jobject j_callback_obj
) {
205 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
206 scoped_ptr
<ScopedJavaGlobalRef
<jobject
>> j_callback(
207 new ScopedJavaGlobalRef
<jobject
>());
208 j_callback
->Reset(env
, j_callback_obj
);
210 GURL
url(ConvertJavaStringToUTF8(env
, j_url
));
211 scoped_refptr
<TopSites
> top_sites(TopSitesFactory::GetForProfile(profile_
));
213 BrowserThread::PostTaskAndReplyWithResult(
214 BrowserThread::DB
, FROM_HERE
,
215 base::Bind(&MaybeFetchLocalThumbnail
, url
, top_sites
),
216 base::Bind(&MostVisitedSites::OnLocalThumbnailFetched
,
217 weak_ptr_factory_
.GetWeakPtr(), url
,
218 base::Passed(&j_callback
)));
221 void MostVisitedSites::OnLocalThumbnailFetched(
223 scoped_ptr
<ScopedJavaGlobalRef
<jobject
>> j_callback
,
224 scoped_ptr
<SkBitmap
> bitmap
) {
225 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
227 // A thumbnail is not locally available for |url|. Make sure it is put in
228 // the list to be fetched at the next visit to this site.
229 scoped_refptr
<TopSites
> top_sites(TopSitesFactory::GetForProfile(profile_
));
231 top_sites
->AddForcedURL(url
, base::Time::Now());
232 SuggestionsService
* suggestions_service
=
233 (mv_source_
== SUGGESTIONS_SERVICE
)
234 ? SuggestionsServiceFactory::GetForProfile(profile_
)
236 if (suggestions_service
) {
237 return suggestions_service
->GetPageThumbnail(
238 url
, base::Bind(&MostVisitedSites::OnObtainedThumbnail
,
239 weak_ptr_factory_
.GetWeakPtr(), false,
240 base::Passed(&j_callback
)));
243 OnObtainedThumbnail(true, j_callback
.Pass(), url
, bitmap
.get());
246 void MostVisitedSites::OnObtainedThumbnail(
247 bool is_local_thumbnail
,
248 scoped_ptr
<ScopedJavaGlobalRef
<jobject
>> j_callback
,
250 const SkBitmap
* bitmap
) {
251 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
252 JNIEnv
* env
= AttachCurrentThread();
253 ScopedJavaLocalRef
<jobject
> j_bitmap
;
255 j_bitmap
= gfx::ConvertToJavaBitmap(bitmap
);
256 if (is_local_thumbnail
) {
259 ++num_server_thumbs_
;
264 Java_ThumbnailCallback_onMostVisitedURLsThumbnailAvailable(
265 env
, j_callback
->obj(), j_bitmap
.obj());
268 void MostVisitedSites::BlacklistUrl(JNIEnv
* env
,
271 GURL
url(ConvertJavaStringToUTF8(env
, j_url
));
273 // Always blacklist in the local TopSites.
274 scoped_refptr
<TopSites
> top_sites
= TopSitesFactory::GetForProfile(profile_
);
276 top_sites
->AddBlacklistedURL(url
);
278 // Only blacklist in the server-side suggestions service if it's active.
279 if (mv_source_
== SUGGESTIONS_SERVICE
) {
280 SuggestionsService
* suggestions_service
=
281 SuggestionsServiceFactory::GetForProfile(profile_
);
282 DCHECK(suggestions_service
);
283 suggestions_service
->BlacklistURL(
284 url
, base::Bind(&MostVisitedSites::OnSuggestionsProfileAvailable
,
285 weak_ptr_factory_
.GetWeakPtr()),
290 void MostVisitedSites::RecordOpenedMostVisitedItem(JNIEnv
* env
,
294 DCHECK_LT(index
, static_cast<int>(tile_sources_
.size()));
295 std::string histogram
= base::StringPrintf(kOpenedItemHistogramFormat
,
296 tile_sources_
[index
].c_str());
297 LogHistogramEvent(histogram
, index
, num_sites_
);
300 void MostVisitedSites::OnStateChanged() {
301 // There have been changes to the sync state. This class cares about a few
302 // (just initialized, enabled/disabled or history sync state changed). Re-run
303 // the query code which will use the proper state.
304 QueryMostVisitedURLs();
308 bool MostVisitedSites::Register(JNIEnv
* env
) {
309 return RegisterNativesImpl(env
);
312 void MostVisitedSites::QueryMostVisitedURLs() {
313 SuggestionsService
* suggestions_service
=
314 SuggestionsServiceFactory::GetForProfile(profile_
);
315 if (suggestions_service
) {
316 // Suggestions service is enabled, initiate a query.
317 suggestions_service
->FetchSuggestionsData(
318 GetSyncState(profile_
),
319 base::Bind(&MostVisitedSites::OnSuggestionsProfileAvailable
,
320 weak_ptr_factory_
.GetWeakPtr()));
322 InitiateTopSitesQuery();
326 void MostVisitedSites::InitiateTopSitesQuery() {
327 scoped_refptr
<TopSites
> top_sites
= TopSitesFactory::GetForProfile(profile_
);
331 top_sites
->GetMostVisitedURLs(
332 base::Bind(&MostVisitedSites::OnMostVisitedURLsAvailable
,
333 weak_ptr_factory_
.GetWeakPtr()),
337 void MostVisitedSites::OnMostVisitedURLsAvailable(
338 const history::MostVisitedURLList
& visited_list
) {
339 std::vector
<base::string16
> titles
;
340 std::vector
<std::string
> urls
;
341 tile_sources_
.clear();
342 int num_tiles
= std::min(static_cast<int>(visited_list
.size()), num_sites_
);
343 for (int i
= 0; i
< num_tiles
; ++i
) {
344 const history::MostVisitedURL
& visited
= visited_list
[i
];
345 if (visited
.url
.is_empty()) {
347 break; // This is the signal that there are no more real visited sites.
349 titles
.push_back(visited
.title
);
350 urls
.push_back(visited
.url
.spec());
351 tile_sources_
.push_back(kHistogramClientName
);
354 received_most_visited_sites_
= true;
355 mv_source_
= TOP_SITES
;
356 AddPopularSites(&titles
, &urls
);
357 NotifyMostVisitedURLsObserver(titles
, urls
);
360 void MostVisitedSites::OnSuggestionsProfileAvailable(
361 const SuggestionsProfile
& suggestions_profile
) {
362 int num_tiles
= suggestions_profile
.suggestions_size();
363 // With no server suggestions, fall back to local Most Visited.
364 if (num_tiles
== 0) {
365 InitiateTopSitesQuery();
368 if (num_sites_
< num_tiles
)
369 num_tiles
= num_sites_
;
371 std::vector
<base::string16
> titles
;
372 std::vector
<std::string
> urls
;
373 tile_sources_
.clear();
374 for (int i
= 0; i
< num_tiles
; ++i
) {
375 const ChromeSuggestion
& suggestion
= suggestions_profile
.suggestions(i
);
376 titles
.push_back(base::UTF8ToUTF16(suggestion
.title()));
377 urls
.push_back(suggestion
.url());
378 std::string tile_source
;
379 if (suggestion
.providers_size() > 0) {
381 base::StringPrintf(kHistogramServerFormat
, suggestion
.providers(0));
383 tile_source
= kHistogramServerName
;
385 tile_sources_
.push_back(tile_source
);
388 received_most_visited_sites_
= true;
389 mv_source_
= SUGGESTIONS_SERVICE
;
390 AddPopularSites(&titles
, &urls
);
391 NotifyMostVisitedURLsObserver(titles
, urls
);
394 void MostVisitedSites::AddPopularSites(std::vector
<base::string16
>* titles
,
395 std::vector
<std::string
>* urls
) {
399 DCHECK_EQ(titles
->size(), urls
->size());
400 DCHECK_EQ(titles
->size(), tile_sources_
.size());
401 DCHECK_LE(static_cast<int>(titles
->size()), num_sites_
);
403 // Collect all non-blacklisted popular suggestions.
404 std::vector
<base::string16
> popular_titles
;
405 std::vector
<std::string
> popular_urls
;
406 scoped_refptr
<TopSites
> top_sites(TopSitesFactory::GetForProfile(profile_
));
407 for (const PopularSites::Site
& popular_site
: popular_sites_
->sites()) {
408 // Skip blacklisted sites.
409 if (top_sites
&& top_sites
->IsBlacklisted(popular_site
.url
))
412 popular_titles
.push_back(popular_site
.title
);
413 popular_urls
.push_back(popular_site
.url
.spec());
414 if (static_cast<int>(popular_titles
.size()) >= num_sites_
)
419 num_sites_
, popular_titles
, popular_urls
, titles
, urls
, &tile_sources_
);
423 void MostVisitedSites::AddPopularSitesImpl(
425 const std::vector
<base::string16
>& popular_titles
,
426 const std::vector
<std::string
>& popular_urls
,
427 std::vector
<base::string16
>* titles
,
428 std::vector
<std::string
>* urls
,
429 std::vector
<std::string
>* tile_sources
) {
430 // Start off with the popular suggestions.
431 std::vector
<base::string16
> new_titles(popular_titles
);
432 std::vector
<std::string
> new_urls(popular_urls
);
433 std::vector
<std::string
> new_tile_sources(new_titles
.size(),
434 kHistogramPopularName
);
436 // Now, go over the personalized suggestions and replace matching popular
437 // suggestions. This is so that when some of the popular suggestions become
438 // personal, they retain their absolute positions.
439 std::vector
<base::string16
> titles_to_insert
;
440 std::vector
<std::string
> urls_to_insert
;
441 std::vector
<std::string
> tile_sources_to_insert
;
442 for (size_t site_index
= 0; site_index
< titles
->size(); site_index
++) {
443 const base::string16
& title
= (*titles
)[site_index
];
444 const std::string
& url
= (*urls
)[site_index
];
445 const std::string
& tile_source
= (*tile_sources
)[site_index
];
446 // See if we already have a matching popular site.
448 for (size_t i
= 0; i
< new_urls
.size(); i
++) {
449 if (new_tile_sources
[i
] == kHistogramPopularName
&&
450 GURL(new_urls
[i
]).host() == GURL(url
).host()) {
451 // We have a matching popular sites suggestion. Replace it with the
452 // actual URL and title.
453 new_titles
[i
] = title
;
455 new_tile_sources
[i
] = tile_source
;
461 titles_to_insert
.push_back(title
);
462 urls_to_insert
.push_back(url
);
463 tile_sources_to_insert
.push_back(tile_source
);
467 // Append personalized suggestions at the end if there's room.
468 size_t num_to_append
=
469 std::min(static_cast<size_t>(num_sites
) - new_titles
.size(),
470 titles_to_insert
.size());
471 new_titles
.insert(new_titles
.end(),
472 titles_to_insert
.end() - num_to_append
,
473 titles_to_insert
.end());
474 new_urls
.insert(new_urls
.end(),
475 urls_to_insert
.end() - num_to_append
,
476 urls_to_insert
.end());
477 new_tile_sources
.insert(new_tile_sources
.end(),
478 tile_sources_to_insert
.end() - num_to_append
,
479 tile_sources_to_insert
.end());
481 // Finally, go over the remaining personalized suggestions and evict popular
482 // suggestions to accommodate them. Do it in reverse order, so the least
483 // important popular suggestions will be evicted.
484 for (size_t i
= titles_to_insert
.size() - num_to_append
; i
> 0; --i
) {
485 const base::string16
& title
= titles_to_insert
[i
- 1];
486 const std::string
& url
= urls_to_insert
[i
- 1];
487 const std::string
& tile_source
= tile_sources_to_insert
[i
- 1];
488 for (size_t insert_i
= new_titles
.size(); insert_i
> 0; --insert_i
) {
489 size_t insert_index
= insert_i
- 1;
490 if (new_tile_sources
[insert_index
] == kHistogramPopularName
) {
491 new_titles
[insert_index
] = title
;
492 new_urls
[insert_index
] = url
;
493 new_tile_sources
[insert_index
] = tile_source
;
499 titles
->swap(new_titles
);
500 urls
->swap(new_urls
);
501 tile_sources
->swap(new_tile_sources
);
504 void MostVisitedSites::NotifyMostVisitedURLsObserver(
505 const std::vector
<base::string16
>& titles
,
506 const std::vector
<std::string
>& urls
) {
507 DCHECK_EQ(titles
.size(), urls
.size());
508 if (received_most_visited_sites_
&& received_popular_sites_
&&
510 RecordImpressionUMAMetrics();
511 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumTilesHistogramName
, titles
.size());
512 recorded_uma_
= true;
514 JNIEnv
* env
= AttachCurrentThread();
515 Java_MostVisitedURLsObserver_onMostVisitedURLsAvailable(
516 env
, observer_
.obj(), ToJavaArrayOfStrings(env
, titles
).obj(),
517 ToJavaArrayOfStrings(env
, urls
).obj());
520 void MostVisitedSites::OnPopularSitesAvailable(bool success
) {
521 received_popular_sites_
= true;
524 LOG(WARNING
) << "Download of popular sites failed";
528 if (observer_
.is_null())
531 std::vector
<std::string
> urls
;
532 std::vector
<std::string
> favicon_urls
;
533 for (const PopularSites::Site
& popular_site
: popular_sites_
->sites()) {
534 urls
.push_back(popular_site
.url
.spec());
535 favicon_urls
.push_back(popular_site
.favicon_url
.spec());
537 JNIEnv
* env
= AttachCurrentThread();
538 Java_MostVisitedURLsObserver_onPopularURLsAvailable(
539 env
, observer_
.obj(), ToJavaArrayOfStrings(env
, urls
).obj(),
540 ToJavaArrayOfStrings(env
, favicon_urls
).obj());
542 QueryMostVisitedURLs();
545 void MostVisitedSites::RecordThumbnailUMAMetrics() {
546 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumLocalThumbnailTilesHistogramName
,
548 num_local_thumbs_
= 0;
549 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumEmptyTilesHistogramName
, num_empty_thumbs_
);
550 num_empty_thumbs_
= 0;
551 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumServerTilesHistogramName
, num_server_thumbs_
);
552 num_server_thumbs_
= 0;
555 void MostVisitedSites::RecordImpressionUMAMetrics() {
556 for (size_t i
= 0; i
< tile_sources_
.size(); i
++) {
557 std::string histogram
= base::StringPrintf(kImpressionHistogramFormat
,
558 tile_sources_
[i
].c_str());
559 LogHistogramEvent(histogram
, static_cast<int>(i
), num_sites_
);
563 void MostVisitedSites::TopSitesLoaded(history::TopSites
* top_sites
) {
566 void MostVisitedSites::TopSitesChanged(history::TopSites
* top_sites
,
567 ChangeReason change_reason
) {
568 if (mv_source_
== TOP_SITES
) {
569 // The displayed suggestions are invalidated.
570 QueryMostVisitedURLs();
574 static jlong
Init(JNIEnv
* env
, jobject obj
, jobject jprofile
) {
575 MostVisitedSites
* most_visited_sites
=
576 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile
));
577 return reinterpret_cast<intptr_t>(most_visited_sites
);