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