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
|
// 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 CHROME_BROWSER_CONTEXTUAL_CUEING_ZERO_STATE_SUGGESTIONS_REQUEST_H_
#define CHROME_BROWSER_CONTEXTUAL_CUEING_ZERO_STATE_SUGGESTIONS_REQUEST_H_
#include <string>
#include <vector>
#include "base/callback_list.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "components/optimization_guide/proto/features/zero_state_suggestions.pb.h"
class OptimizationGuideKeyedService;
namespace content {
class WebContents;
} // namespace content
namespace optimization_guide {
class ModelQualityLogEntry;
struct OptimizationGuideModelExecutionResult;
} // namespace optimization_guide
namespace contextual_cueing {
class ZeroStateSuggestionsPageData;
// Encapsulates logic for a single zero-state suggestions request.
class ZeroStateSuggestionsRequest {
public:
ZeroStateSuggestionsRequest(
OptimizationGuideKeyedService* optimization_guide_keyed_service,
const optimization_guide::proto::ZeroStateSuggestionsRequest&
pending_base_request,
const std::vector<content::WebContents*>& requested_tabs,
const content::WebContents* focused_tab);
~ZeroStateSuggestionsRequest();
static void Destroy(std::unique_ptr<ZeroStateSuggestionsRequest>);
// Adds a callback for this pending request that gets invoked when suggestions
// have been returned.
void AddCallback(base::OnceCallback<void(std::vector<std::string>)>);
// Returns the tabs that were requested to get suggestions for.
std::vector<content::WebContents*> GetRequestedTabs() const;
private:
friend class ContextualCueingServiceTestZeroStateSuggestions;
// Callback invoked when all requested zero state page contexts have been
// extracted.
void OnAllPageContextExtracted(
const std::vector<
std::optional<optimization_guide::proto::ZeroStatePageContext>>&
zero_state_page_contexts);
// Callback invoked when model execution has completed.
void OnModelExecutionResponse(
base::TimeTicks mes_begin_time,
optimization_guide::OptimizationGuideModelExecutionResult result,
std::unique_ptr<optimization_guide::ModelQualityLogEntry> log_entry);
// Caches `suggestions_to_cache` in `focused_tab_page_data_` if the request is
// for focused tab suggestions.
void CacheFocusedTabSuggestions(
const std::vector<std::string>& suggestions_to_cache);
// The time when this request was initiated.
base::TimeTicks begin_time_;
// The request proto to build off of to ultimately send for model execution.
optimization_guide::proto::ZeroStateSuggestionsRequest pending_base_request_;
// The list of callbacks to invoke when model execution has completed.
base::OnceCallbackList<void(std::vector<std::string>)> pending_callbacks_;
// Weak pointer to focused tab page data to cache suggestions later.
base::WeakPtr<ZeroStateSuggestionsPageData> focused_tab_page_data_;
// The tabs requested to get suggestions for.
std::vector<content::WebContents*> requested_tabs_;
// Not owned. Guaranteed to outlive `this`.
raw_ptr<OptimizationGuideKeyedService> optimization_guide_keyed_service_;
base::WeakPtrFactory<ZeroStateSuggestionsRequest> weak_ptr_factory_{this};
};
} // namespace contextual_cueing
#endif // CHROME_BROWSER_CONTEXTUAL_CUEING_ZERO_STATE_SUGGESTIONS_REQUEST_H_
|