1 // Copyright 2018 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_DOCUMENT_SUGGESTIONS_SERVICE_H_ 6 #define COMPONENTS_OMNIBOX_BROWSER_DOCUMENT_SUGGESTIONS_SERVICE_H_ 7 8 #include <memory> 9 #include <string> 10 11 #include "base/memory/scoped_refptr.h" 12 #include "components/keyed_service/core/keyed_service.h" 13 #include "components/signin/public/identity_manager/access_token_info.h" 14 #include "services/network/public/cpp/resource_request.h" 15 #include "services/network/public/cpp/shared_url_loader_factory.h" 16 #include "services/network/public/cpp/simple_url_loader.h" 17 #include "url/gurl.h" 18 19 namespace signin { 20 class IdentityManager; 21 class PrimaryAccountAccessTokenFetcher; 22 } // namespace signin 23 24 class GoogleServiceAuthError; 25 26 // A service to fetch suggestions from a remote endpoint given a URL. 27 class DocumentSuggestionsService : public KeyedService { 28 public: 29 // null may be passed for params, but no request will be issued. 30 DocumentSuggestionsService( 31 signin::IdentityManager* identity_manager, 32 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory); 33 34 ~DocumentSuggestionsService() override; 35 DocumentSuggestionsService(const DocumentSuggestionsService&) = delete; 36 DocumentSuggestionsService& operator=(const DocumentSuggestionsService&) = 37 delete; 38 39 using StartCallback = base::OnceCallback<void( 40 std::unique_ptr<network::SimpleURLLoader> loader)>; 41 42 using CompletionCallback = 43 base::OnceCallback<void(const network::SimpleURLLoader* source, 44 std::unique_ptr<std::string> response_body)>; 45 46 // Creates and starts a document suggestion request for |query|. 47 // May obtain an OAuth2 token for the signed-in user. 48 void CreateDocumentSuggestionsRequest(const base::string16& query, 49 bool is_incognito, 50 StartCallback start_callback, 51 CompletionCallback completion_callback); 52 53 // Advises the service to stop any process that creates a suggestion request. 54 void StopCreatingDocumentSuggestionsRequest(); 55 56 private: 57 // Called when an access token request completes (successfully or not). 58 void AccessTokenAvailable(std::unique_ptr<network::ResourceRequest> request, 59 std::string request_body, 60 net::NetworkTrafficAnnotationTag traffic_annotation, 61 StartCallback start_callback, 62 CompletionCallback completion_callback, 63 GoogleServiceAuthError error, 64 signin::AccessTokenInfo access_token_info); 65 66 // Activates a loader for |request|, wiring it up to |completion_callback|, 67 // and calls |start_callback|. If |request_body| isn't empty, it will be 68 // attached as upload bytes. 69 void StartDownloadAndTransferLoader( 70 std::unique_ptr<network::ResourceRequest> request, 71 std::string request_body, 72 net::NetworkTrafficAnnotationTag traffic_annotation, 73 StartCallback start_callback, 74 CompletionCallback completion_callback); 75 76 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; 77 78 signin::IdentityManager* identity_manager_; 79 80 // Helper for fetching OAuth2 access tokens. Non-null when we have a token 81 // available, or while a token fetch is in progress. 82 std::unique_ptr<signin::PrimaryAccountAccessTokenFetcher> token_fetcher_; 83 }; 84 85 #endif // COMPONENTS_OMNIBOX_BROWSER_DOCUMENT_SUGGESTIONS_SERVICE_H_ 86