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_helpers.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/win/windows_version.h"
14 #include "chrome/browser/extensions/tab_helper.h"
15 #include "chrome/browser/favicon/favicon_util.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/views/constrained_window_views.h"
21 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
22 #include "chrome/browser/web_applications/web_app.h"
23 #include "chrome/common/chrome_constants.h"
24 #include "chrome/common/pref_names.h"
25 #include "components/favicon_base/select_favicon_frames.h"
26 #include "content/public/browser/render_view_host.h"
27 #include "content/public/browser/render_widget_host_view.h"
28 #include "content/public/browser/web_contents.h"
29 #include "extensions/common/extension.h"
30 #include "grit/chromium_strings.h"
31 #include "grit/generated_resources.h"
32 #include "grit/locale_settings.h"
33 #include "grit/theme_resources.h"
34 #include "net/base/load_flags.h"
35 #include "net/url_request/url_request.h"
36 #include "skia/ext/image_operations.h"
37 #include "third_party/skia/include/core/SkBitmap.h"
38 #include "third_party/skia/include/core/SkPaint.h"
39 #include "third_party/skia/include/core/SkRect.h"
40 #include "ui/base/l10n/l10n_util.h"
41 #include "ui/base/layout.h"
42 #include "ui/base/resource/resource_bundle.h"
43 #include "ui/gfx/canvas.h"
44 #include "ui/gfx/codec/png_codec.h"
45 #include "ui/gfx/image/image_family.h"
46 #include "ui/gfx/image/image_skia.h"
47 #include "ui/views/controls/button/checkbox.h"
48 #include "ui/views/controls/image_view.h"
49 #include "ui/views/controls/label.h"
50 #include "ui/views/layout/grid_layout.h"
51 #include "ui/views/layout/layout_constants.h"
52 #include "ui/views/widget/widget.h"
53 #include "ui/views/window/dialog_client_view.h"
58 const int kIconPreviewSizePixels
= 32;
60 // AppInfoView shows the application icon and title.
61 class AppInfoView
: public views::View
{
63 AppInfoView(const base::string16
& title
,
64 const base::string16
& description
,
65 const gfx::ImageFamily
& icon
);
67 // Updates the title/description of the web app.
68 void UpdateText(const base::string16
& title
,
69 const base::string16
& description
);
71 // Updates the icon of the web app.
72 void UpdateIcon(const gfx::ImageFamily
& image
);
74 // Overridden from views::View:
75 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
;
78 // Initializes the controls
79 void Init(const base::string16
& title
,
80 const base::string16
& description
, const gfx::ImageFamily
& icon
);
82 // Creates or updates description label.
83 void PrepareDescriptionLabel(const base::string16
& description
);
85 // Sets up layout manager.
88 views::ImageView
* icon_
;
90 views::Label
* description_
;
93 AppInfoView::AppInfoView(const base::string16
& title
,
94 const base::string16
& description
,
95 const gfx::ImageFamily
& icon
)
99 Init(title
, description
, icon
);
102 void AppInfoView::Init(const base::string16
& title_text
,
103 const base::string16
& description_text
,
104 const gfx::ImageFamily
& icon
) {
105 icon_
= new views::ImageView();
107 icon_
->SetImageSize(gfx::Size(kIconPreviewSizePixels
,
108 kIconPreviewSizePixels
));
110 title_
= new views::Label(
112 ui::ResourceBundle::GetSharedInstance().GetFontList(
113 ui::ResourceBundle::BoldFont
));
114 title_
->SetMultiLine(true);
115 title_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
117 PrepareDescriptionLabel(description_text
);
122 void AppInfoView::PrepareDescriptionLabel(const base::string16
& description
) {
123 // Do not make space for the description if it is empty.
124 if (description
.empty())
127 const size_t kMaxLength
= 200;
128 const base::string16
kEllipsis(base::ASCIIToUTF16(" ... "));
130 base::string16 text
= description
;
131 if (text
.length() > kMaxLength
) {
132 text
= text
.substr(0, kMaxLength
);
137 description_
->SetText(text
);
139 description_
= new views::Label(text
);
140 description_
->SetMultiLine(true);
141 description_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
145 void AppInfoView::SetupLayout() {
146 views::GridLayout
* layout
= views::GridLayout::CreatePanel(this);
147 SetLayoutManager(layout
);
149 static const int kColumnSetId
= 0;
150 views::ColumnSet
* column_set
= layout
->AddColumnSet(kColumnSetId
);
151 column_set
->AddColumn(views::GridLayout::CENTER
, views::GridLayout::LEADING
,
152 20.0f
, views::GridLayout::FIXED
,
153 kIconPreviewSizePixels
, kIconPreviewSizePixels
);
154 column_set
->AddColumn(views::GridLayout::FILL
, views::GridLayout::CENTER
,
155 80.0f
, views::GridLayout::USE_PREF
, 0, 0);
157 layout
->StartRow(0, kColumnSetId
);
158 layout
->AddView(icon_
, 1, description_
? 2 : 1);
159 layout
->AddView(title_
);
162 layout
->StartRow(0, kColumnSetId
);
163 layout
->SkipColumns(1);
164 layout
->AddView(description_
);
168 void AppInfoView::UpdateText(const base::string16
& title
,
169 const base::string16
& description
) {
170 title_
->SetText(title
);
171 PrepareDescriptionLabel(description
);
176 void AppInfoView::UpdateIcon(const gfx::ImageFamily
& image
) {
177 // Get the icon closest to the desired preview size.
178 const gfx::Image
* icon
= image
.GetBest(kIconPreviewSizePixels
,
179 kIconPreviewSizePixels
);
180 if (!icon
|| icon
->IsEmpty())
181 // The family has no icons. Leave the image blank.
183 const SkBitmap
& bitmap
= *icon
->ToSkBitmap();
184 if (bitmap
.width() == kIconPreviewSizePixels
&&
185 bitmap
.height() == kIconPreviewSizePixels
) {
186 icon_
->SetImage(gfx::ImageSkia::CreateFrom1xBitmap(bitmap
));
188 // Resize the image to the desired size.
189 SkBitmap resized_bitmap
= skia::ImageOperations::Resize(
190 bitmap
, skia::ImageOperations::RESIZE_LANCZOS3
,
191 kIconPreviewSizePixels
, kIconPreviewSizePixels
);
193 icon_
->SetImage(gfx::ImageSkia::CreateFrom1xBitmap(resized_bitmap
));
197 void AppInfoView::OnPaint(gfx::Canvas
* canvas
) {
198 gfx::Rect bounds
= GetLocalBounds();
200 SkRect border_rect
= {
201 SkIntToScalar(bounds
.x()),
202 SkIntToScalar(bounds
.y()),
203 SkIntToScalar(bounds
.right()),
204 SkIntToScalar(bounds
.bottom())
207 SkPaint border_paint
;
208 border_paint
.setAntiAlias(true);
209 border_paint
.setARGB(0xFF, 0xC8, 0xC8, 0xC8);
211 canvas
->sk_canvas()->drawRoundRect(border_rect
, SkIntToScalar(2),
212 SkIntToScalar(2), border_paint
);
214 SkRect inner_rect
= {
215 border_rect
.fLeft
+ SkDoubleToScalar(0.5),
216 border_rect
.fTop
+ SkDoubleToScalar(0.5),
217 border_rect
.fRight
- SkDoubleToScalar(0.5),
218 border_rect
.fBottom
- SkDoubleToScalar(0.5),
222 inner_paint
.setAntiAlias(true);
223 inner_paint
.setARGB(0xFF, 0xF8, 0xF8, 0xF8);
224 canvas
->sk_canvas()->drawRoundRect(inner_rect
, SkDoubleToScalar(1.5),
225 SkDoubleToScalar(1.5), inner_paint
);
232 void ShowCreateWebAppShortcutsDialog(gfx::NativeWindow parent_window
,
233 content::WebContents
* web_contents
) {
234 CreateBrowserModalDialogViews(
235 new CreateUrlApplicationShortcutView(web_contents
),
236 parent_window
)->Show();
239 void ShowCreateChromeAppShortcutsDialog(
240 gfx::NativeWindow parent_window
,
242 const extensions::Extension
* app
,
243 const base::Callback
<void(bool)>& close_callback
) {
244 CreateBrowserModalDialogViews(
245 new CreateChromeApplicationShortcutView(profile
, app
, close_callback
),
246 parent_window
)->Show();
249 } // namespace chrome
251 CreateApplicationShortcutView::CreateApplicationShortcutView(Profile
* profile
)
254 create_shortcuts_label_(NULL
),
255 desktop_check_box_(NULL
),
256 menu_check_box_(NULL
),
257 quick_launch_check_box_(NULL
) {}
259 CreateApplicationShortcutView::~CreateApplicationShortcutView() {}
261 void CreateApplicationShortcutView::InitControls() {
263 app_info_
= new AppInfoView(shortcut_info_
.title
, shortcut_info_
.description
,
264 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);
311 layout
->StartRow(0, kHeaderColumnSetId
);
312 layout
->AddView(app_info_
);
314 layout
->AddPaddingRow(0, views::kPanelSubVerticalSpacing
);
315 layout
->StartRow(0, kHeaderColumnSetId
);
316 layout
->AddView(create_shortcuts_label_
);
318 layout
->AddPaddingRow(0, views::kLabelToControlVerticalSpacing
);
319 layout
->StartRow(0, kTableColumnSetId
);
320 layout
->AddView(desktop_check_box_
);
322 if (menu_check_box_
!= NULL
) {
323 layout
->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing
);
324 layout
->StartRow(0, kTableColumnSetId
);
325 layout
->AddView(menu_check_box_
);
328 if (quick_launch_check_box_
!= NULL
) {
329 layout
->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing
);
330 layout
->StartRow(0, kTableColumnSetId
);
331 layout
->AddView(quick_launch_check_box_
);
335 gfx::Size
CreateApplicationShortcutView::GetPreferredSize() const {
336 // TODO(evanm): should this use IDS_CREATE_SHORTCUTS_DIALOG_WIDTH_CHARS?
337 static const int kDialogWidth
= 360;
338 int height
= GetLayoutManager()->GetPreferredHeightForWidth(this,
340 return gfx::Size(kDialogWidth
, height
);
343 base::string16
CreateApplicationShortcutView::GetDialogButtonLabel(
344 ui::DialogButton button
) const {
345 if (button
== ui::DIALOG_BUTTON_OK
)
346 return l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_COMMIT
);
347 return views::DialogDelegateView::GetDialogButtonLabel(button
);
350 bool CreateApplicationShortcutView::IsDialogButtonEnabled(
351 ui::DialogButton button
) const {
352 if (button
== ui::DIALOG_BUTTON_OK
)
353 return desktop_check_box_
->checked() ||
354 ((menu_check_box_
!= NULL
) &&
355 menu_check_box_
->checked()) ||
356 ((quick_launch_check_box_
!= NULL
) &&
357 quick_launch_check_box_
->checked());
362 ui::ModalType
CreateApplicationShortcutView::GetModalType() const {
363 return ui::MODAL_TYPE_WINDOW
;
366 base::string16
CreateApplicationShortcutView::GetWindowTitle() const {
367 return l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_TITLE
);
370 bool CreateApplicationShortcutView::Accept() {
371 if (!IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
))
374 web_app::ShortcutLocations creation_locations
;
375 creation_locations
.on_desktop
= desktop_check_box_
->checked();
376 if (menu_check_box_
!= NULL
&& menu_check_box_
->checked()) {
377 creation_locations
.applications_menu_location
=
378 create_in_chrome_apps_subdir_
?
379 web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS
:
380 web_app::APP_MENU_LOCATION_ROOT
;
384 creation_locations
.in_quick_launch_bar
= quick_launch_check_box_
== NULL
?
385 NULL
: quick_launch_check_box_
->checked();
386 #elif defined(OS_POSIX)
387 // Create shortcut in Mac dock or as Linux (gnome/kde) application launcher
388 // are not implemented yet.
389 creation_locations
.in_quick_launch_bar
= false;
392 web_app::CreateShortcutsForShortcutInfo(
393 web_app::SHORTCUT_CREATION_BY_USER
,
399 views::Checkbox
* CreateApplicationShortcutView::AddCheckbox(
400 const base::string16
& text
, bool checked
) {
401 views::Checkbox
* checkbox
= new views::Checkbox(text
);
402 checkbox
->SetChecked(checked
);
403 checkbox
->set_listener(this);
407 void CreateApplicationShortcutView::ButtonPressed(views::Button
* sender
,
408 const ui::Event
& event
) {
409 if (sender
== desktop_check_box_
) {
410 profile_
->GetPrefs()->SetBoolean(prefs::kWebAppCreateOnDesktop
,
411 desktop_check_box_
->checked());
412 } else if (sender
== menu_check_box_
) {
413 profile_
->GetPrefs()->SetBoolean(prefs::kWebAppCreateInAppsMenu
,
414 menu_check_box_
->checked());
415 } else if (sender
== quick_launch_check_box_
) {
416 profile_
->GetPrefs()->SetBoolean(prefs::kWebAppCreateInQuickLaunchBar
,
417 quick_launch_check_box_
->checked());
420 // When no checkbox is checked we should not have the action button enabled.
421 GetDialogClientView()->UpdateDialogButtons();
424 CreateUrlApplicationShortcutView::CreateUrlApplicationShortcutView(
425 content::WebContents
* web_contents
)
426 : CreateApplicationShortcutView(
427 Profile::FromBrowserContext(web_contents
->GetBrowserContext())),
428 web_contents_(web_contents
),
429 pending_download_id_(-1),
430 weak_ptr_factory_(this) {
432 web_app::GetShortcutInfoForTab(web_contents_
, &shortcut_info_
);
433 const WebApplicationInfo
& app_info
=
434 extensions::TabHelper::FromWebContents(web_contents_
)->web_app_info();
435 if (!app_info
.icons
.empty()) {
436 web_app::GetIconsInfo(app_info
, &unprocessed_icons_
);
440 // Create URL app shortcuts in the top-level menu.
441 create_in_chrome_apps_subdir_
= false;
446 CreateUrlApplicationShortcutView::~CreateUrlApplicationShortcutView() {
449 bool CreateUrlApplicationShortcutView::Accept() {
450 if (!CreateApplicationShortcutView::Accept())
453 // Get the smallest icon in the icon family (should have only 1).
454 const gfx::Image
* icon
= shortcut_info_
.favicon
.GetBest(0, 0);
455 SkBitmap bitmap
= icon
? icon
->AsBitmap() : SkBitmap();
456 extensions::TabHelper::FromWebContents(web_contents_
)->SetAppIcon(bitmap
);
457 Browser
* browser
= chrome::FindBrowserWithWebContents(web_contents_
);
459 chrome::ConvertTabToAppWindow(browser
, web_contents_
);
463 void CreateUrlApplicationShortcutView::FetchIcon() {
464 // There should only be fetch job at a time.
465 DCHECK_EQ(-1, pending_download_id_
);
467 if (unprocessed_icons_
.empty()) // No icons to fetch.
470 int preferred_size
= std::max(unprocessed_icons_
.back().width
,
471 unprocessed_icons_
.back().height
);
472 pending_download_id_
= web_contents_
->DownloadImage(
473 unprocessed_icons_
.back().url
,
474 true, // is a favicon
475 0, // no maximum size
476 base::Bind(&CreateUrlApplicationShortcutView::DidDownloadFavicon
,
477 weak_ptr_factory_
.GetWeakPtr(),
480 unprocessed_icons_
.pop_back();
483 void CreateUrlApplicationShortcutView::DidDownloadFavicon(
486 int http_status_code
,
487 const GURL
& image_url
,
488 const std::vector
<SkBitmap
>& bitmaps
,
489 const std::vector
<gfx::Size
>& original_bitmap_sizes
) {
490 if (id
!= pending_download_id_
)
492 pending_download_id_
= -1;
496 if (!bitmaps
.empty()) {
497 std::vector
<ui::ScaleFactor
> scale_factors
;
498 ui::ScaleFactor scale_factor
= ui::GetSupportedScaleFactor(
499 ui::GetScaleFactorForNativeView(
500 web_contents_
->GetRenderViewHost()->GetView()->GetNativeView()));
501 scale_factors
.push_back(scale_factor
);
502 std::vector
<size_t> closest_indices
;
503 SelectFaviconFrameIndices(original_bitmap_sizes
,
508 size_t closest_index
= closest_indices
[0];
509 image
= bitmaps
[closest_index
];
512 if (!image
.isNull()) {
513 shortcut_info_
.favicon
.Add(gfx::ImageSkia::CreateFrom1xBitmap(image
));
514 static_cast<AppInfoView
*>(app_info_
)->UpdateIcon(shortcut_info_
.favicon
);
520 CreateChromeApplicationShortcutView::CreateChromeApplicationShortcutView(
522 const extensions::Extension
* app
,
523 const base::Callback
<void(bool)>& close_callback
)
524 : CreateApplicationShortcutView(profile
),
525 close_callback_(close_callback
),
526 weak_ptr_factory_(this) {
527 // Required by InitControls().
528 shortcut_info_
.title
= base::UTF8ToUTF16(app
->name());
529 shortcut_info_
.description
= base::UTF8ToUTF16(app
->description());
531 // Place Chrome app shortcuts in the "Chrome Apps" submenu.
532 create_in_chrome_apps_subdir_
= true;
536 // Get shortcut information and icon now; they are needed for our UI.
537 web_app::UpdateShortcutInfoAndIconForApp(
539 base::Bind(&CreateChromeApplicationShortcutView::OnShortcutInfoLoaded
,
540 weak_ptr_factory_
.GetWeakPtr()));
543 CreateChromeApplicationShortcutView::~CreateChromeApplicationShortcutView() {}
545 bool CreateChromeApplicationShortcutView::Accept() {
546 if (!close_callback_
.is_null())
547 close_callback_
.Run(true);
548 return CreateApplicationShortcutView::Accept();
551 bool CreateChromeApplicationShortcutView::Cancel() {
552 if (!close_callback_
.is_null())
553 close_callback_
.Run(false);
554 return CreateApplicationShortcutView::Cancel();
557 // Called when the app's ShortcutInfo (with icon) is loaded.
558 void CreateChromeApplicationShortcutView::OnShortcutInfoLoaded(
559 const web_app::ShortcutInfo
& shortcut_info
) {
560 shortcut_info_
= shortcut_info
;
563 static_cast<AppInfoView
*>(app_info_
)->UpdateIcon(shortcut_info_
.favicon
);