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_
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"
28 class URLRequestContextGetter
;
31 namespace user_prefs
{
32 class PrefRegistrySyncable
;
33 } // namespace user_prefs
35 namespace suggestions
{
38 class SuggestionsStore
;
40 extern const char kSuggestionsFieldTrialName
[];
41 extern const char kSuggestionsFieldTrialControlParam
[];
42 extern const char kSuggestionsFieldTrialStateEnabled
[];
43 extern const char kSuggestionsFieldTrialStateParam
[];
45 extern const char kSuggestionsURL
[];
46 extern const char kSuggestionsBlacklistURLPrefix
[];
47 extern const char kSuggestionsBlacklistURLParam
[];
48 extern const int64 kDefaultExpiryUsec
;
50 // An interface to fetch server suggestions asynchronously.
51 class SuggestionsService
: public KeyedService
, public net::URLFetcherDelegate
{
53 typedef base::Callback
<void(const SuggestionsProfile
&)> ResponseCallback
;
55 // Class taking ownership of |suggestions_store|, |thumbnail_manager| and
58 net::URLRequestContextGetter
* url_request_context
,
59 scoped_ptr
<SuggestionsStore
> suggestions_store
,
60 scoped_ptr
<ImageManager
> thumbnail_manager
,
61 scoped_ptr
<BlacklistStore
> blacklist_store
);
62 ~SuggestionsService() override
;
64 // Whether this service is enabled.
65 static bool IsEnabled();
67 // Whether the user is part of a control group.
68 static bool IsControlGroup();
70 // Request suggestions data, which will be passed to |callback|. |sync_state|
71 // will influence the behavior of this function (see SyncState definition).
73 // |sync_state| must be specified based on the current state of the system
74 // (see suggestions::GetSyncState). Callers should call this function again if
75 // sync state changes.
77 // If state allows for a network request, it is initiated unless a pending one
78 // exists, to fill the cache for next time.
79 void FetchSuggestionsData(SyncState sync_state
,
80 ResponseCallback callback
);
82 // Retrieves stored thumbnail for website |url| asynchronously. Calls
83 // |callback| with Bitmap pointer if found, and NULL otherwise.
84 void GetPageThumbnail(
86 base::Callback
<void(const GURL
&, const SkBitmap
*)> callback
);
88 // Adds a URL to the blacklist cache, invoking |callback| on success or
89 // |fail_callback| otherwise. The URL will eventually be uploaded to the
91 void BlacklistURL(const GURL
& candidate_url
,
92 const ResponseCallback
& callback
,
93 const base::Closure
& fail_callback
);
95 // Removes a URL from the local blacklist, then invokes |callback|. If the URL
96 // cannot be removed, the |fail_callback| is called.
97 void UndoBlacklistURL(const GURL
& url
,
98 const ResponseCallback
& callback
,
99 const base::Closure
& fail_callback
);
101 // Determines which URL a blacklist request was for, irrespective of the
102 // request's status. Returns false if |request| is not a blacklist request.
103 static bool GetBlacklistedUrl(const net::URLFetcher
& request
, GURL
* url
);
105 // Register SuggestionsService related prefs in the Profile prefs.
106 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
108 // Sets default timestamp for suggestions which do not have expiry timestamp.
109 void SetDefaultExpiryTimestamp(SuggestionsProfile
* suggestions
,
110 int64 timestamp_usec
);
112 // Issue a network request if there isn't already one happening. Visible for
114 void IssueRequestIfNoneOngoing(const GURL
& url
);
117 friend class SuggestionsServiceTest
;
118 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest
, BlacklistURL
);
119 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest
, BlacklistURLRequestFails
);
120 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest
, UndoBlacklistURL
);
121 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest
, UndoBlacklistURLFailsHelper
);
122 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest
, UpdateBlacklistDelay
);
124 // Creates a request to the suggestions service, properly setting headers.
125 net::URLFetcher
* CreateSuggestionsRequest(const GURL
& url
);
127 // net::URLFetcherDelegate implementation.
128 // Called when fetch request completes. Parses the received suggestions data,
129 // and dispatches them to callbacks stored in queue.
130 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
132 // KeyedService implementation.
133 void Shutdown() override
;
135 // Loads the cached suggestions (or empty suggestions if no cache) and serves
136 // the requestors with them.
137 void ServeFromCache();
139 // Applies the local blacklist to |suggestions|, then serves the requestors.
140 void FilterAndServe(SuggestionsProfile
* suggestions
);
142 // Schedules a blacklisting request if the local blacklist isn't empty.
143 void ScheduleBlacklistUpload();
145 // If the local blacklist isn't empty, picks a URL from it and issues a
146 // blacklist request for it.
147 void UploadOneFromBlacklist();
149 // Updates |scheduling_delay_| based on the success of the last request.
150 void UpdateBlacklistDelay(bool last_request_successful
);
153 base::TimeDelta
blacklist_delay() const { return scheduling_delay_
; }
154 void set_blacklist_delay(base::TimeDelta delay
) {
155 scheduling_delay_
= delay
; }
157 base::ThreadChecker thread_checker_
;
159 net::URLRequestContextGetter
* url_request_context_
;
161 // The cache for the suggestions.
162 scoped_ptr
<SuggestionsStore
> suggestions_store_
;
164 // Used to obtain server thumbnails, if available.
165 scoped_ptr
<ImageManager
> thumbnail_manager_
;
167 // The local cache for temporary blacklist, until uploaded to the server.
168 scoped_ptr
<BlacklistStore
> blacklist_store_
;
170 // Delay used when scheduling a blacklisting task.
171 base::TimeDelta scheduling_delay_
;
173 // Contains the current suggestions fetch request. Will only have a value
174 // while a request is pending, and will be reset by |OnURLFetchComplete| or
176 scoped_ptr
<net::URLFetcher
> pending_request_
;
178 // The start time of the previous suggestions request. This is used to measure
179 // the latency of requests. Initially zero.
180 base::TimeTicks last_request_started_time_
;
182 // The URL to fetch suggestions data from.
183 GURL suggestions_url_
;
185 // Prefix for building the blacklisting URL.
186 std::string blacklist_url_prefix_
;
188 // Queue of callbacks. These are flushed when fetch request completes.
189 std::vector
<ResponseCallback
> waiting_requestors_
;
191 // For callbacks may be run after destruction.
192 base::WeakPtrFactory
<SuggestionsService
> weak_ptr_factory_
;
194 DISALLOW_COPY_AND_ASSIGN(SuggestionsService
);
197 } // namespace suggestions
199 #endif // COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_H_