Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / extensions / device_permissions_dialog_controller.mm
blob6babaf28c4f369a6bcdc2f351829045c1c723057
1 // Copyright 2014 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/device_permissions_dialog_controller.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "chrome/browser/extensions/api/chrome_device_permissions_prompt.h"
12 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_sheet.h"
13 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_window.h"
14 #import "chrome/browser/ui/cocoa/extensions/device_permissions_view_controller.h"
15 #include "components/web_modal/web_contents_modal_dialog_manager.h"
16 #include "device/usb/usb_device.h"
18 using extensions::DevicePermissionsPrompt;
20 DevicePermissionsDialogController::DevicePermissionsDialogController(
21     content::WebContents* web_contents,
22     scoped_refptr<DevicePermissionsPrompt::Prompt> prompt)
23     : prompt_(prompt) {
24   view_controller_.reset(
25       [[DevicePermissionsViewController alloc] initWithController:this
26                                                            prompt:prompt]);
28   prompt_->SetObserver(this);
30   base::scoped_nsobject<NSWindow> window([[ConstrainedWindowCustomWindow alloc]
31       initWithContentRect:[[view_controller_ view] bounds]]);
32   [[window contentView] addSubview:[view_controller_ view]];
34   base::scoped_nsobject<CustomConstrainedWindowSheet> sheet(
35       [[CustomConstrainedWindowSheet alloc] initWithCustomWindow:window]);
36   constrained_window_.reset(
37       new ConstrainedWindowMac(this, web_contents, sheet));
40 DevicePermissionsDialogController::~DevicePermissionsDialogController() {
41   prompt_->SetObserver(nullptr);
44 void DevicePermissionsDialogController::Dismissed() {
45   constrained_window_->CloseWebContentsModalDialog();
48 void DevicePermissionsDialogController::OnDevicesChanged() {
49   [view_controller_ devicesChanged];
52 void DevicePermissionsDialogController::OnConstrainedWindowClosed(
53     ConstrainedWindowMac* window) {
54   prompt_->Dismissed();
55   base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this);
58 void ChromeDevicePermissionsPrompt::ShowDialog() {
59   web_modal::WebContentsModalDialogManager* manager =
60       web_modal::WebContentsModalDialogManager::FromWebContents(web_contents());
61   if (manager) {
62     // These objects will delete themselves when the dialog closes.
63     new DevicePermissionsDialogController(web_contents(), prompt());
64   } else {
65     base::ThreadTaskRunnerHandle::Get()->PostTask(
66         FROM_HERE,
67         base::Bind(&DevicePermissionsPrompt::Prompt::Dismissed, prompt()));
68   }