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