Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(369)

Side by Side Diff: chrome/browser/android/most_visited_sites.h

Issue 1330773002: [Android] Persist ordering of NTP suggestions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_ 5 #ifndef CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_
6 #define CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_ 6 #define CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_
7 7
8 #include <jni.h> 8 #include <jni.h>
9 9
10 #include <string> 10 #include <string>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/android/scoped_java_ref.h" 13 #include "base/android/scoped_java_ref.h"
14 #include "base/compiler_specific.h" 14 #include "base/compiler_specific.h"
15 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h" 16 #include "base/memory/weak_ptr.h"
17 #include "base/scoped_observer.h" 17 #include "base/scoped_observer.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "components/history/core/browser/history_types.h" 18 #include "components/history/core/browser/history_types.h"
20 #include "components/history/core/browser/top_sites_observer.h" 19 #include "components/history/core/browser/top_sites_observer.h"
21 #include "components/suggestions/proto/suggestions.pb.h" 20 #include "components/suggestions/proto/suggestions.pb.h"
22 #include "components/sync_driver/sync_service_observer.h" 21 #include "components/sync_driver/sync_service_observer.h"
23 22
24 namespace suggestions { 23 namespace suggestions {
25 class SuggestionsService; 24 class SuggestionsService;
26 } 25 }
27 26
27 namespace user_prefs {
28 class PrefRegistrySyncable;
29 }
30
28 class GURL; 31 class GURL;
29 class PopularSites; 32 class PopularSites;
33 class Profile;
30 34
31 // Provides the list of most visited sites and their thumbnails to Java. 35 // Provides the list of most visited sites and their thumbnails to Java.
32 class MostVisitedSites : public sync_driver::SyncServiceObserver, 36 class MostVisitedSites : public sync_driver::SyncServiceObserver,
33 public history::TopSitesObserver { 37 public history::TopSitesObserver {
34 public: 38 public:
35 explicit MostVisitedSites(Profile* profile); 39 explicit MostVisitedSites(Profile* profile);
36 void Destroy(JNIEnv* env, jobject obj); 40 void Destroy(JNIEnv* env, jobject obj);
37 void OnLoadingComplete(JNIEnv* env, jobject obj); 41 void OnLoadingComplete(JNIEnv* env, jobject obj);
38 void SetMostVisitedURLsObserver(JNIEnv* env, 42 void SetMostVisitedURLsObserver(JNIEnv* env,
39 jobject obj, 43 jobject obj,
40 jobject j_observer, 44 jobject j_observer,
41 jint num_sites); 45 jint num_sites);
42 void GetURLThumbnail(JNIEnv* env, 46 void GetURLThumbnail(JNIEnv* env,
43 jobject obj, 47 jobject obj,
44 jstring url, 48 jstring url,
45 jobject j_callback); 49 jobject j_callback);
46 50
47 void BlacklistUrl(JNIEnv* env, jobject obj, jstring j_url); 51 void BlacklistUrl(JNIEnv* env, jobject obj, jstring j_url);
48 void RecordOpenedMostVisitedItem(JNIEnv* env, jobject obj, jint index); 52 void RecordOpenedMostVisitedItem(JNIEnv* env, jobject obj, jint index);
49 53
50 // sync_driver::SyncServiceObserver implementation. 54 // sync_driver::SyncServiceObserver implementation.
51 void OnStateChanged() override; 55 void OnStateChanged() override;
52 56
53 // Registers JNI methods. 57 // Registers JNI methods.
54 static bool Register(JNIEnv* env); 58 static bool Register(JNIEnv* env);
55 59
60 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
61
56 private: 62 private:
57 friend class MostVisitedSitesTest; 63 friend class MostVisitedSitesTest;
58 64
59 // The source of the Most Visited sites. 65 // The source of the Most Visited sites.
60 enum MostVisitedSource { 66 enum MostVisitedSource { TOP_SITES, SUGGESTIONS_SERVICE, POPULAR };
61 TOP_SITES, 67
62 SUGGESTIONS_SERVICE 68 struct Suggestion {
69 base::string16 title;
70 std::string url;
71 std::string host;
Marc Treib 2015/09/09 12:15:01 I think it'd make sense to combine |url| and |host
knn 2015/09/09 13:54:31 Yeah, I though GURL parses the host on fly, I stan
72 MostVisitedSource source;
73 // Only valid for source == SUGGESTIONS_SERVICE (-1 otherwise).
74 int provider_index;
75
76 Suggestion(const base::string16& title,
77 const std::string& url,
78 MostVisitedSource source);
79 Suggestion(const base::string16& title,
80 const std::string& url,
81 const std::string& host,
82 MostVisitedSource source);
83 Suggestion(const base::string16& title,
84 const std::string& url,
85 MostVisitedSource source,
86 int provider_index);
87 ~Suggestion();
88
89 // Get the Histogram name associated with the source.
90 std::string GetSourceHistogramName() const;
91
92 private:
93 DISALLOW_COPY_AND_ASSIGN(Suggestion);
63 }; 94 };
64 95
65 ~MostVisitedSites() override; 96 ~MostVisitedSites() override;
66 void QueryMostVisitedURLs(); 97 void QueryMostVisitedURLs();
67 98
68 // Initialize the query to Top Sites. Called if the SuggestionsService is not 99 // Initialize the query to Top Sites. Called if the SuggestionsService is not
69 // enabled, or if it returns no data. 100 // enabled, or if it returns no data.
70 void InitiateTopSitesQuery(); 101 void InitiateTopSitesQuery();
71 102
72 // Callback for when data is available from TopSites. 103 // Callback for when data is available from TopSites.
73 void OnMostVisitedURLsAvailable( 104 void OnMostVisitedURLsAvailable(
74 const history::MostVisitedURLList& visited_list); 105 const history::MostVisitedURLList& visited_list);
75 106
76 // Callback for when data is available from the SuggestionsService. 107 // Callback for when data is available from the SuggestionsService.
77 void OnSuggestionsProfileAvailable( 108 void OnSuggestionsProfileAvailable(
78 const suggestions::SuggestionsProfile& suggestions_profile); 109 const suggestions::SuggestionsProfile& suggestions_profile);
79 110
80 // Adds the suggestions from |popular_sites_| into |titles| and |urls|. This 111 // Takes the personal suggestions and adds popular suggestions if necessary
81 // might reorder |titles| and |urls| to retain the absolute positions of the 112 // and reorders the suggestions based on the previously displayed order.
82 // popular suggestions. Also updates |tile_sources_| accordingly. 113 void AddPopularSites(ScopedVector<Suggestion>* suggestions);
83 void AddPopularSites(std::vector<base::string16>* titles,
84 std::vector<std::string>* urls);
85 114
86 // Workhorse for AddPopularSites above. Implemented as a separate static 115 // Workhorse for AddPopularSites above. Implemented as a separate static
87 // method for ease of testing. 116 // method for ease of testing.
88 static void AddPopularSitesImpl( 117 static ScopedVector<Suggestion> MergeSuggestions(
89 int num_sites, 118 ScopedVector<Suggestion>* personal_suggestions,
90 const std::vector<base::string16>& popular_titles, 119 ScopedVector<Suggestion>* popular_suggestions,
91 const std::vector<std::string>& popular_urls, 120 const std::vector<std::string>& old_sites_url,
92 std::vector<base::string16>* titles, 121 const std::vector<bool>& old_sites_is_personal);
93 std::vector<std::string>* urls, 122
94 std::vector<std::string>* tile_sources); 123 void GetPreviousNTPSites(size_t num_tiles,
124 std::vector<std::string>* old_sites_url,
125 std::vector<bool>* old_sites_source);
Marc Treib 2015/09/09 12:15:01 nit: const
knn 2015/09/09 13:54:31 Done.
Marc Treib 2015/09/09 14:00:47 I meant the method itself should be const. Making
126
127 void SaveCurrentNTPSites();
128
129 // Takes suggestions from |src_suggestions| and moves them to
130 // |dst_suggestions| if the suggestion's url/host matches
131 // |match_urls|/|match_hosts| respectively. Unmatched suggestion indices from
132 // |src_suggestions| are returned for ease of insertion later.
133 static std::vector<size_t> InsertMatchingSuggestions(
Marc Treib 2015/09/09 12:15:01 nit: I think you could move this and also InsertAl
knn 2015/09/09 13:54:31 Except Suggestions can't be private anymore which
Marc Treib 2015/09/09 14:00:47 Ah, right, missed that, sorry.
134 ScopedVector<MostVisitedSites::Suggestion>* src_suggestions,
Marc Treib 2015/09/09 14:00:47 nit: "MostVisitedSites::" is not necessary anymore
135 ScopedVector<MostVisitedSites::Suggestion>* dst_suggestions,
136 const std::vector<std::string>& match_urls,
137 const std::vector<std::string>& match_hosts);
138
139 // Inserts suggestions from |src_suggestions| at positions |insert_positions|
140 // into |dst_suggestions| where ever empty starting from |start_position|.
141 // Returns the last filled position so that future insertions can start from
142 // there.
143 static size_t InsertAllSuggestions(
144 size_t start_position,
145 const std::vector<size_t>& insert_positions,
146 ScopedVector<MostVisitedSites::Suggestion>* src_suggestions,
147 ScopedVector<MostVisitedSites::Suggestion>* dst_suggestions);
95 148
96 // Notify the Java side observer about the availability of Most Visited Urls. 149 // Notify the Java side observer about the availability of Most Visited Urls.
97 void NotifyMostVisitedURLsObserver(const std::vector<base::string16>& titles, 150 void NotifyMostVisitedURLsObserver();
98 const std::vector<std::string>& urls);
99 151
100 void OnPopularSitesAvailable(bool success); 152 void OnPopularSitesAvailable(bool success);
101 153
102 // Runs on the UI Thread. 154 // Runs on the UI Thread.
103 void OnLocalThumbnailFetched( 155 void OnLocalThumbnailFetched(
104 const GURL& url, 156 const GURL& url,
105 scoped_ptr<base::android::ScopedJavaGlobalRef<jobject>> j_callback, 157 scoped_ptr<base::android::ScopedJavaGlobalRef<jobject>> j_callback,
106 scoped_ptr<SkBitmap> bitmap); 158 scoped_ptr<SkBitmap> bitmap);
107 159
108 // Callback for when the thumbnail lookup is complete. 160 // Callback for when the thumbnail lookup is complete.
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 // Counters for UMA metrics. 200 // Counters for UMA metrics.
149 201
150 // Number of tiles using a local thumbnail image for this NTP session. 202 // Number of tiles using a local thumbnail image for this NTP session.
151 int num_local_thumbs_; 203 int num_local_thumbs_;
152 // Number of tiles for which a server thumbnail is provided. 204 // Number of tiles for which a server thumbnail is provided.
153 int num_server_thumbs_; 205 int num_server_thumbs_;
154 // Number of tiles for which no thumbnail is found/specified. 206 // Number of tiles for which no thumbnail is found/specified.
155 // In this case a gray tile is used as the main tile. 207 // In this case a gray tile is used as the main tile.
156 int num_empty_thumbs_; 208 int num_empty_thumbs_;
157 209
158 // Identifier for where each tile came from (client, server, popular). Used
159 // for logging.
160 std::vector<std::string> tile_sources_;
161
162 ScopedObserver<history::TopSites, history::TopSitesObserver> scoped_observer_; 210 ScopedObserver<history::TopSites, history::TopSitesObserver> scoped_observer_;
163 211
164 MostVisitedSource mv_source_; 212 MostVisitedSource mv_source_;
165 213
166 scoped_ptr<PopularSites> popular_sites_; 214 scoped_ptr<PopularSites> popular_sites_;
167 215
216 ScopedVector<Suggestion> current_suggestions_;
217
168 // For callbacks may be run after destruction. 218 // For callbacks may be run after destruction.
169 base::WeakPtrFactory<MostVisitedSites> weak_ptr_factory_; 219 base::WeakPtrFactory<MostVisitedSites> weak_ptr_factory_;
170 220
171 DISALLOW_COPY_AND_ASSIGN(MostVisitedSites); 221 DISALLOW_COPY_AND_ASSIGN(MostVisitedSites);
172 }; 222 };
173 223
174 #endif // CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_ 224 #endif // CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/android/most_visited_sites.cc » ('j') | chrome/browser/android/most_visited_sites.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698