create_application_shortcut_view.cc revision 868fa2fe829687343ffae624259930155e16dbd8
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/favicon/favicon_util.h"
16#include "chrome/browser/history/select_favicon_frames.h"
17#include "chrome/browser/profiles/profile.h"
18#include "chrome/browser/ui/browser.h"
19#include "chrome/browser/ui/browser_commands.h"
20#include "chrome/browser/ui/browser_finder.h"
21#include "chrome/browser/ui/web_applications/web_app_ui.h"
22#include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
23#include "chrome/common/chrome_constants.h"
24#include "chrome/common/extensions/extension.h"
25#include "chrome/common/pref_names.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 "googleurl/src/gurl.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"
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 string16& title,
63              const string16& description,
64              const gfx::ImageFamily& icon);
65
66  // Updates the title/description of the web app.
67  void UpdateText(const string16& title, const string16& description);
68
69  // Updates the icon of the web app.
70  void UpdateIcon(const gfx::ImageFamily& image);
71
72  // Overridden from views::View:
73  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
74
75 private:
76  // Initializes the controls
77  void Init(const string16& title,
78            const string16& description, const gfx::ImageFamily& icon);
79
80  // Creates or updates description label.
81  void PrepareDescriptionLabel(const string16& description);
82
83  // Sets up layout manager.
84  void SetupLayout();
85
86  views::ImageView* icon_;
87  views::Label* title_;
88  views::Label* description_;
89};
90
91AppInfoView::AppInfoView(const string16& title,
92                         const string16& description,
93                         const gfx::ImageFamily& icon)
94    : icon_(NULL),
95      title_(NULL),
96      description_(NULL) {
97  Init(title, description, icon);
98}
99
100void AppInfoView::Init(const string16& title_text,
101                       const string16& description_text,
102                       const gfx::ImageFamily& icon) {
103  icon_ = new views::ImageView();
104  UpdateIcon(icon);
105  icon_->SetImageSize(gfx::Size(kIconPreviewSizePixels,
106                                kIconPreviewSizePixels));
107
108  title_ = new views::Label(title_text);
109  title_->SetMultiLine(true);
110  title_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
111  title_->SetFont(ui::ResourceBundle::GetSharedInstance().GetFont(
112      ui::ResourceBundle::BaseFont).DeriveFont(0, gfx::Font::BOLD));
113
114  PrepareDescriptionLabel(description_text);
115
116  SetupLayout();
117}
118
119void AppInfoView::PrepareDescriptionLabel(const string16& description) {
120  // Do not make space for the description if it is empty.
121  if (description.empty())
122    return;
123
124  const size_t kMaxLength = 200;
125  const string16 kEllipsis(ASCIIToUTF16(" ... "));
126
127  string16 text = description;
128  if (text.length() > kMaxLength) {
129    text = text.substr(0, kMaxLength);
130    text += kEllipsis;
131  }
132
133  if (description_) {
134    description_->SetText(text);
135  } else {
136    description_ = new views::Label(text);
137    description_->SetMultiLine(true);
138    description_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
139  }
140}
141
142void AppInfoView::SetupLayout() {
143  views::GridLayout* layout = views::GridLayout::CreatePanel(this);
144  SetLayoutManager(layout);
145
146  static const int kColumnSetId = 0;
147  views::ColumnSet* column_set = layout->AddColumnSet(kColumnSetId);
148  column_set->AddColumn(views::GridLayout::CENTER, views::GridLayout::LEADING,
149                        20.0f, views::GridLayout::FIXED,
150                        kIconPreviewSizePixels, kIconPreviewSizePixels);
151  column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER,
152                        80.0f, views::GridLayout::USE_PREF, 0, 0);
153
154  layout->StartRow(0, kColumnSetId);
155  layout->AddView(icon_, 1, description_ ? 2 : 1);
156  layout->AddView(title_);
157
158  if (description_) {
159    layout->StartRow(0, kColumnSetId);
160    layout->SkipColumns(1);
161    layout->AddView(description_);
162  }
163}
164
165void AppInfoView::UpdateText(const string16& title,
166                             const string16& description) {
167  title_->SetText(title);
168  PrepareDescriptionLabel(description);
169
170  SetupLayout();
171}
172
173void AppInfoView::UpdateIcon(const gfx::ImageFamily& image) {
174  // Get the icon closest to the desired preview size.
175  const gfx::Image* icon = image.GetBest(kIconPreviewSizePixels,
176                                         kIconPreviewSizePixels);
177  if (!icon || icon->IsEmpty())
178    // The family has no icons. Leave the image blank.
179    return;
180  const SkBitmap& bitmap = *icon->ToSkBitmap();
181  if (bitmap.width() == kIconPreviewSizePixels &&
182      bitmap.height() == kIconPreviewSizePixels) {
183    icon_->SetImage(gfx::ImageSkia::CreateFrom1xBitmap(bitmap));
184  } else {
185    // Resize the image to the desired size.
186    SkBitmap resized_bitmap = skia::ImageOperations::Resize(
187        bitmap, skia::ImageOperations::RESIZE_LANCZOS3,
188        kIconPreviewSizePixels, kIconPreviewSizePixels);
189
190    icon_->SetImage(gfx::ImageSkia::CreateFrom1xBitmap(resized_bitmap));
191  }
192}
193
194void AppInfoView::OnPaint(gfx::Canvas* canvas) {
195  gfx::Rect bounds = GetLocalBounds();
196
197  SkRect border_rect = {
198    SkIntToScalar(bounds.x()),
199    SkIntToScalar(bounds.y()),
200    SkIntToScalar(bounds.right()),
201    SkIntToScalar(bounds.bottom())
202  };
203
204  SkPaint border_paint;
205  border_paint.setAntiAlias(true);
206  border_paint.setARGB(0xFF, 0xC8, 0xC8, 0xC8);
207
208  canvas->sk_canvas()->drawRoundRect(border_rect, SkIntToScalar(2),
209                                     SkIntToScalar(2), border_paint);
210
211  SkRect inner_rect = {
212    border_rect.fLeft + SkDoubleToScalar(0.5),
213    border_rect.fTop + SkDoubleToScalar(0.5),
214    border_rect.fRight - SkDoubleToScalar(0.5),
215    border_rect.fBottom - SkDoubleToScalar(0.5),
216  };
217
218  SkPaint inner_paint;
219  inner_paint.setAntiAlias(true);
220  inner_paint.setARGB(0xFF, 0xF8, 0xF8, 0xF8);
221  canvas->sk_canvas()->drawRoundRect(inner_rect, SkDoubleToScalar(1.5),
222                                     SkDoubleToScalar(1.5), inner_paint);
223}
224
225}  // namespace
226
227namespace chrome {
228
229void ShowCreateWebAppShortcutsDialog(gfx::NativeWindow parent_window,
230                                     content::WebContents* web_contents) {
231  views::DialogDelegate::CreateDialogWidget(
232      new CreateUrlApplicationShortcutView(web_contents),
233      NULL, parent_window)->Show();
234}
235
236void ShowCreateChromeAppShortcutsDialog(gfx::NativeWindow parent_window,
237                                        Profile* profile,
238                                        const extensions::Extension* app) {
239  views::DialogDelegate::CreateDialogWidget(
240      new CreateChromeApplicationShortcutView(profile, app),
241      NULL, parent_window)->Show();
242}
243
244}  // namespace chrome
245
246CreateApplicationShortcutView::CreateApplicationShortcutView(Profile* profile)
247    : profile_(profile),
248      app_info_(NULL),
249      create_shortcuts_label_(NULL),
250      desktop_check_box_(NULL),
251      menu_check_box_(NULL),
252      quick_launch_check_box_(NULL) {}
253
254CreateApplicationShortcutView::~CreateApplicationShortcutView() {}
255
256void CreateApplicationShortcutView::InitControls() {
257  // Create controls
258  app_info_ = new AppInfoView(shortcut_info_.title, shortcut_info_.description,
259                              shortcut_info_.favicon);
260  create_shortcuts_label_ = new views::Label(
261      l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_LABEL));
262  create_shortcuts_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
263
264  desktop_check_box_ = AddCheckbox(
265      l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_DESKTOP_CHKBOX),
266      profile_->GetPrefs()->GetBoolean(prefs::kWebAppCreateOnDesktop));
267
268  menu_check_box_ = NULL;
269  quick_launch_check_box_ = NULL;
270
271#if defined(OS_WIN)
272  // Do not allow creating shortcuts on the Start Screen for Windows 8.
273  if (base::win::GetVersion() < base::win::VERSION_WIN8) {
274    menu_check_box_ = AddCheckbox(
275        l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_START_MENU_CHKBOX),
276        profile_->GetPrefs()->GetBoolean(prefs::kWebAppCreateInAppsMenu));
277  }
278
279  quick_launch_check_box_ = AddCheckbox(
280      (base::win::GetVersion() >= base::win::VERSION_WIN7) ?
281        l10n_util::GetStringUTF16(IDS_PIN_TO_TASKBAR_CHKBOX) :
282        l10n_util::GetStringUTF16(
283            IDS_CREATE_SHORTCUTS_QUICK_LAUNCH_BAR_CHKBOX),
284      profile_->GetPrefs()->GetBoolean(prefs::kWebAppCreateInQuickLaunchBar));
285#elif defined(OS_POSIX)
286  menu_check_box_ = AddCheckbox(
287      l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_MENU_CHKBOX),
288      profile_->GetPrefs()->GetBoolean(prefs::kWebAppCreateInAppsMenu));
289#endif
290
291  // Layout controls
292  views::GridLayout* layout = views::GridLayout::CreatePanel(this);
293  SetLayoutManager(layout);
294
295  static const int kHeaderColumnSetId = 0;
296  views::ColumnSet* column_set = layout->AddColumnSet(kHeaderColumnSetId);
297  column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER,
298                        100.0f, views::GridLayout::FIXED, 0, 0);
299
300  static const int kTableColumnSetId = 1;
301  column_set = layout->AddColumnSet(kTableColumnSetId);
302  column_set->AddPaddingColumn(0, views::kPanelHorizIndentation);
303  column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL,
304                        100.0f, views::GridLayout::USE_PREF, 0, 0);
305
306  layout->StartRow(0, kHeaderColumnSetId);
307  layout->AddView(app_info_);
308
309  layout->AddPaddingRow(0, views::kPanelSubVerticalSpacing);
310  layout->StartRow(0, kHeaderColumnSetId);
311  layout->AddView(create_shortcuts_label_);
312
313  layout->AddPaddingRow(0, views::kLabelToControlVerticalSpacing);
314  layout->StartRow(0, kTableColumnSetId);
315  layout->AddView(desktop_check_box_);
316
317  if (menu_check_box_ != NULL) {
318    layout->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing);
319    layout->StartRow(0, kTableColumnSetId);
320    layout->AddView(menu_check_box_);
321  }
322
323  if (quick_launch_check_box_ != NULL) {
324    layout->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing);
325    layout->StartRow(0, kTableColumnSetId);
326    layout->AddView(quick_launch_check_box_);
327  }
328}
329
330gfx::Size CreateApplicationShortcutView::GetPreferredSize() {
331  // TODO(evanm): should this use IDS_CREATE_SHORTCUTS_DIALOG_WIDTH_CHARS?
332  static const int kDialogWidth = 360;
333  int height = GetLayoutManager()->GetPreferredHeightForWidth(this,
334      kDialogWidth);
335  return gfx::Size(kDialogWidth, height);
336}
337
338string16 CreateApplicationShortcutView::GetDialogButtonLabel(
339    ui::DialogButton button) const {
340  if (button == ui::DIALOG_BUTTON_OK)
341    return l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_COMMIT);
342  return views::DialogDelegateView::GetDialogButtonLabel(button);
343}
344
345bool CreateApplicationShortcutView::IsDialogButtonEnabled(
346    ui::DialogButton button) const {
347  if (button == ui::DIALOG_BUTTON_OK)
348    return desktop_check_box_->checked() ||
349           ((menu_check_box_ != NULL) &&
350            menu_check_box_->checked()) ||
351           ((quick_launch_check_box_ != NULL) &&
352            quick_launch_check_box_->checked());
353
354  return true;
355}
356
357ui::ModalType CreateApplicationShortcutView::GetModalType() const {
358  return ui::MODAL_TYPE_WINDOW;
359}
360
361string16 CreateApplicationShortcutView::GetWindowTitle() const {
362  return l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_TITLE);
363}
364
365bool CreateApplicationShortcutView::Accept() {
366  if (!IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK))
367    return false;
368
369  ShellIntegration::ShortcutLocations creation_locations;
370  creation_locations.on_desktop = desktop_check_box_->checked();
371  creation_locations.in_applications_menu = menu_check_box_ == NULL ? false :
372      menu_check_box_->checked();
373  creation_locations.applications_menu_subdir = shortcut_menu_subdir_;
374
375#if defined(OS_WIN)
376  creation_locations.in_quick_launch_bar = quick_launch_check_box_ == NULL ?
377      NULL : quick_launch_check_box_->checked();
378#elif defined(OS_POSIX)
379  // Create shortcut in Mac dock or as Linux (gnome/kde) application launcher
380  // are not implemented yet.
381  creation_locations.in_quick_launch_bar = false;
382#endif
383
384  web_app::CreateShortcuts(shortcut_info_, creation_locations);
385  return true;
386}
387
388views::Checkbox* CreateApplicationShortcutView::AddCheckbox(
389    const string16& text, bool checked) {
390  views::Checkbox* checkbox = new views::Checkbox(text);
391  checkbox->SetChecked(checked);
392  checkbox->set_listener(this);
393  return checkbox;
394}
395
396void CreateApplicationShortcutView::ButtonPressed(views::Button* sender,
397                                                  const ui::Event& event) {
398  if (sender == desktop_check_box_) {
399    profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateOnDesktop,
400                                     desktop_check_box_->checked());
401  } else if (sender == menu_check_box_) {
402    profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateInAppsMenu,
403                                     menu_check_box_->checked());
404  } else if (sender == quick_launch_check_box_) {
405    profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateInQuickLaunchBar,
406                                     quick_launch_check_box_->checked());
407  }
408
409  // When no checkbox is checked we should not have the action button enabled.
410  GetDialogClientView()->UpdateDialogButtons();
411}
412
413CreateUrlApplicationShortcutView::CreateUrlApplicationShortcutView(
414    content::WebContents* web_contents)
415    : CreateApplicationShortcutView(
416          Profile::FromBrowserContext(web_contents->GetBrowserContext())),
417      web_contents_(web_contents),
418      pending_download_id_(-1)  {
419
420  web_app::GetShortcutInfoForTab(web_contents_, &shortcut_info_);
421  const WebApplicationInfo& app_info =
422      extensions::TabHelper::FromWebContents(web_contents_)->web_app_info();
423  if (!app_info.icons.empty()) {
424    web_app::GetIconsInfo(app_info, &unprocessed_icons_);
425    FetchIcon();
426  }
427
428  // NOTE: Leave shortcut_menu_subdir_ blank to create URL app shortcuts in the
429  // top-level menu.
430
431  InitControls();
432}
433
434CreateUrlApplicationShortcutView::~CreateUrlApplicationShortcutView() {
435}
436
437bool CreateUrlApplicationShortcutView::Accept() {
438  if (!CreateApplicationShortcutView::Accept())
439    return false;
440
441  // Get the smallest icon in the icon family (should have only 1).
442  const gfx::Image* icon = shortcut_info_.favicon.GetBest(0, 0);
443  SkBitmap bitmap = icon ? icon->AsBitmap() : SkBitmap();
444  extensions::TabHelper::FromWebContents(web_contents_)->SetAppIcon(bitmap);
445  Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
446  if (browser)
447    chrome::ConvertTabToAppWindow(browser, web_contents_);
448  return true;
449}
450
451void CreateUrlApplicationShortcutView::FetchIcon() {
452  // There should only be fetch job at a time.
453  DCHECK_EQ(-1, pending_download_id_);
454
455  if (unprocessed_icons_.empty())  // No icons to fetch.
456    return;
457
458  pending_download_id_ = web_contents_->DownloadImage(
459      unprocessed_icons_.back().url,
460      true,
461      std::max(unprocessed_icons_.back().width,
462               unprocessed_icons_.back().height),
463      base::Bind(&CreateUrlApplicationShortcutView::DidDownloadFavicon,
464                 base::Unretained(this)));
465
466  unprocessed_icons_.pop_back();
467}
468
469void CreateUrlApplicationShortcutView::DidDownloadFavicon(
470    int id,
471    int http_status_code,
472    const GURL& image_url,
473    int requested_size,
474    const std::vector<SkBitmap>& bitmaps) {
475  if (id != pending_download_id_)
476    return;
477  pending_download_id_ = -1;
478
479  SkBitmap image;
480
481  if (!bitmaps.empty()) {
482    std::vector<ui::ScaleFactor> scale_factors;
483    ui::ScaleFactor scale_factor = ui::GetScaleFactorForNativeView(
484        web_contents_->GetRenderViewHost()->GetView()->GetNativeView());
485    scale_factors.push_back(scale_factor);
486    size_t closest_index = FaviconUtil::SelectBestFaviconFromBitmaps(
487        bitmaps,
488        scale_factors,
489        requested_size);
490    image = bitmaps[closest_index];
491  }
492
493  if (!image.isNull()) {
494    shortcut_info_.favicon.Add(gfx::ImageSkia::CreateFrom1xBitmap(image));
495    static_cast<AppInfoView*>(app_info_)->UpdateIcon(shortcut_info_.favicon);
496  } else {
497    FetchIcon();
498  }
499}
500
501CreateChromeApplicationShortcutView::CreateChromeApplicationShortcutView(
502    Profile* profile,
503    const extensions::Extension* app) :
504      CreateApplicationShortcutView(profile),
505      app_(app),
506      weak_ptr_factory_(this) {
507  // Required by InitControls().
508  shortcut_info_.title = UTF8ToUTF16(app->name());
509  shortcut_info_.description = UTF8ToUTF16(app->description());
510
511  // Place Chrome app shortcuts in the "Chrome Apps" submenu.
512  shortcut_menu_subdir_ = web_app::GetAppShortcutsSubdirName();
513
514  InitControls();
515
516  // Get shortcut information and icon now; they are needed for our UI.
517  web_app::UpdateShortcutInfoAndIconForApp(
518      *app, profile,
519      base::Bind(&CreateChromeApplicationShortcutView::OnShortcutInfoLoaded,
520                 weak_ptr_factory_.GetWeakPtr()));
521}
522
523CreateChromeApplicationShortcutView::~CreateChromeApplicationShortcutView() {}
524
525// Called when the app's ShortcutInfo (with icon) is loaded.
526void CreateChromeApplicationShortcutView::OnShortcutInfoLoaded(
527    const ShellIntegration::ShortcutInfo& shortcut_info) {
528  shortcut_info_ = shortcut_info;
529
530  CHECK(app_info_);
531  static_cast<AppInfoView*>(app_info_)->UpdateIcon(shortcut_info_.favicon);
532}
533