BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / components / suggestions / suggestions_service.h
blob01e36b1eeaee0beeabd2c6f54afcd7167c8705c4
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 const base::Callback<void(const GURL&, const SkBitmap*)>& callback);
78 // A version of |GetPageThumbnail| that explicitly supplies the download URL
79 // for the thumbnail. Replaces any pre-existing thumbnail URL with the
80 // supplied one.
81 void GetPageThumbnailWithURL(
82 const GURL& url,
83 const GURL& thumbnail_url,
84 const base::Callback<void(const GURL&, const SkBitmap*)>& callback);
86 // Adds a URL to the blacklist cache, invoking |callback| on success or
87 // |fail_callback| otherwise. The URL will eventually be uploaded to the
88 // server.
89 void BlacklistURL(const GURL& candidate_url,
90 const ResponseCallback& callback,
91 const base::Closure& fail_callback);
93 // Removes a URL from the local blacklist, then invokes |callback|. If the URL
94 // cannot be removed, the |fail_callback| is called.
95 void UndoBlacklistURL(const GURL& url,
96 const ResponseCallback& callback,
97 const base::Closure& fail_callback);
99 // Removes all URLs from the blacklist then invokes |callback|.
100 void ClearBlacklist(const ResponseCallback& callback);
102 // Determines which URL a blacklist request was for, irrespective of the
103 // request's status. Returns false if |request| is not a blacklist request.
104 static bool GetBlacklistedUrl(const net::URLFetcher& request, GURL* url);
106 // Register SuggestionsService related prefs in the Profile prefs.
107 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
109 // Sets default timestamp for suggestions which do not have expiry timestamp.
110 void SetDefaultExpiryTimestamp(SuggestionsProfile* suggestions,
111 int64 timestamp_usec);
113 // Issue a network request if there isn't already one happening. Visible for
114 // testing.
115 void IssueRequestIfNoneOngoing(const GURL& url);
117 private:
118 friend class SuggestionsServiceTest;
119 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, BlacklistURL);
120 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, BlacklistURLRequestFails);
121 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, ClearBlacklist);
122 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, UndoBlacklistURL);
123 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, UndoBlacklistURLFailsHelper);
124 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, UpdateBlacklistDelay);
126 // Creates a request to the suggestions service, properly setting headers.
127 scoped_ptr<net::URLFetcher> CreateSuggestionsRequest(const GURL& url);
129 // net::URLFetcherDelegate implementation.
130 // Called when fetch request completes. Parses the received suggestions data,
131 // and dispatches them to callbacks stored in queue.
132 void OnURLFetchComplete(const net::URLFetcher* source) override;
134 // KeyedService implementation.
135 void Shutdown() override;
137 // Loads the cached suggestions (or empty suggestions if no cache) and serves
138 // the requestors with them.
139 void ServeFromCache();
141 // Applies the local blacklist to |suggestions|, then serves the requestors.
142 void FilterAndServe(SuggestionsProfile* suggestions);
144 // Schedules a blacklisting request if the local blacklist isn't empty.
145 void ScheduleBlacklistUpload();
147 // If the local blacklist isn't empty, picks a URL from it and issues a
148 // blacklist request for it.
149 void UploadOneFromBlacklist();
151 // Updates |scheduling_delay_| based on the success of the last request.
152 void UpdateBlacklistDelay(bool last_request_successful);
154 // Adds extra data to suggestions profile.
155 void PopulateExtraData(SuggestionsProfile* suggestions);
157 // Test seams.
158 base::TimeDelta blacklist_delay() const { return scheduling_delay_; }
159 void set_blacklist_delay(base::TimeDelta delay) {
160 scheduling_delay_ = delay; }
162 base::ThreadChecker thread_checker_;
164 net::URLRequestContextGetter* url_request_context_;
166 // The cache for the suggestions.
167 scoped_ptr<SuggestionsStore> suggestions_store_;
169 // Used to obtain server thumbnails, if available.
170 scoped_ptr<ImageManager> thumbnail_manager_;
172 // The local cache for temporary blacklist, until uploaded to the server.
173 scoped_ptr<BlacklistStore> blacklist_store_;
175 // Delay used when scheduling a blacklisting task.
176 base::TimeDelta scheduling_delay_;
178 // Contains the current suggestions fetch request. Will only have a value
179 // while a request is pending, and will be reset by |OnURLFetchComplete| or
180 // if cancelled.
181 scoped_ptr<net::URLFetcher> pending_request_;
183 // The start time of the previous suggestions request. This is used to measure
184 // the latency of requests. Initially zero.
185 base::TimeTicks last_request_started_time_;
187 // The URL to fetch suggestions data from.
188 GURL suggestions_url_;
190 // Prefix for building the blacklisting URL.
191 std::string blacklist_url_prefix_;
193 // Queue of callbacks. These are flushed when fetch request completes.
194 std::vector<ResponseCallback> waiting_requestors_;
196 // For callbacks may be run after destruction.
197 base::WeakPtrFactory<SuggestionsService> weak_ptr_factory_;
199 DISALLOW_COPY_AND_ASSIGN(SuggestionsService);
202 } // namespace suggestions
204 #endif // COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_H_