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