File: password_bubble_view_base.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 (303 lines) | stat: -rw-r--r-- 14,079 bytes parent folder | download | duplicates (3)
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright 2017 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/passwords/password_bubble_view_base.h"

#include "base/functional/bind.h"
#include "base/notreached.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/actions/chrome_action_id.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_actions.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/passwords/passwords_model_delegate.h"
#include "chrome/browser/ui/views/accessibility/theme_tracking_non_accessible_image_view.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/chrome_typography.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/toolbar_button_provider.h"
#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
#include "chrome/browser/ui/views/passwords/manage_passwords_icon_views.h"
#include "chrome/browser/ui/views/passwords/manage_passwords_view.h"
#include "chrome/browser/ui/views/passwords/move_to_account_store_bubble_view.h"
#include "chrome/browser/ui/views/passwords/password_add_username_view.h"
#include "chrome/browser/ui/views/passwords/password_auto_sign_in_view.h"
#include "chrome/browser/ui/views/passwords/password_change/password_change_view_factory.h"
#include "chrome/browser/ui/views/passwords/password_save_unsynced_credentials_locally_view.h"
#include "chrome/browser/ui/views/passwords/password_save_update_view.h"
#include "chrome/browser/ui/views/passwords/post_save_compromised_bubble_view.h"
#include "chrome/browser/ui/views/passwords/shared_passwords_notification_view.h"
#include "chrome/browser/ui/views/toolbar/pinned_toolbar_actions_container.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/browser/ui/views/webauthn/passkey_deleted_confirmation_view.h"
#include "chrome/browser/ui/views/webauthn/passkey_not_accepted_bubble_view.h"
#include "chrome/browser/ui/views/webauthn/passkey_saved_confirmation_view.h"
#include "chrome/browser/ui/views/webauthn/passkey_updated_confirmation_view.h"
#include "chrome/browser/ui/views/webauthn/passkey_upgrade_bubble_view.h"
#include "chrome/grit/generated_resources.h"
#include "components/password_manager/core/browser/features/password_features.h"
#include "components/password_manager/core/browser/password_form.h"
#include "components/password_manager/core/common/password_manager_ui.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/views/controls/button/button.h"

#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
#include "chrome/browser/ui/views/passwords/password_relaunch_chrome_view.h"
#endif

#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ui/views/passwords/biometric_authentication_confirmation_bubble_view.h"
#include "chrome/browser/ui/views/passwords/biometric_authentication_for_filling_bubble_view.h"
#endif

namespace {
PageActionIconType GetPageActionIconType(content::WebContents* web_contents) {
  base::WeakPtr<PasswordsModelDelegate> delegate =
      PasswordsModelDelegateFromWebContents(web_contents);
  password_manager::ui::State model_state = delegate->GetState();
  if (model_state == password_manager::ui::PASSWORD_CHANGE_STATE) {
    return PageActionIconType::kChangePassword;
  }
  return PageActionIconType::kManagePasswords;
}
}  // namespace

// static
PasswordBubbleViewBase* PasswordBubbleViewBase::g_manage_passwords_bubble_ =
    nullptr;

// static
void PasswordBubbleViewBase::ShowBubble(content::WebContents* web_contents,
                                        DisplayReason reason) {
  Browser* browser = chrome::FindBrowserWithTab(web_contents);
  DCHECK(browser);
  DCHECK(browser->window());
  DCHECK(!g_manage_passwords_bubble_ ||
         !g_manage_passwords_bubble_->GetWidget()->IsVisible());

  BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser);
  ToolbarButtonProvider* button_provider =
      browser_view->toolbar_button_provider();
  views::View* anchor_view =
      button_provider->GetAnchorView(kActionShowPasswordsBubbleOrPage);

  PasswordBubbleViewBase* bubble =
      CreateBubble(web_contents, anchor_view, reason);
  DCHECK(bubble);
  DCHECK_EQ(bubble, g_manage_passwords_bubble_);
  // TODO(crbug.com/40218026): In non-DCHECK mode we could fall through here and
  // hard-crash if we requested a bubble and were in the wrong state. In the
  // meantime we will abort if we did not create a bubble.
  if (!g_manage_passwords_bubble_) {
    return;
  }

  // If the anchor_view is a button, it will automatically be used as the
  // highlighted button by BubbleDialogDelegate. If not, we set the page action
  // icon as the highlighted button here.
  if (!views::Button::AsButton(anchor_view)) {
    g_manage_passwords_bubble_->SetHighlightedButton(
        button_provider->GetPageActionIconView(
            GetPageActionIconType(web_contents)));
  }

  views::BubbleDialogDelegateView::CreateBubble(g_manage_passwords_bubble_);

  g_manage_passwords_bubble_->ShowForReason(reason);
  g_manage_passwords_bubble_->RegisterWindowClosingCallback(base::BindOnce(
      [](PasswordBubbleViewBase* closing_bubble) {
        if (closing_bubble == g_manage_passwords_bubble_) {
          g_manage_passwords_bubble_ = nullptr;
        }
      },
      bubble));

    auto* passwords_action_item = actions::ActionManager::Get().FindAction(
        kActionShowPasswordsBubbleOrPage,
        browser->browser_actions()->root_action_item());
    CHECK(passwords_action_item);
    bool should_suppress_next_button_trigger =
        g_manage_passwords_bubble_->ShouldCloseOnDeactivate();
    passwords_action_item->SetIsShowingBubble(
        should_suppress_next_button_trigger);
}

// static
PasswordBubbleViewBase* PasswordBubbleViewBase::CreateBubble(
    content::WebContents* web_contents,
    views::View* anchor_view,
    DisplayReason reason) {
  PasswordBubbleViewBase* view = nullptr;
  base::WeakPtr<PasswordsModelDelegate> delegate =
      PasswordsModelDelegateFromWebContents(web_contents);
  password_manager::ui::State model_state = delegate->GetState();
  if (model_state == password_manager::ui::MANAGE_STATE) {
    view = new ManagePasswordsView(web_contents, anchor_view);
  } else if (model_state == password_manager::ui::AUTO_SIGNIN_STATE) {
    view = new PasswordAutoSignInView(web_contents, anchor_view);
  } else if (model_state == password_manager::ui::SAVE_CONFIRMATION_STATE ||
             model_state == password_manager::ui::UPDATE_CONFIRMATION_STATE) {
    view = new ManagePasswordsView(web_contents, anchor_view);
  } else if (model_state ==
                 password_manager::ui::PENDING_PASSWORD_UPDATE_STATE ||
             model_state == password_manager::ui::PENDING_PASSWORD_STATE) {
    view = new PasswordSaveUpdateView(web_contents, anchor_view, reason);
  } else if (model_state == password_manager::ui::
                                WILL_DELETE_UNSYNCED_ACCOUNT_PASSWORDS_STATE) {
    view = new PasswordSaveUnsyncedCredentialsLocallyView(web_contents,
                                                          anchor_view);
  } else if (model_state ==
                 password_manager::ui::MOVE_CREDENTIAL_AFTER_LOG_IN_STATE ||
             model_state == password_manager::ui::
                                MOVE_CREDENTIAL_FROM_MANAGE_BUBBLE_STATE) {
    view = new MoveToAccountStoreBubbleView(web_contents, anchor_view);
  } else if (model_state == password_manager::ui::PASSWORD_UPDATED_SAFE_STATE ||
             model_state ==
                 password_manager::ui::PASSWORD_UPDATED_MORE_TO_FIX) {
    view = new PostSaveCompromisedBubbleView(web_contents, anchor_view);
  } else if (model_state ==
             password_manager::ui::GENERATED_PASSWORD_CONFIRMATION_STATE) {
    view = new PasswordAddUsernameView(web_contents, anchor_view, reason);
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_CHROMEOS)
  } else if (model_state ==
             password_manager::ui::BIOMETRIC_AUTHENTICATION_FOR_FILLING_STATE) {
    view = new BiometricAuthenticationForFillingBubbleView(
        web_contents, anchor_view,
        Profile::FromBrowserContext(web_contents->GetBrowserContext())
            ->GetPrefs(),
        reason);
  } else if (model_state == password_manager::ui::
                                BIOMETRIC_AUTHENTICATION_CONFIRMATION_STATE) {
    view = new BiometricAuthenticationConfirmationBubbleView(web_contents,
                                                             anchor_view);
#endif
  } else if (model_state ==
             password_manager::ui::NOTIFY_RECEIVED_SHARED_CREDENTIALS) {
    view = new SharedPasswordsNotificationView(web_contents, anchor_view);
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
  } else if (model_state == password_manager::ui::KEYCHAIN_ERROR_STATE) {
    view = new RelaunchChromeView(
        web_contents, anchor_view,
        Profile::FromBrowserContext(web_contents->GetBrowserContext())
            ->GetPrefs());
#endif
  } else if (model_state ==
             password_manager::ui::PASSKEY_SAVED_CONFIRMATION_STATE) {
    view = new PasskeySavedConfirmationView(web_contents, anchor_view,
                                            delegate->PasskeyRpId());
  } else if (model_state ==
             password_manager::ui::PASSKEY_DELETED_CONFIRMATION_STATE) {
    view =
        new PasskeyDeletedConfirmationView(web_contents, anchor_view, reason);
  } else if (model_state ==
             password_manager::ui::PASSKEY_UPDATED_CONFIRMATION_STATE) {
    view = new PasskeyUpdatedConfirmationView(web_contents, anchor_view, reason,
                                              delegate->PasskeyRpId());
  } else if (model_state == password_manager::ui::PASSKEY_NOT_ACCEPTED_STATE) {
    view = new PasskeyNotAcceptedBubbleView(web_contents, anchor_view, reason,
                                            delegate->PasskeyRpId());
  } else if (model_state == password_manager::ui::PASSKEY_UPGRADE_STATE) {
    view = new PasskeyUpgradeBubbleView(web_contents, anchor_view, reason,
                                        delegate->PasskeyRpId());
  } else if (model_state == password_manager::ui::PASSWORD_CHANGE_STATE) {
    view = CreatePasswordChangeBubbleView(delegate->GetPasswordChangeDelegate(),
                                          web_contents, anchor_view);
  } else {
    NOTREACHED();
  }

  g_manage_passwords_bubble_ = view;
  return view;
}

// static
void PasswordBubbleViewBase::CloseCurrentBubble() {
  if (g_manage_passwords_bubble_) {
    // It can be the case that a password bubble is being closed while another
    // password bubble is being opened. The metrics recorder can be shared
    // between them and it doesn't understand the sequence [open1, open2,
    // close1, close2]. Therefore, we reset the model early (before the bubble
    // destructor) to get the following sequence of events [open1, close1,
    // open2, close2].
    PasswordBubbleControllerBase* controller =
        g_manage_passwords_bubble_->GetController();
    DCHECK(controller);
    controller->OnBubbleClosing();
    if (auto* const widget = g_manage_passwords_bubble_->GetWidget()) {
      widget->Close();
    }
  }
}

// static
void PasswordBubbleViewBase::ActivateBubble() {
  DCHECK(g_manage_passwords_bubble_);
  DCHECK(g_manage_passwords_bubble_->GetWidget()->IsVisible());
  g_manage_passwords_bubble_->GetWidget()->Activate();
}

const content::WebContents* PasswordBubbleViewBase::GetWebContents() const {
  const PasswordBubbleControllerBase* controller = GetController();
  DCHECK(controller);
  return controller->GetWebContents();
}

PasswordBubbleViewBase::PasswordBubbleViewBase(
    content::WebContents* web_contents,
    views::View* anchor_view,
    bool easily_dismissable)
    : LocationBarBubbleDelegateView(anchor_view,
                                    web_contents,
                                    /*autosize=*/true) {
  SetShowCloseButton(true);
  set_fixed_width(views::LayoutProvider::Get()->GetDistanceMetric(
      views::DISTANCE_BUBBLE_PREFERRED_WIDTH));
  set_close_on_deactivate(easily_dismissable);

  browser_ = chrome::FindBrowserWithTab(web_contents);
}

PasswordBubbleViewBase::~PasswordBubbleViewBase() {
  CHECK(this != g_manage_passwords_bubble_);
  // It is possible in tests for |browser_| not to exist.
  if (browser_) {
    auto* passwords_action_item = actions::ActionManager::Get().FindAction(
        kActionShowPasswordsBubbleOrPage,
        browser_->browser_actions()->root_action_item());
    CHECK(passwords_action_item);
    passwords_action_item->SetIsShowingBubble(false);
  }
}

void PasswordBubbleViewBase::SetBubbleHeader(int light_image_id,
                                             int dark_image_id) {
  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
  auto image_view = std::make_unique<ThemeTrackingNonAccessibleImageView>(
      *bundle.GetImageSkiaNamed(light_image_id),
      *bundle.GetImageSkiaNamed(dark_image_id),
      base::BindRepeating(&views::BubbleDialogDelegate::background_color,
                          base::Unretained(this)));

  gfx::Size preferred_size = image_view->GetPreferredSize();
  if (preferred_size.width()) {
    float scale =
        static_cast<float>(ChromeLayoutProvider::Get()->GetDistanceMetric(
            views::DISTANCE_BUBBLE_PREFERRED_WIDTH)) /
        preferred_size.width();
    preferred_size = gfx::ScaleToRoundedSize(preferred_size, scale);
    image_view->SetImageSize(preferred_size);
  }
  GetBubbleFrameView()->SetHeaderView(std::move(image_view));
}

void PasswordBubbleViewBase::Init() {
  LocationBarBubbleDelegateView::Init();
  const PasswordBubbleControllerBase* controller = GetController();
  DCHECK(controller);
  SetTitle(controller->GetTitle());
  SetShowTitle(!controller->GetTitle().empty());
}

BEGIN_METADATA(PasswordBubbleViewBase)
END_METADATA