NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / views / create_application_shortcut_view.cc
blobfca0654992b9e80512ae1f9c88f3150cc09eda6e
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>
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/views/constrained_window_views.h"
22 #include "chrome/browser/ui/web_applications/web_app_ui.h"
23 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
24 #include "chrome/common/chrome_constants.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 "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"
54 #include "url/gurl.h"
56 namespace {
58 const int kIconPreviewSizePixels = 32;
60 // AppInfoView shows the application icon and title.
61 class AppInfoView : public views::View {
62 public:
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;
77 private:
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.
86 void SetupLayout();
88 views::ImageView* icon_;
89 views::Label* title_;
90 views::Label* description_;
93 AppInfoView::AppInfoView(const base::string16& title,
94 const base::string16& description,
95 const gfx::ImageFamily& icon)
96 : icon_(NULL),
97 title_(NULL),
98 description_(NULL) {
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();
106 UpdateIcon(icon);
107 icon_->SetImageSize(gfx::Size(kIconPreviewSizePixels,
108 kIconPreviewSizePixels));
110 title_ = new views::Label(
111 title_text,
112 ui::ResourceBundle::GetSharedInstance().GetFontList(
113 ui::ResourceBundle::BoldFont));
114 title_->SetMultiLine(true);
115 title_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
117 PrepareDescriptionLabel(description_text);
119 SetupLayout();
122 void AppInfoView::PrepareDescriptionLabel(const base::string16& description) {
123 // Do not make space for the description if it is empty.
124 if (description.empty())
125 return;
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);
133 text += kEllipsis;
136 if (description_) {
137 description_->SetText(text);
138 } else {
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_);
161 if (description_) {
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);
173 SetupLayout();
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.
182 return;
183 const SkBitmap& bitmap = *icon->ToSkBitmap();
184 if (bitmap.width() == kIconPreviewSizePixels &&
185 bitmap.height() == kIconPreviewSizePixels) {
186 icon_->SetImage(gfx::ImageSkia::CreateFrom1xBitmap(bitmap));
187 } else {
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),
221 SkPaint inner_paint;
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);
228 } // namespace
230 namespace chrome {
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,
241 Profile* profile,
242 const extensions::Extension* app,
243 const base::Closure& close_callback) {
244 CreateBrowserModalDialogViews(
245 new CreateChromeApplicationShortcutView(profile, app, close_callback),
246 parent_window)->Show();
249 } // namespace chrome
251 CreateApplicationShortcutView::CreateApplicationShortcutView(Profile* profile)
252 : profile_(profile),
253 app_info_(NULL),
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() {
262 // Create controls
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;
276 #if defined(OS_WIN)
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));
294 #endif
296 // Layout controls
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() {
336 // TODO(evanm): should this use IDS_CREATE_SHORTCUTS_DIALOG_WIDTH_CHARS?
337 static const int kDialogWidth = 360;
338 int height = GetLayoutManager()->GetPreferredHeightForWidth(this,
339 kDialogWidth);
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());
359 return true;
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))
372 return false;
374 ShellIntegration::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 ShellIntegration::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS :
380 ShellIntegration::APP_MENU_LOCATION_ROOT;
383 #if defined(OS_WIN)
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;
390 #endif
392 web_app::CreateShortcuts(shortcut_info_, creation_locations,
393 web_app::SHORTCUT_CREATION_BY_USER);
394 return true;
397 views::Checkbox* CreateApplicationShortcutView::AddCheckbox(
398 const base::string16& text, bool checked) {
399 views::Checkbox* checkbox = new views::Checkbox(text);
400 checkbox->SetChecked(checked);
401 checkbox->set_listener(this);
402 return checkbox;
405 void CreateApplicationShortcutView::ButtonPressed(views::Button* sender,
406 const ui::Event& event) {
407 if (sender == desktop_check_box_) {
408 profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateOnDesktop,
409 desktop_check_box_->checked());
410 } else if (sender == menu_check_box_) {
411 profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateInAppsMenu,
412 menu_check_box_->checked());
413 } else if (sender == quick_launch_check_box_) {
414 profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateInQuickLaunchBar,
415 quick_launch_check_box_->checked());
418 // When no checkbox is checked we should not have the action button enabled.
419 GetDialogClientView()->UpdateDialogButtons();
422 CreateUrlApplicationShortcutView::CreateUrlApplicationShortcutView(
423 content::WebContents* web_contents)
424 : CreateApplicationShortcutView(
425 Profile::FromBrowserContext(web_contents->GetBrowserContext())),
426 web_contents_(web_contents),
427 pending_download_id_(-1),
428 weak_ptr_factory_(this) {
430 web_app::GetShortcutInfoForTab(web_contents_, &shortcut_info_);
431 const WebApplicationInfo& app_info =
432 extensions::TabHelper::FromWebContents(web_contents_)->web_app_info();
433 if (!app_info.icons.empty()) {
434 web_app::GetIconsInfo(app_info, &unprocessed_icons_);
435 FetchIcon();
438 // Create URL app shortcuts in the top-level menu.
439 create_in_chrome_apps_subdir_ = false;
441 InitControls();
444 CreateUrlApplicationShortcutView::~CreateUrlApplicationShortcutView() {
447 bool CreateUrlApplicationShortcutView::Accept() {
448 if (!CreateApplicationShortcutView::Accept())
449 return false;
451 // Get the smallest icon in the icon family (should have only 1).
452 const gfx::Image* icon = shortcut_info_.favicon.GetBest(0, 0);
453 SkBitmap bitmap = icon ? icon->AsBitmap() : SkBitmap();
454 extensions::TabHelper::FromWebContents(web_contents_)->SetAppIcon(bitmap);
455 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
456 if (browser)
457 chrome::ConvertTabToAppWindow(browser, web_contents_);
458 return true;
461 void CreateUrlApplicationShortcutView::FetchIcon() {
462 // There should only be fetch job at a time.
463 DCHECK_EQ(-1, pending_download_id_);
465 if (unprocessed_icons_.empty()) // No icons to fetch.
466 return;
468 int preferred_size = std::max(unprocessed_icons_.back().width,
469 unprocessed_icons_.back().height);
470 pending_download_id_ = web_contents_->DownloadImage(
471 unprocessed_icons_.back().url,
472 true, // is a favicon
473 0, // no maximum size
474 base::Bind(&CreateUrlApplicationShortcutView::DidDownloadFavicon,
475 weak_ptr_factory_.GetWeakPtr(),
476 preferred_size));
478 unprocessed_icons_.pop_back();
481 void CreateUrlApplicationShortcutView::DidDownloadFavicon(
482 int requested_size,
483 int id,
484 int http_status_code,
485 const GURL& image_url,
486 const std::vector<SkBitmap>& bitmaps,
487 const std::vector<gfx::Size>& original_bitmap_sizes) {
488 if (id != pending_download_id_)
489 return;
490 pending_download_id_ = -1;
492 SkBitmap image;
494 if (!bitmaps.empty()) {
495 std::vector<ui::ScaleFactor> scale_factors;
496 ui::ScaleFactor scale_factor = ui::GetScaleFactorForNativeView(
497 web_contents_->GetRenderViewHost()->GetView()->GetNativeView());
498 scale_factors.push_back(scale_factor);
499 std::vector<size_t> closest_indices;
500 SelectFaviconFrameIndices(original_bitmap_sizes,
501 scale_factors,
502 requested_size,
503 &closest_indices,
504 NULL);
505 size_t closest_index = closest_indices[0];
506 image = bitmaps[closest_index];
509 if (!image.isNull()) {
510 shortcut_info_.favicon.Add(gfx::ImageSkia::CreateFrom1xBitmap(image));
511 static_cast<AppInfoView*>(app_info_)->UpdateIcon(shortcut_info_.favicon);
512 } else {
513 FetchIcon();
517 CreateChromeApplicationShortcutView::CreateChromeApplicationShortcutView(
518 Profile* profile,
519 const extensions::Extension* app,
520 const base::Closure& close_callback)
521 : CreateApplicationShortcutView(profile),
522 app_(app),
523 close_callback_(close_callback),
524 weak_ptr_factory_(this) {
525 // Required by InitControls().
526 shortcut_info_.title = base::UTF8ToUTF16(app->name());
527 shortcut_info_.description = base::UTF8ToUTF16(app->description());
529 // Place Chrome app shortcuts in the "Chrome Apps" submenu.
530 create_in_chrome_apps_subdir_ = true;
532 InitControls();
534 // Get shortcut information and icon now; they are needed for our UI.
535 web_app::UpdateShortcutInfoAndIconForApp(
536 *app, profile,
537 base::Bind(&CreateChromeApplicationShortcutView::OnShortcutInfoLoaded,
538 weak_ptr_factory_.GetWeakPtr()));
541 CreateChromeApplicationShortcutView::~CreateChromeApplicationShortcutView() {}
543 bool CreateChromeApplicationShortcutView::Accept() {
544 if (!close_callback_.is_null())
545 close_callback_.Run();
546 return CreateApplicationShortcutView::Accept();
549 bool CreateChromeApplicationShortcutView::Cancel() {
550 if (!close_callback_.is_null())
551 close_callback_.Run();
552 return CreateApplicationShortcutView::Cancel();
555 // Called when the app's ShortcutInfo (with icon) is loaded.
556 void CreateChromeApplicationShortcutView::OnShortcutInfoLoaded(
557 const ShellIntegration::ShortcutInfo& shortcut_info) {
558 shortcut_info_ = shortcut_info;
560 CHECK(app_info_);
561 static_cast<AppInfoView*>(app_info_)->UpdateIcon(shortcut_info_.favicon);