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