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 "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/sync/profile_sync_service_observer.h"
15 #include "components/history/core/browser/history_types.h"
16 #include "components/suggestions/proto/suggestions.pb.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
20 namespace suggestions {
21 class SuggestionsService;
24 // Provides the list of most visited sites and their thumbnails to Java.
25 class MostVisitedSites : public ProfileSyncServiceObserver,
26 public content::NotificationObserver {
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 // content::NotificationObserver implementation.
48 virtual void Observe(int type,
49 const content::NotificationSource& source,
50 const content::NotificationDetails& details) OVERRIDE;
52 // ProfileSyncServiceObserver implementation.
53 virtual void OnStateChanged() OVERRIDE;
55 // Registers JNI methods.
56 static bool Register(JNIEnv* env);
59 virtual ~MostVisitedSites();
60 void QueryMostVisitedURLs();
62 // Initialize the query to Top Sites. Called if the SuggestionsService is not
63 // enabled, or if it returns no data.
64 void InitiateTopSitesQuery();
66 // Callback for when data is available from TopSites.
67 void OnMostVisitedURLsAvailable(
68 base::android::ScopedJavaGlobalRef<jobject>* j_observer,
70 const history::MostVisitedURLList& visited_list);
72 // Callback for when data is available from the SuggestionsService.
73 void OnSuggestionsProfileAvailable(
74 base::android::ScopedJavaGlobalRef<jobject>* j_observer,
75 const suggestions::SuggestionsProfile& suggestions_profile);
77 // Callback for when the local thumbnail lookup is complete.
78 void OnObtainedThumbnail(
79 base::android::ScopedJavaGlobalRef<jobject>* bitmap,
80 base::android::ScopedJavaGlobalRef<jobject>* j_callback);
82 // Requests a server thumbnail from the |suggestions_service|.
83 void GetSuggestionsThumbnailOnUIThread(
84 suggestions::SuggestionsService* suggestions_service,
85 const std::string& url_string,
86 base::android::ScopedJavaGlobalRef<jobject>* j_callback);
88 // Callback from the SuggestionsServer regarding the server thumbnail lookup.
89 void OnSuggestionsThumbnailAvailable(
90 base::android::ScopedJavaGlobalRef<jobject>* j_callback,
92 const SkBitmap* bitmap);
94 // Records specific UMA histogram metrics.
95 void RecordUMAMetrics();
97 // The profile whose most visited sites will be queried.
100 // The observer to be notified when the list of most visited sites changes.
101 base::android::ScopedJavaGlobalRef<jobject> observer_;
103 // The maximum number of most visited sites to return.
106 // Whether the user is in a control group for the purposes of logging.
107 bool is_control_group_;
109 // Counters for UMA metrics.
111 // Number of tiles using a local thumbnail image for this NTP session.
112 int num_local_thumbs_;
113 // Number of tiles for which a server thumbnail is provided.
114 int num_server_thumbs_;
115 // Number of tiles for which no thumbnail is found/specified and a gray tile
116 // is used as the main tile.
117 int num_empty_thumbs_;
119 // Copy of the server suggestions (if enabled). Used for logging.
120 suggestions::SuggestionsProfile server_suggestions_;
122 // For callbacks may be run after destruction.
123 base::WeakPtrFactory<MostVisitedSites> weak_ptr_factory_;
125 content::NotificationRegistrar registrar_;
127 // The source of the Most Visited sites.
128 enum MostVisitedSource {
132 MostVisitedSource mv_source_;
134 DISALLOW_COPY_AND_ASSIGN(MostVisitedSites);
137 #endif // CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_