uninstall_view.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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.h"
8#include "base/process_util.h"
9#include "base/run_loop.h"
10#include "chrome/browser/shell_integration.h"
11#include "chrome/browser/ui/uninstall_browser_prompt.h"
12#include "chrome/common/chrome_result_codes.h"
13#include "chrome/installer/util/browser_distribution.h"
14#include "chrome/installer/util/shell_util.h"
15#include "grit/chromium_strings.h"
16#include "ui/base/l10n/l10n_util.h"
17#include "ui/views/controls/button/checkbox.h"
18#include "ui/views/controls/combobox/combobox.h"
19#include "ui/views/controls/label.h"
20#include "ui/views/focus/accelerator_handler.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    : confirm_label_(NULL),
28      delete_profile_(NULL),
29      change_default_browser_(NULL),
30      browsers_combo_(NULL),
31      user_selection_(*user_selection),
32      quit_closure_(quit_closure) {
33  SetupControls();
34}
35
36UninstallView::~UninstallView() {
37  // Exit the message loop we were started with so that uninstall can continue.
38  quit_closure_.Run();
39}
40
41void UninstallView::SetupControls() {
42  using views::ColumnSet;
43  using views::GridLayout;
44
45  GridLayout* layout = GridLayout::CreatePanel(this);
46  SetLayoutManager(layout);
47
48  // Message to confirm uninstallation.
49  int column_set_id = 0;
50  ColumnSet* column_set = layout->AddColumnSet(column_set_id);
51  column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0,
52                        GridLayout::USE_PREF, 0, 0);
53  layout->StartRow(0, column_set_id);
54  confirm_label_ = new views::Label(
55      l10n_util::GetStringUTF16(IDS_UNINSTALL_VERIFY));
56  confirm_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
57  layout->AddView(confirm_label_);
58
59  layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing);
60
61  // The "delete profile" check box.
62  ++column_set_id;
63  column_set = layout->AddColumnSet(column_set_id);
64  column_set->AddPaddingColumn(0, views::kPanelHorizIndentation);
65  column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0,
66                        GridLayout::USE_PREF, 0, 0);
67  layout->StartRow(0, column_set_id);
68  delete_profile_ = new views::Checkbox(
69      l10n_util::GetStringUTF16(IDS_UNINSTALL_DELETE_PROFILE));
70  layout->AddView(delete_profile_);
71
72  // Set default browser combo box. If the default should not or cannot be
73  // changed, widgets are not shown. We assume here that if Chrome cannot
74  // be set programatically as default, neither can any other browser (for
75  // instance because the OS doesn't permit that).
76  BrowserDistribution* dist = BrowserDistribution::GetDistribution();
77  if (dist->CanSetAsDefault() &&
78      ShellIntegration::IsDefaultBrowser() &&
79      (ShellIntegration::CanSetAsDefaultBrowser() !=
80          ShellIntegration::SET_DEFAULT_INTERACTIVE)) {
81    browsers_.reset(new BrowsersMap());
82    ShellUtil::GetRegisteredBrowsers(dist, browsers_.get());
83    if (!browsers_->empty()) {
84      layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
85
86      ++column_set_id;
87      column_set = layout->AddColumnSet(column_set_id);
88      column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing);
89      column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0,
90                            GridLayout::USE_PREF, 0, 0);
91      column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing);
92      column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0,
93                            GridLayout::USE_PREF, 0, 0);
94      layout->StartRow(0, column_set_id);
95      change_default_browser_ = new views::Checkbox(
96          l10n_util::GetStringUTF16(IDS_UNINSTALL_SET_DEFAULT_BROWSER));
97      change_default_browser_->set_listener(this);
98      layout->AddView(change_default_browser_);
99      browsers_combo_ = new views::Combobox(this);
100      layout->AddView(browsers_combo_);
101      browsers_combo_->SetEnabled(false);
102    }
103  }
104
105  layout->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing);
106}
107
108bool UninstallView::Accept() {
109  user_selection_ = content::RESULT_CODE_NORMAL_EXIT;
110  if (delete_profile_->checked())
111    user_selection_ = chrome::RESULT_CODE_UNINSTALL_DELETE_PROFILE;
112  if (change_default_browser_ && change_default_browser_->checked()) {
113    BrowsersMap::const_iterator i = browsers_->begin();
114    std::advance(i, browsers_combo_->selected_index());
115    base::LaunchOptions options;
116    options.start_hidden = true;
117    base::LaunchProcess(i->second, options, NULL);
118  }
119  return true;
120}
121
122bool UninstallView::Cancel() {
123  user_selection_ = chrome::RESULT_CODE_UNINSTALL_USER_CANCEL;
124  return true;
125}
126
127string16 UninstallView::GetDialogButtonLabel(ui::DialogButton button) const {
128  // We only want to give custom name to OK button - 'Uninstall'. Cancel
129  // button remains same.
130  if (button == ui::DIALOG_BUTTON_OK)
131    return l10n_util::GetStringUTF16(IDS_UNINSTALL_BUTTON_TEXT);
132  return string16();
133}
134
135void UninstallView::ButtonPressed(views::Button* sender,
136                                  const ui::Event& event) {
137  if (change_default_browser_ == sender) {
138    // Disable the browsers combobox if the user unchecks the checkbox.
139    DCHECK(browsers_combo_);
140    browsers_combo_->SetEnabled(change_default_browser_->checked());
141  }
142}
143
144string16 UninstallView::GetWindowTitle() const {
145  return l10n_util::GetStringUTF16(IDS_UNINSTALL_CHROME);
146}
147
148views::View* UninstallView::GetContentsView() {
149  return this;
150}
151
152int UninstallView::GetItemCount() const {
153  DCHECK(!browsers_->empty());
154  return browsers_->size();
155}
156
157string16 UninstallView::GetItemAt(int index) {
158  DCHECK_LT(index, static_cast<int>(browsers_->size()));
159  BrowsersMap::const_iterator i = browsers_->begin();
160  std::advance(i, index);
161  return i->first;
162}
163
164namespace chrome {
165
166int ShowUninstallBrowserPrompt() {
167  DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type());
168  int result = content::RESULT_CODE_NORMAL_EXIT;
169  views::AcceleratorHandler accelerator_handler;
170  base::RunLoop run_loop(&accelerator_handler);
171  UninstallView* view = new UninstallView(&result, run_loop.QuitClosure());
172  views::Widget::CreateWindow(view)->Show();
173  run_loop.Run();
174  return result;
175}
176
177}  // namespace chrome
178