Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / android / most_visited_sites.h
blob4ecc9676c53e187daa9a3a4bdd2d9d32013f13d7
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 <string>
11 #include <vector>
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;
28 class GURL;
29 class PopularSites;
31 // Provides the list of most visited sites and their thumbnails to Java.
32 class MostVisitedSites : public sync_driver::SyncServiceObserver,
33 public history::TopSitesObserver {
34 public:
35 explicit MostVisitedSites(Profile* profile);
36 void Destroy(JNIEnv* env, jobject obj);
37 void OnLoadingComplete(JNIEnv* env, jobject obj);
38 void SetMostVisitedURLsObserver(JNIEnv* env,
39 jobject obj,
40 jobject j_observer,
41 jint num_sites);
42 void GetURLThumbnail(JNIEnv* env,
43 jobject obj,
44 jstring url,
45 jobject j_callback);
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);
56 private:
57 friend class MostVisitedSitesTest;
59 // The source of the Most Visited sites.
60 enum MostVisitedSource {
61 TOP_SITES,
62 SUGGESTIONS_SERVICE
65 ~MostVisitedSites() override;
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 const history::MostVisitedURLList& visited_list);
76 // Callback for when data is available from the SuggestionsService.
77 void OnSuggestionsProfileAvailable(
78 const suggestions::SuggestionsProfile& suggestions_profile);
80 // Adds the suggestions from |popular_sites_| into |titles| and |urls|. This
81 // might reorder |titles| and |urls| to retain the absolute positions of the
82 // popular suggestions. Also updates |tile_sources_| accordingly.
83 void AddPopularSites(std::vector<base::string16>* titles,
84 std::vector<std::string>* urls);
86 // Workhorse for AddPopularSites above. Implemented as a separate static
87 // method for ease of testing.
88 static void AddPopularSitesImpl(
89 int num_sites,
90 const std::vector<base::string16>& popular_titles,
91 const std::vector<std::string>& popular_urls,
92 std::vector<base::string16>* titles,
93 std::vector<std::string>* urls,
94 std::vector<std::string>* tile_sources);
96 // Notify the Java side observer about the availability of Most Visited Urls.
97 void NotifyMostVisitedURLsObserver(const std::vector<base::string16>& titles,
98 const std::vector<std::string>& urls);
100 void OnPopularSitesAvailable(bool success);
102 // Runs on the UI Thread.
103 void OnLocalThumbnailFetched(
104 const GURL& url,
105 scoped_ptr<base::android::ScopedJavaGlobalRef<jobject>> j_callback,
106 scoped_ptr<SkBitmap> bitmap);
108 // Callback for when the thumbnail lookup is complete.
109 // Runs on the UI Thread.
110 void OnObtainedThumbnail(
111 bool is_local_thumbnail,
112 scoped_ptr<base::android::ScopedJavaGlobalRef<jobject>> j_callback,
113 const GURL& url,
114 const SkBitmap* bitmap);
116 // Records thumbnail-related UMA histogram metrics.
117 void RecordThumbnailUMAMetrics();
119 // Records UMA histogram metrics related to the number of impressions.
120 void RecordImpressionUMAMetrics();
122 // history::TopSitesObserver implementation.
123 void TopSitesLoaded(history::TopSites* top_sites) override;
124 void TopSitesChanged(history::TopSites* top_sites,
125 ChangeReason change_reason) override;
127 // The profile whose most visited sites will be queried.
128 Profile* profile_;
130 // The observer to be notified when the list of most visited sites changes.
131 base::android::ScopedJavaGlobalRef<jobject> observer_;
133 // The maximum number of most visited sites to return.
134 int num_sites_;
136 // Whether we have received an initial set of most visited sites (from either
137 // TopSites or the SuggestionsService).
138 bool received_most_visited_sites_;
140 // Whether we have received the set of popular sites. Immediately set to true
141 // if popular sites are disabled.
142 bool received_popular_sites_;
144 // Whether we have recorded one-shot UMA metrics such as impressions. They are
145 // recorded once both the previous flags are true.
146 bool recorded_uma_;
148 // Counters for UMA metrics.
150 // Number of tiles using a local thumbnail image for this NTP session.
151 int num_local_thumbs_;
152 // Number of tiles for which a server thumbnail is provided.
153 int num_server_thumbs_;
154 // Number of tiles for which no thumbnail is found/specified.
155 // In this case a gray tile is used as the main tile.
156 int num_empty_thumbs_;
158 // Identifier for where each tile came from (client, server, popular). Used
159 // for logging.
160 std::vector<std::string> tile_sources_;
162 ScopedObserver<history::TopSites, history::TopSitesObserver> scoped_observer_;
164 MostVisitedSource mv_source_;
166 scoped_ptr<PopularSites> popular_sites_;
168 // For callbacks may be run after destruction.
169 base::WeakPtrFactory<MostVisitedSites> weak_ptr_factory_;
171 DISALLOW_COPY_AND_ASSIGN(MostVisitedSites);
174 #endif // CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_