Chromium Code Reviews| 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 kSuggestionsFieldTrialBlacklistUrlParamParam[]; | |
| 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 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, FetchSuggestionsData); |
| 80 | 88 |
| 89 // Issue a request. | |
| 90 void IssueRequest(const GURL& url); | |
| 91 | |
| 92 // Creates a request to the suggestions service, properly setting headers. | |
| 93 net::URLFetcher* CreateSuggestionsRequest(const GURL& url); | |
| 94 | |
| 81 // Called to service the requestors if the issued suggestions request has | 95 // Called to service the requestors if the issued suggestions request has |
| 82 // not completed in a given amount of time. | 96 // not completed in a given amount of time. |
| 83 virtual void OnRequestTimeout(); | 97 virtual void OnRequestTimeout(); |
| 84 | 98 |
| 85 // net::URLFetcherDelegate implementation. | 99 // net::URLFetcherDelegate implementation. |
| 86 // Called when fetch request completes. Parses the received suggestions data, | 100 // Called when fetch request completes. Parses the received suggestions data, |
| 87 // and dispatches them to callbacks stored in queue. | 101 // and dispatches them to callbacks stored in queue. |
| 88 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 102 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 89 | 103 |
| 90 // KeyedService implementation. | 104 // KeyedService implementation. |
| 91 virtual void Shutdown() OVERRIDE; | 105 virtual void Shutdown() OVERRIDE; |
| 92 | 106 |
| 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. | 107 // Load the cached suggestions and service the requestors with them. |
| 100 void ServeFromCache(); | 108 void ServeFromCache(); |
| 101 | 109 |
| 110 // Apply the local blacklist to |suggestions|, then serve the requestors. | |
| 111 void FilterAndServe(SuggestionsProfile* suggestions); | |
| 112 | |
| 113 // Schedule a blacklisting request if the local blacklist isn't empty. | |
| 114 // |last_request_successful| is used for exponentially backing off when | |
| 115 // requests fail. | |
| 116 void ScheduleBlacklistUpload(bool last_request_successful); | |
| 117 | |
| 118 // If the local blacklist isn't empty, pick a URL from it and issue a | |
| 119 // blacklist request for it. | |
| 120 void UploadOneFromBlacklist(); | |
| 121 | |
| 122 // Updates |blacklist_delay_sec_| based on the success of the last request. | |
| 123 void UpdateBlacklistDelay(bool last_request_successful); | |
| 124 | |
| 125 // Test seams. | |
| 126 int GetBlacklistDelay() const; | |
|
Mathieu
2014/06/18 13:56:23
these could be blacklist_delay() and set_blacklist
manzagop (departed)
2014/06/18 18:44:55
Done.
| |
| 127 void SetBlacklistDelay(int delay); | |
| 128 | |
| 102 // The cache for the suggestions. | 129 // The cache for the suggestions. |
| 103 scoped_ptr<SuggestionsStore> suggestions_store_; | 130 scoped_ptr<SuggestionsStore> suggestions_store_; |
| 104 | 131 |
| 132 // The local cache for temporary blacklist, until uploaded to the server. | |
| 133 scoped_ptr<BlacklistStore> blacklist_store_; | |
| 134 | |
| 105 // Contains the current suggestions fetch request. Will only have a value | 135 // Contains the current suggestions fetch request. Will only have a value |
| 106 // while a request is pending, and will be reset by |OnURLFetchComplete|. | 136 // while a request is pending, and will be reset by |OnURLFetchComplete|. |
| 107 scoped_ptr<net::URLFetcher> pending_request_; | 137 scoped_ptr<net::URLFetcher> pending_request_; |
| 108 | 138 |
| 109 // A closure that is run on a timeout from issuing the suggestions fetch | 139 // A closure that is run on a timeout from issuing the suggestions fetch |
| 110 // request, if the request hasn't completed. | 140 // request, if the request hasn't completed. |
| 111 scoped_ptr<base::CancelableClosure> pending_timeout_closure_; | 141 scoped_ptr<base::CancelableClosure> pending_timeout_closure_; |
| 112 | 142 |
| 113 // The start time of the previous suggestions request. This is used to measure | 143 // The start time of the previous suggestions request. This is used to measure |
| 114 // the latency of requests. Initially zero. | 144 // the latency of requests. Initially zero. |
| 115 base::TimeTicks last_request_started_time_; | 145 base::TimeTicks last_request_started_time_; |
| 116 | 146 |
| 117 // The URL to fetch suggestions data from. | 147 // The URL to fetch suggestions data from. |
| 118 GURL suggestions_url_; | 148 GURL suggestions_url_; |
| 119 | 149 |
| 120 // Prefix for building the blacklisting URL. | 150 // Prefix for building the blacklisting URL. |
| 121 std::string blacklist_url_prefix_; | 151 std::string blacklist_url_prefix_; |
| 122 | 152 |
| 123 // Queue of callbacks. These are flushed when fetch request completes. | 153 // Queue of callbacks. These are flushed when fetch request completes. |
| 124 std::vector<ResponseCallback> waiting_requestors_; | 154 std::vector<ResponseCallback> waiting_requestors_; |
| 125 | 155 |
| 126 // Used to obtain server thumbnails, if available. | 156 // Used to obtain server thumbnails, if available. |
| 127 scoped_ptr<ThumbnailManager> thumbnail_manager_; | 157 scoped_ptr<ThumbnailManager> thumbnail_manager_; |
| 128 | 158 |
| 129 Profile* profile_; | 159 Profile* profile_; |
| 130 | 160 |
| 161 // Delay used when scheduling a blacklisting task. | |
| 162 int blacklist_delay_sec_; | |
| 163 | |
| 131 // For callbacks may be run after destruction. | 164 // For callbacks may be run after destruction. |
| 132 base::WeakPtrFactory<SuggestionsService> weak_ptr_factory_; | 165 base::WeakPtrFactory<SuggestionsService> weak_ptr_factory_; |
| 133 | 166 |
| 167 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, UpdateBlacklistDelay); | |
|
Mathieu
2014/06/18 13:56:23
you already have FRIEND_TEST above for another tes
manzagop (departed)
2014/06/18 18:44:55
Moved the one from above to here.
| |
| 168 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, BlacklistURLFails); | |
| 169 | |
| 134 DISALLOW_COPY_AND_ASSIGN(SuggestionsService); | 170 DISALLOW_COPY_AND_ASSIGN(SuggestionsService); |
| 135 }; | 171 }; |
| 136 | 172 |
| 137 } // namespace suggestions | 173 } // namespace suggestions |
| 138 | 174 |
| 139 #endif // CHROME_BROWSER_SEARCH_SUGGESTIONS_SUGGESTIONS_SERVICE_H_ | 175 #endif // CHROME_BROWSER_SEARCH_SUGGESTIONS_SUGGESTIONS_SERVICE_H_ |
| OLD | NEW |