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> | 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/keyed_service/core/keyed_service.h" |
22 #include "components/suggestions/proto/suggestions.pb.h" | 23 #include "components/suggestions/proto/suggestions.pb.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; |
(...skipping 15 matching lines...) Expand all Loading... | |
43 class PrefRegistrySyncable; | 44 class PrefRegistrySyncable; |
44 } // namespace user_prefs | 45 } // namespace user_prefs |
45 | 46 |
46 namespace suggestions { | 47 namespace suggestions { |
47 | 48 |
48 class BlacklistStore; | 49 class BlacklistStore; |
49 class ImageManager; | 50 class ImageManager; |
50 class SuggestionsStore; | 51 class SuggestionsStore; |
51 | 52 |
52 // An interface to fetch server suggestions asynchronously. | 53 // An interface to fetch server suggestions asynchronously. |
53 class SuggestionsService : public KeyedService, | 54 class SuggestionsService : public KeyedService { |
54 public net::URLFetcherDelegate, | |
55 public syncer::SyncServiceObserver { | |
56 public: | 55 public: |
57 using ResponseCallback = base::Callback<void(const SuggestionsProfile&)>; | 56 using ResponseCallback = base::Callback<void(const SuggestionsProfile&)>; |
58 using BitmapCallback = base::Callback<void(const GURL&, const gfx::Image&)>; | 57 using BitmapCallback = base::Callback<void(const GURL&, const gfx::Image&)>; |
59 | 58 |
60 using ResponseCallbackList = | 59 using ResponseCallbackList = |
61 base::CallbackList<void(const SuggestionsProfile&)>; | 60 base::CallbackList<void(const SuggestionsProfile&)>; |
62 | 61 |
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 | 62 // 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, | 63 // is no pending request. Returns true iff sync state allowed for a request, |
74 // whether a new request was actually sent or not. | 64 // whether a new request was actually sent or not. |
75 bool FetchSuggestionsData(); | 65 virtual bool FetchSuggestionsData() = 0; |
76 | 66 |
77 // Returns the current set of suggestions from the cache. | 67 // Returns the current set of suggestions from the cache. |
78 SuggestionsProfile GetSuggestionsDataFromCache() const; | 68 virtual base::Optional<SuggestionsProfile> GetSuggestionsDataFromCache() |
69 const = 0; | |
79 | 70 |
80 // Adds a callback that is called when the suggestions are updated. | 71 // Adds a callback that is called when the suggestions are updated. |
81 std::unique_ptr<ResponseCallbackList::Subscription> AddCallback( | 72 virtual std::unique_ptr<ResponseCallbackList::Subscription> AddCallback( |
82 const ResponseCallback& callback) WARN_UNUSED_RESULT; | 73 const ResponseCallback& callback) WARN_UNUSED_RESULT = 0; |
83 | 74 |
84 // Retrieves stored thumbnail for website |url| asynchronously. Calls | 75 // Retrieves stored thumbnail for website |url| asynchronously. Calls |
85 // |callback| with Bitmap pointer if found, and NULL otherwise. | 76 // |callback| with Bitmap pointer if found, and NULL otherwise. |
86 void GetPageThumbnail(const GURL& url, const BitmapCallback& callback); | 77 virtual void GetPageThumbnail(const GURL& url, |
78 const BitmapCallback& callback) = 0; | |
87 | 79 |
88 // A version of |GetPageThumbnail| that explicitly supplies the download URL | 80 // A version of |GetPageThumbnail| that explicitly supplies the download URL |
89 // for the thumbnail. Replaces any pre-existing thumbnail URL with the | 81 // for the thumbnail. Replaces any pre-existing thumbnail URL with the |
90 // supplied one. | 82 // supplied one. |
91 void GetPageThumbnailWithURL(const GURL& url, | 83 virtual void GetPageThumbnailWithURL(const GURL& url, |
92 const GURL& thumbnail_url, | 84 const GURL& thumbnail_url, |
93 const BitmapCallback& callback); | 85 const BitmapCallback& callback) = 0; |
94 | 86 |
95 // Adds a URL to the blacklist cache, returning true on success or false on | 87 // 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. | 88 // failure. The URL will eventually be uploaded to the server. |
97 bool BlacklistURL(const GURL& candidate_url); | 89 virtual bool BlacklistURL(const GURL& candidate_url) = 0; |
98 | 90 |
99 // Removes a URL from the local blacklist, returning true on success or false | 91 // Removes a URL from the local blacklist, returning true on success or false |
100 // on failure. | 92 // on failure. |
101 bool UndoBlacklistURL(const GURL& url); | 93 virtual bool UndoBlacklistURL(const GURL& url) = 0; |
102 | 94 |
103 // Removes all URLs from the blacklist. | 95 // Removes all URLs from the blacklist. |
104 void ClearBlacklist(); | 96 virtual void ClearBlacklist() = 0; |
97 }; | |
98 | |
99 // Actual (non-test) implementation of the SuggestionsService interface. | |
100 class SuggestionsServiceImpl : public SuggestionsService, | |
Marc Treib
2016/12/13 15:12:07
Hm, I think nobody expect the factory (and tests)
mastiz
2016/12/13 16:18:49
Done, moved impl to dedicated file, ptal. git cl f
Marc Treib
2016/12/13 16:30:27
Nah that's fine, IMO it's generally a Good Thing t
| |
101 public net::URLFetcherDelegate, | |
102 public syncer::SyncServiceObserver { | |
103 public: | |
104 SuggestionsServiceImpl(const SigninManagerBase* signin_manager, | |
105 OAuth2TokenService* token_service, | |
106 syncer::SyncService* sync_service, | |
107 net::URLRequestContextGetter* url_request_context, | |
108 std::unique_ptr<SuggestionsStore> suggestions_store, | |
109 std::unique_ptr<ImageManager> thumbnail_manager, | |
110 std::unique_ptr<BlacklistStore> blacklist_store); | |
111 ~SuggestionsServiceImpl() override; | |
112 | |
113 // SuggestionsService implementation. | |
114 bool FetchSuggestionsData() override; | |
115 base::Optional<SuggestionsProfile> GetSuggestionsDataFromCache() | |
116 const override; | |
117 std::unique_ptr<ResponseCallbackList::Subscription> AddCallback( | |
118 const ResponseCallback& callback) override WARN_UNUSED_RESULT; | |
119 void GetPageThumbnail(const GURL& url, | |
120 const BitmapCallback& callback) override; | |
121 void GetPageThumbnailWithURL(const GURL& url, | |
122 const GURL& thumbnail_url, | |
123 const BitmapCallback& callback) override; | |
124 bool BlacklistURL(const GURL& candidate_url) override; | |
125 bool UndoBlacklistURL(const GURL& url) override; | |
126 void ClearBlacklist() override; | |
105 | 127 |
106 // Determines which URL a blacklist request was for, irrespective of the | 128 // Determines which URL a blacklist request was for, irrespective of the |
107 // request's status. Returns false if |request| is not a blacklist request. | 129 // request's status. Returns false if |request| is not a blacklist request. |
108 static bool GetBlacklistedUrl(const net::URLFetcher& request, GURL* url); | 130 static bool GetBlacklistedUrl(const net::URLFetcher& request, GURL* url); |
109 | 131 |
110 // Register SuggestionsService related prefs in the Profile prefs. | 132 // Register SuggestionsService related prefs in the Profile prefs. |
111 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | 133 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
112 | 134 |
113 private: | 135 private: |
114 friend class SuggestionsServiceTest; | 136 friend class SuggestionsServiceTest; |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 // if cancelled. | 237 // if cancelled. |
216 std::unique_ptr<net::URLFetcher> pending_request_; | 238 std::unique_ptr<net::URLFetcher> pending_request_; |
217 | 239 |
218 // The start time of the previous suggestions request. This is used to measure | 240 // The start time of the previous suggestions request. This is used to measure |
219 // the latency of requests. Initially zero. | 241 // the latency of requests. Initially zero. |
220 base::TimeTicks last_request_started_time_; | 242 base::TimeTicks last_request_started_time_; |
221 | 243 |
222 ResponseCallbackList callback_list_; | 244 ResponseCallbackList callback_list_; |
223 | 245 |
224 // For callbacks may be run after destruction. | 246 // For callbacks may be run after destruction. |
225 base::WeakPtrFactory<SuggestionsService> weak_ptr_factory_; | 247 base::WeakPtrFactory<SuggestionsServiceImpl> weak_ptr_factory_; |
226 | 248 |
227 DISALLOW_COPY_AND_ASSIGN(SuggestionsService); | 249 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceImpl); |
228 }; | 250 }; |
229 | 251 |
230 } // namespace suggestions | 252 } // namespace suggestions |
231 | 253 |
232 #endif // COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_H_ | 254 #endif // COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_H_ |
OLD | NEW |