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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
// Copyright 2022 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_PAGE_IMAGE_SERVICE_IMAGE_SERVICE_H_
#define COMPONENTS_PAGE_IMAGE_SERVICE_IMAGE_SERVICE_H_
#include <memory>
#include <string>
#include "base/containers/flat_map.h"
#include "base/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/optimization_guide/core/optimization_guide_decision.h"
#include "components/page_image_service/mojom/page_image_service.mojom.h"
#include "components/sync/service/sync_service.h"
class AutocompleteSchemeClassifier;
class RemoteSuggestionsService;
class SearchTermsData;
class TemplateURL;
class TemplateURLService;
namespace optimization_guide {
class OptimizationGuideDecider;
} // namespace optimization_guide
namespace page_image_service {
class ImageServiceConsentHelper;
enum class PageImageServiceConsentStatus;
// Through my manual testing, 16ms (which is about a frame at 60hz) allowed
// for decent aggregation without introducing any perceptible lag.
constexpr base::TimeDelta kOptimizationGuideBatchingTimeout =
base::Milliseconds(16);
// Used to get the image URL associated with a cluster. It doesn't actually
// fetch the image, that's up to the UI to do.
class ImageService : public KeyedService {
public:
using ResultCallback = base::OnceCallback<void(const GURL& image_url)>;
ImageService(TemplateURLService* template_url_service,
RemoteSuggestionsService* remote_suggestions_service,
optimization_guide::OptimizationGuideDecider* opt_guide,
syncer::SyncService* sync_service,
std::unique_ptr<AutocompleteSchemeClassifier>
autocomplete_scheme_classifier);
ImageService(const ImageService&) = delete;
ImageService& operator=(const ImageService&) = delete;
~ImageService() override;
// Gets a weak pointer to this service. Used when UIs want to create an
// object whose lifetime might exceed the service.
base::WeakPtr<ImageService> GetWeakPtr();
// Fetches an image appropriate for `page_url`, returning the result
// asynchronously to `callback`. The callback is always invoked. If there are
// no images available, it is invoked with an empty GURL result.
void FetchImageFor(mojom::ClientId client_id,
const GURL& page_url,
const mojom::Options& options,
ResultCallback callback);
// Asynchronously returns whether `client_id` has consent to fetch an image.
// Public for testing purposes only.
void GetConsentToFetchImage(
mojom::ClientId client_id,
base::OnceCallback<void(PageImageServiceConsentStatus)> callback);
private:
class SuggestEntityImageURLFetcher;
struct OptGuideRequest {
OptGuideRequest();
~OptGuideRequest();
OptGuideRequest(OptGuideRequest&& other);
GURL url;
ResultCallback callback;
};
// Callback to `GetConsentToFetchImage`, proceeds to call the appropriate
// backend.
void OnConsentResult(mojom::ClientId client_id,
const GURL& page_url,
const mojom::Options& options,
ResultCallback callback,
PageImageServiceConsentStatus status);
// Fetches an image from Suggest appropriate for `search_query` and
// `entity_id`, returning the result asynchronously to `callback`.
void FetchSuggestImage(const TemplateURL* template_url,
const SearchTermsData& search_terms_data,
mojom::ClientId client_id,
const std::u16string& search_query,
const std::string& entity_id,
ResultCallback callback);
// Callback for `FetchSuggestImage`.
void OnSuggestImageFetched(
std::unique_ptr<SuggestEntityImageURLFetcher> fetcher,
ResultCallback callback,
const GURL& image_url);
// Fetches an image from Optimization Guide appropriate for the parameters.
// This internally puts the request into a queue for batching purposes.
virtual void FetchOptimizationGuideImage(mojom::ClientId client_id,
const GURL& page_url,
ResultCallback callback);
// Processes all the enqueued optimization guide requests for
// `client_id` in a single batch.
void ProcessAllBatchedOptimizationGuideRequests(mojom::ClientId client_id);
// Callback for `ProcessAllBatchedOptimizationGuideRequests`.
// Takes ownership of `original_requests`, which has all the original
// `OnceCallback`s that this method fulfills.
void OnOptimizationGuideImageFetched(
mojom::ClientId client_id,
const GURL& url,
const base::flat_map<
optimization_guide::proto::OptimizationType,
optimization_guide::OptimizationGuideDecisionWithMetadata>&
decisions);
// Non-owning pointers to service dependencies. They may be nullptr.
raw_ptr<TemplateURLService> template_url_service_ = nullptr;
raw_ptr<RemoteSuggestionsService> remote_suggestions_service_ = nullptr;
raw_ptr<optimization_guide::OptimizationGuideDecider> opt_guide_ = nullptr;
// The History consent throttle, used for most clients.
std::unique_ptr<ImageServiceConsentHelper> history_consent_helper_;
// The Bookmarks consent throttle.
std::unique_ptr<ImageServiceConsentHelper> bookmarks_consent_helper_;
// Used to make proper suggest requests.
std::unique_ptr<AutocompleteSchemeClassifier> autocomplete_scheme_classifier_;
// Stores all the Optimization Guide requests that are still waiting to be
// aggregated into a batch and sent. When sent in a batch, the requests are
// moved to `sent_opt_guide_requests_`.
base::flat_map<mojom::ClientId, std::vector<OptGuideRequest>>
unsent_opt_guide_requests_;
// Stores all the Optimization Guide requests that have already been sent, and
// are awaiting a response from the Optimization Guide service.
base::flat_map<mojom::ClientId, std::vector<OptGuideRequest>>
sent_opt_guide_requests_;
// The timers used to allow for some requests to accumulate before sending a
// batch request to Optimization Guide Service. One timer per client ID.
// Insertion doesn't compile unless the timer is wrapped in a unique pointer.
base::flat_map<mojom::ClientId, std::unique_ptr<base::OneShotTimer>>
opt_guide_timers_;
base::WeakPtrFactory<ImageService> weak_factory_{this};
};
} // namespace page_image_service
#endif // COMPONENTS_PAGE_IMAGE_SERVICE_IMAGE_SERVICE_H_
|