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 <deque> | |
| 9 #include <string> | |
| 8 #include <vector> | 10 #include <vector> |
| 9 | 11 |
| 10 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 11 #include "base/callback.h" | 13 #include "base/callback.h" |
| 12 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
| 13 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 15 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" | 17 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" |
| 16 #include "components/keyed_service/core/keyed_service.h" | 18 #include "components/keyed_service/core/keyed_service.h" |
| 17 #include "net/url_request/url_fetcher_delegate.h" | 19 #include "net/url_request/url_fetcher_delegate.h" |
| 18 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 19 | 21 |
| 20 class Profile; | 22 class Profile; |
| 21 | 23 |
| 22 namespace suggestions { | 24 namespace suggestions { |
| 23 | 25 |
| 24 extern const char kSuggestionsFieldTrialName[]; | 26 extern const char kSuggestionsFieldTrialName[]; |
| 25 extern const char kSuggestionsFieldTrialURLParam[]; | 27 extern const char kSuggestionsFieldTrialURLParam[]; |
| 28 extern const char kSuggestionsFieldTrialSuggestionsSuffixParam[]; | |
| 29 extern const char kSuggestionsFieldTrialBlacklistSuffixParam[]; | |
| 26 extern const char kSuggestionsFieldTrialStateParam[]; | 30 extern const char kSuggestionsFieldTrialStateParam[]; |
| 27 extern const char kSuggestionsFieldTrialStateEnabled[]; | 31 extern const char kSuggestionsFieldTrialStateEnabled[]; |
| 28 | 32 |
| 29 // An interface to fetch server suggestions asynchronously. | 33 // An interface to fetch server suggestions asynchronously. |
| 30 class SuggestionsService : public KeyedService, public net::URLFetcherDelegate { | 34 class SuggestionsService : public KeyedService, public net::URLFetcherDelegate { |
| 31 public: | 35 public: |
| 32 typedef base::Callback<void(const SuggestionsProfile&)> ResponseCallback; | 36 typedef base::Callback<void(const SuggestionsProfile&)> ResponseCallback; |
| 33 | 37 |
| 34 explicit SuggestionsService(Profile* profile); | 38 explicit SuggestionsService(Profile* profile); |
| 35 virtual ~SuggestionsService(); | 39 virtual ~SuggestionsService(); |
| 36 | 40 |
| 37 // Whether this service is enabled. | 41 // Whether this service is enabled. |
| 38 static bool IsEnabled(); | 42 static bool IsEnabled(); |
| 39 | 43 |
| 40 // Request suggestions data, which will be passed to |callback|. Initiates a | 44 // Request suggestions data, which will be passed to |callback|. Initiates a |
| 41 // fetch request unless a pending one exists. To prevent multiple requests, | 45 // fetch request unless a pending one exists. To prevent multiple requests, |
| 42 // we place all |callback|s in a queue and update them simultaneously when | 46 // we place all |callback|s in a queue and update them simultaneously when |
| 43 // fetch request completes. | 47 // fetch request completes. |
| 44 void FetchSuggestionsData(ResponseCallback callback); | 48 void FetchSuggestionsData(ResponseCallback callback); |
| 45 | 49 |
| 50 // Issue a blacklist request. If there is already a blacklist request | |
| 51 // in-flight, the new blacklist request is ignored. | |
|
Jered
2014/05/27 22:33:17
nit: omit the - between in and flight
manzagop (departed)
2014/05/29 21:40:31
Done.
| |
| 52 void AddBlacklistedURL(const GURL& candidate_url, | |
| 53 ResponseCallback callback); | |
| 54 | |
| 46 private: | 55 private: |
| 47 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, FetchSuggestionsData); | 56 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, FetchSuggestionsData); |
| 48 | 57 |
| 49 // net::URLFetcherDelegate implementation. | 58 // net::URLFetcherDelegate implementation. |
| 50 // Called when fetch request completes. Parses the received suggestions data, | 59 // Called when fetch request completes. Parses the received suggestions data, |
| 51 // and dispatches them to callbacks stored in queue. | 60 // and dispatches them to callbacks stored in queue. |
| 52 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 61 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 53 | 62 |
| 54 // KeyedService implementation. | 63 // KeyedService implementation. |
| 55 virtual void Shutdown() OVERRIDE; | 64 virtual void Shutdown() OVERRIDE; |
| 56 | 65 |
| 66 // Determines whether |request| is a blacklisting request. | |
| 67 bool IsBlacklistRequest(net::URLFetcher* request) const; | |
| 68 | |
| 69 // Creates a request to the suggestions service, properly setting headers. | |
| 70 net::URLFetcher* CreateSuggestionsRequest(const GURL& url); | |
| 71 | |
| 57 // Contains the current suggestions fetch request. Will only have a value | 72 // Contains the current suggestions fetch request. Will only have a value |
| 58 // while a request is pending, and will be reset by |OnURLFetchComplete|. | 73 // while a request is pending, and will be reset by |OnURLFetchComplete|. |
| 59 scoped_ptr<net::URLFetcher> pending_request_; | 74 scoped_ptr<net::URLFetcher> pending_request_; |
| 60 | 75 |
| 61 // The start time of the previous suggestions request. This is used to measure | 76 // The start time of the previous suggestions request. This is used to measure |
| 62 // the latency of requests. Initially zero. | 77 // the latency of requests. Initially zero. |
| 63 base::TimeTicks last_request_started_time_; | 78 base::TimeTicks last_request_started_time_; |
| 64 | 79 |
| 65 // The URL to fetch suggestions data from. | 80 // The URL to fetch suggestions data from. |
| 66 GURL suggestions_url_; | 81 GURL suggestions_url_; |
| 67 | 82 |
| 83 // Prefix for building the blacklisting URL. | |
| 84 std::string blacklist_url_prefix_; | |
| 85 | |
| 68 // Queue of callbacks. These are flushed when fetch request completes. | 86 // Queue of callbacks. These are flushed when fetch request completes. |
| 69 std::vector<ResponseCallback> waiting_requestors_; | 87 std::vector<ResponseCallback> waiting_requestors_; |
| 70 | 88 |
| 71 Profile* profile_; | 89 Profile* profile_; |
| 72 | 90 |
| 73 DISALLOW_COPY_AND_ASSIGN(SuggestionsService); | 91 DISALLOW_COPY_AND_ASSIGN(SuggestionsService); |
| 74 }; | 92 }; |
| 75 | 93 |
| 76 } // namespace suggestions | 94 } // namespace suggestions |
| 77 | 95 |
| 78 #endif // CHROME_BROWSER_SEARCH_SUGGESTIONS_SUGGESTIONS_SERVICE_H_ | 96 #endif // CHROME_BROWSER_SEARCH_SUGGESTIONS_SUGGESTIONS_SERVICE_H_ |
| OLD | NEW |