1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_OMNIBOX_BROWSER_ENTERPRISE_SEARCH_AGGREGATOR_SUGGESTIONS_SERVICE_H_
#define COMPONENTS_OMNIBOX_BROWSER_ENTERPRISE_SEARCH_AGGREGATOR_SUGGESTIONS_SERVICE_H_
#include <memory>
#include <string>
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/scoped_observation.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/signin/public/identity_manager/access_token_info.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "url/gurl.h"
namespace signin {
class PrimaryAccountAccessTokenFetcher;
} // namespace signin
class GoogleServiceAuthError;
namespace network {
struct ResourceRequest;
class SharedURLLoaderFactory;
class SimpleURLLoader;
} // namespace network
// Identifier expected by the server in the body of suggestion requests.
constexpr char kEnterpriseSearchAggregatorExperimentId[] = "103277467";
// A service to fetch suggestions from the search aggregator endpoint URL.
class EnterpriseSearchAggregatorSuggestionsService : public KeyedService {
public:
EnterpriseSearchAggregatorSuggestionsService(
signin::IdentityManager* identity_manager,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
~EnterpriseSearchAggregatorSuggestionsService() override;
EnterpriseSearchAggregatorSuggestionsService(
const EnterpriseSearchAggregatorSuggestionsService&) = delete;
EnterpriseSearchAggregatorSuggestionsService& operator=(
const EnterpriseSearchAggregatorSuggestionsService&) = delete;
// TODO(crbug.com/385756623): Factor out callback methods so it can be used
// across document_suggestions_service and
// enterprise_search_aggregator_suggestions_service.
using CreationCallback =
base::RepeatingCallback<void(network::ResourceRequest* request)>;
using StartCallback = base::RepeatingCallback<void(
int request_index,
std::unique_ptr<network::SimpleURLLoader> loader,
const std::string& request_body)>;
using CompletionCallback =
base::RepeatingCallback<void(const network::SimpleURLLoader* source,
int request_index,
std::unique_ptr<std::string> response_body)>;
// Creates one request for each list within `suggestion_types`. Each request
// will request types in `suggestion_types[i]`.
void CreateEnterpriseSearchAggregatorSuggestionsRequest(
const std::u16string& query,
const GURL& suggest_url,
std::vector<int> callback_indexes,
std::vector<std::vector<int>> suggestion_types,
CreationCallback creation_callback,
StartCallback start_callback,
CompletionCallback completion_callback);
// Stops creating the request. Already created requests aren't affected.
void StopCreatingEnterpriseSearchAggregatorSuggestionsRequest();
private:
// Called when an access token request completes (successfully or not).
void AccessTokenAvailable(
std::vector<std::unique_ptr<network::ResourceRequest>> requests,
const std::u16string& query,
std::vector<int> callback_indexes,
std::vector<std::vector<int>> suggestion_types,
net::NetworkTrafficAnnotationTag traffic_annotation,
StartCallback start_callback,
CompletionCallback completion_callback,
GoogleServiceAuthError error,
signin::AccessTokenInfo access_token_info);
// TODO(crbug.com/385756623): Factor out this method so it can be used across
// document_suggestions_service and
// enterprise_search_aggregator_suggestions_service.
void StartDownloadAndTransferLoader(
std::unique_ptr<network::ResourceRequest> request,
std::string request_body,
net::NetworkTrafficAnnotationTag traffic_annotation,
int request_index,
StartCallback start_callback,
CompletionCallback completion_callback);
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
// This will outlive this instance because of the factory dependencies.
raw_ptr<signin::IdentityManager> identity_manager_;
// Helper for fetching OAuth2 access tokens. Non-null when we have a token
// available, or while a token fetch is in progress.
std::unique_ptr<signin::PrimaryAccountAccessTokenFetcher> token_fetcher_;
};
#endif // COMPONENTS_OMNIBOX_BROWSER_ENTERPRISE_SEARCH_AGGREGATOR_SUGGESTIONS_SERVICE_H_
|