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_
13 #include "base/android/scoped_java_ref.h"
14 #include "base/compiler_specific.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/scoped_observer.h"
18 #include "components/history/core/browser/history_types.h"
19 #include "components/history/core/browser/top_sites_observer.h"
20 #include "components/suggestions/proto/suggestions.pb.h"
21 #include "components/sync_driver/sync_service_observer.h"
24 namespace suggestions
{
25 class SuggestionsService
;
28 namespace user_prefs
{
29 class PrefRegistrySyncable
;
35 // Provides the list of most visited sites and their thumbnails to Java.
36 class MostVisitedSites
: public sync_driver::SyncServiceObserver
,
37 public history::TopSitesObserver
{
39 explicit MostVisitedSites(Profile
* profile
);
40 void Destroy(JNIEnv
* env
, jobject obj
);
41 void OnLoadingComplete(JNIEnv
* env
, jobject obj
);
42 void SetMostVisitedURLsObserver(JNIEnv
* env
,
46 void GetURLThumbnail(JNIEnv
* env
,
51 void BlacklistUrl(JNIEnv
* env
, jobject obj
, jstring j_url
);
52 void RecordOpenedMostVisitedItem(JNIEnv
* env
, jobject obj
, jint index
);
54 // sync_driver::SyncServiceObserver implementation.
55 void OnStateChanged() override
;
57 // Registers JNI methods.
58 static bool Register(JNIEnv
* env
);
60 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
63 friend class MostVisitedSitesTest
;
65 // The source of the Most Visited sites.
66 enum MostVisitedSource
{ TOP_SITES
, SUGGESTIONS_SERVICE
, POPULAR
};
71 MostVisitedSource source
;
72 // Only valid for source == SUGGESTIONS_SERVICE (-1 otherwise).
75 Suggestion(const base::string16
& title
,
76 const std::string
& url
,
77 MostVisitedSource source
);
78 Suggestion(const base::string16
& title
,
80 MostVisitedSource source
);
81 Suggestion(const base::string16
& title
,
82 const std::string
& url
,
83 MostVisitedSource source
,
87 // Get the Histogram name associated with the source.
88 std::string
GetSourceHistogramName() const;
91 DISALLOW_COPY_AND_ASSIGN(Suggestion
);
94 ~MostVisitedSites() override
;
95 void QueryMostVisitedURLs();
97 // Initialize the query to Top Sites. Called if the SuggestionsService is not
98 // enabled, or if it returns no data.
99 void InitiateTopSitesQuery();
101 // Callback for when data is available from TopSites.
102 void OnMostVisitedURLsAvailable(
103 const history::MostVisitedURLList
& visited_list
);
105 // Callback for when data is available from the SuggestionsService.
106 void OnSuggestionsProfileAvailable(
107 const suggestions::SuggestionsProfile
& suggestions_profile
);
109 // Takes the personal suggestions and adds popular suggestions if necessary
110 // and reorders the suggestions based on the previously displayed order.
111 void AddPopularSites(ScopedVector
<Suggestion
>* suggestions
);
113 // Workhorse for AddPopularSites above. Implemented as a separate static
114 // method for ease of testing.
115 static ScopedVector
<Suggestion
> MergeSuggestions(
116 ScopedVector
<Suggestion
>* personal_suggestions
,
117 ScopedVector
<Suggestion
>* popular_suggestions
,
118 const std::vector
<std::string
>& old_sites_url
,
119 const std::vector
<bool>& old_sites_is_personal
);
121 void GetPreviousNTPSites(size_t num_tiles
,
122 std::vector
<std::string
>* old_sites_url
,
123 std::vector
<bool>* old_sites_source
) const;
125 void SaveCurrentNTPSites();
127 // Takes suggestions from |src_suggestions| and moves them to
128 // |dst_suggestions| if the suggestion's url/host matches
129 // |match_urls|/|match_hosts| respectively. Unmatched suggestion indices from
130 // |src_suggestions| are returned for ease of insertion later.
131 static std::vector
<size_t> InsertMatchingSuggestions(
132 ScopedVector
<Suggestion
>* src_suggestions
,
133 ScopedVector
<Suggestion
>* dst_suggestions
,
134 const std::vector
<std::string
>& match_urls
,
135 const std::vector
<std::string
>& match_hosts
);
137 // Inserts suggestions from |src_suggestions| at positions |insert_positions|
138 // into |dst_suggestions| where ever empty starting from |start_position|.
139 // Returns the last filled position so that future insertions can start from
141 static size_t InsertAllSuggestions(
142 size_t start_position
,
143 const std::vector
<size_t>& insert_positions
,
144 ScopedVector
<Suggestion
>* src_suggestions
,
145 ScopedVector
<Suggestion
>* dst_suggestions
);
147 // Notify the Java side observer about the availability of Most Visited Urls.
148 void NotifyMostVisitedURLsObserver();
150 void OnPopularSitesAvailable(bool success
);
152 // Runs on the UI Thread.
153 void OnLocalThumbnailFetched(
155 scoped_ptr
<base::android::ScopedJavaGlobalRef
<jobject
>> j_callback
,
156 scoped_ptr
<SkBitmap
> bitmap
);
158 // Callback for when the thumbnail lookup is complete.
159 // Runs on the UI Thread.
160 void OnObtainedThumbnail(
161 bool is_local_thumbnail
,
162 scoped_ptr
<base::android::ScopedJavaGlobalRef
<jobject
>> j_callback
,
164 const SkBitmap
* bitmap
);
166 // Records thumbnail-related UMA histogram metrics.
167 void RecordThumbnailUMAMetrics();
169 // Records UMA histogram metrics related to the number of impressions.
170 void RecordImpressionUMAMetrics();
172 // history::TopSitesObserver implementation.
173 void TopSitesLoaded(history::TopSites
* top_sites
) override
;
174 void TopSitesChanged(history::TopSites
* top_sites
,
175 ChangeReason change_reason
) override
;
177 // The profile whose most visited sites will be queried.
180 // The observer to be notified when the list of most visited sites changes.
181 base::android::ScopedJavaGlobalRef
<jobject
> observer_
;
183 // The maximum number of most visited sites to return.
186 // Whether we have received an initial set of most visited sites (from either
187 // TopSites or the SuggestionsService).
188 bool received_most_visited_sites_
;
190 // Whether we have received the set of popular sites. Immediately set to true
191 // if popular sites are disabled.
192 bool received_popular_sites_
;
194 // Whether we have recorded one-shot UMA metrics such as impressions. They are
195 // recorded once both the previous flags are true.
198 // Counters for UMA metrics.
200 // Number of tiles using a local thumbnail image for this NTP session.
201 int num_local_thumbs_
;
202 // Number of tiles for which a server thumbnail is provided.
203 int num_server_thumbs_
;
204 // Number of tiles for which no thumbnail is found/specified.
205 // In this case a gray tile is used as the main tile.
206 int num_empty_thumbs_
;
208 ScopedObserver
<history::TopSites
, history::TopSitesObserver
> scoped_observer_
;
210 MostVisitedSource mv_source_
;
212 scoped_ptr
<PopularSites
> popular_sites_
;
214 ScopedVector
<Suggestion
> current_suggestions_
;
216 // For callbacks may be run after destruction.
217 base::WeakPtrFactory
<MostVisitedSites
> weak_ptr_factory_
;
219 DISALLOW_COPY_AND_ASSIGN(MostVisitedSites
);
222 #endif // CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_