Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / create_application_shortcut_view.cc
blob8983b44e0468d2ac8b89ce33235d31035dc63300
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"
7 #include <algorithm>
8 #include <cmath>
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"
52 #include "url/gurl.h"
54 namespace {
56 const int kIconPreviewSizePixels = 32;
58 // AppInfoView shows the application icon and title.
59 class AppInfoView : public views::View {
60 public:
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;
75 private:
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.
84 void SetupLayout();
86 views::ImageView* icon_;
87 views::Label* title_;
88 views::Label* description_;
91 AppInfoView::AppInfoView(const base::string16& title,
92 const base::string16& description,
93 const gfx::ImageFamily& icon)
94 : icon_(NULL),
95 title_(NULL),
96 description_(NULL) {
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();
104 UpdateIcon(icon);
105 icon_->SetImageSize(gfx::Size(kIconPreviewSizePixels,
106 kIconPreviewSizePixels));
108 title_ = new views::Label(
109 title_text,
110 ui::ResourceBundle::GetSharedInstance().GetFontList(
111 ui::ResourceBundle::BoldFont));
112 title_->SetMultiLine(true);
113 title_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
115 PrepareDescriptionLabel(description_text);
117 SetupLayout();
120 void AppInfoView::PrepareDescriptionLabel(const base::string16& description) {
121 // Do not make space for the description if it is empty.
122 if (description.empty())
123 return;
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);
131 text += kEllipsis;
134 if (description_) {
135 description_->SetText(text);
136 } else {
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_);
159 if (description_) {
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);
171 SetupLayout();
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.
180 return;
181 const SkBitmap& bitmap = *icon->ToSkBitmap();
182 if (bitmap.width() == kIconPreviewSizePixels &&
183 bitmap.height() == kIconPreviewSizePixels) {
184 icon_->SetImage(gfx::ImageSkia::CreateFrom1xBitmap(bitmap));
185 } else {
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),
219 SkPaint inner_paint;
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);
226 } // namespace
228 namespace chrome {
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,
239 Profile* profile,
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)
250 : profile_(profile),
251 app_info_(NULL),
252 create_shortcuts_label_(NULL),
253 desktop_check_box_(NULL),
254 menu_check_box_(NULL),
255 quick_launch_check_box_(NULL) {
258 CreateApplicationShortcutView::~CreateApplicationShortcutView() {}
260 void CreateApplicationShortcutView::InitControls(DialogLayout dialog_layout) {
261 if (dialog_layout == DIALOG_LAYOUT_URL_SHORTCUT && shortcut_info_) {
262 app_info_ =
263 new AppInfoView(shortcut_info_->title, shortcut_info_->description,
264 shortcut_info_->favicon);
266 create_shortcuts_label_ = new views::Label(
267 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_LABEL));
268 create_shortcuts_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
270 desktop_check_box_ = AddCheckbox(
271 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_DESKTOP_CHKBOX),
272 profile_->GetPrefs()->GetBoolean(prefs::kWebAppCreateOnDesktop));
274 menu_check_box_ = NULL;
275 quick_launch_check_box_ = NULL;
277 #if defined(OS_WIN)
278 // Do not allow creating shortcuts on the Start Screen for Windows 8.
279 if (base::win::GetVersion() < base::win::VERSION_WIN8) {
280 menu_check_box_ = AddCheckbox(
281 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_START_MENU_CHKBOX),
282 profile_->GetPrefs()->GetBoolean(prefs::kWebAppCreateInAppsMenu));
285 quick_launch_check_box_ = AddCheckbox(
286 (base::win::GetVersion() >= base::win::VERSION_WIN7) ?
287 l10n_util::GetStringUTF16(IDS_PIN_TO_TASKBAR_CHKBOX) :
288 l10n_util::GetStringUTF16(
289 IDS_CREATE_SHORTCUTS_QUICK_LAUNCH_BAR_CHKBOX),
290 profile_->GetPrefs()->GetBoolean(prefs::kWebAppCreateInQuickLaunchBar));
291 #elif defined(OS_POSIX)
292 menu_check_box_ = AddCheckbox(
293 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_MENU_CHKBOX),
294 profile_->GetPrefs()->GetBoolean(prefs::kWebAppCreateInAppsMenu));
295 #endif
297 // Layout controls
298 views::GridLayout* layout = views::GridLayout::CreatePanel(this);
299 SetLayoutManager(layout);
301 static const int kHeaderColumnSetId = 0;
302 views::ColumnSet* column_set = layout->AddColumnSet(kHeaderColumnSetId);
303 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER,
304 100.0f, views::GridLayout::FIXED, 0, 0);
306 static const int kTableColumnSetId = 1;
307 column_set = layout->AddColumnSet(kTableColumnSetId);
308 column_set->AddPaddingColumn(0, views::kPanelHorizIndentation);
309 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL,
310 100.0f, views::GridLayout::USE_PREF, 0, 0);
312 if (app_info_) {
313 layout->StartRow(0, kHeaderColumnSetId);
314 layout->AddView(app_info_);
315 layout->AddPaddingRow(0, views::kPanelSubVerticalSpacing);
318 layout->StartRow(0, kHeaderColumnSetId);
319 layout->AddView(create_shortcuts_label_);
321 layout->AddPaddingRow(0, views::kLabelToControlVerticalSpacing);
322 layout->StartRow(0, kTableColumnSetId);
323 layout->AddView(desktop_check_box_);
325 if (menu_check_box_ != NULL) {
326 layout->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing);
327 layout->StartRow(0, kTableColumnSetId);
328 layout->AddView(menu_check_box_);
331 if (quick_launch_check_box_ != NULL) {
332 layout->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing);
333 layout->StartRow(0, kTableColumnSetId);
334 layout->AddView(quick_launch_check_box_);
338 gfx::Size CreateApplicationShortcutView::GetPreferredSize() const {
339 // TODO(evanm): should this use IDS_CREATE_SHORTCUTS_DIALOG_WIDTH_CHARS?
340 static const int kDialogWidth = 360;
341 int height = GetLayoutManager()->GetPreferredHeightForWidth(this,
342 kDialogWidth);
343 return gfx::Size(kDialogWidth, height);
346 base::string16 CreateApplicationShortcutView::GetDialogButtonLabel(
347 ui::DialogButton button) const {
348 if (button == ui::DIALOG_BUTTON_OK)
349 return l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_COMMIT);
350 return views::DialogDelegateView::GetDialogButtonLabel(button);
353 bool CreateApplicationShortcutView::IsDialogButtonEnabled(
354 ui::DialogButton button) const {
355 if (button == ui::DIALOG_BUTTON_OK)
356 return desktop_check_box_->checked() ||
357 ((menu_check_box_ != NULL) &&
358 menu_check_box_->checked()) ||
359 ((quick_launch_check_box_ != NULL) &&
360 quick_launch_check_box_->checked());
362 return true;
365 ui::ModalType CreateApplicationShortcutView::GetModalType() const {
366 return ui::MODAL_TYPE_WINDOW;
369 base::string16 CreateApplicationShortcutView::GetWindowTitle() const {
370 return l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_TITLE);
373 bool CreateApplicationShortcutView::Accept() {
374 // NOTE: This procedure will reset |shortcut_info_| to null.
376 // Can happen if the shortcut data is not yet loaded.
377 if (!shortcut_info_)
378 return false;
380 if (!IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK))
381 return false;
383 web_app::ShortcutLocations creation_locations;
384 creation_locations.on_desktop = desktop_check_box_->checked();
385 if (menu_check_box_ != NULL && menu_check_box_->checked()) {
386 creation_locations.applications_menu_location =
387 create_in_chrome_apps_subdir_ ?
388 web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS :
389 web_app::APP_MENU_LOCATION_ROOT;
392 #if defined(OS_WIN)
393 creation_locations.in_quick_launch_bar = quick_launch_check_box_ == NULL ?
394 NULL : quick_launch_check_box_->checked();
395 #elif defined(OS_POSIX)
396 // Create shortcut in Mac dock or as Linux (gnome/kde) application launcher
397 // are not implemented yet.
398 creation_locations.in_quick_launch_bar = false;
399 #endif
401 web_app::CreateShortcutsWithInfo(web_app::SHORTCUT_CREATION_BY_USER,
402 creation_locations, shortcut_info_.Pass(),
403 file_handlers_info_);
404 return true;
407 views::Checkbox* CreateApplicationShortcutView::AddCheckbox(
408 const base::string16& text, bool checked) {
409 views::Checkbox* checkbox = new views::Checkbox(text);
410 checkbox->SetChecked(checked);
411 checkbox->set_listener(this);
412 return checkbox;
415 void CreateApplicationShortcutView::ButtonPressed(views::Button* sender,
416 const ui::Event& event) {
417 if (sender == desktop_check_box_) {
418 profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateOnDesktop,
419 desktop_check_box_->checked());
420 } else if (sender == menu_check_box_) {
421 profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateInAppsMenu,
422 menu_check_box_->checked());
423 } else if (sender == quick_launch_check_box_) {
424 profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateInQuickLaunchBar,
425 quick_launch_check_box_->checked());
428 // When no checkbox is checked we should not have the action button enabled.
429 GetDialogClientView()->UpdateDialogButtons();
432 CreateUrlApplicationShortcutView::CreateUrlApplicationShortcutView(
433 content::WebContents* web_contents)
434 : CreateApplicationShortcutView(
435 Profile::FromBrowserContext(web_contents->GetBrowserContext())),
436 web_contents_(web_contents),
437 pending_download_id_(-1),
438 weak_ptr_factory_(this) {
439 shortcut_info_ = web_app::GetShortcutInfoForTab(web_contents_);
440 const WebApplicationInfo& app_info =
441 extensions::TabHelper::FromWebContents(web_contents_)->web_app_info();
442 if (!app_info.icons.empty()) {
443 web_app::GetIconsInfo(app_info, &unprocessed_icons_);
444 FetchIcon();
447 // Create URL app shortcuts in the top-level menu.
448 create_in_chrome_apps_subdir_ = false;
450 InitControls(DIALOG_LAYOUT_URL_SHORTCUT);
453 CreateUrlApplicationShortcutView::~CreateUrlApplicationShortcutView() {
456 bool CreateUrlApplicationShortcutView::Accept() {
457 // Get the smallest icon in the icon family (should have only 1). This must be
458 // done before the call to Accept(), which will reset |shortcut_info_|.
459 DCHECK(shortcut_info_);
460 const gfx::Image* icon = shortcut_info_->favicon.GetBest(0, 0);
461 SkBitmap bitmap = icon ? icon->AsBitmap() : SkBitmap();
463 if (!CreateApplicationShortcutView::Accept())
464 return false;
466 extensions::TabHelper::FromWebContents(web_contents_)->SetAppIcon(bitmap);
467 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
468 if (browser)
469 chrome::ConvertTabToAppWindow(browser, web_contents_);
470 return true;
473 void CreateUrlApplicationShortcutView::FetchIcon() {
474 // There should only be fetch job at a time.
475 DCHECK_EQ(-1, pending_download_id_);
477 if (unprocessed_icons_.empty()) // No icons to fetch.
478 return;
480 int preferred_size = std::max(unprocessed_icons_.back().width,
481 unprocessed_icons_.back().height);
482 pending_download_id_ = web_contents_->DownloadImage(
483 unprocessed_icons_.back().url,
484 true, // is a favicon
485 0, // no maximum size
486 false, // normal cache policy
487 base::Bind(&CreateUrlApplicationShortcutView::DidDownloadFavicon,
488 weak_ptr_factory_.GetWeakPtr(),
489 preferred_size));
491 unprocessed_icons_.pop_back();
494 void CreateUrlApplicationShortcutView::DidDownloadFavicon(
495 int requested_size,
496 int id,
497 int http_status_code,
498 const GURL& image_url,
499 const std::vector<SkBitmap>& bitmaps,
500 const std::vector<gfx::Size>& original_bitmap_sizes) {
501 if (id != pending_download_id_)
502 return;
503 pending_download_id_ = -1;
505 // Can happen if the dialog has already been accepted.
506 if (!shortcut_info_)
507 return;
509 gfx::ImageSkia image_skia = CreateFaviconImageSkia(
510 bitmaps,
511 original_bitmap_sizes,
512 requested_size,
513 NULL);
514 if (!image_skia.isNull()) {
515 // As |shortcut_info_| will be passed to the FILE thread upon accepting the
516 // dialog, this image must be made read-only and thread-safe.
517 image_skia.MakeThreadSafe();
518 shortcut_info_->favicon.Add(image_skia);
519 static_cast<AppInfoView*>(app_info_)->UpdateIcon(shortcut_info_->favicon);
520 } else {
521 FetchIcon();
525 CreateChromeApplicationShortcutView::CreateChromeApplicationShortcutView(
526 Profile* profile,
527 const extensions::Extension* app,
528 const base::Callback<void(bool)>& close_callback)
529 : CreateApplicationShortcutView(profile),
530 close_callback_(close_callback),
531 weak_ptr_factory_(this) {
532 // Place Chrome app shortcuts in the "Chrome Apps" submenu.
533 create_in_chrome_apps_subdir_ = true;
535 InitControls(DIALOG_LAYOUT_APP_SHORTCUT);
537 // Get shortcut, icon and file handler information; they are needed for
538 // creating the shortcut.
539 web_app::GetInfoForApp(
540 app,
541 profile,
542 base::Bind(&CreateChromeApplicationShortcutView::OnAppInfoLoaded,
543 weak_ptr_factory_.GetWeakPtr()));
546 CreateChromeApplicationShortcutView::~CreateChromeApplicationShortcutView() {}
548 bool CreateChromeApplicationShortcutView::Accept() {
549 if (!close_callback_.is_null())
550 close_callback_.Run(true);
551 return CreateApplicationShortcutView::Accept();
554 bool CreateChromeApplicationShortcutView::Cancel() {
555 if (!close_callback_.is_null())
556 close_callback_.Run(false);
557 return CreateApplicationShortcutView::Cancel();
560 void CreateChromeApplicationShortcutView::OnAppInfoLoaded(
561 scoped_ptr<web_app::ShortcutInfo> shortcut_info,
562 const extensions::FileHandlersInfo& file_handlers_info) {
563 shortcut_info_ = shortcut_info.Pass();
564 file_handlers_info_ = file_handlers_info;