| 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" |
| 22 #include "url/gurl.h" |
| 23 | 23 |
| 24 namespace suggestions { | 24 namespace suggestions { |
| 25 class SuggestionsService; | 25 class SuggestionsService; |
| 26 } | 26 } |
| 27 | 27 |
| 28 class GURL; | 28 namespace user_prefs { |
| 29 class PrefRegistrySyncable; |
| 30 } |
| 31 |
| 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 GURL url; |
| 71 MostVisitedSource source; |
| 72 // Only valid for source == SUGGESTIONS_SERVICE (-1 otherwise). |
| 73 int provider_index; |
| 74 |
| 75 Suggestion(const base::string16& title, |
| 76 const std::string& url, |
| 77 MostVisitedSource source); |
| 78 Suggestion(const base::string16& title, |
| 79 const GURL& url, |
| 80 MostVisitedSource source); |
| 81 Suggestion(const base::string16& title, |
| 82 const std::string& url, |
| 83 MostVisitedSource source, |
| 84 int provider_index); |
| 85 ~Suggestion(); |
| 86 |
| 87 // Get the Histogram name associated with the source. |
| 88 std::string GetSourceHistogramName() const; |
| 89 |
| 90 private: |
| 91 DISALLOW_COPY_AND_ASSIGN(Suggestion); |
| 63 }; | 92 }; |
| 64 | 93 |
| 65 ~MostVisitedSites() override; | 94 ~MostVisitedSites() override; |
| 66 void QueryMostVisitedURLs(); | 95 void QueryMostVisitedURLs(); |
| 67 | 96 |
| 68 // Initialize the query to Top Sites. Called if the SuggestionsService is not | 97 // Initialize the query to Top Sites. Called if the SuggestionsService is not |
| 69 // enabled, or if it returns no data. | 98 // enabled, or if it returns no data. |
| 70 void InitiateTopSitesQuery(); | 99 void InitiateTopSitesQuery(); |
| 71 | 100 |
| 72 // Callback for when data is available from TopSites. | 101 // Callback for when data is available from TopSites. |
| 73 void OnMostVisitedURLsAvailable( | 102 void OnMostVisitedURLsAvailable( |
| 74 const history::MostVisitedURLList& visited_list); | 103 const history::MostVisitedURLList& visited_list); |
| 75 | 104 |
| 76 // Callback for when data is available from the SuggestionsService. | 105 // Callback for when data is available from the SuggestionsService. |
| 77 void OnSuggestionsProfileAvailable( | 106 void OnSuggestionsProfileAvailable( |
| 78 const suggestions::SuggestionsProfile& suggestions_profile); | 107 const suggestions::SuggestionsProfile& suggestions_profile); |
| 79 | 108 |
| 80 // Adds the suggestions from |popular_sites_| into |titles| and |urls|. This | 109 // Takes the personal suggestions and adds popular suggestions if necessary |
| 81 // might reorder |titles| and |urls| to retain the absolute positions of the | 110 // and reorders the suggestions based on the previously displayed order. |
| 82 // popular suggestions. Also updates |tile_sources_| accordingly. | 111 void AddPopularSites(ScopedVector<Suggestion>* suggestions); |
| 83 void AddPopularSites(std::vector<base::string16>* titles, | |
| 84 std::vector<std::string>* urls); | |
| 85 | 112 |
| 86 // Workhorse for AddPopularSites above. Implemented as a separate static | 113 // Workhorse for AddPopularSites above. Implemented as a separate static |
| 87 // method for ease of testing. | 114 // method for ease of testing. |
| 88 static void AddPopularSitesImpl( | 115 static ScopedVector<Suggestion> MergeSuggestions( |
| 89 int num_sites, | 116 ScopedVector<Suggestion>* personal_suggestions, |
| 90 const std::vector<base::string16>& popular_titles, | 117 ScopedVector<Suggestion>* popular_suggestions, |
| 91 const std::vector<std::string>& popular_urls, | 118 const std::vector<std::string>& old_sites_url, |
| 92 std::vector<base::string16>* titles, | 119 const std::vector<bool>& old_sites_is_personal); |
| 93 std::vector<std::string>* urls, | 120 |
| 94 std::vector<std::string>* tile_sources); | 121 void GetPreviousNTPSites(size_t num_tiles, |
| 122 std::vector<std::string>* old_sites_url, |
| 123 std::vector<bool>* old_sites_source) const; |
| 124 |
| 125 void SaveCurrentNTPSites(); |
| 126 |
| 127 // Takes suggestions from |src_suggestions| and moves them to |
| 128 // |dst_suggestions| if the suggestion's url/host matches |
| 129 // |match_urls|/|match_hosts| respectively. Unmatched suggestion indices from |
| 130 // |src_suggestions| are returned for ease of insertion later. |
| 131 static std::vector<size_t> InsertMatchingSuggestions( |
| 132 ScopedVector<Suggestion>* src_suggestions, |
| 133 ScopedVector<Suggestion>* dst_suggestions, |
| 134 const std::vector<std::string>& match_urls, |
| 135 const std::vector<std::string>& match_hosts); |
| 136 |
| 137 // Inserts suggestions from |src_suggestions| at positions |insert_positions| |
| 138 // into |dst_suggestions| where ever empty starting from |start_position|. |
| 139 // Returns the last filled position so that future insertions can start from |
| 140 // there. |
| 141 static size_t InsertAllSuggestions( |
| 142 size_t start_position, |
| 143 const std::vector<size_t>& insert_positions, |
| 144 ScopedVector<Suggestion>* src_suggestions, |
| 145 ScopedVector<Suggestion>* dst_suggestions); |
| 95 | 146 |
| 96 // Notify the Java side observer about the availability of Most Visited Urls. | 147 // Notify the Java side observer about the availability of Most Visited Urls. |
| 97 void NotifyMostVisitedURLsObserver(const std::vector<base::string16>& titles, | 148 void NotifyMostVisitedURLsObserver(); |
| 98 const std::vector<std::string>& urls); | |
| 99 | 149 |
| 100 void OnPopularSitesAvailable(bool success); | 150 void OnPopularSitesAvailable(bool success); |
| 101 | 151 |
| 102 // Runs on the UI Thread. | 152 // Runs on the UI Thread. |
| 103 void OnLocalThumbnailFetched( | 153 void OnLocalThumbnailFetched( |
| 104 const GURL& url, | 154 const GURL& url, |
| 105 scoped_ptr<base::android::ScopedJavaGlobalRef<jobject>> j_callback, | 155 scoped_ptr<base::android::ScopedJavaGlobalRef<jobject>> j_callback, |
| 106 scoped_ptr<SkBitmap> bitmap); | 156 scoped_ptr<SkBitmap> bitmap); |
| 107 | 157 |
| 108 // Callback for when the thumbnail lookup is complete. | 158 // 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. | 198 // Counters for UMA metrics. |
| 149 | 199 |
| 150 // Number of tiles using a local thumbnail image for this NTP session. | 200 // Number of tiles using a local thumbnail image for this NTP session. |
| 151 int num_local_thumbs_; | 201 int num_local_thumbs_; |
| 152 // Number of tiles for which a server thumbnail is provided. | 202 // Number of tiles for which a server thumbnail is provided. |
| 153 int num_server_thumbs_; | 203 int num_server_thumbs_; |
| 154 // Number of tiles for which no thumbnail is found/specified. | 204 // Number of tiles for which no thumbnail is found/specified. |
| 155 // In this case a gray tile is used as the main tile. | 205 // In this case a gray tile is used as the main tile. |
| 156 int num_empty_thumbs_; | 206 int num_empty_thumbs_; |
| 157 | 207 |
| 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_; | 208 ScopedObserver<history::TopSites, history::TopSitesObserver> scoped_observer_; |
| 163 | 209 |
| 164 MostVisitedSource mv_source_; | 210 MostVisitedSource mv_source_; |
| 165 | 211 |
| 166 scoped_ptr<PopularSites> popular_sites_; | 212 scoped_ptr<PopularSites> popular_sites_; |
| 167 | 213 |
| 214 ScopedVector<Suggestion> current_suggestions_; |
| 215 |
| 168 // For callbacks may be run after destruction. | 216 // For callbacks may be run after destruction. |
| 169 base::WeakPtrFactory<MostVisitedSites> weak_ptr_factory_; | 217 base::WeakPtrFactory<MostVisitedSites> weak_ptr_factory_; |
| 170 | 218 |
| 171 DISALLOW_COPY_AND_ASSIGN(MostVisitedSites); | 219 DISALLOW_COPY_AND_ASSIGN(MostVisitedSites); |
| 172 }; | 220 }; |
| 173 | 221 |
| 174 #endif // CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_ | 222 #endif // CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_ |
| OLD | NEW |