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.
5 #include "chrome/browser/ui/views/create_application_shortcut_view.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/win/windows_version.h"
15 #include "chrome/browser/extensions/tab_helper.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_commands.h"
19 #include "chrome/browser/ui/browser_finder.h"
20 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
21 #include "chrome/browser/web_applications/web_app.h"
22 #include "chrome/common/chrome_constants.h"
23 #include "chrome/common/pref_names.h"
24 #include "chrome/grit/generated_resources.h"
25 #include "chrome/grit/locale_settings.h"
26 #include "components/constrained_window/constrained_window_views.h"
27 #include "components/favicon_base/select_favicon_frames.h"
28 #include "content/public/browser/render_view_host.h"
29 #include "content/public/browser/render_widget_host_view.h"
30 #include "content/public/browser/web_contents.h"
31 #include "extensions/common/extension.h"
32 #include "net/base/load_flags.h"
33 #include "net/url_request/url_request.h"
34 #include "skia/ext/image_operations.h"
35 #include "third_party/skia/include/core/SkBitmap.h"
36 #include "third_party/skia/include/core/SkPaint.h"
37 #include "third_party/skia/include/core/SkRect.h"
38 #include "ui/base/l10n/l10n_util.h"
39 #include "ui/base/layout.h"
40 #include "ui/base/resource/resource_bundle.h"
41 #include "ui/gfx/canvas.h"
42 #include "ui/gfx/codec/png_codec.h"
43 #include "ui/gfx/image/image_family.h"
44 #include "ui/gfx/image/image_skia.h"
45 #include "ui/views/controls/button/checkbox.h"
46 #include "ui/views/controls/image_view.h"
47 #include "ui/views/controls/label.h"
48 #include "ui/views/layout/grid_layout.h"
49 #include "ui/views/layout/layout_constants.h"
50 #include "ui/views/widget/widget.h"
51 #include "ui/views/window/dialog_client_view.h"
56 const int kIconPreviewSizePixels
= 32;
58 // AppInfoView shows the application icon and title.
59 class AppInfoView
: public views::View
{
61 AppInfoView(const base::string16
& title
,
62 const base::string16
& description
,
63 const gfx::ImageFamily
& icon
);
65 // Updates the title/description of the web app.
66 void UpdateText(const base::string16
& title
,
67 const base::string16
& description
);
69 // Updates the icon of the web app.
70 void UpdateIcon(const gfx::ImageFamily
& image
);
72 // Overridden from views::View:
73 void OnPaint(gfx::Canvas
* canvas
) override
;
76 // Initializes the controls
77 void Init(const base::string16
& title
,
78 const base::string16
& description
, const gfx::ImageFamily
& icon
);
80 // Creates or updates description label.
81 void PrepareDescriptionLabel(const base::string16
& description
);
83 // Sets up layout manager.
86 views::ImageView
* icon_
;
88 views::Label
* description_
;
91 AppInfoView::AppInfoView(const base::string16
& title
,
92 const base::string16
& description
,
93 const gfx::ImageFamily
& icon
)
97 Init(title
, description
, icon
);
100 void AppInfoView::Init(const base::string16
& title_text
,
101 const base::string16
& description_text
,
102 const gfx::ImageFamily
& icon
) {
103 icon_
= new views::ImageView();
105 icon_
->SetImageSize(gfx::Size(kIconPreviewSizePixels
,
106 kIconPreviewSizePixels
));
108 title_
= new views::Label(
110 ui::ResourceBundle::GetSharedInstance().GetFontList(
111 ui::ResourceBundle::BoldFont
));
112 title_
->SetMultiLine(true);
113 title_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
115 PrepareDescriptionLabel(description_text
);
120 void AppInfoView::PrepareDescriptionLabel(const base::string16
& description
) {
121 // Do not make space for the description if it is empty.
122 if (description
.empty())
125 const size_t kMaxLength
= 200;
126 const base::string16
kEllipsis(base::ASCIIToUTF16(" ... "));
128 base::string16 text
= description
;
129 if (text
.length() > kMaxLength
) {
130 text
= text
.substr(0, kMaxLength
);
135 description_
->SetText(text
);
137 description_
= new views::Label(text
);
138 description_
->SetMultiLine(true);
139 description_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
143 void AppInfoView::SetupLayout() {
144 views::GridLayout
* layout
= views::GridLayout::CreatePanel(this);
145 SetLayoutManager(layout
);
147 static const int kColumnSetId
= 0;
148 views::ColumnSet
* column_set
= layout
->AddColumnSet(kColumnSetId
);
149 column_set
->AddColumn(views::GridLayout::CENTER
, views::GridLayout::LEADING
,
150 20.0f
, views::GridLayout::FIXED
,
151 kIconPreviewSizePixels
, kIconPreviewSizePixels
);
152 column_set
->AddColumn(views::GridLayout::FILL
, views::GridLayout::CENTER
,
153 80.0f
, views::GridLayout::USE_PREF
, 0, 0);
155 layout
->StartRow(0, kColumnSetId
);
156 layout
->AddView(icon_
, 1, description_
? 2 : 1);
157 layout
->AddView(title_
);
160 layout
->StartRow(0, kColumnSetId
);
161 layout
->SkipColumns(1);
162 layout
->AddView(description_
);
166 void AppInfoView::UpdateText(const base::string16
& title
,
167 const base::string16
& description
) {
168 title_
->SetText(title
);
169 PrepareDescriptionLabel(description
);
174 void AppInfoView::UpdateIcon(const gfx::ImageFamily
& image
) {
175 // Get the icon closest to the desired preview size.
176 const gfx::Image
* icon
= image
.GetBest(kIconPreviewSizePixels
,
177 kIconPreviewSizePixels
);
178 if (!icon
|| icon
->IsEmpty())
179 // The family has no icons. Leave the image blank.
181 const SkBitmap
& bitmap
= *icon
->ToSkBitmap();
182 if (bitmap
.width() == kIconPreviewSizePixels
&&
183 bitmap
.height() == kIconPreviewSizePixels
) {
184 icon_
->SetImage(gfx::ImageSkia::CreateFrom1xBitmap(bitmap
));
186 // Resize the image to the desired size.
187 SkBitmap resized_bitmap
= skia::ImageOperations::Resize(
188 bitmap
, skia::ImageOperations::RESIZE_LANCZOS3
,
189 kIconPreviewSizePixels
, kIconPreviewSizePixels
);
191 icon_
->SetImage(gfx::ImageSkia::CreateFrom1xBitmap(resized_bitmap
));
195 void AppInfoView::OnPaint(gfx::Canvas
* canvas
) {
196 gfx::Rect bounds
= GetLocalBounds();
198 SkRect border_rect
= {
199 SkIntToScalar(bounds
.x()),
200 SkIntToScalar(bounds
.y()),
201 SkIntToScalar(bounds
.right()),
202 SkIntToScalar(bounds
.bottom())
205 SkPaint border_paint
;
206 border_paint
.setAntiAlias(true);
207 border_paint
.setARGB(0xFF, 0xC8, 0xC8, 0xC8);
209 canvas
->sk_canvas()->drawRoundRect(border_rect
, SkIntToScalar(2),
210 SkIntToScalar(2), border_paint
);
212 SkRect inner_rect
= {
213 border_rect
.fLeft
+ SkDoubleToScalar(0.5),
214 border_rect
.fTop
+ SkDoubleToScalar(0.5),
215 border_rect
.fRight
- SkDoubleToScalar(0.5),
216 border_rect
.fBottom
- SkDoubleToScalar(0.5),
220 inner_paint
.setAntiAlias(true);
221 inner_paint
.setARGB(0xFF, 0xF8, 0xF8, 0xF8);
222 canvas
->sk_canvas()->drawRoundRect(inner_rect
, SkDoubleToScalar(1.5),
223 SkDoubleToScalar(1.5), inner_paint
);
230 void ShowCreateWebAppShortcutsDialog(gfx::NativeWindow parent_window
,
231 content::WebContents
* web_contents
) {
232 constrained_window::CreateBrowserModalDialogViews(
233 new CreateUrlApplicationShortcutView(web_contents
),
234 parent_window
)->Show();
237 void ShowCreateChromeAppShortcutsDialog(
238 gfx::NativeWindow parent_window
,
240 const extensions::Extension
* app
,
241 const base::Callback
<void(bool)>& close_callback
) {
242 constrained_window::CreateBrowserModalDialogViews(
243 new CreateChromeApplicationShortcutView(profile
, app
, close_callback
),
244 parent_window
)->Show();
247 } // namespace chrome
249 CreateApplicationShortcutView::CreateApplicationShortcutView(Profile
* profile
)
252 create_shortcuts_label_(NULL
),
253 desktop_check_box_(NULL
),
254 menu_check_box_(NULL
),
255 quick_launch_check_box_(NULL
) {}
257 CreateApplicationShortcutView::~CreateApplicationShortcutView() {}
259 void CreateApplicationShortcutView::InitControls(DialogLayout dialog_layout
) {
260 if (dialog_layout
== DIALOG_LAYOUT_URL_SHORTCUT
) {
261 app_info_
= new AppInfoView(shortcut_info_
.title
,
262 shortcut_info_
.description
,
263 shortcut_info_
.favicon
);
265 create_shortcuts_label_
= new views::Label(
266 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_LABEL
));
267 create_shortcuts_label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
269 desktop_check_box_
= AddCheckbox(
270 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_DESKTOP_CHKBOX
),
271 profile_
->GetPrefs()->GetBoolean(prefs::kWebAppCreateOnDesktop
));
273 menu_check_box_
= NULL
;
274 quick_launch_check_box_
= NULL
;
277 // Do not allow creating shortcuts on the Start Screen for Windows 8.
278 if (base::win::GetVersion() < base::win::VERSION_WIN8
) {
279 menu_check_box_
= AddCheckbox(
280 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_START_MENU_CHKBOX
),
281 profile_
->GetPrefs()->GetBoolean(prefs::kWebAppCreateInAppsMenu
));
284 quick_launch_check_box_
= AddCheckbox(
285 (base::win::GetVersion() >= base::win::VERSION_WIN7
) ?
286 l10n_util::GetStringUTF16(IDS_PIN_TO_TASKBAR_CHKBOX
) :
287 l10n_util::GetStringUTF16(
288 IDS_CREATE_SHORTCUTS_QUICK_LAUNCH_BAR_CHKBOX
),
289 profile_
->GetPrefs()->GetBoolean(prefs::kWebAppCreateInQuickLaunchBar
));
290 #elif defined(OS_POSIX)
291 menu_check_box_
= AddCheckbox(
292 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_MENU_CHKBOX
),
293 profile_
->GetPrefs()->GetBoolean(prefs::kWebAppCreateInAppsMenu
));
297 views::GridLayout
* layout
= views::GridLayout::CreatePanel(this);
298 SetLayoutManager(layout
);
300 static const int kHeaderColumnSetId
= 0;
301 views::ColumnSet
* column_set
= layout
->AddColumnSet(kHeaderColumnSetId
);
302 column_set
->AddColumn(views::GridLayout::FILL
, views::GridLayout::CENTER
,
303 100.0f
, views::GridLayout::FIXED
, 0, 0);
305 static const int kTableColumnSetId
= 1;
306 column_set
= layout
->AddColumnSet(kTableColumnSetId
);
307 column_set
->AddPaddingColumn(0, views::kPanelHorizIndentation
);
308 column_set
->AddColumn(views::GridLayout::FILL
, views::GridLayout::FILL
,
309 100.0f
, views::GridLayout::USE_PREF
, 0, 0);
312 layout
->StartRow(0, kHeaderColumnSetId
);
313 layout
->AddView(app_info_
);
314 layout
->AddPaddingRow(0, views::kPanelSubVerticalSpacing
);
317 layout
->StartRow(0, kHeaderColumnSetId
);
318 layout
->AddView(create_shortcuts_label_
);
320 layout
->AddPaddingRow(0, views::kLabelToControlVerticalSpacing
);
321 layout
->StartRow(0, kTableColumnSetId
);
322 layout
->AddView(desktop_check_box_
);
324 if (menu_check_box_
!= NULL
) {
325 layout
->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing
);
326 layout
->StartRow(0, kTableColumnSetId
);
327 layout
->AddView(menu_check_box_
);
330 if (quick_launch_check_box_
!= NULL
) {
331 layout
->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing
);
332 layout
->StartRow(0, kTableColumnSetId
);
333 layout
->AddView(quick_launch_check_box_
);
337 gfx::Size
CreateApplicationShortcutView::GetPreferredSize() const {
338 // TODO(evanm): should this use IDS_CREATE_SHORTCUTS_DIALOG_WIDTH_CHARS?
339 static const int kDialogWidth
= 360;
340 int height
= GetLayoutManager()->GetPreferredHeightForWidth(this,
342 return gfx::Size(kDialogWidth
, height
);
345 base::string16
CreateApplicationShortcutView::GetDialogButtonLabel(
346 ui::DialogButton button
) const {
347 if (button
== ui::DIALOG_BUTTON_OK
)
348 return l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_COMMIT
);
349 return views::DialogDelegateView::GetDialogButtonLabel(button
);
352 bool CreateApplicationShortcutView::IsDialogButtonEnabled(
353 ui::DialogButton button
) const {
354 if (button
== ui::DIALOG_BUTTON_OK
)
355 return desktop_check_box_
->checked() ||
356 ((menu_check_box_
!= NULL
) &&
357 menu_check_box_
->checked()) ||
358 ((quick_launch_check_box_
!= NULL
) &&
359 quick_launch_check_box_
->checked());
364 ui::ModalType
CreateApplicationShortcutView::GetModalType() const {
365 return ui::MODAL_TYPE_WINDOW
;
368 base::string16
CreateApplicationShortcutView::GetWindowTitle() const {
369 return l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_TITLE
);
372 bool CreateApplicationShortcutView::Accept() {
373 if (!IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
))
376 web_app::ShortcutLocations creation_locations
;
377 creation_locations
.on_desktop
= desktop_check_box_
->checked();
378 if (menu_check_box_
!= NULL
&& menu_check_box_
->checked()) {
379 creation_locations
.applications_menu_location
=
380 create_in_chrome_apps_subdir_
?
381 web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS
:
382 web_app::APP_MENU_LOCATION_ROOT
;
386 creation_locations
.in_quick_launch_bar
= quick_launch_check_box_
== NULL
?
387 NULL
: quick_launch_check_box_
->checked();
388 #elif defined(OS_POSIX)
389 // Create shortcut in Mac dock or as Linux (gnome/kde) application launcher
390 // are not implemented yet.
391 creation_locations
.in_quick_launch_bar
= false;
394 web_app::CreateShortcutsWithInfo(web_app::SHORTCUT_CREATION_BY_USER
,
397 file_handlers_info_
);
401 views::Checkbox
* CreateApplicationShortcutView::AddCheckbox(
402 const base::string16
& text
, bool checked
) {
403 views::Checkbox
* checkbox
= new views::Checkbox(text
);
404 checkbox
->SetChecked(checked
);
405 checkbox
->set_listener(this);
409 void CreateApplicationShortcutView::ButtonPressed(views::Button
* sender
,
410 const ui::Event
& event
) {
411 if (sender
== desktop_check_box_
) {
412 profile_
->GetPrefs()->SetBoolean(prefs::kWebAppCreateOnDesktop
,
413 desktop_check_box_
->checked());
414 } else if (sender
== menu_check_box_
) {
415 profile_
->GetPrefs()->SetBoolean(prefs::kWebAppCreateInAppsMenu
,
416 menu_check_box_
->checked());
417 } else if (sender
== quick_launch_check_box_
) {
418 profile_
->GetPrefs()->SetBoolean(prefs::kWebAppCreateInQuickLaunchBar
,
419 quick_launch_check_box_
->checked());
422 // When no checkbox is checked we should not have the action button enabled.
423 GetDialogClientView()->UpdateDialogButtons();
426 CreateUrlApplicationShortcutView::CreateUrlApplicationShortcutView(
427 content::WebContents
* web_contents
)
428 : CreateApplicationShortcutView(
429 Profile::FromBrowserContext(web_contents
->GetBrowserContext())),
430 web_contents_(web_contents
),
431 pending_download_id_(-1),
432 weak_ptr_factory_(this) {
433 web_app::GetShortcutInfoForTab(web_contents_
, &shortcut_info_
);
434 const WebApplicationInfo
& app_info
=
435 extensions::TabHelper::FromWebContents(web_contents_
)->web_app_info();
436 if (!app_info
.icons
.empty()) {
437 web_app::GetIconsInfo(app_info
, &unprocessed_icons_
);
441 // Create URL app shortcuts in the top-level menu.
442 create_in_chrome_apps_subdir_
= false;
444 InitControls(DIALOG_LAYOUT_URL_SHORTCUT
);
447 CreateUrlApplicationShortcutView::~CreateUrlApplicationShortcutView() {
450 bool CreateUrlApplicationShortcutView::Accept() {
451 if (!CreateApplicationShortcutView::Accept())
454 // Get the smallest icon in the icon family (should have only 1).
455 const gfx::Image
* icon
= shortcut_info_
.favicon
.GetBest(0, 0);
456 SkBitmap bitmap
= icon
? icon
->AsBitmap() : SkBitmap();
457 extensions::TabHelper::FromWebContents(web_contents_
)->SetAppIcon(bitmap
);
458 Browser
* browser
= chrome::FindBrowserWithWebContents(web_contents_
);
460 chrome::ConvertTabToAppWindow(browser
, web_contents_
);
464 void CreateUrlApplicationShortcutView::FetchIcon() {
465 // There should only be fetch job at a time.
466 DCHECK_EQ(-1, pending_download_id_
);
468 if (unprocessed_icons_
.empty()) // No icons to fetch.
471 int preferred_size
= std::max(unprocessed_icons_
.back().width
,
472 unprocessed_icons_
.back().height
);
473 pending_download_id_
= web_contents_
->DownloadImage(
474 unprocessed_icons_
.back().url
,
475 true, // is a favicon
476 0, // no maximum size
477 false, // normal cache policy
478 base::Bind(&CreateUrlApplicationShortcutView::DidDownloadFavicon
,
479 weak_ptr_factory_
.GetWeakPtr(),
482 unprocessed_icons_
.pop_back();
485 void CreateUrlApplicationShortcutView::DidDownloadFavicon(
488 int http_status_code
,
489 const GURL
& image_url
,
490 const std::vector
<SkBitmap
>& bitmaps
,
491 const std::vector
<gfx::Size
>& original_bitmap_sizes
) {
492 if (id
!= pending_download_id_
)
494 pending_download_id_
= -1;
496 gfx::ImageSkia image_skia
= CreateFaviconImageSkia(
498 original_bitmap_sizes
,
501 if (!image_skia
.isNull()) {
502 // As |shortcut_info_| will be passed to the FILE thread upon accepting the
503 // dialog, this image must be made read-only and thread-safe.
504 image_skia
.MakeThreadSafe();
505 shortcut_info_
.favicon
.Add(image_skia
);
506 static_cast<AppInfoView
*>(app_info_
)->UpdateIcon(shortcut_info_
.favicon
);
512 CreateChromeApplicationShortcutView::CreateChromeApplicationShortcutView(
514 const extensions::Extension
* app
,
515 const base::Callback
<void(bool)>& close_callback
)
516 : CreateApplicationShortcutView(profile
),
517 close_callback_(close_callback
),
518 weak_ptr_factory_(this) {
519 // Place Chrome app shortcuts in the "Chrome Apps" submenu.
520 create_in_chrome_apps_subdir_
= true;
522 InitControls(DIALOG_LAYOUT_APP_SHORTCUT
);
524 // Get shortcut, icon and file handler information; they are needed for
525 // creating the shortcut.
526 web_app::GetInfoForApp(
529 base::Bind(&CreateChromeApplicationShortcutView::OnAppInfoLoaded
,
530 weak_ptr_factory_
.GetWeakPtr()));
533 CreateChromeApplicationShortcutView::~CreateChromeApplicationShortcutView() {}
535 bool CreateChromeApplicationShortcutView::Accept() {
536 if (!close_callback_
.is_null())
537 close_callback_
.Run(true);
538 return CreateApplicationShortcutView::Accept();
541 bool CreateChromeApplicationShortcutView::Cancel() {
542 if (!close_callback_
.is_null())
543 close_callback_
.Run(false);
544 return CreateApplicationShortcutView::Cancel();
547 void CreateChromeApplicationShortcutView::OnAppInfoLoaded(
548 const web_app::ShortcutInfo
& shortcut_info
,
549 const extensions::FileHandlersInfo
& file_handlers_info
) {
550 shortcut_info_
= shortcut_info
;
551 file_handlers_info_
= file_handlers_info
;