Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / android / most_visited_sites.h
blob63c1ed40cef55d33ecb5a14808a8af105bdbbadc
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_
8 #include <jni.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 {
27 public:
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,
37 jobject obj,
38 jobject j_observer,
39 jint num_sites);
40 void GetURLThumbnail(JNIEnv* env,
41 jobject obj,
42 jstring url,
43 jobject j_callback);
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);
58 private:
59 // The source of the Most Visited sites.
60 enum MostVisitedSource {
61 TOP_SITES,
62 SUGGESTIONS_SERVICE
65 virtual ~MostVisitedSites();
66 void QueryMostVisitedURLs();
68 // Initialize the query to Top Sites. Called if the SuggestionsService is not
69 // enabled, or if it returns no data.
70 void InitiateTopSitesQuery();
72 // Callback for when data is available from TopSites.
73 void OnMostVisitedURLsAvailable(
74 base::android::ScopedJavaGlobalRef<jobject>* j_observer,
75 int num_sites,
76 const history::MostVisitedURLList& visited_list);
78 // Callback for when data is available from the SuggestionsService.
79 void OnSuggestionsProfileAvailable(
80 base::android::ScopedJavaGlobalRef<jobject>* j_observer,
81 const suggestions::SuggestionsProfile& suggestions_profile);
83 // Callback for when the local thumbnail lookup is complete.
84 void OnObtainedThumbnail(
85 base::android::ScopedJavaGlobalRef<jobject>* bitmap,
86 base::android::ScopedJavaGlobalRef<jobject>* j_callback);
88 // Requests a server thumbnail from the |suggestions_service|.
89 void GetSuggestionsThumbnailOnUIThread(
90 suggestions::SuggestionsService* suggestions_service,
91 const std::string& url_string,
92 base::android::ScopedJavaGlobalRef<jobject>* j_callback);
94 // Callback from the SuggestionsServer regarding the server thumbnail lookup.
95 void OnSuggestionsThumbnailAvailable(
96 base::android::ScopedJavaGlobalRef<jobject>* j_callback,
97 const GURL& url,
98 const SkBitmap* bitmap);
100 // Records specific UMA histogram metrics.
101 void RecordUMAMetrics();
103 // The profile whose most visited sites will be queried.
104 Profile* profile_;
106 // The observer to be notified when the list of most visited sites changes.
107 base::android::ScopedJavaGlobalRef<jobject> observer_;
109 // The maximum number of most visited sites to return.
110 int num_sites_;
112 // Whether the user is in a control group for the purposes of logging.
113 bool is_control_group_;
115 // Counters for UMA metrics.
117 // Number of tiles using a local thumbnail image for this NTP session.
118 int num_local_thumbs_;
119 // Number of tiles for which a server thumbnail is provided.
120 int num_server_thumbs_;
121 // Number of tiles for which no thumbnail is found/specified and a gray tile
122 // is used as the main tile.
123 int num_empty_thumbs_;
125 // Copy of the server suggestions (if enabled). Used for logging.
126 suggestions::SuggestionsProfile server_suggestions_;
128 content::NotificationRegistrar registrar_;
130 MostVisitedSource mv_source_;
132 // For callbacks may be run after destruction.
133 base::WeakPtrFactory<MostVisitedSites> weak_ptr_factory_;
135 DISALLOW_COPY_AND_ASSIGN(MostVisitedSites);
138 #endif // CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_