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 typedef base::Callback
<
29 void(base::android::ScopedJavaGlobalRef
<jobject
>* bitmap
,
30 base::android::ScopedJavaGlobalRef
<jobject
>* j_callback
)>
31 LookupSuccessCallback
;
33 explicit MostVisitedSites(Profile
* profile
);
34 void Destroy(JNIEnv
* env
, jobject obj
);
35 void OnLoadingComplete(JNIEnv
* env
, jobject obj
);
36 void SetMostVisitedURLsObserver(JNIEnv
* env
,
40 void GetURLThumbnail(JNIEnv
* env
,
44 void BlacklistUrl(JNIEnv
* env
, jobject obj
, jstring j_url
);
45 void RecordOpenedMostVisitedItem(JNIEnv
* env
, jobject obj
, jint index
);
47 // sync_driver::SyncServiceObserver implementation.
48 void OnStateChanged() override
;
50 // Registers JNI methods.
51 static bool Register(JNIEnv
* env
);
54 // The source of the Most Visited sites.
55 enum MostVisitedSource
{
60 ~MostVisitedSites() override
;
61 void QueryMostVisitedURLs();
63 // Initialize the query to Top Sites. Called if the SuggestionsService is not
64 // enabled, or if it returns no data.
65 void InitiateTopSitesQuery();
67 // Callback for when data is available from TopSites.
68 void OnMostVisitedURLsAvailable(
69 base::android::ScopedJavaGlobalRef
<jobject
>* j_observer
,
71 const history::MostVisitedURLList
& visited_list
);
73 // Callback for when data is available from the SuggestionsService.
74 void OnSuggestionsProfileAvailable(
75 base::android::ScopedJavaGlobalRef
<jobject
>* j_observer
,
76 const suggestions::SuggestionsProfile
& suggestions_profile
);
78 // Callback for when the local thumbnail lookup is complete.
79 void OnObtainedThumbnail(
80 base::android::ScopedJavaGlobalRef
<jobject
>* bitmap
,
81 base::android::ScopedJavaGlobalRef
<jobject
>* j_callback
);
83 // Requests a server thumbnail from the |suggestions_service|.
84 void GetSuggestionsThumbnailOnUIThread(
85 suggestions::SuggestionsService
* suggestions_service
,
86 const std::string
& url_string
,
87 base::android::ScopedJavaGlobalRef
<jobject
>* j_callback
);
89 // Callback from the SuggestionsServer regarding the server thumbnail lookup.
90 void OnSuggestionsThumbnailAvailable(
91 base::android::ScopedJavaGlobalRef
<jobject
>* j_callback
,
93 const SkBitmap
* bitmap
);
95 // Records specific UMA histogram metrics.
96 void RecordUMAMetrics();
98 // history::TopSitesObserver implementation.
99 void TopSitesLoaded(history::TopSites
* top_sites
) override
;
100 void TopSitesChanged(history::TopSites
* top_sites
) override
;
102 // The profile whose most visited sites will be queried.
105 // The observer to be notified when the list of most visited sites changes.
106 base::android::ScopedJavaGlobalRef
<jobject
> observer_
;
108 // The maximum number of most visited sites to return.
111 // Keeps track of whether the initial NTP load has been done.
112 bool initial_load_done_
;
114 // Counters for UMA metrics.
116 // Number of tiles using a local thumbnail image for this NTP session.
117 int num_local_thumbs_
;
118 // Number of tiles for which a server thumbnail is provided.
119 int num_server_thumbs_
;
120 // Number of tiles for which no thumbnail is found/specified and a gray tile
121 // is used as the main tile.
122 int num_empty_thumbs_
;
124 // Copy of the server suggestions (if enabled). Used for logging.
125 suggestions::SuggestionsProfile server_suggestions_
;
127 ScopedObserver
<history::TopSites
, history::TopSitesObserver
> scoped_observer_
;
129 MostVisitedSource mv_source_
;
131 // For callbacks may be run after destruction.
132 base::WeakPtrFactory
<MostVisitedSites
> weak_ptr_factory_
;
134 DISALLOW_COPY_AND_ASSIGN(MostVisitedSites
);
137 #endif // CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_