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 #include "chrome/browser/extensions/extension_install_prompt_show_params.h"
11 #include "chrome/browser/profiles/profile.h"
12 #import "chrome/browser/ui/cocoa/extensions/extension_install_view_controller.h"
13 #include "content/public/browser/web_contents.h"
14 #include "ui/base/cocoa/window_size_constants.h"
16 @interface WindowedInstallController
17 : NSWindowController<NSWindowDelegate> {
19 base::scoped_nsobject<ExtensionInstallViewController> installViewController_;
20 WindowedInstallDialogController* dialogController_; // Weak. Owns us.
23 @property(readonly, nonatomic) ExtensionInstallViewController* viewController;
25 - (id)initWithProfile:(Profile*)profile
26 navigator:(content::PageNavigator*)navigator
27 delegate:(WindowedInstallDialogController*)delegate
28 prompt:(scoped_refptr<ExtensionInstallPrompt::Prompt>)prompt;
32 WindowedInstallDialogController::WindowedInstallDialogController(
33 ExtensionInstallPromptShowParams* show_params,
34 ExtensionInstallPrompt::Delegate* delegate,
35 scoped_refptr<ExtensionInstallPrompt::Prompt> prompt)
36 : delegate_(delegate) {
37 install_controller_.reset([[WindowedInstallController alloc]
38 initWithProfile:show_params->profile()
39 navigator:show_params->GetParentWebContents()
42 [[install_controller_ window] makeKeyAndOrderFront:nil];
45 WindowedInstallDialogController::~WindowedInstallDialogController() {
46 DCHECK(!install_controller_);
50 void WindowedInstallDialogController::OnWindowClosing() {
51 install_controller_.reset();
53 delegate_->InstallUIAbort(false);
56 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
59 ExtensionInstallViewController*
60 WindowedInstallDialogController::GetViewController() {
61 return [install_controller_ viewController];
64 void WindowedInstallDialogController::InstallUIProceed() {
65 delegate_->InstallUIProceed();
67 [[install_controller_ window] close];
70 void WindowedInstallDialogController::InstallUIAbort(bool user_initiated) {
71 delegate_->InstallUIAbort(user_initiated);
73 [[install_controller_ window] close];
76 @implementation WindowedInstallController
78 - (id)initWithProfile:(Profile*)profile
79 navigator:(content::PageNavigator*)navigator
80 delegate:(WindowedInstallDialogController*)delegate
81 prompt:(scoped_refptr<ExtensionInstallPrompt::Prompt>)prompt {
82 base::scoped_nsobject<NSWindow> controlledPanel(
83 [[NSPanel alloc] initWithContentRect:ui::kWindowSizeDeterminedLater
84 styleMask:NSTitledWindowMask
85 backing:NSBackingStoreBuffered
87 if ((self = [super initWithWindow:controlledPanel])) {
88 dialogController_ = delegate;
89 installViewController_.reset([[ExtensionInstallViewController alloc]
90 initWithProfile:profile
94 NSWindow* window = [self window];
96 // Ensure the window does not display behind the app launcher window, and is
97 // otherwise hard to lose behind other windows (since it is not modal).
98 [window setLevel:NSDockWindowLevel];
100 // Animate the window when ordered in, the same way as an NSAlert.
101 if ([window respondsToSelector:@selector(setAnimationBehavior:)])
102 [window setAnimationBehavior:NSWindowAnimationBehaviorAlertPanel];
104 [window setTitle:base::SysUTF16ToNSString(prompt->GetDialogTitle())];
105 NSRect viewFrame = [[installViewController_ view] frame];
106 [window setFrame:[window frameRectForContentRect:viewFrame]
108 [window setContentView:[installViewController_ view]];
109 [window setDelegate:self];
115 - (ExtensionInstallViewController*)viewController {
116 return installViewController_;
119 - (void)windowWillClose:(NSNotification*)notification {
120 [[self window] setDelegate:nil];
121 dialogController_->OnWindowClosing();