File: uninstall_view.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (172 lines) | stat: -rw-r--r-- 7,283 bytes parent folder | download | duplicates (6)
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
// 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 "chrome/browser/ui/views/uninstall_view.h"

#include "base/process/launch.h"
#include "base/run_loop.h"
#include "base/task/current_thread.h"
#include "chrome/browser/shell_integration.h"
#include "chrome/browser/ui/uninstall_browser_prompt.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/common/chrome_result_codes.h"
#include "chrome/grit/branded_strings.h"
#include "chrome/installer/util/shell_util.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/views/accessibility/accessibility_paint_checks.h"
#include "ui/views/controls/button/checkbox.h"
#include "ui/views/controls/combobox/combobox.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/box_layout_view.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/widget/widget.h"

UninstallView::UninstallView(int* user_selection,
                             const base::RepeatingClosure& quit_closure)
    : user_selection_(*user_selection), quit_closure_(quit_closure) {
  SetupControls();
}

UninstallView::~UninstallView() {
  // Exit the message loop we were started with so that uninstall can continue.
  quit_closure_.Run();
}

void UninstallView::SetupControls() {
  ChromeLayoutProvider* provider = ChromeLayoutProvider::Get();
  const int checkbox_indent =
      provider->GetDistanceMetric(DISTANCE_SUBSECTION_HORIZONTAL_INDENT);
  const int unrelated_vertical_spacing =
      provider->GetDistanceMetric(views::DISTANCE_UNRELATED_CONTROL_VERTICAL);
  const int related_vertical_spacing =
      provider->GetDistanceMetric(views::DISTANCE_RELATED_CONTROL_VERTICAL);
  const int related_horizontal_spacing =
      provider->GetDistanceMetric(views::DISTANCE_RELATED_CONTROL_HORIZONTAL);
  const int related_vertical_small =
      provider->GetDistanceMetric(DISTANCE_RELATED_CONTROL_VERTICAL_SMALL);

  auto builder =
      views::Builder<UninstallView>(this)
          .SetButtonLabel(ui::mojom::DialogButton::kOk,
                          l10n_util::GetStringUTF16(IDS_UNINSTALL_BUTTON_TEXT))
          .SetTitle(IDS_UNINSTALL_CHROME)
          .SetAcceptCallback(base::BindOnce(&UninstallView::OnDialogAccepted,
                                            base::Unretained(this)))
          .SetCancelCallback(base::BindOnce(&UninstallView::OnDialogCancelled,
                                            base::Unretained(this)))
          .SetCloseCallback(base::BindOnce(&UninstallView::OnDialogCancelled,
                                           base::Unretained(this)))
          .set_margins(provider->GetDialogInsetsForContentType(
              views::DialogContentType::kText, views::DialogContentType::kText))
          .SetLayoutManager(std::make_unique<views::BoxLayout>(
              views::BoxLayout::Orientation::kVertical))
          .AddChildren(
              // Message to confirm uninstallation.
              views::Builder<views::Label>()
                  .SetText(l10n_util::GetStringUTF16(IDS_UNINSTALL_VERIFY))
                  .SetHorizontalAlignment(gfx::ALIGN_LEFT)
                  .SetProperty(
                      views::kMarginsKey,
                      gfx::Insets::TLBR(0, 0, unrelated_vertical_spacing, 0)),
              // The "delete profile" check box.
              views::Builder<views::Checkbox>()
                  .CopyAddressTo(&delete_profile_)
                  .SetText(
                      l10n_util::GetStringUTF16(IDS_UNINSTALL_DELETE_PROFILE))
                  .SetProperty(views::kMarginsKey,
                               gfx::Insets::TLBR(0, checkbox_indent, 0, 0)));

  // Set default browser combo box. If the default should not or cannot be
  // changed, widgets are not shown. We assume here that if Chrome cannot
  // be set programatically as default, neither can any other browser (for
  // instance because the OS doesn't permit that).
  if (ShellUtil::CanMakeChromeDefaultUnattended() &&
      shell_integration::GetDefaultBrowser() == shell_integration::IS_DEFAULT) {
    browsers_ = std::make_unique<BrowsersMap>();
    ShellUtil::GetRegisteredBrowsers(browsers_.get());
    if (!browsers_->empty()) {
      builder.AddChildren(
          views::Builder<views::View>().SetProperty(
              views::kMarginsKey,
              gfx::Insets::TLBR(related_vertical_spacing, 0, 0, 0)),
          views::Builder<views::BoxLayoutView>()
              .SetOrientation(views::BoxLayout::Orientation::kHorizontal)
              .SetBetweenChildSpacing(related_horizontal_spacing)
              .AddChildren(
                  views::Builder<views::Checkbox>()
                      .CopyAddressTo(&change_default_browser_)
                      .SetText(l10n_util::GetStringUTF16(
                          IDS_UNINSTALL_SET_DEFAULT_BROWSER))
                      .SetCallback(base::BindRepeating(
                          [](UninstallView* view) {
                            view->browsers_combo_->SetEnabled(
                                view->change_default_browser_->GetChecked());
                          },
                          base::Unretained(this)))
                      .SetProperty(views::kMarginsKey,
                                   gfx::Insets::TLBR(0, checkbox_indent, 0, 0)),
                  views::Builder<views::Combobox>()
                      .CopyAddressTo(&browsers_combo_)
                      .SetModel(this)
                      .SetEnabled(false)));
    }
  }

  std::move(builder)
      .AddChild(views::Builder<views::View>().SetProperty(
          views::kMarginsKey,
          gfx::Insets::TLBR(related_vertical_small, 0, 0, 0)))
      .BuildChildren();
}

void UninstallView::OnDialogAccepted() {
  *user_selection_ = content::RESULT_CODE_NORMAL_EXIT;
  if (delete_profile_->GetChecked()) {
    *user_selection_ = CHROME_RESULT_CODE_UNINSTALL_DELETE_PROFILE;
  }
  if (change_default_browser_ && change_default_browser_->GetChecked()) {
    BrowsersMap::const_iterator i = browsers_->begin();
    std::advance(i, browsers_combo_->GetSelectedIndex().value());
    base::LaunchOptions options;
    options.start_hidden = true;
    base::LaunchProcess(i->second, options);
  }
}

void UninstallView::OnDialogCancelled() {
  *user_selection_ = CHROME_RESULT_CODE_UNINSTALL_USER_CANCEL;
}

size_t UninstallView::GetItemCount() const {
  DCHECK(!browsers_->empty());
  return browsers_->size();
}

std::u16string UninstallView::GetItemAt(size_t index) const {
  DCHECK_LT(index, browsers_->size());
  BrowsersMap::const_iterator i = browsers_->begin();
  std::advance(i, index);
  return base::WideToUTF16(i->first);
}

BEGIN_METADATA(UninstallView)
END_METADATA

namespace chrome {

int ShowUninstallBrowserPrompt() {
  DCHECK(base::CurrentUIThread::IsSet());
  int result = content::RESULT_CODE_NORMAL_EXIT;

  base::RunLoop run_loop;
  UninstallView* view = new UninstallView(&result, run_loop.QuitClosure());
  views::DialogDelegate::CreateDialogWidget(view, NULL, NULL)->Show();
  run_loop.Run();
  return result;
}

}  // namespace chrome