Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / components / suggestions / suggestions_service.h
blob7f0aab3525adaa6cd9cefb2e16db3910f763b57c
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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_H_
6 #define COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/cancelable_callback.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/threading/thread_checker.h"
18 #include "base/time/time.h"
19 #include "components/keyed_service/core/keyed_service.h"
20 #include "components/suggestions/image_manager.h"
21 #include "components/suggestions/proto/suggestions.pb.h"
22 #include "components/suggestions/suggestions_utils.h"
23 #include "net/url_request/url_fetcher_delegate.h"
24 #include "ui/gfx/image/image_skia.h"
25 #include "url/gurl.h"
27 namespace net {
28 class URLRequestContextGetter;
29 } // namespace net
31 namespace user_prefs {
32 class PrefRegistrySyncable;
33 } // namespace user_prefs
35 namespace suggestions {
37 class BlacklistStore;
38 class SuggestionsStore;
40 extern const char kSuggestionsURL[];
41 extern const char kSuggestionsBlacklistURLPrefix[];
42 extern const char kSuggestionsBlacklistURLParam[];
43 extern const char kSuggestionsBlacklistClearURL[];
44 extern const int64 kDefaultExpiryUsec;
46 // An interface to fetch server suggestions asynchronously.
47 class SuggestionsService : public KeyedService, public net::URLFetcherDelegate {
48 public:
49 typedef base::Callback<void(const SuggestionsProfile&)> ResponseCallback;
51 // Class taking ownership of |suggestions_store|, |thumbnail_manager| and
52 // |blacklist_store|.
53 SuggestionsService(
54 net::URLRequestContextGetter* url_request_context,
55 scoped_ptr<SuggestionsStore> suggestions_store,
56 scoped_ptr<ImageManager> thumbnail_manager,
57 scoped_ptr<BlacklistStore> blacklist_store);
58 ~SuggestionsService() override;
60 // Request suggestions data, which will be passed to |callback|. |sync_state|
61 // will influence the behavior of this function (see SyncState definition).
63 // |sync_state| must be specified based on the current state of the system
64 // (see suggestions::GetSyncState). Callers should call this function again if
65 // sync state changes.
67 // If state allows for a network request, it is initiated unless a pending one
68 // exists, to fill the cache for next time.
69 void FetchSuggestionsData(SyncState sync_state,
70 ResponseCallback callback);
72 // Retrieves stored thumbnail for website |url| asynchronously. Calls
73 // |callback| with Bitmap pointer if found, and NULL otherwise.
74 void GetPageThumbnail(
75 const GURL& url,
76 base::Callback<void(const GURL&, const SkBitmap*)> callback);
78 // Adds a URL to the blacklist cache, invoking |callback| on success or
79 // |fail_callback| otherwise. The URL will eventually be uploaded to the
80 // server.
81 void BlacklistURL(const GURL& candidate_url,
82 const ResponseCallback& callback,
83 const base::Closure& fail_callback);
85 // Removes a URL from the local blacklist, then invokes |callback|. If the URL
86 // cannot be removed, the |fail_callback| is called.
87 void UndoBlacklistURL(const GURL& url,
88 const ResponseCallback& callback,
89 const base::Closure& fail_callback);
91 // Removes all URLs from the blacklist then invokes |callback|.
92 void ClearBlacklist(const ResponseCallback& callback);
94 // Determines which URL a blacklist request was for, irrespective of the
95 // request's status. Returns false if |request| is not a blacklist request.
96 static bool GetBlacklistedUrl(const net::URLFetcher& request, GURL* url);
98 // Register SuggestionsService related prefs in the Profile prefs.
99 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
101 // Sets default timestamp for suggestions which do not have expiry timestamp.
102 void SetDefaultExpiryTimestamp(SuggestionsProfile* suggestions,
103 int64 timestamp_usec);
105 // Issue a network request if there isn't already one happening. Visible for
106 // testing.
107 void IssueRequestIfNoneOngoing(const GURL& url);
109 private:
110 friend class SuggestionsServiceTest;
111 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, BlacklistURL);
112 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, BlacklistURLRequestFails);
113 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, ClearBlacklist);
114 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, UndoBlacklistURL);
115 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, UndoBlacklistURLFailsHelper);
116 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, UpdateBlacklistDelay);
118 // Creates a request to the suggestions service, properly setting headers.
119 scoped_ptr<net::URLFetcher> CreateSuggestionsRequest(const GURL& url);
121 // net::URLFetcherDelegate implementation.
122 // Called when fetch request completes. Parses the received suggestions data,
123 // and dispatches them to callbacks stored in queue.
124 void OnURLFetchComplete(const net::URLFetcher* source) override;
126 // KeyedService implementation.
127 void Shutdown() override;
129 // Loads the cached suggestions (or empty suggestions if no cache) and serves
130 // the requestors with them.
131 void ServeFromCache();
133 // Applies the local blacklist to |suggestions|, then serves the requestors.
134 void FilterAndServe(SuggestionsProfile* suggestions);
136 // Schedules a blacklisting request if the local blacklist isn't empty.
137 void ScheduleBlacklistUpload();
139 // If the local blacklist isn't empty, picks a URL from it and issues a
140 // blacklist request for it.
141 void UploadOneFromBlacklist();
143 // Updates |scheduling_delay_| based on the success of the last request.
144 void UpdateBlacklistDelay(bool last_request_successful);
146 // Adds favicon URLs to suggestions profile.
147 void PopulateFaviconUrls(SuggestionsProfile* suggestions);
149 // Test seams.
150 base::TimeDelta blacklist_delay() const { return scheduling_delay_; }
151 void set_blacklist_delay(base::TimeDelta delay) {
152 scheduling_delay_ = delay; }
154 base::ThreadChecker thread_checker_;
156 net::URLRequestContextGetter* url_request_context_;
158 // The cache for the suggestions.
159 scoped_ptr<SuggestionsStore> suggestions_store_;
161 // Used to obtain server thumbnails, if available.
162 scoped_ptr<ImageManager> thumbnail_manager_;
164 // The local cache for temporary blacklist, until uploaded to the server.
165 scoped_ptr<BlacklistStore> blacklist_store_;
167 // Delay used when scheduling a blacklisting task.
168 base::TimeDelta scheduling_delay_;
170 // Contains the current suggestions fetch request. Will only have a value
171 // while a request is pending, and will be reset by |OnURLFetchComplete| or
172 // if cancelled.
173 scoped_ptr<net::URLFetcher> pending_request_;
175 // The start time of the previous suggestions request. This is used to measure
176 // the latency of requests. Initially zero.
177 base::TimeTicks last_request_started_time_;
179 // The URL to fetch suggestions data from.
180 GURL suggestions_url_;
182 // Prefix for building the blacklisting URL.
183 std::string blacklist_url_prefix_;
185 // Queue of callbacks. These are flushed when fetch request completes.
186 std::vector<ResponseCallback> waiting_requestors_;
188 // For callbacks may be run after destruction.
189 base::WeakPtrFactory<SuggestionsService> weak_ptr_factory_;
191 DISALLOW_COPY_AND_ASSIGN(SuggestionsService);
194 } // namespace suggestions
196 #endif // COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_H_