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.
5 #include "chrome/browser/ui/views/extensions/bookmark_app_bubble_view.h"
7 #include "base/strings/string16.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/extensions/app_icon_loader_impl.h"
11 #include "chrome/browser/extensions/bookmark_app_helper.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/launch_util.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/app_list/app_list_service.h"
16 #include "chrome/browser/ui/app_list/app_list_util.h"
17 #include "chrome/browser/ui/browser_navigator.h"
18 #include "chrome/browser/ui/host_desktop.h"
19 #include "chrome/common/extensions/extension_constants.h"
20 #include "chrome/common/url_constants.h"
21 #include "chrome/grit/generated_resources.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/browser/web_contents.h"
24 #include "extensions/browser/extension_prefs.h"
25 #include "extensions/browser/extension_registry.h"
26 #include "extensions/browser/extension_system.h"
27 #include "extensions/browser/pref_names.h"
28 #include "extensions/browser/uninstall_reason.h"
29 #include "extensions/common/constants.h"
30 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/base/resource/resource_bundle.h"
32 #include "ui/events/keycodes/keyboard_codes.h"
33 #include "ui/views/controls/button/checkbox.h"
34 #include "ui/views/controls/button/label_button.h"
35 #include "ui/views/controls/image_view.h"
36 #include "ui/views/controls/label.h"
37 #include "ui/views/controls/textfield/textfield.h"
38 #include "ui/views/layout/grid_layout.h"
39 #include "ui/views/layout/layout_constants.h"
40 #include "ui/views/widget/widget.h"
42 using views::ColumnSet
;
43 using views::GridLayout
;
47 // Minimum width of the the bubble.
48 const int kMinBubbleWidth
= 300;
49 // Minimum width of the the textfield.
50 const int kMinTextfieldWidth
= 200;
52 const int kIconSize
= extension_misc::EXTENSION_ICON_MEDIUM
;
54 ExtensionService
* GetExtensionService(Profile
* profile
) {
55 return extensions::ExtensionSystem::Get(profile
)->extension_service();
60 BookmarkAppBubbleView
* BookmarkAppBubbleView::bookmark_app_bubble_
= NULL
;
62 BookmarkAppBubbleView::~BookmarkAppBubbleView() {
66 void BookmarkAppBubbleView::ShowBubble(views::View
* anchor_view
,
68 const WebApplicationInfo
& web_app_info
,
69 const std::string
& extension_id
) {
70 if (bookmark_app_bubble_
!= NULL
)
73 bookmark_app_bubble_
= new BookmarkAppBubbleView(
74 anchor_view
, profile
, web_app_info
, extension_id
);
75 views::BubbleDelegateView::CreateBubble(bookmark_app_bubble_
)->Show();
76 // Select the entire title textfield contents when the bubble is first shown.
77 bookmark_app_bubble_
->title_tf_
->SelectAll(true);
78 bookmark_app_bubble_
->SetArrowPaintType(views::BubbleBorder::PAINT_NONE
);
81 BookmarkAppBubbleView::BookmarkAppBubbleView(
82 views::View
* anchor_view
,
84 const WebApplicationInfo
& web_app_info
,
85 const std::string
& extension_id
)
86 : BubbleDelegateView(anchor_view
, views::BubbleBorder::TOP_RIGHT
),
88 web_app_info_(web_app_info
),
89 extension_id_(extension_id
),
92 open_as_tab_checkbox_(NULL
),
95 app_icon_loader_(new extensions::AppIconLoaderImpl(profile
,
98 const SkColor background_color
= GetNativeTheme()->GetSystemColor(
99 ui::NativeTheme::kColorId_DialogBackground
);
100 set_arrow(views::BubbleBorder::TOP_CENTER
);
101 set_color(background_color
);
102 set_background(views::Background::CreateSolidBackground(background_color
));
103 set_margins(gfx::Insets(views::kPanelVertMargin
, 0, 0, 0));
106 void BookmarkAppBubbleView::Init() {
107 views::Label
* title_label
= new views::Label(
108 l10n_util::GetStringUTF16(IDS_BOOKMARK_APP_BUBBLE_TITLE
));
109 ui::ResourceBundle
* rb
= &ui::ResourceBundle::GetSharedInstance();
110 title_label
->SetFontList(rb
->GetFontList(ui::ResourceBundle::MediumFont
));
111 title_label
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
114 new views::LabelButton(this, l10n_util::GetStringUTF16(IDS_ADD
));
115 add_button_
->SetStyle(views::Button::STYLE_BUTTON
);
116 add_button_
->SetIsDefault(true);
119 new views::LabelButton(this, l10n_util::GetStringUTF16(IDS_CANCEL
));
120 cancel_button_
->SetStyle(views::Button::STYLE_BUTTON
);
122 GridLayout
* layout
= new GridLayout(this);
123 SetLayoutManager(layout
);
125 // Column sets used in the layout of the bubble.
128 TITLE_TEXT_COLUMN_SET_ID
,
129 CONTENT_COLUMN_SET_ID
132 // The column layout used for the title and checkbox.
133 ColumnSet
* cs
= layout
->AddColumnSet(TITLE_COLUMN_SET_ID
);
134 cs
->AddPaddingColumn(0, views::kButtonHEdgeMarginNew
);
136 GridLayout::LEADING
, GridLayout::CENTER
, 0, GridLayout::USE_PREF
, 0, 0);
137 cs
->AddPaddingColumn(0, views::kButtonHEdgeMarginNew
);
139 // The column layout used for the icon and text box.
140 cs
= layout
->AddColumnSet(TITLE_TEXT_COLUMN_SET_ID
);
141 cs
->AddPaddingColumn(0, views::kButtonHEdgeMarginNew
);
142 cs
->AddColumn(GridLayout::LEADING
,
145 GridLayout::USE_PREF
,
148 cs
->AddPaddingColumn(0, views::kButtonHEdgeMarginNew
);
149 cs
->AddColumn(GridLayout::FILL
,
152 GridLayout::USE_PREF
,
155 cs
->AddPaddingColumn(0, views::kButtonHEdgeMarginNew
);
157 // The column layout used for the row with buttons.
158 cs
= layout
->AddColumnSet(CONTENT_COLUMN_SET_ID
);
159 cs
->AddPaddingColumn(0, views::kButtonHEdgeMarginNew
);
161 GridLayout::LEADING
, GridLayout::CENTER
, 0, GridLayout::USE_PREF
, 0, 0);
162 cs
->AddPaddingColumn(1, views::kUnrelatedControlHorizontalSpacing
);
164 GridLayout::LEADING
, GridLayout::CENTER
, 0, GridLayout::USE_PREF
, 0, 0);
165 cs
->AddPaddingColumn(0, views::kRelatedButtonHSpacing
);
167 GridLayout::LEADING
, GridLayout::CENTER
, 0, GridLayout::USE_PREF
, 0, 0);
168 cs
->AddPaddingColumn(0, views::kButtonHEdgeMarginNew
);
170 layout
->StartRow(0, TITLE_COLUMN_SET_ID
);
171 layout
->AddView(title_label
);
172 layout
->AddPaddingRow(0, views::kRelatedControlVerticalSpacing
);
174 const extensions::Extension
* extension
=
175 extensions::ExtensionRegistry::Get(profile_
)->GetExtensionById(
176 extension_id_
, extensions::ExtensionRegistry::EVERYTHING
);
178 layout
->StartRow(0, TITLE_TEXT_COLUMN_SET_ID
);
179 icon_image_view_
= new views::ImageView();
180 icon_image_view_
->SetImageSize(gfx::Size(kIconSize
, kIconSize
));
181 layout
->AddView(icon_image_view_
);
182 app_icon_loader_
->FetchImage(extension_id_
);
184 title_tf_
= new views::Textfield();
185 title_tf_
->SetText(extension
? base::UTF8ToUTF16(extension
->name())
186 : web_app_info_
.title
);
187 layout
->AddView(title_tf_
);
188 layout
->AddPaddingRow(0, views::kRelatedControlVerticalSpacing
);
190 layout
->StartRow(0, CONTENT_COLUMN_SET_ID
);
191 open_as_tab_checkbox_
= new views::Checkbox(
192 l10n_util::GetStringUTF16(IDS_BOOKMARK_APP_BUBBLE_OPEN_AS_TAB
));
193 open_as_tab_checkbox_
->SetChecked(
194 profile_
->GetPrefs()->GetInteger(
195 extensions::pref_names::kBookmarkAppCreationLaunchType
) ==
196 extensions::LAUNCH_TYPE_REGULAR
);
197 layout
->AddView(open_as_tab_checkbox_
);
198 layout
->AddView(add_button_
);
199 layout
->AddView(cancel_button_
);
200 layout
->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing
);
202 AddAccelerator(ui::Accelerator(ui::VKEY_RETURN
, ui::EF_NONE
));
205 views::View
* BookmarkAppBubbleView::GetInitiallyFocusedView() {
209 void BookmarkAppBubbleView::WindowClosing() {
210 // We have to reset |bookmark_app_bubble_| here, not in our destructor,
211 // because we'll be destroyed asynchronously and the shown state will be
212 // checked before then.
213 DCHECK_EQ(bookmark_app_bubble_
, this);
214 bookmark_app_bubble_
= NULL
;
217 GetExtensionService(profile_
)
218 ->UninstallExtension(extension_id_
,
219 extensions::UNINSTALL_REASON_INSTALL_CANCELED
,
220 base::Bind(&base::DoNothing
),
227 bool BookmarkAppBubbleView::AcceleratorPressed(
228 const ui::Accelerator
& accelerator
) {
229 if (accelerator
.key_code() == ui::VKEY_RETURN
) {
230 HandleButtonPressed(add_button_
);
233 return BubbleDelegateView::AcceleratorPressed(accelerator
);
236 gfx::Size
BookmarkAppBubbleView::GetMinimumSize() const {
237 gfx::Size
size(views::BubbleDelegateView::GetPreferredSize());
238 size
.SetToMax(gfx::Size(kMinBubbleWidth
, 0));
242 void BookmarkAppBubbleView::ButtonPressed(views::Button
* sender
,
243 const ui::Event
& event
) {
244 HandleButtonPressed(sender
);
247 void BookmarkAppBubbleView::SetAppImage(const std::string
& id
,
248 const gfx::ImageSkia
& image
) {
249 DCHECK_EQ(extension_id_
, id
);
250 icon_image_view_
->SetImage(image
);
253 void BookmarkAppBubbleView::HandleButtonPressed(views::Button
* sender
) {
254 // Unset |remove_app_| so we don't delete the bookmark after the window
256 if (sender
== add_button_
)
259 GetWidget()->Close();
262 void BookmarkAppBubbleView::ApplyEdits() {
263 // Set the launch type based on the checkbox.
264 extensions::LaunchType launch_type
= open_as_tab_checkbox_
->checked()
265 ? extensions::LAUNCH_TYPE_REGULAR
266 : extensions::LAUNCH_TYPE_WINDOW
;
267 profile_
->GetPrefs()->SetInteger(
268 extensions::pref_names::kBookmarkAppCreationLaunchType
, launch_type
);
269 extensions::SetLaunchType(GetExtensionService(profile_
),
273 const extensions::Extension
* extension
=
274 extensions::ExtensionRegistry::Get(profile_
)->GetExtensionById(
275 extension_id_
, extensions::ExtensionRegistry::EVERYTHING
);
280 if (base::UTF8ToUTF16(extension
->name()) != title_tf_
->text()) {
281 // Reinstall the app with an updated name.
282 WebApplicationInfo
install_info(web_app_info_
);
283 install_info
.title
= title_tf_
->text();
285 // This will asynchronously reload the extension, causing the Extension*
286 // we have to be destroyed. The extension ID will stay the same so that is
287 // used later on to highlight the app.
288 extensions::CreateOrUpdateBookmarkApp(GetExtensionService(profile_
),
292 // As the extension could be destroyed after this point, set it to null to
293 // prevent anyone trying to use it in future.
296 // Show the newly installed app in the app launcher or chrome://apps.
297 Profile
* current_profile
= profile_
->GetOriginalProfile();
298 if (IsAppLauncherEnabled()) {
299 chrome::HostDesktopType desktop
= chrome::GetHostDesktopTypeForNativeWindow(
300 GetWidget()->GetNativeWindow());
301 AppListService::Get(desktop
)
302 ->ShowForAppInstall(current_profile
, extension_id_
, false);
306 chrome::NavigateParams
params(current_profile
,
307 GURL(chrome::kChromeUIAppsURL
),
308 ui::PAGE_TRANSITION_LINK
);
309 params
.disposition
= SINGLETON_TAB
;
310 chrome::Navigate(¶ms
);
312 content::NotificationService::current()->Notify(
313 chrome::NOTIFICATION_APP_INSTALLED_TO_NTP
,
314 content::Source
<content::WebContents
>(params
.target_contents
),
315 content::Details
<const std::string
>(&extension_id_
));