OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_IMPL_H_ |
6 #define COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_H_ | 6 #define COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_IMPL_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/callback_list.h" | 14 #include "base/callback_list.h" |
15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 18 #include "base/optional.h" |
18 #include "base/scoped_observer.h" | 19 #include "base/scoped_observer.h" |
19 #include "base/threading/thread_checker.h" | 20 #include "base/threading/thread_checker.h" |
20 #include "base/time/time.h" | 21 #include "base/time/time.h" |
21 #include "components/keyed_service/core/keyed_service.h" | |
22 #include "components/suggestions/proto/suggestions.pb.h" | 22 #include "components/suggestions/proto/suggestions.pb.h" |
| 23 #include "components/suggestions/suggestions_service.h" |
23 #include "components/sync/driver/sync_service_observer.h" | 24 #include "components/sync/driver/sync_service_observer.h" |
24 #include "net/url_request/url_fetcher_delegate.h" | 25 #include "net/url_request/url_fetcher_delegate.h" |
25 #include "url/gurl.h" | 26 #include "url/gurl.h" |
26 | 27 |
27 class OAuth2TokenService; | 28 class OAuth2TokenService; |
28 class SigninManagerBase; | 29 class SigninManagerBase; |
29 | 30 |
30 namespace gfx { | |
31 class Image; | |
32 } // namespce gfx | |
33 | |
34 namespace net { | 31 namespace net { |
35 class URLRequestContextGetter; | 32 class URLRequestContextGetter; |
36 } // namespace net | 33 } // namespace net |
37 | 34 |
38 namespace syncer { | 35 namespace syncer { |
39 class SyncService; | 36 class SyncService; |
40 } // namespace syncer | 37 } // namespace syncer |
41 | 38 |
42 namespace user_prefs { | 39 namespace user_prefs { |
43 class PrefRegistrySyncable; | 40 class PrefRegistrySyncable; |
44 } // namespace user_prefs | 41 } // namespace user_prefs |
45 | 42 |
46 namespace suggestions { | 43 namespace suggestions { |
47 | 44 |
48 class BlacklistStore; | 45 class BlacklistStore; |
49 class ImageManager; | 46 class ImageManager; |
50 class SuggestionsStore; | 47 class SuggestionsStore; |
51 | 48 |
52 // An interface to fetch server suggestions asynchronously. | 49 // Actual (non-test) implementation of the SuggestionsService interface. |
53 class SuggestionsService : public KeyedService, | 50 class SuggestionsServiceImpl : public SuggestionsService, |
54 public net::URLFetcherDelegate, | 51 public net::URLFetcherDelegate, |
55 public syncer::SyncServiceObserver { | 52 public syncer::SyncServiceObserver { |
56 public: | 53 public: |
57 using ResponseCallback = base::Callback<void(const SuggestionsProfile&)>; | 54 SuggestionsServiceImpl(const SigninManagerBase* signin_manager, |
58 using BitmapCallback = base::Callback<void(const GURL&, const gfx::Image&)>; | 55 OAuth2TokenService* token_service, |
| 56 syncer::SyncService* sync_service, |
| 57 net::URLRequestContextGetter* url_request_context, |
| 58 std::unique_ptr<SuggestionsStore> suggestions_store, |
| 59 std::unique_ptr<ImageManager> thumbnail_manager, |
| 60 std::unique_ptr<BlacklistStore> blacklist_store); |
| 61 ~SuggestionsServiceImpl() override; |
59 | 62 |
60 using ResponseCallbackList = | 63 // SuggestionsService implementation. |
61 base::CallbackList<void(const SuggestionsProfile&)>; | 64 bool FetchSuggestionsData() override; |
62 | 65 base::Optional<SuggestionsProfile> GetSuggestionsDataFromCache() |
63 SuggestionsService(const SigninManagerBase* signin_manager, | 66 const override; |
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 | |
73 // is no pending request. Returns true iff sync state allowed for a request, | |
74 // whether a new request was actually sent or not. | |
75 bool FetchSuggestionsData(); | |
76 | |
77 // Returns the current set of suggestions from the cache. | |
78 SuggestionsProfile GetSuggestionsDataFromCache() const; | |
79 | |
80 // Adds a callback that is called when the suggestions are updated. | |
81 std::unique_ptr<ResponseCallbackList::Subscription> AddCallback( | 67 std::unique_ptr<ResponseCallbackList::Subscription> AddCallback( |
82 const ResponseCallback& callback) WARN_UNUSED_RESULT; | 68 const ResponseCallback& callback) override WARN_UNUSED_RESULT; |
83 | 69 void GetPageThumbnail(const GURL& url, |
84 // Retrieves stored thumbnail for website |url| asynchronously. Calls | 70 const BitmapCallback& callback) override; |
85 // |callback| with Bitmap pointer if found, and NULL otherwise. | |
86 void GetPageThumbnail(const GURL& url, const BitmapCallback& callback); | |
87 | |
88 // A version of |GetPageThumbnail| that explicitly supplies the download URL | |
89 // for the thumbnail. Replaces any pre-existing thumbnail URL with the | |
90 // supplied one. | |
91 void GetPageThumbnailWithURL(const GURL& url, | 71 void GetPageThumbnailWithURL(const GURL& url, |
92 const GURL& thumbnail_url, | 72 const GURL& thumbnail_url, |
93 const BitmapCallback& callback); | 73 const BitmapCallback& callback) override; |
94 | 74 bool BlacklistURL(const GURL& candidate_url) override; |
95 // Adds a URL to the blacklist cache, returning true on success or false on | 75 bool UndoBlacklistURL(const GURL& url) override; |
96 // failure. The URL will eventually be uploaded to the server. | 76 void ClearBlacklist() override; |
97 bool BlacklistURL(const GURL& candidate_url); | |
98 | |
99 // Removes a URL from the local blacklist, returning true on success or false | |
100 // on failure. | |
101 bool UndoBlacklistURL(const GURL& url); | |
102 | |
103 // Removes all URLs from the blacklist. | |
104 void ClearBlacklist(); | |
105 | 77 |
106 // Determines which URL a blacklist request was for, irrespective of the | 78 // Determines which URL a blacklist request was for, irrespective of the |
107 // request's status. Returns false if |request| is not a blacklist request. | 79 // request's status. Returns false if |request| is not a blacklist request. |
108 static bool GetBlacklistedUrl(const net::URLFetcher& request, GURL* url); | 80 static bool GetBlacklistedUrl(const net::URLFetcher& request, GURL* url); |
109 | 81 |
110 // Register SuggestionsService related prefs in the Profile prefs. | 82 // Register SuggestionsService related prefs in the Profile prefs. |
111 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | 83 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
112 | 84 |
113 private: | 85 private: |
114 friend class SuggestionsServiceTest; | 86 friend class SuggestionsServiceTest; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 void UploadOneFromBlacklist(); | 148 void UploadOneFromBlacklist(); |
177 | 149 |
178 // Updates |scheduling_delay_| based on the success of the last request. | 150 // Updates |scheduling_delay_| based on the success of the last request. |
179 void UpdateBlacklistDelay(bool last_request_successful); | 151 void UpdateBlacklistDelay(bool last_request_successful); |
180 | 152 |
181 // Adds extra data to suggestions profile. | 153 // Adds extra data to suggestions profile. |
182 void PopulateExtraData(SuggestionsProfile* suggestions); | 154 void PopulateExtraData(SuggestionsProfile* suggestions); |
183 | 155 |
184 // Test seams. | 156 // Test seams. |
185 base::TimeDelta blacklist_delay() const { return scheduling_delay_; } | 157 base::TimeDelta blacklist_delay() const { return scheduling_delay_; } |
186 void set_blacklist_delay(base::TimeDelta delay) { | 158 void set_blacklist_delay(base::TimeDelta delay) { scheduling_delay_ = delay; } |
187 scheduling_delay_ = delay; } | |
188 | 159 |
189 base::ThreadChecker thread_checker_; | 160 base::ThreadChecker thread_checker_; |
190 | 161 |
191 syncer::SyncService* sync_service_; | 162 syncer::SyncService* sync_service_; |
192 ScopedObserver<syncer::SyncService, syncer::SyncServiceObserver> | 163 ScopedObserver<syncer::SyncService, syncer::SyncServiceObserver> |
193 sync_service_observer_; | 164 sync_service_observer_; |
194 | 165 |
195 net::URLRequestContextGetter* url_request_context_; | 166 net::URLRequestContextGetter* url_request_context_; |
196 | 167 |
197 // The cache for the suggestions. | 168 // The cache for the suggestions. |
(...skipping 17 matching lines...) Expand all Loading... |
215 // if cancelled. | 186 // if cancelled. |
216 std::unique_ptr<net::URLFetcher> pending_request_; | 187 std::unique_ptr<net::URLFetcher> pending_request_; |
217 | 188 |
218 // The start time of the previous suggestions request. This is used to measure | 189 // The start time of the previous suggestions request. This is used to measure |
219 // the latency of requests. Initially zero. | 190 // the latency of requests. Initially zero. |
220 base::TimeTicks last_request_started_time_; | 191 base::TimeTicks last_request_started_time_; |
221 | 192 |
222 ResponseCallbackList callback_list_; | 193 ResponseCallbackList callback_list_; |
223 | 194 |
224 // For callbacks may be run after destruction. | 195 // For callbacks may be run after destruction. |
225 base::WeakPtrFactory<SuggestionsService> weak_ptr_factory_; | 196 base::WeakPtrFactory<SuggestionsServiceImpl> weak_ptr_factory_; |
226 | 197 |
227 DISALLOW_COPY_AND_ASSIGN(SuggestionsService); | 198 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceImpl); |
228 }; | 199 }; |
229 | 200 |
230 } // namespace suggestions | 201 } // namespace suggestions |
231 | 202 |
232 #endif // COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_H_ | 203 #endif // COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_IMPL_H_ |
OLD | NEW |