Add an exponential backoff to rechecking the app list doodle.
[chromium-blink-merge.git] / components / suggestions / suggestions_service.h
blobb0d0f4c54461f1dbdd0e47af80c3c1d0e1d28d00
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 int64 kDefaultExpiryUsec;
45 // An interface to fetch server suggestions asynchronously.
46 class SuggestionsService : public KeyedService, public net::URLFetcherDelegate {
47 public:
48 typedef base::Callback<void(const SuggestionsProfile&)> ResponseCallback;
50 // Class taking ownership of |suggestions_store|, |thumbnail_manager| and
51 // |blacklist_store|.
52 SuggestionsService(
53 net::URLRequestContextGetter* url_request_context,
54 scoped_ptr<SuggestionsStore> suggestions_store,
55 scoped_ptr<ImageManager> thumbnail_manager,
56 scoped_ptr<BlacklistStore> blacklist_store);
57 ~SuggestionsService() override;
59 // Request suggestions data, which will be passed to |callback|. |sync_state|
60 // will influence the behavior of this function (see SyncState definition).
62 // |sync_state| must be specified based on the current state of the system
63 // (see suggestions::GetSyncState). Callers should call this function again if
64 // sync state changes.
66 // If state allows for a network request, it is initiated unless a pending one
67 // exists, to fill the cache for next time.
68 void FetchSuggestionsData(SyncState sync_state,
69 ResponseCallback callback);
71 // Retrieves stored thumbnail for website |url| asynchronously. Calls
72 // |callback| with Bitmap pointer if found, and NULL otherwise.
73 void GetPageThumbnail(
74 const GURL& url,
75 base::Callback<void(const GURL&, const SkBitmap*)> callback);
77 // Adds a URL to the blacklist cache, invoking |callback| on success or
78 // |fail_callback| otherwise. The URL will eventually be uploaded to the
79 // server.
80 void BlacklistURL(const GURL& candidate_url,
81 const ResponseCallback& callback,
82 const base::Closure& fail_callback);
84 // Removes a URL from the local blacklist, then invokes |callback|. If the URL
85 // cannot be removed, the |fail_callback| is called.
86 void UndoBlacklistURL(const GURL& url,
87 const ResponseCallback& callback,
88 const base::Closure& fail_callback);
90 // Determines which URL a blacklist request was for, irrespective of the
91 // request's status. Returns false if |request| is not a blacklist request.
92 static bool GetBlacklistedUrl(const net::URLFetcher& request, GURL* url);
94 // Register SuggestionsService related prefs in the Profile prefs.
95 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
97 // Sets default timestamp for suggestions which do not have expiry timestamp.
98 void SetDefaultExpiryTimestamp(SuggestionsProfile* suggestions,
99 int64 timestamp_usec);
101 // Issue a network request if there isn't already one happening. Visible for
102 // testing.
103 void IssueRequestIfNoneOngoing(const GURL& url);
105 private:
106 friend class SuggestionsServiceTest;
107 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, BlacklistURL);
108 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, BlacklistURLRequestFails);
109 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, UndoBlacklistURL);
110 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, UndoBlacklistURLFailsHelper);
111 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, UpdateBlacklistDelay);
113 // Creates a request to the suggestions service, properly setting headers.
114 net::URLFetcher* CreateSuggestionsRequest(const GURL& url);
116 // net::URLFetcherDelegate implementation.
117 // Called when fetch request completes. Parses the received suggestions data,
118 // and dispatches them to callbacks stored in queue.
119 void OnURLFetchComplete(const net::URLFetcher* source) override;
121 // KeyedService implementation.
122 void Shutdown() override;
124 // Loads the cached suggestions (or empty suggestions if no cache) and serves
125 // the requestors with them.
126 void ServeFromCache();
128 // Applies the local blacklist to |suggestions|, then serves the requestors.
129 void FilterAndServe(SuggestionsProfile* suggestions);
131 // Schedules a blacklisting request if the local blacklist isn't empty.
132 void ScheduleBlacklistUpload();
134 // If the local blacklist isn't empty, picks a URL from it and issues a
135 // blacklist request for it.
136 void UploadOneFromBlacklist();
138 // Updates |scheduling_delay_| based on the success of the last request.
139 void UpdateBlacklistDelay(bool last_request_successful);
141 // Test seams.
142 base::TimeDelta blacklist_delay() const { return scheduling_delay_; }
143 void set_blacklist_delay(base::TimeDelta delay) {
144 scheduling_delay_ = delay; }
146 base::ThreadChecker thread_checker_;
148 net::URLRequestContextGetter* url_request_context_;
150 // The cache for the suggestions.
151 scoped_ptr<SuggestionsStore> suggestions_store_;
153 // Used to obtain server thumbnails, if available.
154 scoped_ptr<ImageManager> thumbnail_manager_;
156 // The local cache for temporary blacklist, until uploaded to the server.
157 scoped_ptr<BlacklistStore> blacklist_store_;
159 // Delay used when scheduling a blacklisting task.
160 base::TimeDelta scheduling_delay_;
162 // Contains the current suggestions fetch request. Will only have a value
163 // while a request is pending, and will be reset by |OnURLFetchComplete| or
164 // if cancelled.
165 scoped_ptr<net::URLFetcher> pending_request_;
167 // The start time of the previous suggestions request. This is used to measure
168 // the latency of requests. Initially zero.
169 base::TimeTicks last_request_started_time_;
171 // The URL to fetch suggestions data from.
172 GURL suggestions_url_;
174 // Prefix for building the blacklisting URL.
175 std::string blacklist_url_prefix_;
177 // Queue of callbacks. These are flushed when fetch request completes.
178 std::vector<ResponseCallback> waiting_requestors_;
180 // For callbacks may be run after destruction.
181 base::WeakPtrFactory<SuggestionsService> weak_ptr_factory_;
183 DISALLOW_COPY_AND_ASSIGN(SuggestionsService);
186 } // namespace suggestions
188 #endif // COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_H_