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