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 #ifndef CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_
6 #define CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_
10 #include "base/android/scoped_java_ref.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/scoped_observer.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "components/history/core/browser/history_types.h"
16 #include "components/history/core/browser/top_sites_observer.h"
17 #include "components/suggestions/proto/suggestions.pb.h"
18 #include "components/sync_driver/sync_service_observer.h"
20 namespace suggestions
{
21 class SuggestionsService
;
24 // Provides the list of most visited sites and their thumbnails to Java.
25 class MostVisitedSites
: public sync_driver::SyncServiceObserver
,
26 public history::TopSitesObserver
{
28 explicit MostVisitedSites(Profile
* profile
);
29 void Destroy(JNIEnv
* env
, jobject obj
);
30 void OnLoadingComplete(JNIEnv
* env
, jobject obj
);
31 void SetMostVisitedURLsObserver(JNIEnv
* env
,
35 void GetURLThumbnail(JNIEnv
* env
,
40 void BlacklistUrl(JNIEnv
* env
, jobject obj
, jstring j_url
);
41 void RecordOpenedMostVisitedItem(JNIEnv
* env
, jobject obj
, jint index
);
43 // sync_driver::SyncServiceObserver implementation.
44 void OnStateChanged() override
;
46 // Registers JNI methods.
47 static bool Register(JNIEnv
* env
);
50 // The source of the Most Visited sites.
51 enum MostVisitedSource
{
56 ~MostVisitedSites() override
;
57 void QueryMostVisitedURLs();
59 // Initialize the query to Top Sites. Called if the SuggestionsService is not
60 // enabled, or if it returns no data.
61 void InitiateTopSitesQuery();
63 // Callback for when data is available from TopSites.
64 void OnMostVisitedURLsAvailable(
65 const history::MostVisitedURLList
& visited_list
);
67 // Callback for when data is available from the SuggestionsService.
68 void OnSuggestionsProfileAvailable(
69 const suggestions::SuggestionsProfile
& suggestions_profile
);
71 // Notify the Java side observer about the availability of Most Visited Urls.
72 void NotifyMostVisitedURLsObserver(const std::vector
<base::string16
>& titles
,
73 const std::vector
<std::string
>& urls
);
75 // Runs on the UI Thread.
76 void OnLocalThumbnailFetched(
78 scoped_ptr
<base::android::ScopedJavaGlobalRef
<jobject
>> j_callback
,
79 scoped_ptr
<SkBitmap
> bitmap
);
81 // Callback for when the thumbnail lookup is complete.
82 // Runs on the UI Thread.
83 void OnObtainedThumbnail(
84 bool is_local_thumbnail
,
85 scoped_ptr
<base::android::ScopedJavaGlobalRef
<jobject
>> j_callback
,
87 const SkBitmap
* bitmap
);
89 // Records specific UMA histogram metrics.
90 void RecordUMAMetrics();
92 // history::TopSitesObserver implementation.
93 void TopSitesLoaded(history::TopSites
* top_sites
) override
;
94 void TopSitesChanged(history::TopSites
* top_sites
) override
;
96 // The profile whose most visited sites will be queried.
99 // The observer to be notified when the list of most visited sites changes.
100 base::android::ScopedJavaGlobalRef
<jobject
> observer_
;
102 // The maximum number of most visited sites to return.
105 // Keeps track of whether the initial NTP load has been done.
106 bool initial_load_done_
;
108 // Counters for UMA metrics.
110 // Number of tiles using a local thumbnail image for this NTP session.
111 int num_local_thumbs_
;
112 // Number of tiles for which a server thumbnail is provided.
113 int num_server_thumbs_
;
114 // Number of tiles for which no thumbnail is found/specified.
115 // In this case a gray tile is used as the main tile.
116 int num_empty_thumbs_
;
118 // Copy of the server suggestions (if enabled). Used for logging.
119 suggestions::SuggestionsProfile server_suggestions_
;
121 ScopedObserver
<history::TopSites
, history::TopSitesObserver
> scoped_observer_
;
123 MostVisitedSource mv_source_
;
125 // For callbacks may be run after destruction.
126 base::WeakPtrFactory
<MostVisitedSites
> weak_ptr_factory_
;
128 DISALLOW_COPY_AND_ASSIGN(MostVisitedSites
);
131 #endif // CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_