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