| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_H_ | 5 #ifndef COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_H_ |
| 6 #define COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_H_ | 6 #define COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <memory> | 8 #include <memory> |
| 11 #include <string> | |
| 12 | 9 |
| 13 #include "base/callback.h" | 10 #include "base/callback.h" |
| 14 #include "base/callback_list.h" | 11 #include "base/callback_list.h" |
| 15 #include "base/gtest_prod_util.h" | |
| 16 #include "base/macros.h" | 12 #include "base/macros.h" |
| 17 #include "base/memory/weak_ptr.h" | 13 #include "base/optional.h" |
| 18 #include "base/scoped_observer.h" | |
| 19 #include "base/threading/thread_checker.h" | |
| 20 #include "base/time/time.h" | |
| 21 #include "components/keyed_service/core/keyed_service.h" | 14 #include "components/keyed_service/core/keyed_service.h" |
| 22 #include "components/suggestions/proto/suggestions.pb.h" | 15 #include "components/suggestions/proto/suggestions.pb.h" |
| 23 #include "components/sync/driver/sync_service_observer.h" | |
| 24 #include "net/url_request/url_fetcher_delegate.h" | |
| 25 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 26 | 17 |
| 27 class OAuth2TokenService; | |
| 28 class SigninManagerBase; | |
| 29 | |
| 30 namespace gfx { | 18 namespace gfx { |
| 31 class Image; | 19 class Image; |
| 32 } // namespce gfx | 20 } // namespce gfx |
| 33 | 21 |
| 34 namespace net { | |
| 35 class URLRequestContextGetter; | |
| 36 } // namespace net | |
| 37 | |
| 38 namespace syncer { | |
| 39 class SyncService; | |
| 40 } // namespace syncer | |
| 41 | |
| 42 namespace user_prefs { | |
| 43 class PrefRegistrySyncable; | |
| 44 } // namespace user_prefs | |
| 45 | |
| 46 namespace suggestions { | 22 namespace suggestions { |
| 47 | 23 |
| 48 class BlacklistStore; | |
| 49 class ImageManager; | |
| 50 class SuggestionsStore; | |
| 51 | |
| 52 // An interface to fetch server suggestions asynchronously. | 24 // An interface to fetch server suggestions asynchronously. |
| 53 class SuggestionsService : public KeyedService, | 25 class SuggestionsService : public KeyedService { |
| 54 public net::URLFetcherDelegate, | |
| 55 public syncer::SyncServiceObserver { | |
| 56 public: | 26 public: |
| 57 using ResponseCallback = base::Callback<void(const SuggestionsProfile&)>; | 27 using ResponseCallback = base::Callback<void(const SuggestionsProfile&)>; |
| 58 using BitmapCallback = base::Callback<void(const GURL&, const gfx::Image&)>; | 28 using BitmapCallback = base::Callback<void(const GURL&, const gfx::Image&)>; |
| 59 | 29 |
| 60 using ResponseCallbackList = | 30 using ResponseCallbackList = |
| 61 base::CallbackList<void(const SuggestionsProfile&)>; | 31 base::CallbackList<void(const SuggestionsProfile&)>; |
| 62 | 32 |
| 63 SuggestionsService(const SigninManagerBase* signin_manager, | |
| 64 OAuth2TokenService* token_service, | |
| 65 syncer::SyncService* sync_service, | |
| 66 net::URLRequestContextGetter* url_request_context, | |
| 67 std::unique_ptr<SuggestionsStore> suggestions_store, | |
| 68 std::unique_ptr<ImageManager> thumbnail_manager, | |
| 69 std::unique_ptr<BlacklistStore> blacklist_store); | |
| 70 ~SuggestionsService() override; | |
| 71 | |
| 72 // Initiates a network request for suggestions if sync state allows and there | 33 // Initiates a network request for suggestions if sync state allows and there |
| 73 // is no pending request. Returns true iff sync state allowed for a request, | 34 // is no pending request. Returns true iff sync state allowed for a request, |
| 74 // whether a new request was actually sent or not. | 35 // whether a new request was actually sent or not. |
| 75 bool FetchSuggestionsData(); | 36 virtual bool FetchSuggestionsData() = 0; |
| 76 | 37 |
| 77 // Returns the current set of suggestions from the cache. | 38 // Returns the current set of suggestions from the cache. |
| 78 SuggestionsProfile GetSuggestionsDataFromCache() const; | 39 virtual base::Optional<SuggestionsProfile> GetSuggestionsDataFromCache() |
| 40 const = 0; |
| 79 | 41 |
| 80 // Adds a callback that is called when the suggestions are updated. | 42 // Adds a callback that is called when the suggestions are updated. |
| 81 std::unique_ptr<ResponseCallbackList::Subscription> AddCallback( | 43 virtual std::unique_ptr<ResponseCallbackList::Subscription> AddCallback( |
| 82 const ResponseCallback& callback) WARN_UNUSED_RESULT; | 44 const ResponseCallback& callback) WARN_UNUSED_RESULT = 0; |
| 83 | 45 |
| 84 // Retrieves stored thumbnail for website |url| asynchronously. Calls | 46 // Retrieves stored thumbnail for website |url| asynchronously. Calls |
| 85 // |callback| with Bitmap pointer if found, and NULL otherwise. | 47 // |callback| with Bitmap pointer if found, and NULL otherwise. |
| 86 void GetPageThumbnail(const GURL& url, const BitmapCallback& callback); | 48 virtual void GetPageThumbnail(const GURL& url, |
| 49 const BitmapCallback& callback) = 0; |
| 87 | 50 |
| 88 // A version of |GetPageThumbnail| that explicitly supplies the download URL | 51 // A version of |GetPageThumbnail| that explicitly supplies the download URL |
| 89 // for the thumbnail. Replaces any pre-existing thumbnail URL with the | 52 // for the thumbnail. Replaces any pre-existing thumbnail URL with the |
| 90 // supplied one. | 53 // supplied one. |
| 91 void GetPageThumbnailWithURL(const GURL& url, | 54 virtual void GetPageThumbnailWithURL(const GURL& url, |
| 92 const GURL& thumbnail_url, | 55 const GURL& thumbnail_url, |
| 93 const BitmapCallback& callback); | 56 const BitmapCallback& callback) = 0; |
| 94 | 57 |
| 95 // Adds a URL to the blacklist cache, returning true on success or false on | 58 // Adds a URL to the blacklist cache, returning true on success or false on |
| 96 // failure. The URL will eventually be uploaded to the server. | 59 // failure. The URL will eventually be uploaded to the server. |
| 97 bool BlacklistURL(const GURL& candidate_url); | 60 virtual bool BlacklistURL(const GURL& candidate_url) = 0; |
| 98 | 61 |
| 99 // Removes a URL from the local blacklist, returning true on success or false | 62 // Removes a URL from the local blacklist, returning true on success or false |
| 100 // on failure. | 63 // on failure. |
| 101 bool UndoBlacklistURL(const GURL& url); | 64 virtual bool UndoBlacklistURL(const GURL& url) = 0; |
| 102 | 65 |
| 103 // Removes all URLs from the blacklist. | 66 // Removes all URLs from the blacklist. |
| 104 void ClearBlacklist(); | 67 virtual void ClearBlacklist() = 0; |
| 105 | |
| 106 // Determines which URL a blacklist request was for, irrespective of the | |
| 107 // request's status. Returns false if |request| is not a blacklist request. | |
| 108 static bool GetBlacklistedUrl(const net::URLFetcher& request, GURL* url); | |
| 109 | |
| 110 // Register SuggestionsService related prefs in the Profile prefs. | |
| 111 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | |
| 112 | |
| 113 private: | |
| 114 friend class SuggestionsServiceTest; | |
| 115 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, FetchSuggestionsData); | |
| 116 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, | |
| 117 FetchSuggestionsDataSyncDisabled); | |
| 118 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, | |
| 119 FetchSuggestionsDataNoAccessToken); | |
| 120 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, | |
| 121 IssueRequestIfNoneOngoingError); | |
| 122 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, | |
| 123 IssueRequestIfNoneOngoingResponseNotOK); | |
| 124 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, BlacklistURL); | |
| 125 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, BlacklistURLRequestFails); | |
| 126 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, ClearBlacklist); | |
| 127 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, UndoBlacklistURL); | |
| 128 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, GetBlacklistedUrl); | |
| 129 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, UpdateBlacklistDelay); | |
| 130 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, CheckDefaultTimeStamps); | |
| 131 | |
| 132 // Helpers to build the various suggestions URLs. These are static members | |
| 133 // rather than local functions in the .cc file to make them accessible to | |
| 134 // tests. | |
| 135 static GURL BuildSuggestionsURL(); | |
| 136 static std::string BuildSuggestionsBlacklistURLPrefix(); | |
| 137 static GURL BuildSuggestionsBlacklistURL(const GURL& candidate_url); | |
| 138 static GURL BuildSuggestionsBlacklistClearURL(); | |
| 139 | |
| 140 // syncer::SyncServiceObserver implementation. | |
| 141 void OnStateChanged() override; | |
| 142 | |
| 143 // Sets default timestamp for suggestions which do not have expiry timestamp. | |
| 144 void SetDefaultExpiryTimestamp(SuggestionsProfile* suggestions, | |
| 145 int64_t timestamp_usec); | |
| 146 | |
| 147 // Issues a network request if there isn't already one happening. | |
| 148 void IssueRequestIfNoneOngoing(const GURL& url); | |
| 149 | |
| 150 // Issues a network request for suggestions (fetch, blacklist, or clear | |
| 151 // blacklist, depending on |url|). |access_token| is used only if OAuth2 | |
| 152 // authentication is enabled. | |
| 153 void IssueSuggestionsRequest(const GURL& url, | |
| 154 const std::string& access_token); | |
| 155 | |
| 156 // Creates a request to the suggestions service, properly setting headers. | |
| 157 // If OAuth2 authentication is enabled, |access_token| should be a valid | |
| 158 // OAuth2 access token, and will be written into an auth header. | |
| 159 std::unique_ptr<net::URLFetcher> CreateSuggestionsRequest( | |
| 160 const GURL& url, | |
| 161 const std::string& access_token); | |
| 162 | |
| 163 // net::URLFetcherDelegate implementation. | |
| 164 // Called when fetch request completes. Parses the received suggestions data, | |
| 165 // and dispatches them to callbacks stored in queue. | |
| 166 void OnURLFetchComplete(const net::URLFetcher* source) override; | |
| 167 | |
| 168 // KeyedService implementation. | |
| 169 void Shutdown() override; | |
| 170 | |
| 171 // Schedules a blacklisting request if the local blacklist isn't empty. | |
| 172 void ScheduleBlacklistUpload(); | |
| 173 | |
| 174 // If the local blacklist isn't empty, picks a URL from it and issues a | |
| 175 // blacklist request for it. | |
| 176 void UploadOneFromBlacklist(); | |
| 177 | |
| 178 // Updates |scheduling_delay_| based on the success of the last request. | |
| 179 void UpdateBlacklistDelay(bool last_request_successful); | |
| 180 | |
| 181 // Adds extra data to suggestions profile. | |
| 182 void PopulateExtraData(SuggestionsProfile* suggestions); | |
| 183 | |
| 184 // Test seams. | |
| 185 base::TimeDelta blacklist_delay() const { return scheduling_delay_; } | |
| 186 void set_blacklist_delay(base::TimeDelta delay) { | |
| 187 scheduling_delay_ = delay; } | |
| 188 | |
| 189 base::ThreadChecker thread_checker_; | |
| 190 | |
| 191 syncer::SyncService* sync_service_; | |
| 192 ScopedObserver<syncer::SyncService, syncer::SyncServiceObserver> | |
| 193 sync_service_observer_; | |
| 194 | |
| 195 net::URLRequestContextGetter* url_request_context_; | |
| 196 | |
| 197 // The cache for the suggestions. | |
| 198 std::unique_ptr<SuggestionsStore> suggestions_store_; | |
| 199 | |
| 200 // Used to obtain server thumbnails, if available. | |
| 201 std::unique_ptr<ImageManager> thumbnail_manager_; | |
| 202 | |
| 203 // The local cache for temporary blacklist, until uploaded to the server. | |
| 204 std::unique_ptr<BlacklistStore> blacklist_store_; | |
| 205 | |
| 206 // Delay used when scheduling a blacklisting task. | |
| 207 base::TimeDelta scheduling_delay_; | |
| 208 | |
| 209 // Helper for fetching OAuth2 access tokens. | |
| 210 class AccessTokenFetcher; | |
| 211 std::unique_ptr<AccessTokenFetcher> token_fetcher_; | |
| 212 | |
| 213 // Contains the current suggestions fetch request. Will only have a value | |
| 214 // while a request is pending, and will be reset by |OnURLFetchComplete| or | |
| 215 // if cancelled. | |
| 216 std::unique_ptr<net::URLFetcher> pending_request_; | |
| 217 | |
| 218 // The start time of the previous suggestions request. This is used to measure | |
| 219 // the latency of requests. Initially zero. | |
| 220 base::TimeTicks last_request_started_time_; | |
| 221 | |
| 222 ResponseCallbackList callback_list_; | |
| 223 | |
| 224 // For callbacks may be run after destruction. | |
| 225 base::WeakPtrFactory<SuggestionsService> weak_ptr_factory_; | |
| 226 | |
| 227 DISALLOW_COPY_AND_ASSIGN(SuggestionsService); | |
| 228 }; | 68 }; |
| 229 | 69 |
| 230 } // namespace suggestions | 70 } // namespace suggestions |
| 231 | 71 |
| 232 #endif // COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_H_ | 72 #endif // COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_H_ |
| OLD | NEW |