OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CHROME_BROWSER_SEARCH_SUGGESTIONS_SUGGESTIONS_SERVICE_H_ | |
6 #define CHROME_BROWSER_SEARCH_SUGGESTIONS_SUGGESTIONS_SERVICE_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
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/time/time.h" | |
18 #include "chrome/browser/search/suggestions/image_manager.h" | |
19 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" | |
20 #include "components/keyed_service/core/keyed_service.h" | |
21 #include "net/url_request/url_fetcher_delegate.h" | |
22 #include "ui/gfx/image/image_skia.h" | |
23 #include "url/gurl.h" | |
24 | |
25 namespace net { | |
26 class URLRequestContextGetter; | |
27 } // namespace net | |
28 | |
29 namespace user_prefs { | |
30 class PrefRegistrySyncable; | |
31 } // namespace user_prefs | |
32 | |
33 namespace suggestions { | |
34 | |
35 class BlacklistStore; | |
36 class SuggestionsStore; | |
37 | |
38 extern const char kSuggestionsFieldTrialName[]; | |
39 extern const char kSuggestionsFieldTrialURLParam[]; | |
40 extern const char kSuggestionsFieldTrialCommonParamsParam[]; | |
41 extern const char kSuggestionsFieldTrialBlacklistPathParam[]; | |
42 extern const char kSuggestionsFieldTrialBlacklistUrlParam[]; | |
43 extern const char kSuggestionsFieldTrialStateParam[]; | |
44 extern const char kSuggestionsFieldTrialControlParam[]; | |
45 extern const char kSuggestionsFieldTrialStateEnabled[]; | |
46 | |
47 // An interface to fetch server suggestions asynchronously. | |
48 class SuggestionsService : public KeyedService, public net::URLFetcherDelegate { | |
49 public: | |
50 typedef base::Callback<void(const SuggestionsProfile&)> ResponseCallback; | |
51 | |
52 SuggestionsService(net::URLRequestContextGetter* url_request_context, | |
53 scoped_ptr<SuggestionsStore> suggestions_store, | |
54 scoped_ptr<ImageManager> thumbnail_manager, | |
55 scoped_ptr<BlacklistStore> blacklist_store); | |
56 virtual ~SuggestionsService(); | |
57 | |
58 // Whether this service is enabled. | |
59 static bool IsEnabled(); | |
60 | |
61 // Whether the user is part of a control group. | |
62 static bool IsControlGroup(); | |
63 | |
64 // Request suggestions data, which will be passed to |callback|. Initiates a | |
65 // fetch request unless a pending one exists. To prevent multiple requests, | |
66 // we place all |callback|s in a queue and update them simultaneously when | |
67 // fetch request completes. Also posts a task to execute OnRequestTimeout | |
68 // if the request hasn't completed in a given amount of time. | |
69 void FetchSuggestionsData(ResponseCallback callback); | |
70 | |
71 // Similar to FetchSuggestionsData but doesn't post a task to execute | |
72 // OnDelaySinceFetch. | |
73 void FetchSuggestionsDataNoTimeout(ResponseCallback callback); | |
74 | |
75 // Retrieves stored thumbnail for website |url| asynchronously. Calls | |
76 // |callback| with Bitmap pointer if found, and NULL otherwise. | |
77 void GetPageThumbnail( | |
78 const GURL& url, | |
79 base::Callback<void(const GURL&, const SkBitmap*)> callback); | |
80 | |
81 // Issue a blacklist request. If there is already a blacklist request | |
82 // in flight, the new blacklist request is ignored. | |
83 void BlacklistURL(const GURL& candidate_url, | |
84 const ResponseCallback& callback); | |
85 | |
86 // Determines which URL a blacklist request was for, irrespective of the | |
87 // request's status. Returns false if |request| is not a blacklist request. | |
88 static bool GetBlacklistedUrl(const net::URLFetcher& request, GURL* url); | |
89 | |
90 // Register SuggestionsService related prefs in the Profile prefs. | |
91 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | |
92 | |
93 private: | |
94 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, BlacklistURLFails); | |
95 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, FetchSuggestionsData); | |
96 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, UpdateBlacklistDelay); | |
97 | |
98 // Issue a request. | |
99 void IssueRequest(const GURL& url); | |
100 | |
101 // Creates a request to the suggestions service, properly setting headers. | |
102 net::URLFetcher* CreateSuggestionsRequest(const GURL& url); | |
103 | |
104 // Called to service the requestors if the issued suggestions request has | |
105 // not completed in a given amount of time. | |
106 virtual void OnRequestTimeout(); | |
107 | |
108 // net::URLFetcherDelegate implementation. | |
109 // Called when fetch request completes. Parses the received suggestions data, | |
110 // and dispatches them to callbacks stored in queue. | |
111 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
112 | |
113 // KeyedService implementation. | |
114 virtual void Shutdown() OVERRIDE; | |
115 | |
116 // Load the cached suggestions and service the requestors with them. | |
117 void ServeFromCache(); | |
118 | |
119 // Apply the local blacklist to |suggestions|, then serve the requestors. | |
120 void FilterAndServe(SuggestionsProfile* suggestions); | |
121 | |
122 // Schedule a blacklisting request if the local blacklist isn't empty. | |
123 // |last_request_successful| is used for exponentially backing off when | |
124 // requests fail. | |
125 void ScheduleBlacklistUpload(bool last_request_successful); | |
126 | |
127 // If the local blacklist isn't empty, pick a URL from it and issue a | |
128 // blacklist request for it. | |
129 void UploadOneFromBlacklist(); | |
130 | |
131 // Updates |blacklist_delay_sec_| based on the success of the last request. | |
132 void UpdateBlacklistDelay(bool last_request_successful); | |
133 | |
134 // Test seams. | |
135 int blacklist_delay() const { return blacklist_delay_sec_; } | |
136 void set_blacklist_delay(int delay) { blacklist_delay_sec_ = delay; } | |
137 | |
138 // The cache for the suggestions. | |
139 scoped_ptr<SuggestionsStore> suggestions_store_; | |
140 | |
141 // The local cache for temporary blacklist, until uploaded to the server. | |
142 scoped_ptr<BlacklistStore> blacklist_store_; | |
143 | |
144 // Contains the current suggestions fetch request. Will only have a value | |
145 // while a request is pending, and will be reset by |OnURLFetchComplete|. | |
146 scoped_ptr<net::URLFetcher> pending_request_; | |
147 | |
148 // A closure that is run on a timeout from issuing the suggestions fetch | |
149 // request, if the request hasn't completed. | |
150 scoped_ptr<base::CancelableClosure> pending_timeout_closure_; | |
151 | |
152 // The start time of the previous suggestions request. This is used to measure | |
153 // the latency of requests. Initially zero. | |
154 base::TimeTicks last_request_started_time_; | |
155 | |
156 // The URL to fetch suggestions data from. | |
157 GURL suggestions_url_; | |
158 | |
159 // Prefix for building the blacklisting URL. | |
160 std::string blacklist_url_prefix_; | |
161 | |
162 // Queue of callbacks. These are flushed when fetch request completes. | |
163 std::vector<ResponseCallback> waiting_requestors_; | |
164 | |
165 // Used to obtain server thumbnails, if available. | |
166 scoped_ptr<ImageManager> thumbnail_manager_; | |
167 | |
168 net::URLRequestContextGetter* url_request_context_; | |
169 | |
170 // Delay used when scheduling a blacklisting task. | |
171 int blacklist_delay_sec_; | |
172 | |
173 // For callbacks may be run after destruction. | |
174 base::WeakPtrFactory<SuggestionsService> weak_ptr_factory_; | |
175 | |
176 // Timeout (in ms) before serving requestors after a fetch suggestions request | |
177 // has been issued. | |
178 int request_timeout_ms_; | |
179 | |
180 DISALLOW_COPY_AND_ASSIGN(SuggestionsService); | |
181 }; | |
182 | |
183 } // namespace suggestions | |
184 | |
185 #endif // CHROME_BROWSER_SEARCH_SUGGESTIONS_SUGGESTIONS_SERVICE_H_ | |
OLD | NEW |