create_application_shortcut_view.cc revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
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.
4
5#include "chrome/browser/ui/views/create_application_shortcut_view.h"
6
7#include <algorithm>
8
9#include "base/bind.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/profiles/profile.h"
16#include "chrome/browser/ui/browser.h"
17#include "chrome/browser/ui/browser_commands.h"
18#include "chrome/browser/ui/browser_finder.h"
19#include "chrome/browser/ui/views/constrained_window_views.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 "components/favicon_base/select_favicon_frames.h"
25#include "content/public/browser/render_view_host.h"
26#include "content/public/browser/render_widget_host_view.h"
27#include "content/public/browser/web_contents.h"
28#include "extensions/common/extension.h"
29#include "grit/chromium_strings.h"
30#include "grit/generated_resources.h"
31#include "grit/locale_settings.h"
32#include "grit/theme_resources.h"
33#include "net/base/load_flags.h"
34#include "net/url_request/url_request.h"
35#include "skia/ext/image_operations.h"
36#include "third_party/skia/include/core/SkBitmap.h"
37#include "third_party/skia/include/core/SkPaint.h"
38#include "third_party/skia/include/core/SkRect.h"
39#include "ui/base/l10n/l10n_util.h"
40#include "ui/base/layout.h"
41#include "ui/base/resource/resource_bundle.h"
42#include "ui/gfx/canvas.h"
43#include "ui/gfx/codec/png_codec.h"
44#include "ui/gfx/image/image_family.h"
45#include "ui/gfx/image/image_skia.h"
46#include "ui/views/controls/button/checkbox.h"
47#include "ui/views/controls/image_view.h"
48#include "ui/views/controls/label.h"
49#include "ui/views/layout/grid_layout.h"
50#include "ui/views/layout/layout_constants.h"
51#include "ui/views/widget/widget.h"
52#include "ui/views/window/dialog_client_view.h"
53#include "url/gurl.h"
54
55namespace {
56
57const int kIconPreviewSizePixels = 32;
58
59// AppInfoView shows the application icon and title.
60class AppInfoView : public views::View {
61 public:
62  AppInfoView(const base::string16& title,
63              const base::string16& description,
64              const gfx::ImageFamily& icon);
65
66  // Updates the title/description of the web app.
67  void UpdateText(const base::string16& title,
68                  const base::string16& description);
69
70  // Updates the icon of the web app.
71  void UpdateIcon(const gfx::ImageFamily& image);
72
73  // Overridden from views::View:
74  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
75
76 private:
77  // Initializes the controls
78  void Init(const base::string16& title,
79            const base::string16& description, const gfx::ImageFamily& icon);
80
81  // Creates or updates description label.
82  void PrepareDescriptionLabel(const base::string16& description);
83
84  // Sets up layout manager.
85  void SetupLayout();
86
87  views::ImageView* icon_;
88  views::Label* title_;
89  views::Label* description_;
90};
91
92AppInfoView::AppInfoView(const base::string16& title,
93                         const base::string16& description,
94                         const gfx::ImageFamily& icon)
95    : icon_(NULL),
96      title_(NULL),
97      description_(NULL) {
98  Init(title, description, icon);
99}
100
101void AppInfoView::Init(const base::string16& title_text,
102                       const base::string16& description_text,
103                       const gfx::ImageFamily& icon) {
104  icon_ = new views::ImageView();
105  UpdateIcon(icon);
106  icon_->SetImageSize(gfx::Size(kIconPreviewSizePixels,
107                                kIconPreviewSizePixels));
108
109  title_ = new views::Label(
110      title_text,
111      ui::ResourceBundle::GetSharedInstance().GetFontList(
112          ui::ResourceBundle::BoldFont));
113  title_->SetMultiLine(true);
114  title_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
115
116  PrepareDescriptionLabel(description_text);
117
118  SetupLayout();
119}
120
121void AppInfoView::PrepareDescriptionLabel(const base::string16& description) {
122  // Do not make space for the description if it is empty.
123  if (description.empty())
124    return;
125
126  const size_t kMaxLength = 200;
127  const base::string16 kEllipsis(base::ASCIIToUTF16(" ... "));
128
129  base::string16 text = description;
130  if (text.length() > kMaxLength) {
131    text = text.substr(0, kMaxLength);
132    text += kEllipsis;
133  }
134
135  if (description_) {
136    description_->SetText(text);
137  } else {
138    description_ = new views::Label(text);
139    description_->SetMultiLine(true);
140    description_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
141  }
142}
143
144void AppInfoView::SetupLayout() {
145  views::GridLayout* layout = views::GridLayout::CreatePanel(this);
146  SetLayoutManager(layout);
147
148  static const int kColumnSetId = 0;
149  views::ColumnSet* column_set = layout->AddColumnSet(kColumnSetId);
150  column_set->AddColumn(views::GridLayout::CENTER, views::GridLayout::LEADING,
151                        20.0f, views::GridLayout::FIXED,
152                        kIconPreviewSizePixels, kIconPreviewSizePixels);
153  column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER,
154                        80.0f, views::GridLayout::USE_PREF, 0, 0);
155
156  layout->StartRow(0, kColumnSetId);
157  layout->AddView(icon_, 1, description_ ? 2 : 1);
158  layout->AddView(title_);
159
160  if (description_) {
161    layout->StartRow(0, kColumnSetId);
162    layout->SkipColumns(1);
163    layout->AddView(description_);
164  }
165}
166
167void AppInfoView::UpdateText(const base::string16& title,
168                             const base::string16& description) {
169  title_->SetText(title);
170  PrepareDescriptionLabel(description);
171
172  SetupLayout();
173}
174
175void AppInfoView::UpdateIcon(const gfx::ImageFamily& image) {
176  // Get the icon closest to the desired preview size.
177  const gfx::Image* icon = image.GetBest(kIconPreviewSizePixels,
178                                         kIconPreviewSizePixels);
179  if (!icon || icon->IsEmpty())
180    // The family has no icons. Leave the image blank.
181    return;
182  const SkBitmap& bitmap = *icon->ToSkBitmap();
183  if (bitmap.width() == kIconPreviewSizePixels &&
184      bitmap.height() == kIconPreviewSizePixels) {
185    icon_->SetImage(gfx::ImageSkia::CreateFrom1xBitmap(bitmap));
186  } else {
187    // Resize the image to the desired size.
188    SkBitmap resized_bitmap = skia::ImageOperations::Resize(
189        bitmap, skia::ImageOperations::RESIZE_LANCZOS3,
190        kIconPreviewSizePixels, kIconPreviewSizePixels);
191
192    icon_->SetImage(gfx::ImageSkia::CreateFrom1xBitmap(resized_bitmap));
193  }
194}
195
196void AppInfoView::OnPaint(gfx::Canvas* canvas) {
197  gfx::Rect bounds = GetLocalBounds();
198
199  SkRect border_rect = {
200    SkIntToScalar(bounds.x()),
201    SkIntToScalar(bounds.y()),
202    SkIntToScalar(bounds.right()),
203    SkIntToScalar(bounds.bottom())
204  };
205
206  SkPaint border_paint;
207  border_paint.setAntiAlias(true);
208  border_paint.setARGB(0xFF, 0xC8, 0xC8, 0xC8);
209
210  canvas->sk_canvas()->drawRoundRect(border_rect, SkIntToScalar(2),
211                                     SkIntToScalar(2), border_paint);
212
213  SkRect inner_rect = {
214    border_rect.fLeft + SkDoubleToScalar(0.5),
215    border_rect.fTop + SkDoubleToScalar(0.5),
216    border_rect.fRight - SkDoubleToScalar(0.5),
217    border_rect.fBottom - SkDoubleToScalar(0.5),
218  };
219
220  SkPaint inner_paint;
221  inner_paint.setAntiAlias(true);
222  inner_paint.setARGB(0xFF, 0xF8, 0xF8, 0xF8);
223  canvas->sk_canvas()->drawRoundRect(inner_rect, SkDoubleToScalar(1.5),
224                                     SkDoubleToScalar(1.5), inner_paint);
225}
226
227}  // namespace
228
229namespace chrome {
230
231void ShowCreateWebAppShortcutsDialog(gfx::NativeWindow parent_window,
232                                     content::WebContents* web_contents) {
233  CreateBrowserModalDialogViews(
234      new CreateUrlApplicationShortcutView(web_contents),
235      parent_window)->Show();
236}
237
238void ShowCreateChromeAppShortcutsDialog(
239    gfx::NativeWindow parent_window,
240    Profile* profile,
241    const extensions::Extension* app,
242    const base::Callback<void(bool)>& close_callback) {
243  CreateBrowserModalDialogViews(
244      new CreateChromeApplicationShortcutView(profile, app, close_callback),
245      parent_window)->Show();
246}
247
248}  // namespace chrome
249
250CreateApplicationShortcutView::CreateApplicationShortcutView(Profile* profile)
251    : profile_(profile),
252      app_info_(NULL),
253      create_shortcuts_label_(NULL),
254      desktop_check_box_(NULL),
255      menu_check_box_(NULL),
256      quick_launch_check_box_(NULL) {}
257
258CreateApplicationShortcutView::~CreateApplicationShortcutView() {}
259
260void CreateApplicationShortcutView::InitControls(DialogLayout dialog_layout) {
261  if (dialog_layout == DIALOG_LAYOUT_URL_SHORTCUT) {
262    app_info_ = new AppInfoView(shortcut_info_.title,
263                                shortcut_info_.description,
264                                shortcut_info_.favicon);
265  }
266  create_shortcuts_label_ = new views::Label(
267      l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_LABEL));
268  create_shortcuts_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
269
270  desktop_check_box_ = AddCheckbox(
271      l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_DESKTOP_CHKBOX),
272      profile_->GetPrefs()->GetBoolean(prefs::kWebAppCreateOnDesktop));
273
274  menu_check_box_ = NULL;
275  quick_launch_check_box_ = NULL;
276
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));
283  }
284
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
296
297  // Layout controls
298  views::GridLayout* layout = views::GridLayout::CreatePanel(this);
299  SetLayoutManager(layout);
300
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);
305
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);
311
312  if (app_info_) {
313    layout->StartRow(0, kHeaderColumnSetId);
314    layout->AddView(app_info_);
315    layout->AddPaddingRow(0, views::kPanelSubVerticalSpacing);
316  }
317
318  layout->StartRow(0, kHeaderColumnSetId);
319  layout->AddView(create_shortcuts_label_);
320
321  layout->AddPaddingRow(0, views::kLabelToControlVerticalSpacing);
322  layout->StartRow(0, kTableColumnSetId);
323  layout->AddView(desktop_check_box_);
324
325  if (menu_check_box_ != NULL) {
326    layout->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing);
327    layout->StartRow(0, kTableColumnSetId);
328    layout->AddView(menu_check_box_);
329  }
330
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_);
335  }
336}
337
338gfx::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);
344}
345
346base::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);
351}
352
353bool 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());
361
362  return true;
363}
364
365ui::ModalType CreateApplicationShortcutView::GetModalType() const {
366  return ui::MODAL_TYPE_WINDOW;
367}
368
369base::string16 CreateApplicationShortcutView::GetWindowTitle() const {
370  return l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_TITLE);
371}
372
373bool CreateApplicationShortcutView::Accept() {
374  if (!IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK))
375    return false;
376
377  web_app::ShortcutLocations creation_locations;
378  creation_locations.on_desktop = desktop_check_box_->checked();
379  if (menu_check_box_ != NULL && menu_check_box_->checked()) {
380    creation_locations.applications_menu_location =
381        create_in_chrome_apps_subdir_ ?
382            web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS :
383            web_app::APP_MENU_LOCATION_ROOT;
384  }
385
386#if defined(OS_WIN)
387  creation_locations.in_quick_launch_bar = quick_launch_check_box_ == NULL ?
388      NULL : quick_launch_check_box_->checked();
389#elif defined(OS_POSIX)
390  // Create shortcut in Mac dock or as Linux (gnome/kde) application launcher
391  // are not implemented yet.
392  creation_locations.in_quick_launch_bar = false;
393#endif
394
395  web_app::CreateShortcutsForShortcutInfo(
396      web_app::SHORTCUT_CREATION_BY_USER,
397      creation_locations,
398      shortcut_info_);
399  return true;
400}
401
402views::Checkbox* CreateApplicationShortcutView::AddCheckbox(
403    const base::string16& text, bool checked) {
404  views::Checkbox* checkbox = new views::Checkbox(text);
405  checkbox->SetChecked(checked);
406  checkbox->set_listener(this);
407  return checkbox;
408}
409
410void CreateApplicationShortcutView::ButtonPressed(views::Button* sender,
411                                                  const ui::Event& event) {
412  if (sender == desktop_check_box_) {
413    profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateOnDesktop,
414                                     desktop_check_box_->checked());
415  } else if (sender == menu_check_box_) {
416    profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateInAppsMenu,
417                                     menu_check_box_->checked());
418  } else if (sender == quick_launch_check_box_) {
419    profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateInQuickLaunchBar,
420                                     quick_launch_check_box_->checked());
421  }
422
423  // When no checkbox is checked we should not have the action button enabled.
424  GetDialogClientView()->UpdateDialogButtons();
425}
426
427CreateUrlApplicationShortcutView::CreateUrlApplicationShortcutView(
428    content::WebContents* web_contents)
429    : CreateApplicationShortcutView(
430          Profile::FromBrowserContext(web_contents->GetBrowserContext())),
431      web_contents_(web_contents),
432      pending_download_id_(-1),
433      weak_ptr_factory_(this)  {
434
435  web_app::GetShortcutInfoForTab(web_contents_, &shortcut_info_);
436  const WebApplicationInfo& app_info =
437      extensions::TabHelper::FromWebContents(web_contents_)->web_app_info();
438  if (!app_info.icons.empty()) {
439    web_app::GetIconsInfo(app_info, &unprocessed_icons_);
440    FetchIcon();
441  }
442
443  // Create URL app shortcuts in the top-level menu.
444  create_in_chrome_apps_subdir_ = false;
445
446  InitControls(DIALOG_LAYOUT_URL_SHORTCUT);
447}
448
449CreateUrlApplicationShortcutView::~CreateUrlApplicationShortcutView() {
450}
451
452bool CreateUrlApplicationShortcutView::Accept() {
453  if (!CreateApplicationShortcutView::Accept())
454    return false;
455
456  // Get the smallest icon in the icon family (should have only 1).
457  const gfx::Image* icon = shortcut_info_.favicon.GetBest(0, 0);
458  SkBitmap bitmap = icon ? icon->AsBitmap() : SkBitmap();
459  extensions::TabHelper::FromWebContents(web_contents_)->SetAppIcon(bitmap);
460  Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
461  if (browser)
462    chrome::ConvertTabToAppWindow(browser, web_contents_);
463  return true;
464}
465
466void CreateUrlApplicationShortcutView::FetchIcon() {
467  // There should only be fetch job at a time.
468  DCHECK_EQ(-1, pending_download_id_);
469
470  if (unprocessed_icons_.empty())  // No icons to fetch.
471    return;
472
473  int preferred_size = std::max(unprocessed_icons_.back().width,
474                                unprocessed_icons_.back().height);
475  pending_download_id_ = web_contents_->DownloadImage(
476      unprocessed_icons_.back().url,
477      true,  // is a favicon
478      0,  // no maximum size
479      base::Bind(&CreateUrlApplicationShortcutView::DidDownloadFavicon,
480                 weak_ptr_factory_.GetWeakPtr(),
481                 preferred_size));
482
483  unprocessed_icons_.pop_back();
484}
485
486void CreateUrlApplicationShortcutView::DidDownloadFavicon(
487    int requested_size,
488    int id,
489    int http_status_code,
490    const GURL& image_url,
491    const std::vector<SkBitmap>& bitmaps,
492    const std::vector<gfx::Size>& original_bitmap_sizes) {
493  if (id != pending_download_id_)
494    return;
495  pending_download_id_ = -1;
496
497  SkBitmap image;
498
499  if (!bitmaps.empty()) {
500    std::vector<ui::ScaleFactor> scale_factors;
501    ui::ScaleFactor scale_factor = ui::GetSupportedScaleFactor(
502        ui::GetScaleFactorForNativeView(
503            web_contents_->GetRenderViewHost()->GetView()->GetNativeView()));
504    scale_factors.push_back(scale_factor);
505    std::vector<size_t> closest_indices;
506    SelectFaviconFrameIndices(original_bitmap_sizes,
507                              scale_factors,
508                              requested_size,
509                              &closest_indices,
510                              NULL);
511    size_t closest_index = closest_indices[0];
512    image = bitmaps[closest_index];
513  }
514
515  if (!image.isNull()) {
516    shortcut_info_.favicon.Add(gfx::ImageSkia::CreateFrom1xBitmap(image));
517    static_cast<AppInfoView*>(app_info_)->UpdateIcon(shortcut_info_.favicon);
518  } else {
519    FetchIcon();
520  }
521}
522
523CreateChromeApplicationShortcutView::CreateChromeApplicationShortcutView(
524    Profile* profile,
525    const extensions::Extension* app,
526    const base::Callback<void(bool)>& close_callback)
527        : CreateApplicationShortcutView(profile),
528          close_callback_(close_callback),
529          weak_ptr_factory_(this) {
530  // Place Chrome app shortcuts in the "Chrome Apps" submenu.
531  create_in_chrome_apps_subdir_ = true;
532
533  InitControls(DIALOG_LAYOUT_APP_SHORTCUT);
534
535  // Get shortcut information and icon; they are needed for creating the
536  // shortcut.
537  web_app::UpdateShortcutInfoAndIconForApp(
538      app,
539      profile,
540      base::Bind(&CreateChromeApplicationShortcutView::OnShortcutInfoLoaded,
541                 weak_ptr_factory_.GetWeakPtr()));
542}
543
544CreateChromeApplicationShortcutView::~CreateChromeApplicationShortcutView() {}
545
546bool CreateChromeApplicationShortcutView::Accept() {
547  if (!close_callback_.is_null())
548    close_callback_.Run(true);
549  return CreateApplicationShortcutView::Accept();
550}
551
552bool CreateChromeApplicationShortcutView::Cancel() {
553  if (!close_callback_.is_null())
554    close_callback_.Run(false);
555  return CreateApplicationShortcutView::Cancel();
556}
557
558// Called when the app's ShortcutInfo (with icon) is loaded.
559void CreateChromeApplicationShortcutView::OnShortcutInfoLoaded(
560    const web_app::ShortcutInfo& shortcut_info) {
561  shortcut_info_ = shortcut_info;
562}
563