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