1 // Copyright 2013 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 #import "chrome/browser/ui/cocoa/extensions/windowed_install_dialog_controller.h"
7 #import "base/mac/sdk_forward_declarations.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/sys_string_conversions.h"
10 #import "chrome/browser/ui/cocoa/extensions/extension_install_view_controller.h"
11 #include "ui/base/cocoa/window_size_constants.h"
13 @interface WindowedInstallController
14 : NSWindowController<NSWindowDelegate> {
16 base::scoped_nsobject<ExtensionInstallViewController> installViewController_;
17 WindowedInstallDialogController* dialogController_; // Weak. Owns us.
20 @property(readonly, nonatomic) ExtensionInstallViewController* viewController;
22 - (id)initWithNavigator:(content::PageNavigator*)navigator
23 delegate:(WindowedInstallDialogController*)delegate
24 prompt:(const ExtensionInstallPrompt::Prompt&)prompt;
28 WindowedInstallDialogController::WindowedInstallDialogController(
29 const ExtensionInstallPrompt::ShowParams& show_params,
30 ExtensionInstallPrompt::Delegate* delegate,
31 const ExtensionInstallPrompt::Prompt& prompt)
32 : delegate_(delegate) {
33 install_controller_.reset([[WindowedInstallController alloc]
34 initWithNavigator:show_params.navigator
37 [[install_controller_ window] makeKeyAndOrderFront:nil];
40 WindowedInstallDialogController::~WindowedInstallDialogController() {
41 DCHECK(!install_controller_);
45 void WindowedInstallDialogController::OnWindowClosing() {
46 install_controller_.reset();
48 delegate_->InstallUIAbort(false);
51 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
54 ExtensionInstallViewController*
55 WindowedInstallDialogController::GetViewController() {
56 return [install_controller_ viewController];
59 void WindowedInstallDialogController::InstallUIProceed() {
60 delegate_->InstallUIProceed();
62 [[install_controller_ window] close];
65 void WindowedInstallDialogController::InstallUIAbort(bool user_initiated) {
66 delegate_->InstallUIAbort(user_initiated);
68 [[install_controller_ window] close];
71 @implementation WindowedInstallController
73 - (id)initWithNavigator:(content::PageNavigator*)navigator
74 delegate:(WindowedInstallDialogController*)delegate
75 prompt:(const ExtensionInstallPrompt::Prompt&)prompt {
76 base::scoped_nsobject<NSWindow> controlledPanel(
77 [[NSPanel alloc] initWithContentRect:ui::kWindowSizeDeterminedLater
78 styleMask:NSTitledWindowMask
79 backing:NSBackingStoreBuffered
81 if ((self = [super initWithWindow:controlledPanel])) {
82 dialogController_ = delegate;
83 installViewController_.reset([[ExtensionInstallViewController alloc]
84 initWithNavigator:navigator
87 NSWindow* window = [self window];
89 // Ensure the window does not display behind the app launcher window, and is
90 // otherwise hard to lose behind other windows (since it is not modal).
91 [window setLevel:NSDockWindowLevel];
93 // Animate the window when ordered in, the same way as an NSAlert.
94 if ([window respondsToSelector:@selector(setAnimationBehavior:)])
95 [window setAnimationBehavior:NSWindowAnimationBehaviorAlertPanel];
97 [window setTitle:base::SysUTF16ToNSString(prompt.GetDialogTitle())];
98 NSRect viewFrame = [[installViewController_ view] frame];
99 [window setFrame:[window frameRectForContentRect:viewFrame]
101 [window setContentView:[installViewController_ view]];
102 [window setDelegate:self];
108 - (ExtensionInstallViewController*)viewController {
109 return installViewController_;
112 - (void)windowWillClose:(NSNotification*)notification {
113 [[self window] setDelegate:nil];
114 dialogController_->OnWindowClosing();