File: web_app_update_review_dialog.cc

package info (click to toggle)
chromium 140.0.7339.80-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,193,064 kB
  • sloc: cpp: 35,092,386; ansic: 7,161,671; javascript: 4,199,703; python: 1,441,797; asm: 949,904; xml: 747,409; 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 (327 lines) | stat: -rw-r--r-- 14,666 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// 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/ui/views/web_apps/web_app_update_review_dialog.h"

#include <optional>

#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "chrome/browser/picture_in_picture/picture_in_picture_occlusion_observer.h"
#include "chrome/browser/picture_in_picture/picture_in_picture_window_manager.h"
#include "chrome/browser/picture_in_picture/scoped_picture_in_picture_occlusion_observation.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/extensions/security_dialog_tracker.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/web_apps/web_app_update_identity_view.h"
#include "chrome/browser/web_applications/ui_manager/update_dialog_types.h"
#include "chrome/browser/web_applications/web_app_install_manager.h"
#include "chrome/browser/web_applications/web_app_install_manager_observer.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_ui_manager.h"
#include "chrome/browser/web_applications/web_app_utils.h"
#include "chrome/grit/generated_resources.h"
#include "components/constrained_window/constrained_window_views.h"
#include "components/vector_icons/vector_icons.h"
#include "components/web_modal/web_contents_modal_dialog_manager.h"
#include "components/webapps/browser/installable/installable_metrics.h"
#include "components/webapps/browser/uninstall_result_code.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/class_property.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/models/image_model.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/base/mojom/ui_base_types.mojom-shared.h"
#include "ui/color/color_id.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/views/bubble/bubble_dialog_model_host.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/table_layout_view.h"
#include "ui/views/style/typography.h"
#include "ui/views/widget/widget_observer.h"

namespace web_app {

namespace {

const int kArrowIconSizeDp = 32;

// The width of the columns left and right of the arrow (containing the name
// of the app (before and after).
const int kIdentityColumnWidth = 170;

int GetDialogTitleMessageId(const WebAppIdentityUpdate& update) {
  bool title_change = update.new_title.has_value();
  bool icon_change = update.new_icon.has_value();
  bool url_migration = update.new_start_url.has_value();

  const int kNameChange = 0b001;
  const int kIconChange = 0b010;
  const int kUrlChange = 0b100;
  int combination_index = (title_change ? kNameChange : 0) |
                          (icon_change ? kIconChange : 0) |
                          (url_migration ? kUrlChange : 0);
  switch (combination_index) {
    case 0:
      NOTREACHED();
    case kNameChange:
      return IDS_WEBAPP_UPDATE_DIALOG_TITLE_NAME;
    case kIconChange:
      return IDS_WEBAPP_UPDATE_DIALOG_TITLE_LOGO;
    case kNameChange | kIconChange:
      return IDS_WEBAPP_UPDATE_DIALOG_TITLE_NAME_AND_LOGO;
    case kUrlChange:
      return IDS_WEBAPP_UPDATE_DIALOG_TITLE_URL;
    case kNameChange | kUrlChange:
      return IDS_WEBAPP_UPDATE_DIALOG_TITLE_NAME_AND_URL;
    case kIconChange | kUrlChange:
      return IDS_WEBAPP_UPDATE_DIALOG_TITLE_LOGO_AND_URL;
    case kNameChange | kIconChange | kUrlChange:
      return IDS_WEBAPP_UPDATE_DIALOG_TITLE_NAME_AND_LOGO_AND_URL;
  }
  NOTREACHED();
}

class UpdateDialogDelegate : public ui::DialogModelDelegate,
                             public PictureInPictureOcclusionObserver,
                             public views::WidgetObserver,
                             public WebAppInstallManagerObserver {
 public:
  UpdateDialogDelegate(const webapps::AppId& app_id,
                       UpdateReviewDialogCallback callback,
                       Browser& browser)
      : app_id_(app_id), callback_(std::move(callback)), browser_(browser) {
    install_manager_observation_.Observe(
        &WebAppProvider::GetForWebApps(browser_->profile())->install_manager());
    browser_->GetBrowserView().SetProperty(kIsPwaUpdateDialogShowingKey, true);
  }
  ~UpdateDialogDelegate() override {
    browser_->GetBrowserView().SetProperty(kIsPwaUpdateDialogShowingKey, false);
  }

  void OnAcceptButtonClicked() {
    std::move(callback_).Run(WebAppIdentityUpdateResult::kAccept);
  }
  void OnIgnoreButtonClicked(const ui::Event& event) {
    std::move(callback_).Run(WebAppIdentityUpdateResult::kIgnore);
  }
  void OnUninstallButtonClicked() {
    std::move(callback_).Run(WebAppIdentityUpdateResult::kUninstallApp);
  }
  void OnClose() {
    // This should not be called, but due to lack of clarity with UI framework
    // assumptions, we should still handle this even if we asked for the close
    // button to be hidden.
    if (!callback_) {
      return;
    }
    std::move(callback_).Run(WebAppIdentityUpdateResult::kUnexpectedError);
  }
  // This is called when the dialog has been either accepted, cancelled, closed
  // or destroyed without an user-action.
  void OnDestroyed() {
    if (!callback_) {
      return;
    }
    std::move(callback_).Run(WebAppIdentityUpdateResult::kUnexpectedError);
  }

  void OnWidgetShownStartTracking(views::Widget* dialog_widget) {
    occlusion_observation_.Observe(dialog_widget);
    widget_observation_.Observe(dialog_widget);
    extensions::SecurityDialogTracker::GetInstance()->AddSecurityDialog(
        dialog_widget);
  }

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

  // PictureInPictureOcclusionObserver overrides:
  void OnOcclusionStateChanged(bool occluded) override {
    // If a picture-in-picture window is occluding the dialog, force it to close
    // to prevent spoofing.
    if (occluded) {
      PictureInPictureWindowManager::GetInstance()->ExitPictureInPicture();
    }
  }

  // views::WidgetObserver overrides:
  void OnWidgetDestroyed(views::Widget* widget) override {
    widget_observation_.Reset();
  }

  // WebAppInstallManagerObserver overrides:
  void OnWebAppWillBeUninstalled(const webapps::AppId& app_id) override {
    if (!dialog_model() || !dialog_model()->host()) {
      return;
    }
    if (app_id != app_id_) {
      return;
    }
    // Calling Close() synchronously deletes `this`, so save `callback_` on the
    // stack first.
    auto callback = std::move(callback_);
    dialog_model()->host()->Close();
    std::move(callback).Run(
        WebAppIdentityUpdateResult::kAppUninstalledDuringDialog);
  }

  void OnWebAppInstallManagerDestroyed() override {
    install_manager_observation_.Reset();
  }

 private:
  const webapps::AppId app_id_;
  UpdateReviewDialogCallback callback_;
  raw_ref<Browser> browser_;
  base::ScopedObservation<views::Widget, views::WidgetObserver>
      widget_observation_{this};
  base::ScopedObservation<WebAppInstallManager, WebAppInstallManagerObserver>
      install_manager_observation_{this};
  ScopedPictureInPictureOcclusionObservation occlusion_observation_{this};
  base::WeakPtrFactory<UpdateDialogDelegate> weak_ptr_factory_{this};
};

}  // namespace

DEFINE_UI_CLASS_PROPERTY_KEY(bool, kIsPwaUpdateDialogShowingKey, false)

DEFINE_ELEMENT_IDENTIFIER_VALUE(kWebAppUpdateReviewDialogAcceptButton);
DEFINE_ELEMENT_IDENTIFIER_VALUE(kWebAppUpdateReviewDialogUninstallButton);
DEFINE_ELEMENT_IDENTIFIER_VALUE(kWebAppUpdateReviewIgnoreButton);

void ShowWebAppReviewUpdateDialog(const webapps::AppId& app_id,
                                  const WebAppIdentityUpdate& update,
                                  Browser* browser,
                                  UpdateReviewDialogCallback callback) {
  CHECK(!callback.is_null());

  // Abort if a review update dialog is already being shown in this browser.
  if (browser->GetBrowserView().GetProperty(kIsPwaUpdateDialogShowingKey)) {
    std::move(callback).Run(WebAppIdentityUpdateResult::kUnexpectedError);
    return;
  }

  bool title_change = update.new_title.has_value();
  bool icon_change = update.new_icon.has_value();
  bool migration = update.new_start_url.has_value();
  CHECK(title_change || icon_change || migration);
  int title = GetDialogTitleMessageId(update);

  CHECK(AreWebAppsEnabled(browser->profile()));

  std::unique_ptr<UpdateDialogDelegate> delegate =
      std::make_unique<UpdateDialogDelegate>(app_id, std::move(callback),
                                             *browser);
  auto delegate_weak_ptr = delegate->AsWeakPtr();

  const ChromeLayoutProvider* layout_provider = ChromeLayoutProvider::Get();
  const int distance_related_horizontal = layout_provider->GetDistanceMetric(
      views::DISTANCE_RELATED_CONTROL_HORIZONTAL);

  std::unique_ptr<ui::DialogModel> dialog_model =
      ui::DialogModel::Builder(std::move(delegate))
          .SetInternalName("WebAppUpdateReviewDialog")
          .SetTitle(l10n_util::GetStringUTF16(title))
          .AddOkButton(
              base::BindOnce(&UpdateDialogDelegate::OnAcceptButtonClicked,
                             delegate_weak_ptr),
              ui::DialogModel::Button::Params()
                  .SetLabel(l10n_util::GetStringUTF16(
                      IDS_WEBAPP_UPDATE_REVIEW_ACCEPT_BUTTON))
                  .SetStyle(ui::ButtonStyle::kProminent)
                  .SetId(kWebAppUpdateReviewDialogAcceptButton))
          .AddCancelButton(
              base::BindOnce(&UpdateDialogDelegate::OnUninstallButtonClicked,
                             delegate_weak_ptr),
              ui::DialogModel::Button::Params()
                  .SetLabel(l10n_util::GetStringUTF16(
                      IDS_WEBAPP_UPDATE_REVIEW_UNINSTALL_BUTTON))
                  .SetId(kWebAppUpdateReviewDialogUninstallButton))
          .AddExtraButton(
              base::BindRepeating(&UpdateDialogDelegate::OnIgnoreButtonClicked,
                                  delegate_weak_ptr),
              ui::DialogModel::Button::Params()
                  .SetLabel(l10n_util::GetStringUTF16(
                      IDS_WEBAPP_UPDATE_REVIEW_IGNORE_BUTTON))
                  .SetId(kWebAppUpdateReviewIgnoreButton))
          .OverrideDefaultButton(ui::mojom::DialogButton::kNone)
          .SetInitiallyFocusedField(kWebAppUpdateReviewIgnoreButton)
          .SetCloseActionCallback(
              base::BindOnce(&UpdateDialogDelegate::OnClose, delegate_weak_ptr))
          .SetDialogDestroyingCallback(base::BindOnce(
              &UpdateDialogDelegate::OnDestroyed, delegate_weak_ptr))
          .AddParagraph(ui::DialogModelLabel(
              l10n_util::GetStringUTF16(IDS_WEBAPP_UPDATE_NEW_EXPLANATION)))

          .AddCustomField(
              std::make_unique<views::BubbleDialogModelHost::CustomView>(
                  views::Builder<views::TableLayoutView>()
                      // Left padding.
                      .AddPaddingColumn(/*horizontal_resize=*/1, /*width=*/0)
                      // The 'before' column
                      .AddColumn(views::LayoutAlignment::kCenter,
                                 views::LayoutAlignment::kStart,
                                 views::TableLayout::kFixedSize,
                                 views::TableLayout::ColumnSize::kFixed,
                                 /*fixed_width=*/kIdentityColumnWidth,
                                 /*min_width=*/0)
                      // Padding between the 'before' and the 'arrow' column.
                      .AddPaddingColumn(views::TableLayout::kFixedSize,
                                        /*width=*/distance_related_horizontal)
                      // The 'arrow' column.
                      .AddColumn(views::LayoutAlignment::kStretch,
                                 views::LayoutAlignment::kCenter,
                                 views::TableLayout::kFixedSize,
                                 views::TableLayout::ColumnSize::kUsePreferred,
                                 /*fixed_width=*/0, /*min_width=*/0)
                      // Padding between the 'arrow' and the 'after' column.
                      .AddPaddingColumn(views::TableLayout::kFixedSize,
                                        /*width=*/distance_related_horizontal)
                      // The 'after' column.
                      .AddColumn(views::LayoutAlignment::kCenter,
                                 views::LayoutAlignment::kStart,
                                 views::TableLayout::kFixedSize,
                                 views::TableLayout::ColumnSize::kFixed,
                                 /*fixed_width=*/kIdentityColumnWidth,
                                 /*min_width=*/0)
                      // Padding at the right of the dialog.
                      .AddPaddingColumn(/*horizontal_resize=*/1, /*width=*/0)
                      .AddRows(
                          /*n=*/1,
                          /*vertical_resize=*/views::TableLayout::kFixedSize,
                          /*height=*/0)
                      .AddChildren(
                          views::Builder<WebAppUpdateIdentityView>(
                              std::make_unique<WebAppUpdateIdentityView>(
                                  update.MakeOldIdentity())),
                          views::Builder<views::ImageView>().SetImage(
                              ui::ImageModel::FromVectorIcon(
                                  vector_icons::kForwardArrowIcon,
                                  ui::kColorIcon, kArrowIconSizeDp)),
                          views::Builder<WebAppUpdateIdentityView>(
                              std::make_unique<WebAppUpdateIdentityView>(
                                  update.MakeNewIdentity())))
                      .SetMinimumSize(gfx::Size(
                          layout_provider->GetDistanceMetric(
                              views::DISTANCE_MODAL_DIALOG_PREFERRED_WIDTH),
                          /*height=*/200))
                      .Build(),
                  views::BubbleDialogModelHost::FieldType::kText))
          .Build();

  views::Widget* widget = constrained_window::ShowBrowserModal(
      std::move(dialog_model), browser->window()->GetNativeWindow());
  delegate_weak_ptr->OnWidgetShownStartTracking(widget);
}

}  // namespace web_app