OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef COMPONENTS_CONTEXTUAL_SUGGESTIONS_CONTEXTUAL_SUGGESTIONS_SERVICE_H_ |
| 6 #define COMPONENTS_CONTEXTUAL_SUGGESTIONS_CONTEXTUAL_SUGGESTIONS_SERVICE_H_ |
| 7 |
| 8 #include <memory> |
| 9 #include <string> |
| 10 |
| 11 #include "base/feature_list.h" |
| 12 #include "components/keyed_service/core/keyed_service.h" |
| 13 #include "components/search_engines/template_url_service.h" |
| 14 #include "components/signin/core/browser/access_token_fetcher.h" |
| 15 #include "components/signin/core/browser/signin_manager_base.h" |
| 16 #include "google_apis/gaia/oauth2_token_service.h" |
| 17 #include "net/url_request/url_fetcher_delegate.h" |
| 18 #include "net/url_request/url_request_context_getter.h" |
| 19 #include "url/gurl.h" |
| 20 |
| 21 namespace contextual_suggestions { |
| 22 |
| 23 class ContextualSuggestionsService : public KeyedService { |
| 24 public: |
| 25 ContextualSuggestionsService(SigninManagerBase* signin_manager, |
| 26 OAuth2TokenService* token_service, |
| 27 TemplateURLService* template_url_service, |
| 28 net::URLRequestContextGetter* request_context); |
| 29 |
| 30 ~ContextualSuggestionsService() override; |
| 31 |
| 32 // Returns true if the folowing conditions are valid: |
| 33 // * The |default_provider| is Google. |
| 34 // * The user is in the ZeroSuggestRedirectToChrome field trial. |
| 35 // * The field trial provides a valid server address where the suggest request |
| 36 // is redirected. |
| 37 // * The suggest request is over HTTPS. This avoids leaking the current page |
| 38 // URL or personal data in unencrypted network traffic. |
| 39 // Note: these checks are in addition to CanSendUrl() on the default |
| 40 // contextual suggestion URL. |
| 41 bool UseExperimentalZeroSuggestSuggestions() const; |
| 42 |
| 43 using ContextualSuggestionsCallback = |
| 44 base::OnceCallback<void(std::unique_ptr<net::URLFetcher> fetcher)>; |
| 45 // Creates a fetch request for experimental contextual suggestions for |
| 46 // |visited_url|, with |fetcher_delegate| as the URLFetcherDelegate that will |
| 47 // process the response of the fetcher. |
| 48 // |
| 49 // If any of the following conditions is true, |callback| is executed |
| 50 // synchronously with a nullptr for |fetcher|. |
| 51 // * The service is already waiting for an authentication token. |
| 52 // * The URL to send the request to, which is constructed using field trial |
| 53 // parameters, is invalid. |
| 54 // |
| 55 // If all conditions above are false, a request for an auth token is made |
| 56 // asynchronously. After the token is obtained and attached to the |fetcher|, |
| 57 // |callback| is called with a valid |fetcher| as a parameter. |
| 58 void CreateContextualSuggestionsRequest( |
| 59 const std::string& visited_url, |
| 60 net::URLFetcherDelegate* fetcher_delegate, |
| 61 ContextualSuggestionsCallback callback); |
| 62 |
| 63 private: |
| 64 // Returns a string representing the address of the server where the zero |
| 65 // suggest requests are being redirected. |
| 66 static GURL ExperimentalZeroSuggestURL(const std::string& visited_url); |
| 67 |
| 68 // Creates an HTTP Get request for experimental contextual suggestions. The |
| 69 // return value does not include a header corresponding to an authorization |
| 70 // token. |
| 71 std::unique_ptr<net::URLFetcher> CreateRequest( |
| 72 const std::string& visited_url, |
| 73 net::URLFetcherDelegate* fetcher_delegate) const; |
| 74 |
| 75 // Called when an access token request completes (successfully or not). |
| 76 void AccessTokenAvailable(std::unique_ptr<net::URLFetcher> fetcher, |
| 77 ContextualSuggestionsCallback callback, |
| 78 const GoogleServiceAuthError& error, |
| 79 const std::string& access_token); |
| 80 |
| 81 net::URLRequestContextGetter* request_context_; |
| 82 SigninManagerBase* signin_manager_; |
| 83 TemplateURLService* template_url_service_; |
| 84 OAuth2TokenService* token_service_; |
| 85 |
| 86 // Helper for fetching OAuth2 access tokens. This is non-null iff an access |
| 87 // token request is currently in progress. |
| 88 std::unique_ptr<AccessTokenFetcher> token_fetcher_; |
| 89 }; |
| 90 |
| 91 } // namespace contextual_suggestions |
| 92 |
| 93 #endif // COMPONENTS_CONTEXTUAL_SUGGESTIONS_CONTEXTUAL_SUGGESTIONS_SERVICE_H_ |
OLD | NEW |