| 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 <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/callback.h" | |
| 12 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
| 13 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 15 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" | 14 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" |
| 16 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" | 15 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" |
| 17 #include "net/url_request/url_fetcher_delegate.h" | 16 #include "net/url_request/url_fetcher_delegate.h" |
| 18 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 19 | 18 |
| 20 class Profile; | 19 class Profile; |
| 21 | 20 |
| 22 namespace suggestions { | 21 namespace suggestions { |
| 23 | 22 |
| 24 extern const char kSuggestionsFieldTrialName[]; | 23 extern const char kSuggestionsFieldTrialName[]; |
| 25 extern const char kSuggestionsFieldTrialURLParam[]; | 24 extern const char kSuggestionsFieldTrialURLParam[]; |
| 26 extern const char kSuggestionsFieldTrialStateParam[]; | 25 extern const char kSuggestionsFieldTrialStateParam[]; |
| 27 extern const char kSuggestionsFieldTrialStateEnabled[]; | 26 extern const char kSuggestionsFieldTrialStateEnabled[]; |
| 28 | 27 |
| 29 // An interface to fetch server suggestions asynchronously. | 28 // Provides an interface for the Suggestions component that provides server |
| 29 // suggestions. |
| 30 class SuggestionsService : public BrowserContextKeyedService, | 30 class SuggestionsService : public BrowserContextKeyedService, |
| 31 public net::URLFetcherDelegate { | 31 public net::URLFetcherDelegate { |
| 32 public: | 32 public: |
| 33 typedef base::Callback<void(const SuggestionsProfile&)> ResponseCallback; | |
| 34 | |
| 35 explicit SuggestionsService(Profile* profile); | 33 explicit SuggestionsService(Profile* profile); |
| 36 virtual ~SuggestionsService(); | 34 virtual ~SuggestionsService(); |
| 37 | 35 |
| 38 // Whether this service is enabled. | 36 // Whether this service is enabled. |
| 39 static bool IsEnabled(); | 37 static bool IsEnabled(); |
| 40 | 38 |
| 41 // Request suggestions data, which will be passed to |callback|. Initiates a | 39 const SuggestionsProfile& suggestions() { return suggestions_; } |
| 42 // fetch request unless a pending one exists. To prevent multiple requests, | |
| 43 // we place all |callback|s in a queue and update them simultaneously when | |
| 44 // fetch request completes. | |
| 45 void FetchSuggestionsData(ResponseCallback callback); | |
| 46 | 40 |
| 47 private: | 41 private: |
| 48 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, FetchSuggestionsData); | 42 FRIEND_TEST_ALL_PREFIXES(SuggestionsServiceTest, FetchSuggestionsData); |
| 49 | 43 |
| 50 // net::URLFetcherDelegate implementation. | 44 // Starts the fetching process once, where |OnURLFetchComplete| is called with |
| 51 // Called when fetch request completes. Parses the received suggestions data, | 45 // the response. |
| 52 // and dispatches them to callbacks stored in queue. | 46 void FetchSuggestionsData(); |
| 47 |
| 48 // net::URLFetcherDelegate implementation: |
| 53 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 49 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 54 | 50 |
| 55 // BrowserContextKeyedService implementation. | 51 // Contains the current suggestions request. Will only have a value while a |
| 56 virtual void Shutdown() OVERRIDE; | 52 // request is pending, and will be reset by |OnURLFetchComplete|. |
| 57 | |
| 58 // Contains the current suggestions fetch request. Will only have a value | |
| 59 // while a request is pending, and will be reset by |OnURLFetchComplete|. | |
| 60 scoped_ptr<net::URLFetcher> pending_request_; | 53 scoped_ptr<net::URLFetcher> pending_request_; |
| 61 | 54 |
| 62 // The start time of the previous suggestions request. This is used to measure | 55 // The start time of the last suggestions request. This is used to measure the |
| 63 // the latency of requests. Initially zero. | 56 // latency of requests. Initially zero. |
| 64 base::TimeTicks last_request_started_time_; | 57 base::TimeTicks last_request_started_time_; |
| 65 | 58 |
| 66 // The URL to fetch suggestions data from. | 59 // The URL to fetch suggestions data from. |
| 67 GURL suggestions_url_; | 60 GURL suggestions_url_; |
| 68 | 61 |
| 69 // Queue of callbacks. These are flushed when fetch request completes. | 62 // Stores the suggestions as they were received from the server. |
| 70 std::vector<ResponseCallback> waiting_requestors_; | 63 SuggestionsProfile suggestions_; |
| 71 | 64 |
| 72 Profile* profile_; | 65 Profile* profile_; |
| 73 | 66 |
| 74 DISALLOW_COPY_AND_ASSIGN(SuggestionsService); | 67 DISALLOW_COPY_AND_ASSIGN(SuggestionsService); |
| 75 }; | 68 }; |
| 76 | 69 |
| 77 } // namespace suggestions | 70 } // namespace suggestions |
| 78 | 71 |
| 79 #endif // CHROME_BROWSER_SEARCH_SUGGESTIONS_SUGGESTIONS_SERVICE_H_ | 72 #endif // CHROME_BROWSER_SEARCH_SUGGESTIONS_SUGGESTIONS_SERVICE_H_ |
| OLD | NEW |