app_info_footer_panel.cc revision 6d86b77056ed63eb6871182f42a9fd5f07550f90
1// Copyright 2014 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/apps/app_info_dialog/app_info_footer_panel.h"
6
7#include "ash/shelf/shelf_delegate.h"
8#include "ash/shell.h"
9#include "chrome/browser/extensions/extension_service.h"
10#include "chrome/browser/profiles/profile.h"
11#include "chrome/browser/ui/app_list/app_list_controller_delegate.h"
12#include "chrome/browser/ui/browser_dialogs.h"
13#include "chrome/browser/ui/host_desktop.h"
14#include "extensions/browser/extension_system.h"
15#include "extensions/browser/management_policy.h"
16#include "extensions/common/extension.h"
17#include "grit/generated_resources.h"
18#include "ui/base/l10n/l10n_util.h"
19#include "ui/events/event.h"
20#include "ui/views/controls/button/label_button.h"
21#include "ui/views/layout/box_layout.h"
22#include "ui/views/layout/layout_constants.h"
23#include "ui/views/view.h"
24#include "ui/views/widget/widget.h"
25
26AppInfoFooterPanel::AppInfoFooterPanel(gfx::NativeWindow parent_window,
27                                       Profile* profile,
28                                       const extensions::Extension* app)
29    : AppInfoPanel(profile, app),
30      parent_window_(parent_window),
31      create_shortcuts_button_(NULL),
32      pin_to_shelf_button_(NULL),
33      unpin_from_shelf_button_(NULL),
34      remove_button_(NULL),
35      weak_ptr_factory_(this) {
36  CreateButtons();
37
38  SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal,
39                                        views::kButtonHEdgeMargin,
40                                        views::kButtonVEdgeMargin,
41                                        views::kRelatedButtonHSpacing));
42
43  LayoutButtons();
44}
45
46AppInfoFooterPanel::~AppInfoFooterPanel() {
47}
48
49void AppInfoFooterPanel::CreateButtons() {
50  if (CanCreateShortcuts()) {
51    create_shortcuts_button_ = new views::LabelButton(
52        this,
53        l10n_util::GetStringUTF16(
54            IDS_APPLICATION_INFO_CREATE_SHORTCUTS_BUTTON_TEXT));
55    create_shortcuts_button_->SetStyle(views::Button::STYLE_BUTTON);
56  }
57
58  if (CanSetPinnedToShelf()) {
59    pin_to_shelf_button_ = new views::LabelButton(
60        this, l10n_util::GetStringUTF16(IDS_APP_LIST_CONTEXT_MENU_PIN));
61    pin_to_shelf_button_->SetStyle(views::Button::STYLE_BUTTON);
62    unpin_from_shelf_button_ = new views::LabelButton(
63        this, l10n_util::GetStringUTF16(IDS_APP_LIST_CONTEXT_MENU_UNPIN));
64    unpin_from_shelf_button_->SetStyle(views::Button::STYLE_BUTTON);
65  }
66
67  if (CanUninstallApp()) {
68    remove_button_ = new views::LabelButton(
69        this,
70        l10n_util::GetStringUTF16(IDS_APPLICATION_INFO_UNINSTALL_BUTTON_TEXT));
71    remove_button_->SetStyle(views::Button::STYLE_BUTTON);
72  }
73}
74
75void AppInfoFooterPanel::LayoutButtons() {
76  if (create_shortcuts_button_)
77    AddChildView(create_shortcuts_button_);
78
79  if (pin_to_shelf_button_)
80    AddChildView(pin_to_shelf_button_);
81  if (unpin_from_shelf_button_)
82    AddChildView(unpin_from_shelf_button_);
83  UpdatePinButtons();
84
85  if (remove_button_)
86    AddChildView(remove_button_);
87}
88
89void AppInfoFooterPanel::UpdatePinButtons() {
90  if (pin_to_shelf_button_ && unpin_from_shelf_button_) {
91    bool is_pinned =
92        !ash::Shell::GetInstance()->GetShelfDelegate()->IsAppPinned(app_->id());
93    pin_to_shelf_button_->SetVisible(is_pinned);
94    unpin_from_shelf_button_->SetVisible(!is_pinned);
95  }
96}
97
98void AppInfoFooterPanel::ButtonPressed(views::Button* sender,
99                                       const ui::Event& event) {
100  if (sender == create_shortcuts_button_) {
101    CreateShortcuts();
102  } else if (sender == pin_to_shelf_button_) {
103    SetPinnedToShelf(true);
104  } else if (sender == unpin_from_shelf_button_) {
105    SetPinnedToShelf(false);
106  } else if (sender == remove_button_) {
107    UninstallApp();
108  } else {
109    NOTREACHED();
110  }
111}
112
113void AppInfoFooterPanel::ExtensionUninstallAccepted() {
114  ExtensionService* service =
115      extensions::ExtensionSystem::Get(profile_)->extension_service();
116  service->UninstallExtension(app_->id(), false, NULL);
117
118  // Close the App Info dialog as well (which will free the dialog too).
119  GetWidget()->Close();
120}
121
122void AppInfoFooterPanel::ExtensionUninstallCanceled() {
123  extension_uninstall_dialog_.reset();
124}
125
126void AppInfoFooterPanel::CreateShortcuts() {
127  DCHECK(CanCreateShortcuts());
128  chrome::ShowCreateChromeAppShortcutsDialog(GetWidget()->GetNativeWindow(),
129                                             profile_,
130                                             app_,
131                                             base::Callback<void(bool)>());
132}
133
134bool AppInfoFooterPanel::CanCreateShortcuts() const {
135  // Ash platforms can't create shortcuts.
136  return (chrome::GetHostDesktopTypeForNativeWindow(parent_window_) !=
137          chrome::HOST_DESKTOP_TYPE_ASH);
138}
139
140void AppInfoFooterPanel::SetPinnedToShelf(bool value) {
141  DCHECK(CanSetPinnedToShelf());
142  ash::ShelfDelegate* shelf_delegate =
143      ash::Shell::GetInstance()->GetShelfDelegate();
144  DCHECK(shelf_delegate);
145  if (value)
146    shelf_delegate->PinAppWithID(app_->id());
147  else
148    shelf_delegate->UnpinAppWithID(app_->id());
149
150  UpdatePinButtons();
151  Layout();
152}
153
154bool AppInfoFooterPanel::CanSetPinnedToShelf() const {
155  // Non-Ash platforms don't have a shelf.
156  if (chrome::GetHostDesktopTypeForNativeWindow(parent_window_) !=
157      chrome::HOST_DESKTOP_TYPE_ASH)
158    return false;
159  return ash::Shell::GetInstance()->GetShelfDelegate()->CanPin();
160}
161
162void AppInfoFooterPanel::UninstallApp() {
163  DCHECK(CanUninstallApp());
164  extension_uninstall_dialog_.reset(
165      extensions::ExtensionUninstallDialog::Create(profile_, NULL, this));
166  extension_uninstall_dialog_->ConfirmUninstall(app_);
167}
168
169bool AppInfoFooterPanel::CanUninstallApp() const {
170  return extensions::ExtensionSystem::Get(profile_)
171      ->management_policy()
172      ->UserMayModifySettings(app_, NULL);
173}
174