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