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 #import "chrome/browser/ui/cocoa/extensions/extension_install_dialog_controller.h"
8 #include "base/logging.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/message_loop/message_loop.h"
11 #include "chrome/browser/extensions/api/experience_sampling_private/experience_sampling.h"
12 #include "chrome/browser/extensions/extension_install_prompt_show_params.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_finder.h"
15 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_sheet.h"
16 #include "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_window.h"
17 #import "chrome/browser/ui/cocoa/extensions/extension_install_view_controller.h"
18 #import "chrome/browser/ui/cocoa/extensions/windowed_install_dialog_controller.h"
19 #include "chrome/browser/ui/tabs/tab_strip_model.h"
20 #include "content/public/browser/web_contents.h"
22 using extensions::ExperienceSamplingEvent;
26 void ShowExtensionInstallDialogImpl(
27 ExtensionInstallPromptShowParams* show_params,
28 ExtensionInstallPrompt::Delegate* delegate,
29 scoped_refptr<ExtensionInstallPrompt::Prompt> prompt) {
30 // These objects will delete themselves when the dialog closes.
31 if (!show_params->GetParentWebContents()) {
32 new WindowedInstallDialogController(show_params, delegate, prompt);
36 new ExtensionInstallDialogController(show_params, delegate, prompt);
41 ExtensionInstallDialogController::ExtensionInstallDialogController(
42 ExtensionInstallPromptShowParams* show_params,
43 ExtensionInstallPrompt::Delegate* delegate,
44 scoped_refptr<ExtensionInstallPrompt::Prompt> prompt)
45 : delegate_(delegate) {
46 view_controller_.reset([[ExtensionInstallViewController alloc]
47 initWithProfile:show_params->profile()
48 navigator:show_params->GetParentWebContents()
52 base::scoped_nsobject<NSWindow> window([[ConstrainedWindowCustomWindow alloc]
53 initWithContentRect:[[view_controller_ view] bounds]]);
54 [[window contentView] addSubview:[view_controller_ view]];
56 base::scoped_nsobject<CustomConstrainedWindowSheet> sheet(
57 [[CustomConstrainedWindowSheet alloc] initWithCustomWindow:window]);
58 constrained_window_.reset(new ConstrainedWindowMac(
59 this, show_params->GetParentWebContents(), sheet));
61 std::string event_name = ExperienceSamplingEvent::kExtensionInstallDialog;
62 event_name.append(ExtensionInstallPrompt::PromptTypeToString(prompt->type()));
63 sampling_event_ = ExperienceSamplingEvent::Create(event_name);
66 ExtensionInstallDialogController::~ExtensionInstallDialogController() {
69 void ExtensionInstallDialogController::InstallUIProceed() {
70 if (sampling_event_.get())
71 sampling_event_->CreateUserDecisionEvent(ExperienceSamplingEvent::kProceed);
72 delegate_->InstallUIProceed();
74 constrained_window_->CloseWebContentsModalDialog();
77 void ExtensionInstallDialogController::InstallUIAbort(bool user_initiated) {
78 if (sampling_event_.get())
79 sampling_event_->CreateUserDecisionEvent(ExperienceSamplingEvent::kDeny);
80 delegate_->InstallUIAbort(user_initiated);
82 constrained_window_->CloseWebContentsModalDialog();
85 void ExtensionInstallDialogController::OnConstrainedWindowClosed(
86 ConstrainedWindowMac* window) {
88 delegate_->InstallUIAbort(false);
89 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
93 ExtensionInstallPrompt::ShowDialogCallback
94 ExtensionInstallPrompt::GetDefaultShowDialogCallback() {
95 return base::Bind(&ShowExtensionInstallDialogImpl);