Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 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_OMNIBOX_BROWSER_CONTEXTUAL_SUGGESTIONS_SERVICE_H_ |
| 6 | #define COMPONENTS_OMNIBOX_BROWSER_CONTEXTUAL_SUGGESTIONS_SERVICE_H_ |
| 7 | |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 | |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame^] | 11 | #include "base/memory/scoped_refptr.h" |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 12 | #include "components/keyed_service/core/keyed_service.h" |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame^] | 13 | #include "net/traffic_annotation/network_traffic_annotation.h" |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 14 | #include "url/gurl.h" |
| 15 | |
Jochen Eisinger | 4f96a31 | 2018-06-11 15:41:26 | [diff] [blame] | 16 | namespace base { |
| 17 | class Time; |
| 18 | } |
| 19 | namespace identity { |
| 20 | class IdentityManager; |
| 21 | class PrimaryAccountAccessTokenFetcher; |
| 22 | } // namespace identity |
Jochen Eisinger | 4f96a31 | 2018-06-11 15:41:26 | [diff] [blame] | 23 | class GoogleServiceAuthError; |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 24 | class TemplateURLService; |
| 25 | |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame^] | 26 | namespace network { |
| 27 | struct ResourceRequest; |
| 28 | class SharedURLLoaderFactory; |
| 29 | class SimpleURLLoader; |
| 30 | } // namespace network |
| 31 | |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 32 | // A service to fetch suggestions from a remote endpoint given a URL. |
| 33 | class ContextualSuggestionsService : public KeyedService { |
| 34 | public: |
Jochen Eisinger | 4f96a31 | 2018-06-11 15:41:26 | [diff] [blame] | 35 | // |identity_manager| may be null but only unauthenticated requests will |
| 36 | // issued. |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame^] | 37 | ContextualSuggestionsService( |
| 38 | identity::IdentityManager* identity_manager, |
| 39 | scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory); |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 40 | |
| 41 | ~ContextualSuggestionsService() override; |
| 42 | |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame^] | 43 | using StartCallback = base::OnceCallback<void( |
| 44 | std::unique_ptr<network::SimpleURLLoader> loader)>; |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 45 | |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame^] | 46 | using CompletionCallback = |
| 47 | base::OnceCallback<void(const network::SimpleURLLoader* source, |
| 48 | std::unique_ptr<std::string> response_body)>; |
| 49 | |
| 50 | // Creates a loader for contextual suggestions for |current_url| and passes |
| 51 | // the loader to |start_callback|. It uses a number of signals to create the |
| 52 | // loader, including field trial / experimental parameters, and it may |
| 53 | // pass a nullptr to |start_callback| (see below for details). |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 54 | // |
| 55 | // |current_url| may be empty, in which case the system will never use the |
| 56 | // experimental suggestions service. It's possible the non-experimental |
| 57 | // service may decide to offer general-purpose suggestions. |
| 58 | // |
Gheorghe Comanici | 034cff6 | 2018-01-27 03:34:00 | [diff] [blame] | 59 | // |visit_time| is the time of the visit for the URL for which suggestions |
| 60 | // should be fetched. |
| 61 | // |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 62 | // |template_url_service| may be null, but some services may be disabled. |
| 63 | // |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame^] | 64 | // |start_callback| is called to transfer ownership of the created loader to |
| 65 | // whatever function/class receives the callback. |
| 66 | // |start_callback| is called with a nullptr argument if: |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 67 | // * The service is waiting for a previously-requested authentication token. |
| 68 | // * The authentication token had any issues. |
| 69 | // |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame^] | 70 | // |completion_callback| will be invoked when the transfer is done. |
| 71 | // |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 72 | // This method sends a request for an OAuth2 token and temporarily |
| 73 | // instantiates |token_fetcher_|. |
| 74 | void CreateContextualSuggestionsRequest( |
| 75 | const std::string& current_url, |
Gheorghe Comanici | 034cff6 | 2018-01-27 03:34:00 | [diff] [blame] | 76 | const base::Time& visit_time, |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 77 | const TemplateURLService* template_url_service, |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame^] | 78 | StartCallback start_callback, |
| 79 | CompletionCallback completion_callback); |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 80 | |
Gheorghe Comanici | 86bbdf6 | 2017-08-28 17:20:33 | [diff] [blame] | 81 | // Advises the service to stop any process that creates a suggestion request. |
| 82 | void StopCreatingContextualSuggestionsRequest(); |
| 83 | |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 84 | // Returns a URL representing the address of the server where the zero suggest |
| 85 | // request is being sent. Does not take into account whether sending this |
| 86 | // request is prohibited (e.g. in an incognito window). |
| 87 | // Returns an invalid URL (i.e.: GURL::is_valid() == false) in case of an |
| 88 | // error. |
| 89 | // |
| 90 | // |current_url| is the page the user is currently browsing and may be empty. |
| 91 | // |
| 92 | // Note that this method is public and is also used by ZeroSuggestProvider for |
| 93 | // suggestions that do not take |current_url| into consideration. |
| 94 | static GURL ContextualSuggestionsUrl( |
| 95 | const std::string& current_url, |
| 96 | const TemplateURLService* template_url_service); |
| 97 | |
| 98 | private: |
| 99 | // Returns a URL representing the address of the server where the zero suggest |
| 100 | // requests are being redirected when serving experimental suggestions. |
| 101 | // |
| 102 | // Returns a valid URL only if all the folowing conditions are true: |
| 103 | // * The |current_url| is non-empty. |
| 104 | // * The |default_provider| is Google. |
| 105 | // * The user is in the ZeroSuggestRedirectToChrome field trial. |
| 106 | // * The field trial provides a valid server address where the suggest request |
| 107 | // is redirected. |
| 108 | // * The suggest request is over HTTPS. This avoids leaking the current page |
| 109 | // URL or personal data in unencrypted network traffic. |
| 110 | // Note: these checks are in addition to CanSendUrl() on the default |
| 111 | // contextual suggestion URL. |
Gheorghe Comanici | f7c6586 | 2017-09-07 18:40:29 | [diff] [blame] | 112 | GURL ExperimentalContextualSuggestionsUrl( |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 113 | const std::string& current_url, |
| 114 | const TemplateURLService* template_url_service) const; |
| 115 | |
Gheorghe Comanici | 92373927 | 2017-09-22 20:32:14 | [diff] [blame] | 116 | // Upon succesful creation of an HTTP GET request for default contextual |
Gheorghe Comanici | f7c6586 | 2017-09-07 18:40:29 | [diff] [blame] | 117 | // suggestions, the |callback| function is run with the HTTP GET request as a |
| 118 | // parameter. |
| 119 | // |
| 120 | // This function is called by CreateContextualSuggestionsRequest. See its |
| 121 | // function definition for details on the parameters. |
| 122 | void CreateDefaultRequest(const std::string& current_url, |
| 123 | const TemplateURLService* template_url_service, |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame^] | 124 | StartCallback start_callback, |
| 125 | CompletionCallback completion_callback); |
Gheorghe Comanici | f7c6586 | 2017-09-07 18:40:29 | [diff] [blame] | 126 | |
Gheorghe Comanici | 92373927 | 2017-09-22 20:32:14 | [diff] [blame] | 127 | // Upon succesful creation of an HTTP POST request for experimental contextual |
Gheorghe Comanici | f7c6586 | 2017-09-07 18:40:29 | [diff] [blame] | 128 | // suggestions, the |callback| function is run with the HTTP POST request as a |
| 129 | // parameter. |
| 130 | // |
| 131 | // This function is called by CreateContextualSuggestionsRequest. See its |
| 132 | // function definition for details on the parameters. |
| 133 | void CreateExperimentalRequest(const std::string& current_url, |
Gheorghe Comanici | 034cff6 | 2018-01-27 03:34:00 | [diff] [blame] | 134 | const base::Time& visit_time, |
Gheorghe Comanici | f7c6586 | 2017-09-07 18:40:29 | [diff] [blame] | 135 | const GURL& suggest_url, |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame^] | 136 | StartCallback start_callback, |
| 137 | CompletionCallback completion_callback); |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 138 | |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame^] | 139 | // Tries to load the suggestions from network, using the token when available. |
| 140 | // |
| 141 | // |request| is expected to be the request for suggestions filled in with |
| 142 | // everything but maybe the authentication token. |
| 143 | // |
| 144 | // |traffic_annotation| is the appropriate annotations for making the network |
| 145 | // request to load the suggestions. |
| 146 | // |
| 147 | // |start_callback|, |completion_callback| are the same as passed to |
| 148 | // CreateContextualSuggestionsRequest() |
| 149 | // |
| 150 | // |error| and |access_token| are the results of token request. |
| 151 | void AccessTokenAvailable(std::unique_ptr<network::ResourceRequest> request, |
| 152 | net::NetworkTrafficAnnotationTag traffic_annotation, |
| 153 | StartCallback start_callback, |
| 154 | CompletionCallback completion_callback, |
Colin Blundell | a29afeac | 2018-06-12 14:05:47 | [diff] [blame] | 155 | GoogleServiceAuthError error, |
| 156 | std::string access_token); |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 157 | |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame^] | 158 | // Activates a loader for |request|, wiring it up to |completion_callback|, |
| 159 | // and calls |start_callback|. |
| 160 | void StartDownloadAndTransferLoader( |
| 161 | std::unique_ptr<network::ResourceRequest> request, |
| 162 | net::NetworkTrafficAnnotationTag traffic_annotation, |
| 163 | StartCallback start_callback, |
| 164 | CompletionCallback completion_callback); |
| 165 | |
| 166 | scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; |
Jochen Eisinger | 4f96a31 | 2018-06-11 15:41:26 | [diff] [blame] | 167 | identity::IdentityManager* identity_manager_; |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 168 | |
| 169 | // Helper for fetching OAuth2 access tokens. This is non-null when an access |
| 170 | // token request is currently in progress. |
Colin Blundell | 0ee42ed6 | 2017-12-20 14:11:29 | [diff] [blame] | 171 | std::unique_ptr<identity::PrimaryAccountAccessTokenFetcher> token_fetcher_; |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 172 | |
| 173 | DISALLOW_COPY_AND_ASSIGN(ContextualSuggestionsService); |
| 174 | }; |
| 175 | |
| 176 | #endif // COMPONENTS_OMNIBOX_BROWSER_CONTEXTUAL_SUGGESTIONS_SERVICE_H_ |