| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_ | |
| 6 #define CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_ | |
| 7 | |
| 8 #include <jni.h> | |
| 9 #include <stddef.h> | |
| 10 | |
| 11 #include <string> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/android/scoped_java_ref.h" | |
| 15 #include "base/compiler_specific.h" | |
| 16 #include "base/files/file_path.h" | |
| 17 #include "base/macros.h" | |
| 18 #include "base/memory/scoped_ptr.h" | |
| 19 #include "base/memory/weak_ptr.h" | |
| 20 #include "base/scoped_observer.h" | |
| 21 #include "chrome/browser/supervised_user/supervised_user_service.h" | |
| 22 #include "chrome/browser/supervised_user/supervised_user_service_observer.h" | |
| 23 #include "components/history/core/browser/history_types.h" | |
| 24 #include "components/history/core/browser/top_sites_observer.h" | |
| 25 #include "components/suggestions/proto/suggestions.pb.h" | |
| 26 #include "components/suggestions/suggestions_service.h" | |
| 27 #include "url/gurl.h" | |
| 28 | |
| 29 namespace suggestions { | |
| 30 class SuggestionsService; | |
| 31 } | |
| 32 | |
| 33 namespace user_prefs { | |
| 34 class PrefRegistrySyncable; | |
| 35 } | |
| 36 | |
| 37 class PopularSites; | |
| 38 class Profile; | |
| 39 | |
| 40 // Provides the list of most visited sites and their thumbnails to Java. | |
| 41 class MostVisitedSites : public history::TopSitesObserver, | |
| 42 public SupervisedUserServiceObserver { | |
| 43 public: | |
| 44 explicit MostVisitedSites(Profile* profile); | |
| 45 void Destroy(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj); | |
| 46 void SetMostVisitedURLsObserver( | |
| 47 JNIEnv* env, | |
| 48 const base::android::JavaParamRef<jobject>& obj, | |
| 49 const base::android::JavaParamRef<jobject>& j_observer, | |
| 50 jint num_sites); | |
| 51 void GetURLThumbnail(JNIEnv* env, | |
| 52 const base::android::JavaParamRef<jobject>& obj, | |
| 53 const base::android::JavaParamRef<jstring>& url, | |
| 54 const base::android::JavaParamRef<jobject>& j_callback); | |
| 55 | |
| 56 void AddOrRemoveBlacklistedUrl( | |
| 57 JNIEnv* env, | |
| 58 const base::android::JavaParamRef<jobject>& obj, | |
| 59 const base::android::JavaParamRef<jstring>& j_url, | |
| 60 jboolean add_url); | |
| 61 void RecordTileTypeMetrics( | |
| 62 JNIEnv* env, | |
| 63 const base::android::JavaParamRef<jobject>& obj, | |
| 64 const base::android::JavaParamRef<jintArray>& jtile_types); | |
| 65 void RecordOpenedMostVisitedItem( | |
| 66 JNIEnv* env, | |
| 67 const base::android::JavaParamRef<jobject>& obj, | |
| 68 jint index, | |
| 69 jint tile_type); | |
| 70 | |
| 71 // SupervisedUserServiceObserver implementation. | |
| 72 void OnURLFilterChanged() override; | |
| 73 | |
| 74 // Registers JNI methods. | |
| 75 static bool Register(JNIEnv* env); | |
| 76 | |
| 77 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | |
| 78 | |
| 79 private: | |
| 80 friend class MostVisitedSitesTest; | |
| 81 | |
| 82 // The source of the Most Visited sites. | |
| 83 enum MostVisitedSource { TOP_SITES, SUGGESTIONS_SERVICE, POPULAR, WHITELIST }; | |
| 84 | |
| 85 struct Suggestion { | |
| 86 base::string16 title; | |
| 87 GURL url; | |
| 88 MostVisitedSource source; | |
| 89 | |
| 90 // Only valid for source == WHITELIST (empty otherwise). | |
| 91 base::FilePath whitelist_icon_path; | |
| 92 | |
| 93 // Only valid for source == SUGGESTIONS_SERVICE (-1 otherwise). | |
| 94 int provider_index; | |
| 95 | |
| 96 Suggestion(); | |
| 97 ~Suggestion(); | |
| 98 | |
| 99 // Get the Histogram name associated with the source. | |
| 100 std::string GetSourceHistogramName() const; | |
| 101 | |
| 102 private: | |
| 103 DISALLOW_COPY_AND_ASSIGN(Suggestion); | |
| 104 }; | |
| 105 | |
| 106 using SuggestionsVector = std::vector<scoped_ptr<Suggestion>>; | |
| 107 | |
| 108 ~MostVisitedSites() override; | |
| 109 void QueryMostVisitedURLs(); | |
| 110 | |
| 111 // Initialize the query to Top Sites. Called if the SuggestionsService is not | |
| 112 // enabled, or if it returns no data. | |
| 113 void InitiateTopSitesQuery(); | |
| 114 | |
| 115 // If there's a whitelist entry point for the URL, return the large icon path. | |
| 116 base::FilePath GetWhitelistLargeIconPath(const GURL& url); | |
| 117 | |
| 118 // Callback for when data is available from TopSites. | |
| 119 void OnMostVisitedURLsAvailable( | |
| 120 const history::MostVisitedURLList& visited_list); | |
| 121 | |
| 122 // Callback for when data is available from the SuggestionsService. | |
| 123 void OnSuggestionsProfileAvailable( | |
| 124 const suggestions::SuggestionsProfile& suggestions_profile); | |
| 125 | |
| 126 // Takes the personal suggestions and creates whitelist entry point | |
| 127 // suggestions if necessary. | |
| 128 SuggestionsVector CreateWhitelistEntryPointSuggestions( | |
| 129 const SuggestionsVector& personal_suggestions); | |
| 130 | |
| 131 // Takes the personal and whitelist suggestions and creates popular | |
| 132 // suggestions if necessary. | |
| 133 SuggestionsVector CreatePopularSitesSuggestions( | |
| 134 const SuggestionsVector& personal_suggestions, | |
| 135 const SuggestionsVector& whitelist_suggestions); | |
| 136 | |
| 137 // Takes the personal suggestions, creates and merges in whitelist and popular | |
| 138 // suggestions if appropriate, and saves the new suggestions. | |
| 139 void SaveNewNTPSuggestions(SuggestionsVector* personal_suggestions); | |
| 140 | |
| 141 // Workhorse for SaveNewNTPSuggestions above. Implemented as a separate static | |
| 142 // method for ease of testing. | |
| 143 static SuggestionsVector MergeSuggestions( | |
| 144 SuggestionsVector* personal_suggestions, | |
| 145 SuggestionsVector* whitelist_suggestions, | |
| 146 SuggestionsVector* popular_suggestions, | |
| 147 const std::vector<std::string>& old_sites_url, | |
| 148 const std::vector<bool>& old_sites_is_personal); | |
| 149 | |
| 150 void GetPreviousNTPSites(size_t num_tiles, | |
| 151 std::vector<std::string>* old_sites_url, | |
| 152 std::vector<bool>* old_sites_source) const; | |
| 153 | |
| 154 void SaveCurrentNTPSites(); | |
| 155 | |
| 156 // Takes suggestions from |src_suggestions| and moves them to | |
| 157 // |dst_suggestions| if the suggestion's url/host matches | |
| 158 // |match_urls|/|match_hosts| respectively. Unmatched suggestion indices from | |
| 159 // |src_suggestions| are returned for ease of insertion later. | |
| 160 static std::vector<size_t> InsertMatchingSuggestions( | |
| 161 SuggestionsVector* src_suggestions, | |
| 162 SuggestionsVector* dst_suggestions, | |
| 163 const std::vector<std::string>& match_urls, | |
| 164 const std::vector<std::string>& match_hosts); | |
| 165 | |
| 166 // Inserts suggestions from |src_suggestions| at positions |insert_positions| | |
| 167 // into |dst_suggestions| where ever empty starting from |start_position|. | |
| 168 // Returns the last filled position so that future insertions can start from | |
| 169 // there. | |
| 170 static size_t InsertAllSuggestions( | |
| 171 size_t start_position, | |
| 172 const std::vector<size_t>& insert_positions, | |
| 173 SuggestionsVector* src_suggestions, | |
| 174 SuggestionsVector* dst_suggestions); | |
| 175 | |
| 176 // Notifies the Java side observer about the availability of suggestions. | |
| 177 // Also records impressions UMA if not done already. | |
| 178 void NotifyMostVisitedURLsObserver(); | |
| 179 | |
| 180 void OnPopularSitesAvailable(bool success); | |
| 181 | |
| 182 // Runs on the UI Thread. | |
| 183 void OnLocalThumbnailFetched( | |
| 184 const GURL& url, | |
| 185 scoped_ptr<base::android::ScopedJavaGlobalRef<jobject>> j_callback, | |
| 186 scoped_ptr<SkBitmap> bitmap); | |
| 187 | |
| 188 // Callback for when the thumbnail lookup is complete. | |
| 189 // Runs on the UI Thread. | |
| 190 void OnObtainedThumbnail( | |
| 191 bool is_local_thumbnail, | |
| 192 scoped_ptr<base::android::ScopedJavaGlobalRef<jobject>> j_callback, | |
| 193 const GURL& url, | |
| 194 const SkBitmap* bitmap); | |
| 195 | |
| 196 // Records thumbnail-related UMA histogram metrics. | |
| 197 void RecordThumbnailUMAMetrics(); | |
| 198 | |
| 199 // Records UMA histogram metrics related to the number of impressions. | |
| 200 void RecordImpressionUMAMetrics(); | |
| 201 | |
| 202 // history::TopSitesObserver implementation. | |
| 203 void TopSitesLoaded(history::TopSites* top_sites) override; | |
| 204 void TopSitesChanged(history::TopSites* top_sites, | |
| 205 ChangeReason change_reason) override; | |
| 206 | |
| 207 // The profile whose most visited sites will be queried. | |
| 208 Profile* profile_; | |
| 209 | |
| 210 // The observer to be notified when the list of most visited sites changes. | |
| 211 base::android::ScopedJavaGlobalRef<jobject> observer_; | |
| 212 | |
| 213 // The maximum number of most visited sites to return. | |
| 214 int num_sites_; | |
| 215 | |
| 216 // Whether we have received an initial set of most visited sites (from either | |
| 217 // TopSites or the SuggestionsService). | |
| 218 bool received_most_visited_sites_; | |
| 219 | |
| 220 // Whether we have received the set of popular sites. Immediately set to true | |
| 221 // if popular sites are disabled. | |
| 222 bool received_popular_sites_; | |
| 223 | |
| 224 // Whether we have recorded one-shot UMA metrics such as impressions. They are | |
| 225 // recorded once both the previous flags are true. | |
| 226 bool recorded_uma_; | |
| 227 | |
| 228 scoped_ptr< | |
| 229 suggestions::SuggestionsService::ResponseCallbackList::Subscription> | |
| 230 suggestions_subscription_; | |
| 231 | |
| 232 ScopedObserver<history::TopSites, history::TopSitesObserver> scoped_observer_; | |
| 233 | |
| 234 MostVisitedSource mv_source_; | |
| 235 | |
| 236 scoped_ptr<PopularSites> popular_sites_; | |
| 237 | |
| 238 SuggestionsVector current_suggestions_; | |
| 239 | |
| 240 // For callbacks may be run after destruction. | |
| 241 base::WeakPtrFactory<MostVisitedSites> weak_ptr_factory_; | |
| 242 | |
| 243 DISALLOW_COPY_AND_ASSIGN(MostVisitedSites); | |
| 244 }; | |
| 245 | |
| 246 #endif // CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_ | |
| OLD | NEW |