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 "chrome/browser/profiles/profile.h"
19 #include "components/history/core/browser/history_types.h"
20 #include "components/history/core/browser/top_sites_observer.h"
21 #include "components/suggestions/proto/suggestions.pb.h"
22 #include "components/sync_driver/sync_service_observer.h"
24 namespace suggestions
{
25 class SuggestionsService
;
31 // Provides the list of most visited sites and their thumbnails to Java.
32 class MostVisitedSites
: public sync_driver::SyncServiceObserver
,
33 public history::TopSitesObserver
{
35 explicit MostVisitedSites(Profile
* profile
);
36 void Destroy(JNIEnv
* env
, jobject obj
);
37 void OnLoadingComplete(JNIEnv
* env
, jobject obj
);
38 void SetMostVisitedURLsObserver(JNIEnv
* env
,
42 void GetURLThumbnail(JNIEnv
* env
,
47 void BlacklistUrl(JNIEnv
* env
, jobject obj
, jstring j_url
);
48 void RecordOpenedMostVisitedItem(JNIEnv
* env
, jobject obj
, jint index
);
50 // sync_driver::SyncServiceObserver implementation.
51 void OnStateChanged() override
;
53 // Registers JNI methods.
54 static bool Register(JNIEnv
* env
);
57 // The source of the Most Visited sites.
58 enum MostVisitedSource
{
63 ~MostVisitedSites() override
;
64 void QueryMostVisitedURLs();
66 // Initialize the query to Top Sites. Called if the SuggestionsService is not
67 // enabled, or if it returns no data.
68 void InitiateTopSitesQuery();
70 // Callback for when data is available from TopSites.
71 void OnMostVisitedURLsAvailable(
72 const history::MostVisitedURLList
& visited_list
);
74 // Callback for when data is available from the SuggestionsService.
75 void OnSuggestionsProfileAvailable(
76 const suggestions::SuggestionsProfile
& suggestions_profile
);
78 void AddPopularSites(std::vector
<base::string16
>* titles
,
79 std::vector
<std::string
>* urls
);
81 // Notify the Java side observer about the availability of Most Visited Urls.
82 void NotifyMostVisitedURLsObserver(const std::vector
<base::string16
>& titles
,
83 const std::vector
<std::string
>& urls
);
85 void OnPopularSitesAvailable(bool success
);
87 // Runs on the UI Thread.
88 void OnLocalThumbnailFetched(
90 scoped_ptr
<base::android::ScopedJavaGlobalRef
<jobject
>> j_callback
,
91 scoped_ptr
<SkBitmap
> bitmap
);
93 // Callback for when the thumbnail lookup is complete.
94 // Runs on the UI Thread.
95 void OnObtainedThumbnail(
96 bool is_local_thumbnail
,
97 scoped_ptr
<base::android::ScopedJavaGlobalRef
<jobject
>> j_callback
,
99 const SkBitmap
* bitmap
);
101 // Records specific UMA histogram metrics.
102 void RecordUMAMetrics();
104 // history::TopSitesObserver implementation.
105 void TopSitesLoaded(history::TopSites
* top_sites
) override
;
106 void TopSitesChanged(history::TopSites
* top_sites
,
107 ChangeReason change_reason
) override
;
109 // The profile whose most visited sites will be queried.
112 // The observer to be notified when the list of most visited sites changes.
113 base::android::ScopedJavaGlobalRef
<jobject
> observer_
;
115 // The maximum number of most visited sites to return.
118 // Keeps track of whether the initial NTP load has been done.
119 bool initial_load_done_
;
121 // Counters for UMA metrics.
123 // Number of tiles using a local thumbnail image for this NTP session.
124 int num_local_thumbs_
;
125 // Number of tiles for which a server thumbnail is provided.
126 int num_server_thumbs_
;
127 // Number of tiles for which no thumbnail is found/specified.
128 // In this case a gray tile is used as the main tile.
129 int num_empty_thumbs_
;
131 // Copy of the server suggestions (if enabled). Used for logging.
132 suggestions::SuggestionsProfile server_suggestions_
;
134 ScopedObserver
<history::TopSites
, history::TopSitesObserver
> scoped_observer_
;
136 MostVisitedSource mv_source_
;
138 scoped_ptr
<PopularSites
> popular_sites_
;
140 // For callbacks may be run after destruction.
141 base::WeakPtrFactory
<MostVisitedSites
> weak_ptr_factory_
;
143 DISALLOW_COPY_AND_ASSIGN(MostVisitedSites
);
146 #endif // CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_