File: glic_zero_state_suggestions_manager.cc

package info (click to toggle)
chromium 140.0.7339.127-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,192,880 kB
  • sloc: cpp: 35,093,808; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (274 lines) | stat: -rw-r--r-- 11,114 bytes parent folder | download | duplicates (4)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// 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.

#include "chrome/browser/glic/glic_zero_state_suggestions_manager.h"

#include "base/functional/bind.h"
#include "chrome/browser/contextual_cueing/contextual_cueing_features.h"
#include "chrome/browser/contextual_cueing/contextual_cueing_service.h"
#include "chrome/browser/glic/host/context/glic_sharing_manager_impl.h"
#include "chrome/browser/glic/host/host.h"
#include "chrome/browser/glic/widget/glic_window_controller.h"
#include "mojo/public/cpp/bindings/callback_helpers.h"

namespace glic {

namespace {

mojom::ZeroStateSuggestionsV2Ptr MakePendingSuggestionsPtr() {
  auto pending_suggestions = mojom::ZeroStateSuggestionsV2::New();
  pending_suggestions->is_pending = true;

  std::vector<mojom::SuggestionContentPtr> empty_suggestions;
  pending_suggestions->suggestions = std::move(empty_suggestions);
  return pending_suggestions;
}

}  // namespace

GlicZeroStateSuggestionsManager::GlicZeroStateSuggestionsManager(
    GlicSharingManagerImpl* sharing_manager,
    GlicWindowController* window_controller,
    contextual_cueing::ContextualCueingService* contextual_cueing_service,
    Host* host)
    : sharing_manager_(sharing_manager),
      window_controller_(window_controller),
      host_(host),
      contextual_cueing_service_(contextual_cueing_service) {}

GlicZeroStateSuggestionsManager::~GlicZeroStateSuggestionsManager() = default;

void GlicZeroStateSuggestionsManager::
    NotifyZeroStateSuggestionsOnFocusedTabDataChanged(
        bool is_first_run,
        const std::vector<std::string>& supported_tools,
        const mojom::TabData* focused_tab_data) {
  if (!window_controller_->IsShowing()) {
    return;
  }

  // Pinned tabs are a more intentional sharing choice than focused tab, so
  // don't refresh the suggestions on focus change if there are pinned tabs.
  if (sharing_manager_->GetNumPinnedTabs()) {
    return;
  }

  content::WebContents* active_web_contents =
      sharing_manager_->GetFocusedTabData().focus()
          ? sharing_manager_->GetFocusedTabData().focus()->GetContents()
          : nullptr;

  if (contextual_cueing_service_ && active_web_contents) {
    // Notify host that suggestions are pending.
    host_->NotifyZeroStateSuggestion(
        MakePendingSuggestionsPtr(),
        mojom::ZeroStateSuggestionsOptions(is_first_run, supported_tools));

    contextual_cueing_service_
        ->GetContextualGlicZeroStateSuggestionsForFocusedTab(
            active_web_contents, is_first_run, supported_tools,
            mojo::WrapCallbackWithDefaultInvokeIfNotRun(
                base::BindOnce(&GlicZeroStateSuggestionsManager::
                                   OnZeroStateSuggestionsNotify,
                               GetWeakPtr(), is_first_run, supported_tools),
                /*returned_suggestions=*/
                std::vector<std::string>({})));
  }
}

void GlicZeroStateSuggestionsManager::
    NotifyZeroStateSuggestionsOnPinnedTabChanged(
        bool is_first_run,
        const std::vector<std::string>& supported_tools,
        const std::vector<content::WebContents*>& pinned_tab_data) {
  if (!window_controller_->IsShowing()) {
    return;
  }

  if (pinned_tab_data.size() >
      static_cast<size_t>(
          contextual_cueing::kMaxPinnedPagesForTriggeringSuggestions.Get())) {
    if (pause_pinned_subscription_updates_) {
      return;
    }
    pause_pinned_subscription_updates_ = true;
  } else {
    pause_pinned_subscription_updates_ = false;
  }

  // Also include the focused tab if there is one.
  FocusedTabData focused_tab_data = sharing_manager_->GetFocusedTabData();
  content::WebContents* active_web_contents =
      focused_tab_data.focus() ? focused_tab_data.focus()->GetContents()
                               : nullptr;
  std::vector<content::WebContents*> contents_for_request = pinned_tab_data;
  if (active_web_contents &&
      !Contains(contents_for_request, active_web_contents)) {
    contents_for_request.push_back(active_web_contents);
  }

  if (contextual_cueing_service_) {
    // Debounce if we already have an outstanding request for the same set.
    std::optional<std::vector<content::WebContents*>>
        outstanding_pinned_tabs_contents =
            contextual_cueing_service_->GetOutstandingPinnedTabsContents();
    if (outstanding_pinned_tabs_contents &&
        outstanding_pinned_tabs_contents->size() ==
            contents_for_request.size() &&
        std::equal(outstanding_pinned_tabs_contents->begin(),
                   outstanding_pinned_tabs_contents->end(),
                   contents_for_request.begin())) {
      return;
    }

    // Notify host that suggestions are pending in case there were suggestions
    // and we are posting an invalid state.
    host_->NotifyZeroStateSuggestion(
        MakePendingSuggestionsPtr(),
        mojom::ZeroStateSuggestionsOptions(is_first_run, supported_tools));

    bool suggestions_pending =
        contextual_cueing_service_
            ->GetContextualGlicZeroStateSuggestionsForPinnedTabs(
                contents_for_request, is_first_run, supported_tools,
                active_web_contents,
                mojo::WrapCallbackWithDefaultInvokeIfNotRun(
                    base::BindOnce(&GlicZeroStateSuggestionsManager::
                                       OnZeroStateSuggestionsNotify,
                                   GetWeakPtr(), is_first_run, supported_tools),
                    /*returned_suggestions=*/
                    std::vector<std::string>({})));

    if (suggestions_pending) {
      // Notify host that suggestions are pending.
      host_->NotifyZeroStateSuggestion(
          MakePendingSuggestionsPtr(),
          mojom::ZeroStateSuggestionsOptions(is_first_run, supported_tools));
    }
  }
}

void GlicZeroStateSuggestionsManager::
    NotifyZeroStateSuggestionsOnPinnedTabDataChanged(
        bool is_first_run,
        const std::vector<std::string>& supported_tools,
        const mojom::TabData* data) {
  NotifyZeroStateSuggestionsOnPinnedTabChanged(
      is_first_run, supported_tools, sharing_manager_->GetPinnedTabs());
}

void GlicZeroStateSuggestionsManager::ObserveZeroStateSuggestions(
    bool is_notifying,
    bool is_first_run,
    const std::vector<std::string>& supported_tools,
    glic::mojom::WebClientHandler::GetZeroStateSuggestionsAndSubscribeCallback
        callback) {
  // Subscribe to changes in sharing.
  if (is_notifying) {
    // If there were previous subscriptions they will be unsubscribed when the
    // old values are destructed on assignment.
    // TODO: b/433738020 - Investigate whether we should listen to a different
    // callback.
    current_zero_state_suggestions_focus_change_subscription_ =
        sharing_manager_->AddFocusedTabDataChangedCallback(base::BindRepeating(
            &GlicZeroStateSuggestionsManager::
                NotifyZeroStateSuggestionsOnFocusedTabDataChanged,
            GetWeakPtr(), is_first_run, supported_tools));
    current_zero_state_suggestions_pinned_tab_change_subscription_ =
        sharing_manager_->AddPinnedTabsChangedCallback(base::BindRepeating(
            &GlicZeroStateSuggestionsManager::
                NotifyZeroStateSuggestionsOnPinnedTabChanged,
            GetWeakPtr(), is_first_run, supported_tools));
    current_zero_state_suggestions_pinned_tab_data_change_subscription_ =
        sharing_manager_->AddPinnedTabDataChangedCallback(base::BindRepeating(
            &GlicZeroStateSuggestionsManager::
                NotifyZeroStateSuggestionsOnPinnedTabDataChanged,
            GetWeakPtr(), is_first_run, supported_tools));

    if (!contextual_cueing_service_) {
      return;
    }

    if (auto pinned_tabs = sharing_manager_->GetPinnedTabs();
        !pinned_tabs.empty()) {
      contextual_cueing_service_
          ->GetContextualGlicZeroStateSuggestionsForPinnedTabs(
              pinned_tabs, is_first_run, supported_tools,
              /* focused_tab=*/nullptr,
              mojo::WrapCallbackWithDefaultInvokeIfNotRun(
                  base::BindOnce(&GlicZeroStateSuggestionsManager::
                                     OnZeroStateSuggestionsFetched,
                                 GetWeakPtr(), std::move(callback)),
                  /*returned_suggestions=*/std::vector<std::string>({})));
      return;
    }

    auto* active_web_contents =
        sharing_manager_->GetFocusedTabData().focus()
            ? sharing_manager_->GetFocusedTabData().focus()->GetContents()
            : nullptr;
    if (active_web_contents) {
      contextual_cueing_service_
          ->GetContextualGlicZeroStateSuggestionsForFocusedTab(
              active_web_contents, is_first_run, supported_tools,
              mojo::WrapCallbackWithDefaultInvokeIfNotRun(
                  base::BindOnce(&GlicZeroStateSuggestionsManager::
                                     OnZeroStateSuggestionsFetched,
                                 GetWeakPtr(), std::move(callback)),
                  /*returned_suggestions=*/std::vector<std::string>({})));
      return;
    }
  } else {
    // If is_notifying is false we need to reset the subscriptions.
    Reset();
  }

  std::move(callback).Run(nullptr);
}

void GlicZeroStateSuggestionsManager::OnZeroStateSuggestionsFetched(
    mojom::WebClientHandler::GetZeroStateSuggestionsAndSubscribeCallback
        callback,
    std::vector<std::string> returned_suggestions) {
  auto suggestions = mojom::ZeroStateSuggestionsV2::New();
  std::vector<mojom::SuggestionContentPtr> output_suggestions;
  for (const std::string& suggestion_string : returned_suggestions) {
    output_suggestions.push_back(
        mojom::SuggestionContent::New(suggestion_string));
  }
  suggestions->suggestions = std::move(output_suggestions);
  suggestions->is_pending = false;

  std::move(callback).Run(std::move(suggestions));
}

void GlicZeroStateSuggestionsManager::OnZeroStateSuggestionsNotify(
    bool is_first_run,
    const std::vector<std::string>& supported_tools,
    std::vector<std::string> returned_suggestions) {
  auto suggestions_v2 = mojom::ZeroStateSuggestionsV2::New();
  std::vector<mojom::SuggestionContentPtr> output_suggestions;
  for (const std::string& suggestion_string : returned_suggestions) {
    output_suggestions.push_back(
        mojom::SuggestionContent::New(suggestion_string));
  }
  suggestions_v2->suggestions = std::move(output_suggestions);
  suggestions_v2->is_pending = false;
  host_->NotifyZeroStateSuggestion(
      std::move(suggestions_v2),
      mojom::ZeroStateSuggestionsOptions(is_first_run, supported_tools));
}

void GlicZeroStateSuggestionsManager::Reset() {
  current_zero_state_suggestions_focus_change_subscription_ = {};
  current_zero_state_suggestions_pinned_tab_change_subscription_ = {};
  current_zero_state_suggestions_pinned_tab_data_change_subscription_ = {};
}

base::WeakPtr<GlicZeroStateSuggestionsManager>
GlicZeroStateSuggestionsManager::GetWeakPtr() {
  return weak_ptr_factory_.GetWeakPtr();
}

}  // namespace glic