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

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: Move things around in a struct 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
62 // The source of the Most Visited sites.
63 enum MostVisitedSource { TOP_SITES, SUGGESTIONS_SERVICE, POPULAR };
64
65 struct Suggestion {
66 base::string16 title;
67 std::string url;
68 std::string host;
69 MostVisitedSource source;
70 // Only for source == SUGGESTIONS_SERVICE
71 int provider_index;
72
73 Suggestion(base::string16 title, std::string url, MostVisitedSource source);
Marc Treib 2015/09/08 11:39:49 nit: Pass strings by const& ?
knn 2015/09/08 13:42:00 Done.
74 Suggestion(base::string16 title,
75 std::string url,
76 std::string host,
77 MostVisitedSource source);
78 Suggestion(base::string16 title,
79 std::string url,
80 MostVisitedSource source,
81 int provider_index);
82 std::string GetSourceString();
Marc Treib 2015/09/08 11:39:49 const :)
knn 2015/09/08 13:42:00 Done.
83 };
84
56 private: 85 private:
57 friend class MostVisitedSitesTest; 86 friend class MostVisitedSitesTest;
58 87
59 // The source of the Most Visited sites.
60 enum MostVisitedSource {
61 TOP_SITES,
62 SUGGESTIONS_SERVICE
63 };
64
65 ~MostVisitedSites() override; 88 ~MostVisitedSites() override;
66 void QueryMostVisitedURLs(); 89 void QueryMostVisitedURLs();
67 90
68 // Initialize the query to Top Sites. Called if the SuggestionsService is not 91 // Initialize the query to Top Sites. Called if the SuggestionsService is not
69 // enabled, or if it returns no data. 92 // enabled, or if it returns no data.
70 void InitiateTopSitesQuery(); 93 void InitiateTopSitesQuery();
71 94
72 // Callback for when data is available from TopSites. 95 // Callback for when data is available from TopSites.
73 void OnMostVisitedURLsAvailable( 96 void OnMostVisitedURLsAvailable(
74 const history::MostVisitedURLList& visited_list); 97 const history::MostVisitedURLList& visited_list);
75 98
76 // Callback for when data is available from the SuggestionsService. 99 // Callback for when data is available from the SuggestionsService.
77 void OnSuggestionsProfileAvailable( 100 void OnSuggestionsProfileAvailable(
78 const suggestions::SuggestionsProfile& suggestions_profile); 101 const suggestions::SuggestionsProfile& suggestions_profile);
79 102
80 // Adds the suggestions from |popular_sites_| into |titles| and |urls|. This 103 // Takes the personal suggestions and adds popular suggestions if necessary
81 // might reorder |titles| and |urls| to retain the absolute positions of the 104 // and reorders the suggestions based on the previously displayed order.
82 // popular suggestions. Also updates |tile_sources_| accordingly. 105 void AddPopularSites(ScopedVector<Suggestion>* suggestions);
83 void AddPopularSites(std::vector<base::string16>* titles,
84 std::vector<std::string>* urls);
85 106
86 // Workhorse for AddPopularSites above. Implemented as a separate static 107 // Workhorse for AddPopularSites above. Implemented as a separate static
87 // method for ease of testing. 108 // method for ease of testing.
88 static void AddPopularSitesImpl( 109 static void MergeSuggestions(ScopedVector<Suggestion>* personal_suggestions,
89 int num_sites, 110 ScopedVector<Suggestion>* popular_suggestions,
90 const std::vector<base::string16>& popular_titles, 111 const std::vector<std::string>& old_sites_url,
91 const std::vector<std::string>& popular_urls, 112 const std::vector<bool>& old_sites_is_personal,
92 std::vector<base::string16>* titles, 113 // Return value.
93 std::vector<std::string>* urls, 114 ScopedVector<Suggestion>* merged_suggestions);
94 std::vector<std::string>* tile_sources);
95 115
96 // Notify the Java side observer about the availability of Most Visited Urls. 116 // Notify the Java side observer about the availability of Most Visited Urls.
97 void NotifyMostVisitedURLsObserver(const std::vector<base::string16>& titles, 117 void NotifyMostVisitedURLsObserver(ScopedVector<Suggestion>* suggestions);
98 const std::vector<std::string>& urls);
99 118
100 void OnPopularSitesAvailable(bool success); 119 void OnPopularSitesAvailable(bool success);
101 120
102 // Runs on the UI Thread. 121 // Runs on the UI Thread.
103 void OnLocalThumbnailFetched( 122 void OnLocalThumbnailFetched(
104 const GURL& url, 123 const GURL& url,
105 scoped_ptr<base::android::ScopedJavaGlobalRef<jobject>> j_callback, 124 scoped_ptr<base::android::ScopedJavaGlobalRef<jobject>> j_callback,
106 scoped_ptr<SkBitmap> bitmap); 125 scoped_ptr<SkBitmap> bitmap);
107 126
108 // Callback for when the thumbnail lookup is complete. 127 // 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. 167 // Counters for UMA metrics.
149 168
150 // Number of tiles using a local thumbnail image for this NTP session. 169 // Number of tiles using a local thumbnail image for this NTP session.
151 int num_local_thumbs_; 170 int num_local_thumbs_;
152 // Number of tiles for which a server thumbnail is provided. 171 // Number of tiles for which a server thumbnail is provided.
153 int num_server_thumbs_; 172 int num_server_thumbs_;
154 // Number of tiles for which no thumbnail is found/specified. 173 // Number of tiles for which no thumbnail is found/specified.
155 // In this case a gray tile is used as the main tile. 174 // In this case a gray tile is used as the main tile.
156 int num_empty_thumbs_; 175 int num_empty_thumbs_;
157 176
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_; 177 ScopedObserver<history::TopSites, history::TopSitesObserver> scoped_observer_;
163 178
164 MostVisitedSource mv_source_; 179 MostVisitedSource mv_source_;
165 180
166 scoped_ptr<PopularSites> popular_sites_; 181 scoped_ptr<PopularSites> popular_sites_;
167 182
183 ScopedVector<Suggestion> current_suggestions_;
184
168 // For callbacks may be run after destruction. 185 // For callbacks may be run after destruction.
169 base::WeakPtrFactory<MostVisitedSites> weak_ptr_factory_; 186 base::WeakPtrFactory<MostVisitedSites> weak_ptr_factory_;
170 187
171 DISALLOW_COPY_AND_ASSIGN(MostVisitedSites); 188 DISALLOW_COPY_AND_ASSIGN(MostVisitedSites);
172 }; 189 };
173 190
174 #endif // CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_ 191 #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