uninstall_view.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/ui/views/uninstall_view.h"
6
7#include "base/message_loop/message_loop.h"
8#include "base/process/launch.h"
9#include "base/run_loop.h"
10#include "chrome/browser/browser_process.h"
11#include "chrome/browser/shell_integration.h"
12#include "chrome/browser/ui/uninstall_browser_prompt.h"
13#include "chrome/common/chrome_result_codes.h"
14#include "chrome/installer/util/browser_distribution.h"
15#include "chrome/installer/util/shell_util.h"
16#include "grit/chromium_strings.h"
17#include "ui/base/l10n/l10n_util.h"
18#include "ui/views/controls/button/checkbox.h"
19#include "ui/views/controls/combobox/combobox.h"
20#include "ui/views/controls/label.h"
21#include "ui/views/layout/grid_layout.h"
22#include "ui/views/layout/layout_constants.h"
23#include "ui/views/widget/widget.h"
24
25UninstallView::UninstallView(int* user_selection,
26                             const base::Closure& quit_closure,
27                             bool show_delete_profile)
28    : confirm_label_(NULL),
29      show_delete_profile_(show_delete_profile),
30      delete_profile_(NULL),
31      change_default_browser_(NULL),
32      browsers_combo_(NULL),
33      user_selection_(*user_selection),
34      quit_closure_(quit_closure) {
35  SetupControls();
36}
37
38UninstallView::~UninstallView() {
39  // Exit the message loop we were started with so that uninstall can continue.
40  quit_closure_.Run();
41
42  // Delete Combobox as it holds a reference to us.
43  delete browsers_combo_;
44}
45
46void UninstallView::SetupControls() {
47  using views::ColumnSet;
48  using views::GridLayout;
49
50  GridLayout* layout = GridLayout::CreatePanel(this);
51  SetLayoutManager(layout);
52
53  // Message to confirm uninstallation.
54  int column_set_id = 0;
55  ColumnSet* column_set = layout->AddColumnSet(column_set_id);
56  column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0,
57                        GridLayout::USE_PREF, 0, 0);
58  layout->StartRow(0, column_set_id);
59  confirm_label_ = new views::Label(
60      l10n_util::GetStringUTF16(IDS_UNINSTALL_VERIFY));
61  confirm_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
62  layout->AddView(confirm_label_);
63
64  layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing);
65
66  // The "delete profile" check box.
67  if (show_delete_profile_) {
68    ++column_set_id;
69    column_set = layout->AddColumnSet(column_set_id);
70    column_set->AddPaddingColumn(0, views::kPanelHorizIndentation);
71    column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0,
72                          GridLayout::USE_PREF, 0, 0);
73    layout->StartRow(0, column_set_id);
74    delete_profile_ = new views::Checkbox(
75        l10n_util::GetStringUTF16(IDS_UNINSTALL_DELETE_PROFILE));
76    layout->AddView(delete_profile_);
77  }
78
79  // Set default browser combo box. If the default should not or cannot be
80  // changed, widgets are not shown. We assume here that if Chrome cannot
81  // be set programatically as default, neither can any other browser (for
82  // instance because the OS doesn't permit that).
83  BrowserDistribution* dist = BrowserDistribution::GetDistribution();
84  if (dist->GetDefaultBrowserControlPolicy() !=
85          BrowserDistribution::DEFAULT_BROWSER_UNSUPPORTED &&
86      ShellIntegration::GetDefaultBrowser() == ShellIntegration::IS_DEFAULT &&
87      (ShellIntegration::CanSetAsDefaultBrowser() !=
88          ShellIntegration::SET_DEFAULT_INTERACTIVE)) {
89    browsers_.reset(new BrowsersMap());
90    ShellUtil::GetRegisteredBrowsers(dist, browsers_.get());
91    if (!browsers_->empty()) {
92      layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
93
94      ++column_set_id;
95      column_set = layout->AddColumnSet(column_set_id);
96      column_set->AddPaddingColumn(0, views::kPanelHorizIndentation);
97      column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0,
98                            GridLayout::USE_PREF, 0, 0);
99      column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing);
100      column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0,
101                            GridLayout::USE_PREF, 0, 0);
102      layout->StartRow(0, column_set_id);
103      change_default_browser_ = new views::Checkbox(
104          l10n_util::GetStringUTF16(IDS_UNINSTALL_SET_DEFAULT_BROWSER));
105      change_default_browser_->set_listener(this);
106      layout->AddView(change_default_browser_);
107      browsers_combo_ = new views::Combobox(this);
108      layout->AddView(browsers_combo_);
109      browsers_combo_->SetEnabled(false);
110    }
111  }
112
113  layout->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing);
114}
115
116bool UninstallView::Accept() {
117  user_selection_ = content::RESULT_CODE_NORMAL_EXIT;
118  if (show_delete_profile_ && delete_profile_->checked())
119    user_selection_ = chrome::RESULT_CODE_UNINSTALL_DELETE_PROFILE;
120  if (change_default_browser_ && change_default_browser_->checked()) {
121    BrowsersMap::const_iterator i = browsers_->begin();
122    std::advance(i, browsers_combo_->selected_index());
123    base::LaunchOptions options;
124    options.start_hidden = true;
125    base::LaunchProcess(i->second, options, NULL);
126  }
127  return true;
128}
129
130bool UninstallView::Cancel() {
131  user_selection_ = chrome::RESULT_CODE_UNINSTALL_USER_CANCEL;
132  return true;
133}
134
135base::string16 UninstallView::GetDialogButtonLabel(
136    ui::DialogButton button) const {
137  // Label the OK button 'Uninstall'; Cancel remains the same.
138  if (button == ui::DIALOG_BUTTON_OK)
139    return l10n_util::GetStringUTF16(IDS_UNINSTALL_BUTTON_TEXT);
140  return views::DialogDelegateView::GetDialogButtonLabel(button);
141}
142
143void UninstallView::ButtonPressed(views::Button* sender,
144                                  const ui::Event& event) {
145  if (change_default_browser_ == sender) {
146    // Disable the browsers combobox if the user unchecks the checkbox.
147    DCHECK(browsers_combo_);
148    browsers_combo_->SetEnabled(change_default_browser_->checked());
149  }
150}
151
152base::string16 UninstallView::GetWindowTitle() const {
153  return l10n_util::GetStringUTF16(IDS_UNINSTALL_CHROME);
154}
155
156int UninstallView::GetItemCount() const {
157  DCHECK(!browsers_->empty());
158  return browsers_->size();
159}
160
161base::string16 UninstallView::GetItemAt(int index) {
162  DCHECK_LT(index, static_cast<int>(browsers_->size()));
163  BrowsersMap::const_iterator i = browsers_->begin();
164  std::advance(i, index);
165  return i->first;
166}
167
168namespace chrome {
169
170int ShowUninstallBrowserPrompt(bool show_delete_profile) {
171  DCHECK(base::MessageLoopForUI::IsCurrent());
172  int result = content::RESULT_CODE_NORMAL_EXIT;
173
174  // Take a reference on g_browser_process while showing the dialog. This is
175  // done because the dialog uses the views framework which may increment
176  // and decrement the module ref count during the course of displaying UI and
177  // this code can be called while the module refcount is still at 0.
178  // Note that this reference is never released, as this code is shown on a path
179  // that immediately exits Chrome anyway.
180  // See http://crbug.com/241366 for details.
181  g_browser_process->AddRefModule();
182
183  base::RunLoop run_loop;
184  UninstallView* view = new UninstallView(&result,
185                                          run_loop.QuitClosure(),
186                                          show_delete_profile);
187  views::DialogDelegate::CreateDialogWidget(view, NULL, NULL)->Show();
188  run_loop.Run();
189  return result;
190}
191
192}  // namespace chrome
193