File: show_promo_in_page.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 (179 lines) | stat: -rw-r--r-- 6,603 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
// 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.

#include "chrome/browser/ui/user_education/show_promo_in_page.h"

#include <memory>

#include "base/callback_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/no_destructor.h"
#include "base/threading/thread_checker.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/user_education/user_education_types.h"
#include "chrome/browser/user_education/user_education_service.h"
#include "chrome/browser/user_education/user_education_service_factory.h"
#include "components/user_education/common/feature_promo/feature_promo_controller.h"
#include "components/user_education/common/help_bubble/help_bubble_factory_registry.h"
#include "components/user_education/common/help_bubble/help_bubble_params.h"
#include "components/user_education/webui/help_bubble_webui.h"
#include "content/public/browser/navigation_handle.h"
#include "ui/base/interaction/element_tracker.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/page_transition_types.h"
#include "ui/base/window_open_disposition.h"

namespace {

constexpr base::TimeDelta kShowPromoInPageTimeout = base::Seconds(30);

class ShowPromoInPageImpl : public ShowPromoInPage {
 public:
  explicit ShowPromoInPageImpl(Browser* browser, Params params)
      : browser_(browser->AsWeakPtr()), callback_(std::move(params.callback)) {
    DCHECK(callback_);
    DCHECK(browser_);
    DCHECK(!params.bubble_text.empty());

    base::TimeDelta timeout =
        params.timeout_override_for_testing.value_or(kShowPromoInPageTimeout);
    DCHECK(timeout.is_positive());

    bubble_params_.body_text = params.bubble_text;
    bubble_params_.arrow = params.bubble_arrow;
    bubble_params_.focus_on_show_hint = false;

    if (params.close_button_alt_text_id) {
      bubble_params_.close_button_alt_text =
          l10n_util::GetStringUTF16(params.close_button_alt_text_id.value());
    }

    anchor_subscription_ =
        ui::ElementTracker::GetElementTracker()
            ->AddElementShownInAnyContextCallback(
                params.bubble_anchor_id,
                base::BindRepeating(&ShowPromoInPageImpl::OnAnchorShown,
                                    base::Unretained(this)));

    if (params.target_url.has_value()) {
      NavigateParams navigate_params(browser, params.target_url.value(),
                                     ui::PAGE_TRANSITION_LINK);
      navigate_params.disposition =
          user_education::GetWindowOpenDisposition(params.page_open_mode);
      navigate_params.window_action = NavigateParams::SHOW_WINDOW;
      navigate_handle_ = Navigate(&navigate_params);
    } else {
      auto* visible_element =
          ui::ElementTracker::GetElementTracker()->GetElementInAnyContext(
              params.bubble_anchor_id);

      if (visible_element) {
        OnAnchorShown(visible_element);
      }
    }

    timeout_.Start(FROM_HERE, timeout,
                   base::BindOnce(&ShowPromoInPageImpl::OnTimeout,
                                  base::Unretained(this)));
  }

  ~ShowPromoInPageImpl() override {
    DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  }

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

  user_education::HelpBubble* GetHelpBubbleForTesting() override {
    return help_bubble_.get();
  }

 private:
  void OnAnchorShown(ui::TrackedElement* anchor_element) {
    DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
    anchor_subscription_ = base::CallbackListSubscription();
    navigate_handle_.reset();
    timeout_.Stop();

    // It's possible that the browser window was closed and somehow the tab
    // opened in another window. It's an edge case but an important one since a
    // HelpBubbleFactoryRegistry is needed to create the help bubble.
    if (browser_) {
      auto& factory =
          UserEducationServiceFactory::GetForBrowserContext(browser_->profile())
              ->help_bubble_factory_registry();
      help_bubble_ =
          factory.CreateHelpBubble(anchor_element, std::move(bubble_params_));
      DCHECK(help_bubble_);

      // Maybe focus the web contents containing the bubble (if it's the main
      // contents).
      if (help_bubble_) {
        if (auto* const bubble =
                help_bubble_->AsA<user_education::HelpBubbleWebUI>()) {
          if (browser_->tab_strip_model()->GetActiveWebContents() ==
              bubble->GetWebContents()) {
            browser_->window()->FocusWebContentsPane();
          }
        }
      }
    }

    if (!help_bubble_) {
      std::move(callback_).Run(this, false);
      delete this;
      return;
    }
    help_bubble_closed_subscription_ = help_bubble_->AddOnCloseCallback(
        base::BindOnce(&ShowPromoInPageImpl::OnBubbleClosed, GetWeakPtr()));
    std::move(callback_).Run(this, true);
  }

  void OnBubbleClosed(user_education::HelpBubble*,
                      user_education::HelpBubble::CloseReason) {
    delete this;
  }

  void OnTimeout() {
    DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
    anchor_subscription_ = base::CallbackListSubscription();
    navigate_handle_.reset();
    std::move(callback_).Run(this, false);
    delete this;
  }

  const base::WeakPtr<Browser> browser_;
  std::unique_ptr<user_education::HelpBubble> help_bubble_;
  base::CallbackListSubscription help_bubble_closed_subscription_;
  user_education::HelpBubbleParams bubble_params_;
  Callback callback_;
  base::WeakPtr<content::NavigationHandle> navigate_handle_;
  base::CallbackListSubscription anchor_subscription_;
  base::OneShotTimer timeout_;
  THREAD_CHECKER(thread_checker_);
  base::WeakPtrFactory<ShowPromoInPageImpl> weak_ptr_factory_{this};
};

}  // namespace

ShowPromoInPage::Params::Params() = default;
ShowPromoInPage::Params::Params(Params&& other) noexcept = default;
ShowPromoInPage::Params& ShowPromoInPage::Params::operator=(
    Params&& other) noexcept = default;
ShowPromoInPage::Params::~Params() = default;

ShowPromoInPage::ShowPromoInPage() = default;
ShowPromoInPage::~ShowPromoInPage() = default;

base::WeakPtr<ShowPromoInPage> ShowPromoInPage::Start(Browser* browser,
                                                      Params params) {
  return (new ShowPromoInPageImpl(browser, std::move(params)))->GetWeakPtr();
}