File: browser_feature_promo_preconditions.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (241 lines) | stat: -rw-r--r-- 10,954 bytes parent folder | download | duplicates (2)
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
// Copyright 2024 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/ui/views/user_education/impl/browser_feature_promo_preconditions.h"

#include "base/time/default_clock.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_service.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_service_factory.h"
#include "chrome/browser/search_engine_choice/search_engine_choice_dialog_service.h"
#include "chrome/browser/search_engine_choice/search_engine_choice_dialog_service_factory.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h"
#include "chrome/browser/ui/exclusive_access/fullscreen_controller.h"
#include "chrome/browser/ui/location_bar/location_bar.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
#include "chrome/browser/ui/views/toolbar/toolbar_controller.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "components/omnibox/browser/omnibox_edit_model.h"
#include "components/omnibox/browser/omnibox_popup_view.h"
#include "components/omnibox/browser/omnibox_view.h"
#include "components/user_education/common/feature_promo/feature_promo_controller.h"
#include "components/user_education/common/feature_promo/feature_promo_precondition.h"
#include "components/user_education/common/feature_promo/feature_promo_result.h"
#include "components/user_education/common/feature_promo/impl/common_preconditions.h"
#include "components/user_education/common/feature_promo/impl/precondition_data.h"
#include "components/user_education/common/user_education_features.h"
#include "components/user_education/webui/help_bubble_handler.h"
#include "components/user_education/webui/tracked_element_webui.h"
#include "content/public/browser/web_contents.h"
#include "ui/events/types/event_type.h"
#include "ui/views/interaction/element_tracker_views.h"
#include "ui/views/widget/widget.h"

DEFINE_FEATURE_PROMO_PRECONDITION_IDENTIFIER_VALUE(kWindowActivePrecondition);
DEFINE_FEATURE_PROMO_PRECONDITION_IDENTIFIER_VALUE(
    kContentNotFullscreenPrecondition);
DEFINE_FEATURE_PROMO_PRECONDITION_IDENTIFIER_VALUE(kOmniboxNotOpenPrecondition);
DEFINE_FEATURE_PROMO_PRECONDITION_IDENTIFIER_VALUE(
    kToolbarNotCollapsedPrecondition);
DEFINE_FEATURE_PROMO_PRECONDITION_IDENTIFIER_VALUE(
    kBrowserNotClosingPrecondition);
DEFINE_FEATURE_PROMO_PRECONDITION_IDENTIFIER_VALUE(
    kNoCriticalNoticeShowingPrecondition);
DEFINE_FEATURE_PROMO_PRECONDITION_IDENTIFIER_VALUE(kUserNotActivePrecondition);

WindowActivePrecondition::WindowActivePrecondition()
    : FeaturePromoPreconditionBase(kWindowActivePrecondition,
                                   "Target window is active") {}
WindowActivePrecondition::~WindowActivePrecondition() = default;

user_education::FeaturePromoResult WindowActivePrecondition::CheckPrecondition(
    ComputedData& data) const {
  if (user_education::FeaturePromoControllerCommon::
          active_window_check_blocked()) {
    return user_education::FeaturePromoResult::Success();
  }
  auto& element_ref =
      data.Get(user_education::AnchorElementPrecondition::kAnchorElement);
  views::Widget* widget = nullptr;
  if (auto* const view_el = element_ref.get_as<views::TrackedElementViews>()) {
    widget = view_el->view()->GetWidget();
  } else if (auto* web_el =
                 element_ref.get_as<user_education::TrackedElementWebUI>()) {
    auto* const contents = web_el->handler()->GetWebContents();
    widget = views::Widget::GetWidgetForNativeWindow(
        contents->GetTopLevelNativeWindow());
  }
  if (widget) {
    // For some reason sometimes primary can be null;
    // see https://crbug.com/400921315
    if (auto* const primary = widget->GetPrimaryWindowWidget()) {
      widget = primary;
    }
  }
  return widget && widget->ShouldPaintAsActive()
             ? user_education::FeaturePromoResult::Success()
             : user_education::FeaturePromoResult::kAnchorSurfaceNotActive;
}

ContentNotFullscreenPrecondition::ContentNotFullscreenPrecondition(
    Browser& browser)
    : FeaturePromoPreconditionBase(kContentNotFullscreenPrecondition,
                                   "Content is not fullscreen"),
      browser_(browser) {}
ContentNotFullscreenPrecondition::~ContentNotFullscreenPrecondition() = default;

user_education::FeaturePromoResult
ContentNotFullscreenPrecondition::CheckPrecondition(ComputedData& data) const {
  auto* const fullscreen_controller =
      browser_->exclusive_access_manager()->fullscreen_controller();
  if (fullscreen_controller->IsWindowFullscreenForTabOrPending() ||
      fullscreen_controller->IsExtensionFullscreenOrPending()) {
    return user_education::FeaturePromoResult::kBlockedByUi;
  }
  return user_education::FeaturePromoResult::Success();
}

OmniboxNotOpenPrecondition::OmniboxNotOpenPrecondition(
    const BrowserView& browser_view)
    : FeaturePromoPreconditionBase(kOmniboxNotOpenPrecondition,
                                   "Omnibox is not open"),
      browser_view_(browser_view) {}
OmniboxNotOpenPrecondition::~OmniboxNotOpenPrecondition() = default;

user_education::FeaturePromoResult
OmniboxNotOpenPrecondition::CheckPrecondition(ComputedData&) const {
  const OmniboxPopupView* const popup = browser_view_->GetLocationBarView()
                                            ->GetOmniboxView()
                                            ->model()
                                            ->get_popup_view();
  return popup && popup->IsOpen()
             ? user_education::FeaturePromoResult::kBlockedByUi
             : user_education::FeaturePromoResult::Success();
}

ToolbarNotCollapsedPrecondition::ToolbarNotCollapsedPrecondition(
    BrowserView& browser_view)
    : FeaturePromoPreconditionBase(kToolbarNotCollapsedPrecondition,
                                   "Toolbar is not collapsed"),
      browser_view_(browser_view) {}
ToolbarNotCollapsedPrecondition::~ToolbarNotCollapsedPrecondition() = default;

user_education::FeaturePromoResult
ToolbarNotCollapsedPrecondition::CheckPrecondition(ComputedData&) const {
  if (const auto* const controller =
          browser_view_->toolbar()->toolbar_controller()) {
    if (controller->InOverflowMode()) {
      return user_education::FeaturePromoResult::kWindowTooSmall;
    }
  }
  return user_education::FeaturePromoResult::Success();
}

BrowserNotClosingPrecondition::BrowserNotClosingPrecondition(
    BrowserView& browser_view)
    : FeaturePromoPreconditionBase(kBrowserNotClosingPrecondition,
                                   "Browser is not closing"),
      browser_view_(browser_view) {}
BrowserNotClosingPrecondition::~BrowserNotClosingPrecondition() = default;

user_education::FeaturePromoResult
BrowserNotClosingPrecondition::CheckPrecondition(ComputedData&) const {
  if (browser_view_->browser()->IsBrowserClosing() ||
      browser_view_->GetWidget()->IsClosed()) {
    return user_education::FeaturePromoResult::kBlockedByContext;
  }
  return user_education::FeaturePromoResult::Success();
}

NoCriticalNoticeShowingPrecondition::NoCriticalNoticeShowingPrecondition(
    BrowserView& browser_view)
    : FeaturePromoPreconditionBase(kNoCriticalNoticeShowingPrecondition,
                                   "No critical notice is showing"),
      browser_view_(browser_view) {}
NoCriticalNoticeShowingPrecondition::~NoCriticalNoticeShowingPrecondition() =
    default;

user_education::FeaturePromoResult
NoCriticalNoticeShowingPrecondition::CheckPrecondition(ComputedData&) const {
  // Turn off IPH while a required privacy interstitial is visible or pending.
  auto* const privacy_sandbox_service =
      PrivacySandboxServiceFactory::GetForProfile(browser_view_->GetProfile());
  if (privacy_sandbox_service &&
      privacy_sandbox_service->GetRequiredPromptType(
          PrivacySandboxService::SurfaceType::kDesktop) !=
          PrivacySandboxService::PromptType::kNone) {
    return user_education::FeaturePromoResult::kBlockedByUi;
  }

  // Turn off IPH while a required search engine choice dialog is visible or
  // pending.
  SearchEngineChoiceDialogService* const search_engine_choice_dialog_service =
      SearchEngineChoiceDialogServiceFactory::GetForProfile(
          browser_view_->GetProfile());
  if (search_engine_choice_dialog_service &&
      search_engine_choice_dialog_service->HasPendingDialog(
          *browser_view_->browser())) {
    return user_education::FeaturePromoResult::kBlockedByUi;
  }

  return user_education::FeaturePromoResult::Success();
}

UserNotActivePrecondition::UserNotActivePrecondition(
    BrowserView& browser_view,
    const user_education::UserEducationTimeProvider& time_provider)
    : FeaturePromoPreconditionBase(kUserNotActivePrecondition,
                                   "The user is not actively sending input"),
      browser_view_(browser_view),
      time_provider_(time_provider) {
  if (browser_view.GetWidget()) {
    CreateEventMonitor();
  } else {
    browser_view_observation_.Observe(&browser_view);
  }
}

UserNotActivePrecondition::~UserNotActivePrecondition() = default;

void UserNotActivePrecondition::CreateEventMonitor() {
  // Note that null is a valid value for the second parameter here; if for
  // some reason there is no native window it simply falls back to
  // application-wide event-sniffing, which for this case is better than not
  // watching events at all.
  event_monitor_ = views::EventMonitor::CreateWindowMonitor(
      this, browser_view_->GetWidget()->GetTopLevelWidget()->GetNativeWindow(),
      {ui::EventType::kKeyPressed, ui::EventType::kKeyReleased});
}

void UserNotActivePrecondition::OnEvent(const ui::Event& event) {
  // Delay heavyweight IPH for the prescribed amount of time.
  last_active_time_ = time_provider_->GetCurrentTime();
}

user_education::FeaturePromoResult UserNotActivePrecondition::CheckPrecondition(
    ComputedData&) const {
  // Only do check if min idle time is nonzero and positive; otherwise this is a
  // no-op. Explicitly verify this in case of non-monotonic clock weirdness.
  const auto min_idle_time =
      user_education::features::GetIdleTimeBeforeHeavyweightPromo();
  if (min_idle_time.is_positive()) {
    const auto elapsed = time_provider_->GetCurrentTime() - last_active_time_;
    return elapsed < min_idle_time
               ? user_education::FeaturePromoResult::kBlockedByUserActivity
               : user_education::FeaturePromoResult::Success();
  } else {
    return user_education::FeaturePromoResult::Success();
  }
}

void UserNotActivePrecondition::OnViewAddedToWidget(
    views::View* observed_view) {
  browser_view_observation_.Reset();
  CreateEventMonitor();
}

void UserNotActivePrecondition::OnViewIsDeleting(views::View* observed_view) {
  browser_view_observation_.Reset();
}