1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/cocoa/browser_window_cocoa.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/logging.h"
#import "base/mac/sdk_forward_declarations.h"
#include "base/message_loop/message_loop.h"
#include "base/prefs/pref_service.h"
#include "base/strings/sys_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/download/download_shelf.h"
#include "chrome/browser/extensions/bookmark_app_helper.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/launch_util.h"
#include "chrome/browser/extensions/tab_helper.h"
#include "chrome/browser/fullscreen.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/shell_integration.h"
#include "chrome/browser/signin/signin_header_helper.h"
#include "chrome/browser/translate/chrome_translate_client.h"
#include "chrome/browser/ui/app_list/app_list_util.h"
#include "chrome/browser/ui/app_list/app_list_service.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_command_controller.h"
#include "chrome/browser/ui/browser_commands_mac.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window_state.h"
#import "chrome/browser/ui/cocoa/browser/edit_search_engine_cocoa_controller.h"
#import "chrome/browser/ui/cocoa/browser_window_controller.h"
#import "chrome/browser/ui/cocoa/browser_window_utils.h"
#import "chrome/browser/ui/cocoa/chrome_event_processing_window.h"
#import "chrome/browser/ui/cocoa/download/download_shelf_controller.h"
#include "chrome/browser/ui/cocoa/find_bar/find_bar_bridge.h"
#import "chrome/browser/ui/cocoa/info_bubble_view.h"
#include "chrome/browser/ui/cocoa/key_equivalent_constants.h"
#import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
#import "chrome/browser/ui/cocoa/nsmenuitem_additions.h"
#import "chrome/browser/ui/cocoa/profiles/avatar_base_controller.h"
#import "chrome/browser/ui/cocoa/profiles/avatar_menu_bubble_controller.h"
#include "chrome/browser/ui/cocoa/restart_browser.h"
#include "chrome/browser/ui/cocoa/status_bubble_mac.h"
#include "chrome/browser/ui/cocoa/task_manager_mac.h"
#import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h"
#import "chrome/browser/ui/cocoa/web_dialog_window_controller.h"
#import "chrome/browser/ui/cocoa/website_settings/website_settings_bubble_controller.h"
#include "chrome/browser/ui/exclusive_access/fullscreen_controller.h"
#include "chrome/browser/ui/search/search_model.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/browser/web_applications/web_app_mac.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h"
#include "components/translate/core/browser/language_state.h"
#include "content/public/browser/native_web_keyboard_event.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/pref_names.h"
#include "extensions/common/constants.h"
#include "ui/base/l10n/l10n_util_mac.h"
#include "ui/gfx/geometry/rect.h"
#if defined(ENABLE_ONE_CLICK_SIGNIN)
#import "chrome/browser/ui/cocoa/one_click_signin_bubble_controller.h"
#import "chrome/browser/ui/cocoa/one_click_signin_dialog_controller.h"
#endif
using content::NativeWebKeyboardEvent;
using content::SSLStatus;
using content::WebContents;
namespace {
// These UI constants are used in BrowserWindowCocoa::ShowBookmarkAppBubble.
// Used for defining the layout of the NSAlert and NSTextField within the
// accessory view.
const int kAppTextFieldVerticalSpacing = 2;
const int kAppTextFieldWidth = 200;
const int kAppTextFieldHeight = 22;
const int kBookmarkAppBubbleViewWidth = 200;
const int kBookmarkAppBubbleViewHeight = 46;
const int kIconPreviewTargetSize = 64;
} // namespace
BrowserWindowCocoa::BrowserWindowCocoa(Browser* browser,
BrowserWindowController* controller)
: browser_(browser),
controller_(controller),
initial_show_state_(ui::SHOW_STATE_DEFAULT),
attention_request_id_(0) {
gfx::Rect bounds;
chrome::GetSavedWindowBoundsAndShowState(browser_,
&bounds,
&initial_show_state_);
browser_->search_model()->AddObserver(this);
}
BrowserWindowCocoa::~BrowserWindowCocoa() {
browser_->search_model()->RemoveObserver(this);
}
void BrowserWindowCocoa::Show() {
// The Browser associated with this browser window must become the active
// browser at the time |Show()| is called. This is the natural behaviour under
// Windows, but |-makeKeyAndOrderFront:| won't send |-windowDidBecomeMain:|
// until we return to the runloop. Therefore any calls to
// |chrome::FindLastActiveWithHostDesktopType| will return the previous
// browser instead if we don't explicitly set it here.
BrowserList::SetLastActive(browser_);
bool is_session_restore = browser_->is_session_restore();
NSWindowAnimationBehavior saved_animation_behavior =
NSWindowAnimationBehaviorDefault;
bool did_save_animation_behavior = false;
// Turn off swishing when restoring windows or showing an app.
if ((is_session_restore || browser_->is_app()) &&
[window() respondsToSelector:@selector(animationBehavior)] &&
[window() respondsToSelector:@selector(setAnimationBehavior:)]) {
did_save_animation_behavior = true;
saved_animation_behavior = [window() animationBehavior];
[window() setAnimationBehavior:NSWindowAnimationBehaviorNone];
}
{
TRACE_EVENT0("ui", "BrowserWindowCocoa::Show makeKeyAndOrderFront");
// This call takes up a substantial part of startup time, and an even more
// substantial part of startup time when any CALayers are part of the
// window's NSView heirarchy.
[window() makeKeyAndOrderFront:controller_];
}
// When creating windows from nibs it is necessary to |makeKeyAndOrderFront:|
// prior to |orderOut:| then |miniaturize:| when restoring windows in the
// minimized state.
if (initial_show_state_ == ui::SHOW_STATE_MINIMIZED) {
[window() orderOut:controller_];
[window() miniaturize:controller_];
} else if (initial_show_state_ == ui::SHOW_STATE_FULLSCREEN) {
chrome::ToggleFullscreenWithToolbarOrFallback(browser_);
}
initial_show_state_ = ui::SHOW_STATE_DEFAULT;
// Restore window animation behavior.
if (did_save_animation_behavior)
[window() setAnimationBehavior:saved_animation_behavior];
browser_->OnWindowDidShow();
}
void BrowserWindowCocoa::ShowInactive() {
[window() orderFront:controller_];
}
void BrowserWindowCocoa::Hide() {
[window() orderOut:controller_];
}
void BrowserWindowCocoa::SetBounds(const gfx::Rect& bounds) {
gfx::Rect real_bounds = [controller_ enforceMinWindowSize:bounds];
ExitFullscreen();
NSRect cocoa_bounds = NSMakeRect(real_bounds.x(), 0,
real_bounds.width(),
real_bounds.height());
// Flip coordinates based on the primary screen.
NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
cocoa_bounds.origin.y =
NSHeight([screen frame]) - real_bounds.height() - real_bounds.y();
[window() setFrame:cocoa_bounds display:YES];
}
// Callers assume that this doesn't immediately delete the Browser object.
// The controller implementing the window delegate methods called from
// |-performClose:| must take precautions to ensure that.
void BrowserWindowCocoa::Close() {
// If there is an overlay window, we contain a tab being dragged between
// windows. Don't hide the window as it makes the UI extra confused. We can
// still close the window, as that will happen when the drag completes.
if ([controller_ overlayWindow]) {
[controller_ deferPerformClose];
} else {
// Using |-performClose:| can prevent the window from actually closing if
// a JavaScript beforeunload handler opens an alert during shutdown, as
// documented at <http://crbug.com/118424>. Re-implement
// -[NSWindow performClose:] as closely as possible to how Apple documents
// it.
//
// Before calling |-close|, hide the window immediately. |-performClose:|
// would do something similar, and this ensures that the window is removed
// from AppKit's display list. Not doing so can lead to crashes like
// <http://crbug.com/156101>.
id<NSWindowDelegate> delegate = [window() delegate];
SEL window_should_close = @selector(windowShouldClose:);
if ([delegate respondsToSelector:window_should_close]) {
if ([delegate windowShouldClose:window()]) {
[window() orderOut:nil];
[window() close];
}
} else if ([window() respondsToSelector:window_should_close]) {
if ([window() performSelector:window_should_close withObject:window()]) {
[window() orderOut:nil];
[window() close];
}
} else {
[window() orderOut:nil];
[window() close];
}
}
}
void BrowserWindowCocoa::Activate() {
[controller_ activate];
}
void BrowserWindowCocoa::Deactivate() {
// TODO(jcivelli): http://crbug.com/51364 Implement me.
NOTIMPLEMENTED();
}
void BrowserWindowCocoa::FlashFrame(bool flash) {
if (flash) {
attention_request_id_ = [NSApp requestUserAttention:NSInformationalRequest];
} else {
[NSApp cancelUserAttentionRequest:attention_request_id_];
attention_request_id_ = 0;
}
}
bool BrowserWindowCocoa::IsAlwaysOnTop() const {
return false;
}
void BrowserWindowCocoa::SetAlwaysOnTop(bool always_on_top) {
// Not implemented for browser windows.
NOTIMPLEMENTED();
}
bool BrowserWindowCocoa::IsActive() const {
return [window() isKeyWindow];
}
gfx::NativeWindow BrowserWindowCocoa::GetNativeWindow() const {
return window();
}
StatusBubble* BrowserWindowCocoa::GetStatusBubble() {
return [controller_ statusBubble];
}
void BrowserWindowCocoa::UpdateTitleBar() {
NSString* newTitle =
base::SysUTF16ToNSString(browser_->GetWindowTitleForCurrentTab());
pending_window_title_.reset(
[BrowserWindowUtils scheduleReplaceOldTitle:pending_window_title_.get()
withNewTitle:newTitle
forWindow:window()]);
}
void BrowserWindowCocoa::BookmarkBarStateChanged(
BookmarkBar::AnimateChangeType change_type) {
[[controller_ bookmarkBarController]
updateState:browser_->bookmark_bar_state()
changeType:change_type];
}
void BrowserWindowCocoa::UpdateDevTools() {
[controller_ updateDevToolsForContents:
browser_->tab_strip_model()->GetActiveWebContents()];
}
void BrowserWindowCocoa::UpdateLoadingAnimations(bool should_animate) {
// Do nothing on Mac.
}
void BrowserWindowCocoa::SetStarredState(bool is_starred) {
[controller_ setStarredState:is_starred];
}
void BrowserWindowCocoa::SetTranslateIconToggled(bool is_lit) {
[controller_ setCurrentPageIsTranslated:is_lit];
}
void BrowserWindowCocoa::OnActiveTabChanged(content::WebContents* old_contents,
content::WebContents* new_contents,
int index,
int reason) {
// TODO(pkasting): Perhaps the code in
// TabStripController::activateTabWithContents should move here? Or this
// should call that (instead of TabStripModelObserverBridge doing so)? It's
// not obvious to me why Mac doesn't handle tab changes in BrowserWindow the
// way views and GTK do.
}
void BrowserWindowCocoa::ZoomChangedForActiveTab(bool can_show_bubble) {
[controller_ zoomChangedForActiveTab:can_show_bubble ? YES : NO];
}
gfx::Rect BrowserWindowCocoa::GetRestoredBounds() const {
// Flip coordinates based on the primary screen.
NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
NSRect frame = [controller_ regularWindowFrame];
gfx::Rect bounds(frame.origin.x, 0, NSWidth(frame), NSHeight(frame));
bounds.set_y(NSHeight([screen frame]) - NSMaxY(frame));
return bounds;
}
ui::WindowShowState BrowserWindowCocoa::GetRestoredState() const {
if (IsMaximized())
return ui::SHOW_STATE_MAXIMIZED;
if (IsMinimized())
return ui::SHOW_STATE_MINIMIZED;
return ui::SHOW_STATE_NORMAL;
}
gfx::Rect BrowserWindowCocoa::GetBounds() const {
return GetRestoredBounds();
}
bool BrowserWindowCocoa::IsMaximized() const {
return [window() isZoomed];
}
bool BrowserWindowCocoa::IsMinimized() const {
return [window() isMiniaturized];
}
void BrowserWindowCocoa::Maximize() {
// Zoom toggles so only call if not already maximized.
if (!IsMaximized())
[window() zoom:controller_];
}
void BrowserWindowCocoa::Minimize() {
[window() miniaturize:controller_];
}
void BrowserWindowCocoa::Restore() {
if (IsMaximized())
[window() zoom:controller_]; // Toggles zoom mode.
else if (IsMinimized())
[window() deminiaturize:controller_];
}
// See browser_window_controller.h for a detailed explanation of the logic in
// this method.
void BrowserWindowCocoa::EnterFullscreen(const GURL& url,
ExclusiveAccessBubbleType bubble_type,
bool with_toolbar) {
if (browser_->fullscreen_controller()->IsWindowFullscreenForTabOrPending())
[controller_ enterWebContentFullscreenForURL:url bubbleType:bubble_type];
else if (!url.is_empty())
[controller_ enterExtensionFullscreenForURL:url bubbleType:bubble_type];
else
[controller_ enterBrowserFullscreenWithToolbar:with_toolbar];
}
void BrowserWindowCocoa::ExitFullscreen() {
[controller_ exitAnyFullscreen];
}
void BrowserWindowCocoa::UpdateFullscreenExitBubbleContent(
const GURL& url,
ExclusiveAccessBubbleType bubble_type) {
[controller_ updateFullscreenExitBubbleURL:url bubbleType:bubble_type];
}
bool BrowserWindowCocoa::ShouldHideUIForFullscreen() const {
// On Mac, fullscreen mode has most normal things (in a slide-down panel).
return false;
}
bool BrowserWindowCocoa::IsFullscreen() const {
return [controller_ isInAnyFullscreenMode];
}
bool BrowserWindowCocoa::IsFullscreenBubbleVisible() const {
return false;
}
bool BrowserWindowCocoa::SupportsFullscreenWithToolbar() const {
return chrome::mac::SupportsSystemFullscreen();
}
void BrowserWindowCocoa::UpdateFullscreenWithToolbar(bool with_toolbar) {
[controller_ updateFullscreenWithToolbar:with_toolbar];
}
bool BrowserWindowCocoa::IsFullscreenWithToolbar() const {
return IsFullscreen() && ![controller_ inPresentationMode];
}
void BrowserWindowCocoa::ConfirmAddSearchProvider(
TemplateURL* template_url,
Profile* profile) {
// The controller will release itself when the window closes.
EditSearchEngineCocoaController* editor =
[[EditSearchEngineCocoaController alloc] initWithProfile:profile
delegate:NULL
templateURL:template_url];
[NSApp beginSheet:[editor window]
modalForWindow:window()
modalDelegate:controller_
didEndSelector:@selector(sheetDidEnd:returnCode:context:)
contextInfo:NULL];
}
LocationBar* BrowserWindowCocoa::GetLocationBar() const {
return [controller_ locationBarBridge];
}
void BrowserWindowCocoa::SetFocusToLocationBar(bool select_all) {
[controller_ focusLocationBar:select_all ? YES : NO];
}
void BrowserWindowCocoa::UpdateReloadStopState(bool is_loading, bool force) {
[controller_ setIsLoading:is_loading force:force];
}
void BrowserWindowCocoa::UpdateToolbar(content::WebContents* contents) {
[controller_ updateToolbarWithContents:contents];
}
void BrowserWindowCocoa::ResetToolbarTabState(content::WebContents* contents) {
[controller_ resetTabState:contents];
}
void BrowserWindowCocoa::FocusToolbar() {
// Not needed on the Mac.
}
void BrowserWindowCocoa::FocusAppMenu() {
// Chrome uses the standard Mac OS X menu bar, so this isn't needed.
}
void BrowserWindowCocoa::RotatePaneFocus(bool forwards) {
// Not needed on the Mac.
}
void BrowserWindowCocoa::FocusBookmarksToolbar() {
// Not needed on the Mac.
}
void BrowserWindowCocoa::FocusInfobars() {
// Not needed on the Mac.
}
bool BrowserWindowCocoa::IsBookmarkBarVisible() const {
return browser_->profile()->GetPrefs()->GetBoolean(
bookmarks::prefs::kShowBookmarkBar);
}
bool BrowserWindowCocoa::IsBookmarkBarAnimating() const {
return [controller_ isBookmarkBarAnimating];
}
bool BrowserWindowCocoa::IsTabStripEditable() const {
return ![controller_ isDragSessionActive];
}
bool BrowserWindowCocoa::IsToolbarVisible() const {
return browser_->SupportsWindowFeature(Browser::FEATURE_TOOLBAR) ||
browser_->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR);
}
gfx::Rect BrowserWindowCocoa::GetRootWindowResizerRect() const {
if (IsDownloadShelfVisible())
return gfx::Rect();
NSRect tabRect = [controller_ selectedTabGrowBoxRect];
return gfx::Rect(NSRectToCGRect(tabRect));
}
void BrowserWindowCocoa::AddFindBar(
FindBarCocoaController* find_bar_cocoa_controller) {
[controller_ addFindBar:find_bar_cocoa_controller];
}
void BrowserWindowCocoa::ShowUpdateChromeDialog() {
restart_browser::RequestRestart(window());
}
void BrowserWindowCocoa::ShowBookmarkBubble(const GURL& url,
bool already_bookmarked) {
[controller_ showBookmarkBubbleForURL:url
alreadyBookmarked:(already_bookmarked ? YES : NO)];
}
void BrowserWindowCocoa::ShowBookmarkAppBubble(
const WebApplicationInfo& web_app_info,
const std::string& extension_id) {
Profile* profile = browser_->profile();
base::scoped_nsobject<NSAlert> alert([[NSAlert alloc] init]);
[alert setMessageText:l10n_util::GetNSString(IDS_BOOKMARK_APP_BUBBLE_TITLE)];
[alert setAlertStyle:NSInformationalAlertStyle];
NSButton* continue_button =
[alert addButtonWithTitle:l10n_util::GetNSString(IDS_OK)];
[continue_button setKeyEquivalent:kKeyEquivalentReturn];
NSButton* cancel_button =
[alert addButtonWithTitle:l10n_util::GetNSString(IDS_CANCEL)];
[cancel_button setKeyEquivalent:kKeyEquivalentEscape];
base::scoped_nsobject<NSButton> open_as_tab_checkbox(
[[NSButton alloc] initWithFrame:NSZeroRect]);
[open_as_tab_checkbox setButtonType:NSSwitchButton];
[open_as_tab_checkbox
setTitle:l10n_util::GetNSString(IDS_BOOKMARK_APP_BUBBLE_OPEN_AS_TAB)];
[open_as_tab_checkbox setState:
profile->GetPrefs()->GetInteger(
extensions::pref_names::kBookmarkAppCreationLaunchType) ==
extensions::LAUNCH_TYPE_REGULAR];
[open_as_tab_checkbox sizeToFit];
base::scoped_nsobject<NSTextField> app_title([[NSTextField alloc]
initWithFrame:NSMakeRect(0, kAppTextFieldHeight +
kAppTextFieldVerticalSpacing,
kAppTextFieldWidth, kAppTextFieldHeight)]);
NSString* original_title = SysUTF16ToNSString(web_app_info.title);
[[app_title cell] setWraps:NO];
[[app_title cell] setScrollable:YES];
[app_title setStringValue:original_title];
base::scoped_nsobject<NSView> view([[NSView alloc]
initWithFrame:NSMakeRect(0, 0, kBookmarkAppBubbleViewWidth,
kBookmarkAppBubbleViewHeight)]);
[view addSubview:open_as_tab_checkbox];
[view addSubview:app_title];
[alert setAccessoryView:view];
// Find the image with target size.
// Assumes that the icons are sorted in ascending order of size.
if (!web_app_info.icons.empty()) {
const WebApplicationInfo::IconInfo& info = web_app_info.icons.back();
gfx::Image icon_image = gfx::Image::CreateFrom1xBitmap(info.data);
[icon_image.ToNSImage()
setSize:NSMakeSize(kIconPreviewTargetSize, kIconPreviewTargetSize)];
[alert setIcon:icon_image.ToNSImage()];
}
ExtensionService* service =
extensions::ExtensionSystem::Get(profile)->extension_service();
if ([alert runModal] == NSAlertFirstButtonReturn) {
// Save launch type preferences for later when creating another hosted app.
extensions::LaunchType launch_type =
[open_as_tab_checkbox state] == NSOnState
? extensions::LAUNCH_TYPE_REGULAR
: extensions::LAUNCH_TYPE_WINDOW;
profile->GetPrefs()->SetInteger(
extensions::pref_names::kBookmarkAppCreationLaunchType, launch_type);
extensions::SetLaunchType(service, extension_id, launch_type);
// Update name of app.
NSString* new_title = [app_title stringValue];
if (![original_title isEqualToString:new_title]) {
WebApplicationInfo new_web_app_info(web_app_info);
new_web_app_info.title = base::SysNSStringToUTF16(new_title);
extensions::CreateOrUpdateBookmarkApp(service, &new_web_app_info);
}
// If we're not creating app shims, no need to reveal it in Finder.
// Otherwise reveal the app in the app launcher. If not installed,
// then open the chrome://apps page.
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableHostedAppShimCreation)) {
extensions::ExtensionRegistry* registry =
extensions::ExtensionRegistry::Get(profile);
const extensions::Extension* app = registry->GetExtensionById(
extension_id, extensions::ExtensionRegistry::ENABLED);
web_app::RevealAppShimInFinderForApp(profile, app);
} else {
if (IsAppLauncherEnabled()) {
AppListService::Get(chrome::GetHostDesktopTypeForNativeWindow(
browser_->window()->GetNativeWindow()))
->ShowForAppInstall(profile, extension_id, false);
} else {
chrome::NavigateParams params(profile, GURL(chrome::kChromeUIAppsURL),
ui::PAGE_TRANSITION_LINK);
params.disposition = NEW_FOREGROUND_TAB;
chrome::Navigate(¶ms);
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_APP_INSTALLED_TO_NTP,
content::Source<content::WebContents>(params.target_contents),
content::Details<const std::string>(&extension_id));
}
}
} else {
service->UninstallExtension(extension_id,
extensions::UNINSTALL_REASON_INSTALL_CANCELED,
base::Bind(&base::DoNothing), NULL);
}
}
void BrowserWindowCocoa::ShowTranslateBubble(
content::WebContents* contents,
translate::TranslateStep step,
translate::TranslateErrors::Type error_type,
bool is_user_gesture) {
ChromeTranslateClient* chrome_translate_client =
ChromeTranslateClient::FromWebContents(contents);
translate::LanguageState& language_state =
chrome_translate_client->GetLanguageState();
language_state.SetTranslateEnabled(true);
[controller_ showTranslateBubbleForWebContents:contents
step:step
errorType:error_type];
}
bool BrowserWindowCocoa::ShowSessionCrashedBubble() {
return false;
}
bool BrowserWindowCocoa::IsProfileResetBubbleSupported() const {
return false;
}
GlobalErrorBubbleViewBase* BrowserWindowCocoa::ShowProfileResetBubble(
const base::WeakPtr<ProfileResetGlobalError>& global_error) {
NOTREACHED();
return nullptr;
}
#if defined(ENABLE_ONE_CLICK_SIGNIN)
void BrowserWindowCocoa::ShowOneClickSigninBubble(
OneClickSigninBubbleType type,
const base::string16& email,
const base::string16& error_message,
const StartSyncCallback& start_sync_callback) {
WebContents* web_contents =
browser_->tab_strip_model()->GetActiveWebContents();
if (type == ONE_CLICK_SIGNIN_BUBBLE_TYPE_BUBBLE) {
base::scoped_nsobject<OneClickSigninBubbleController> bubble_controller([
[OneClickSigninBubbleController alloc]
initWithBrowserWindowController:cocoa_controller()
webContents:web_contents
errorMessage:base::SysUTF16ToNSString(error_message)
callback:start_sync_callback]);
[bubble_controller showWindow:nil];
} else {
// Deletes itself when the dialog closes.
new OneClickSigninDialogController(
web_contents, start_sync_callback, email);
}
}
#endif
bool BrowserWindowCocoa::IsDownloadShelfVisible() const {
return [controller_ isDownloadShelfVisible] != NO;
}
DownloadShelf* BrowserWindowCocoa::GetDownloadShelf() {
[controller_ createAndAddDownloadShelf];
DownloadShelfController* shelfController = [controller_ downloadShelf];
return [shelfController bridge];
}
// We allow closing the window here since the real quit decision on Mac is made
// in [AppController quit:].
void BrowserWindowCocoa::ConfirmBrowserCloseWithPendingDownloads(
int download_count,
Browser::DownloadClosePreventionType dialog_type,
bool app_modal,
const base::Callback<void(bool)>& callback) {
callback.Run(true);
}
void BrowserWindowCocoa::UserChangedTheme() {
[controller_ userChangedTheme];
}
int BrowserWindowCocoa::GetExtraRenderViewHeight() const {
// Currently this is only used on linux.
return 0;
}
void BrowserWindowCocoa::WebContentsFocused(WebContents* contents) {
NOTIMPLEMENTED();
}
void BrowserWindowCocoa::ShowWebsiteSettings(
Profile* profile,
content::WebContents* web_contents,
const GURL& url,
const content::SSLStatus& ssl) {
WebsiteSettingsUIBridge::Show(window(), profile, web_contents, url, ssl);
}
void BrowserWindowCocoa::ShowAppMenu() {
// No-op. Mac doesn't support showing the menus via alt keys.
}
bool BrowserWindowCocoa::PreHandleKeyboardEvent(
const NativeWebKeyboardEvent& event, bool* is_keyboard_shortcut) {
if (![BrowserWindowUtils shouldHandleKeyboardEvent:event])
return false;
if (event.type == blink::WebInputEvent::RawKeyDown &&
[controller_
handledByExtensionCommand:event.os_event
priority:ui::AcceleratorManager::kHighPriority])
return true;
int id = [BrowserWindowUtils getCommandId:event];
if (id == -1)
return false;
if (browser_->command_controller()->IsReservedCommandOrKey(id, event)) {
return [BrowserWindowUtils handleKeyboardEvent:event.os_event
inWindow:window()];
}
DCHECK(is_keyboard_shortcut);
*is_keyboard_shortcut = true;
return false;
}
void BrowserWindowCocoa::HandleKeyboardEvent(
const NativeWebKeyboardEvent& event) {
if ([BrowserWindowUtils shouldHandleKeyboardEvent:event])
[BrowserWindowUtils handleKeyboardEvent:event.os_event inWindow:window()];
}
void BrowserWindowCocoa::Cut() {
[NSApp sendAction:@selector(cut:) to:nil from:nil];
}
void BrowserWindowCocoa::Copy() {
[NSApp sendAction:@selector(copy:) to:nil from:nil];
}
void BrowserWindowCocoa::Paste() {
[NSApp sendAction:@selector(paste:) to:nil from:nil];
}
WindowOpenDisposition BrowserWindowCocoa::GetDispositionForPopupBounds(
const gfx::Rect& bounds) {
// When using Cocoa's System Fullscreen mode, convert popups into tabs.
if ([controller_ isInAppKitFullscreen])
return NEW_FOREGROUND_TAB;
return NEW_POPUP;
}
FindBar* BrowserWindowCocoa::CreateFindBar() {
// We could push the AddFindBar() call into the FindBarBridge
// constructor or the FindBarCocoaController init, but that makes
// unit testing difficult, since we would also require a
// BrowserWindow object.
FindBarBridge* bridge = new FindBarBridge(browser_);
AddFindBar(bridge->find_bar_cocoa_controller());
return bridge;
}
web_modal::WebContentsModalDialogHost*
BrowserWindowCocoa::GetWebContentsModalDialogHost() {
return NULL;
}
extensions::ActiveTabPermissionGranter*
BrowserWindowCocoa::GetActiveTabPermissionGranter() {
WebContents* web_contents =
browser_->tab_strip_model()->GetActiveWebContents();
if (!web_contents)
return NULL;
extensions::TabHelper* tab_helper =
extensions::TabHelper::FromWebContents(web_contents);
return tab_helper ? tab_helper->active_tab_permission_granter() : NULL;
}
void BrowserWindowCocoa::ModelChanged(const SearchModel::State& old_state,
const SearchModel::State& new_state) {
}
void BrowserWindowCocoa::DestroyBrowser() {
[controller_ destroyBrowser];
// at this point the controller is dead (autoreleased), so
// make sure we don't try to reference it any more.
}
NSWindow* BrowserWindowCocoa::window() const {
return [controller_ window];
}
void BrowserWindowCocoa::ShowAvatarBubbleFromAvatarButton(
AvatarBubbleMode mode,
const signin::ManageAccountsParams& manage_accounts_params) {
AvatarBaseController* controller = [controller_ avatarButtonController];
NSView* anchor = [controller buttonView];
if ([anchor isHiddenOrHasHiddenAncestor])
anchor = [[controller_ toolbarController] wrenchButton];
[controller showAvatarBubbleAnchoredAt:anchor
withMode:mode
withServiceType:manage_accounts_params.service_type];
}
int
BrowserWindowCocoa::GetRenderViewHeightInsetWithDetachedBookmarkBar() {
if (browser_->bookmark_bar_state() != BookmarkBar::DETACHED)
return 0;
return 40;
}
void BrowserWindowCocoa::ExecuteExtensionCommand(
const extensions::Extension* extension,
const extensions::Command& command) {
[cocoa_controller() executeExtensionCommand:extension->id() command:command];
}
|