File: extension_uninstall_dialog_impl.cc

package info (click to toggle)
chromium 140.0.7339.127-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, 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 (164 lines) | stat: -rw-r--r-- 6,355 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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>
#include <string>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/extensions/extension_uninstall_dialog.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/ui/extensions/extension_dialog_utils.h"
#include "chrome/grit/generated_resources.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/dialog_model.h"
#include "ui/gfx/image/image_skia_operations.h"

namespace {

DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kCheckboxId);

// The implementation of the uninstall dialog.
class ExtensionUninstallDialogImpl
    : public extensions::ExtensionUninstallDialog {
 public:
  ExtensionUninstallDialogImpl(
      Profile* profile,
      gfx::NativeWindow parent,
      extensions::ExtensionUninstallDialog::Delegate* delegate);
  ExtensionUninstallDialogImpl(const ExtensionUninstallDialogImpl&) = delete;
  ExtensionUninstallDialogImpl& operator=(const ExtensionUninstallDialogImpl&) =
      delete;
  ~ExtensionUninstallDialogImpl() override;

  // Forwards that the dialog has been accepted to the delegate.
  void DialogAccepted();
  // Reports a canceled dialog to the delegate (unless accepted).
  void DialogClosing();

 private:
  void Show() override;
  void Close() override;

  // Pointer to the DialogModel for the dialog. This is cleared when the dialog
  // is being closed and OnDialogClosed is reported. As such it prevents access
  // to the dialog after it's been closed, as well as preventing multiple
  // reports of OnDialogClosed.
  raw_ptr<ui::DialogModel> dialog_model_ = nullptr;

  // WeakPtrs because the associated dialog may outlive |this|, which is owned
  // by the caller of extensions::ExtensionsUninstallDialog::Create().
  base::WeakPtrFactory<ExtensionUninstallDialogImpl> weak_ptr_factory_{this};
};

ExtensionUninstallDialogImpl::ExtensionUninstallDialogImpl(
    Profile* profile,
    gfx::NativeWindow parent,
    extensions::ExtensionUninstallDialog::Delegate* delegate)
    : extensions::ExtensionUninstallDialog(profile, parent, delegate) {}

ExtensionUninstallDialogImpl::~ExtensionUninstallDialogImpl() {
  if (dialog_model_) {
    dialog_model_->host()->Close();
  }
  DCHECK(!dialog_model_);
}

void ExtensionUninstallDialogImpl::Show() {
  ui::DialogModel::Builder dialog_builder;
  dialog_builder.SetInternalName("ExtensionUninstallDialog")
      .SetTitle(l10n_util::GetStringFUTF16(
          IDS_EXTENSION_PROMPT_UNINSTALL_TITLE,
          extensions::util::GetFixupExtensionNameForUIDisplay(
              extension()->name())))
      .OverrideShowCloseButton(false)
      .SetDialogDestroyingCallback(
          base::BindOnce(&ExtensionUninstallDialogImpl::DialogClosing,
                         weak_ptr_factory_.GetWeakPtr()))
      .SetIcon(ui::ImageModel::FromImageSkia(
          gfx::ImageSkiaOperations::CreateResizedImage(
              icon(), skia::ImageOperations::ResizeMethod::RESIZE_GOOD,
              gfx::Size(extension_misc::EXTENSION_ICON_SMALL,
                        extension_misc::EXTENSION_ICON_SMALL))))
      .AddOkButton(base::BindOnce(&ExtensionUninstallDialogImpl::DialogAccepted,
                                  weak_ptr_factory_.GetWeakPtr()),
                   ui::DialogModel::Button::Params()
                       .SetLabel(l10n_util::GetStringUTF16(
                           IDS_EXTENSION_PROMPT_UNINSTALL_BUTTON))
                       .SetId(kOkButtonElementId))
      .AddCancelButton(
          base::DoNothing() /* Cancel is covered by WindowClosingCallback */,
          ui::DialogModel::Button::Params().SetId(kCancelButtonElementId));

  if (triggering_extension()) {
    dialog_builder.AddParagraph(
        ui::DialogModelLabel(
            l10n_util::GetStringFUTF16(
                IDS_EXTENSION_PROMPT_UNINSTALL_TRIGGERED_BY_EXTENSION,
                extensions::util::GetFixupExtensionNameForUIDisplay(
                    triggering_extension()->name())))
            .set_is_secondary()
            .set_allow_character_break());
  }

  if (ShouldShowCheckbox()) {
    std::u16string checkbox_label =
        triggering_extension()
            ? l10n_util::GetStringFUTF16(
                  IDS_EXTENSION_PROMPT_UNINSTALL_REPORT_ABUSE_FROM_EXTENSION,
                  extensions::util::GetFixupExtensionNameForUIDisplay(
                      extension()->name()))
            : l10n_util::GetStringUTF16(
                  IDS_EXTENSION_PROMPT_UNINSTALL_REPORT_ABUSE);

    dialog_builder.AddCheckbox(kCheckboxId,
                               ui::DialogModelLabel(checkbox_label));
  }

  std::unique_ptr<ui::DialogModel> dialog_model = dialog_builder.Build();
  dialog_model_ = dialog_model.get();

  ShowDialog(parent(), extension()->id(), std::move(dialog_model));
}

void ExtensionUninstallDialogImpl::Close() {
  DCHECK(dialog_model_);
  dialog_model_->host()->Close();
}

void ExtensionUninstallDialogImpl::DialogAccepted() {
  DCHECK(dialog_model_);
  const bool checkbox_is_checked =
      ShouldShowCheckbox() &&
      dialog_model_->GetCheckboxByUniqueId(kCheckboxId)->is_checked();
  dialog_model_ = nullptr;
  OnDialogClosed(checkbox_is_checked
                     ? CLOSE_ACTION_UNINSTALL_AND_CHECKBOX_CHECKED
                     : CLOSE_ACTION_UNINSTALL);
}

void ExtensionUninstallDialogImpl::DialogClosing() {
  if (!dialog_model_) {
    return;
  }
  dialog_model_ = nullptr;
  OnDialogClosed(CLOSE_ACTION_CANCELED);
}

}  // namespace

DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(extensions::ExtensionUninstallDialog,
                                      kCancelButtonElementId);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(extensions::ExtensionUninstallDialog,
                                      kOkButtonElementId);

// static
std::unique_ptr<extensions::ExtensionUninstallDialog>
extensions::ExtensionUninstallDialog::Create(Profile* profile,
                                             gfx::NativeWindow parent,
                                             Delegate* delegate) {
  return std::make_unique<ExtensionUninstallDialogImpl>(profile, parent,
                                                        delegate);
}