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 #include "extensions/browser/api/bluetooth/bluetooth_api_pairing_delegate.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/values.h"
9 #include "content/public/browser/browser_context.h"
10 #include "device/bluetooth/bluetooth_device.h"
11 #include "extensions/browser/api/bluetooth/bluetooth_api_utils.h"
12 #include "extensions/browser/event_router.h"
13 #include "extensions/browser/extension_system.h"
14 #include "extensions/common/api/bluetooth_private.h"
16 namespace extensions
{
18 namespace bt_private
= core_api::bluetooth_private
;
22 void PopulatePairingEvent(const device::BluetoothDevice
* device
,
23 bt_private::PairingEventType type
,
24 bt_private::PairingEvent
* out
) {
25 core_api::bluetooth::BluetoothDeviceToApiDevice(*device
, &out
->device
);
31 BluetoothApiPairingDelegate::BluetoothApiPairingDelegate(
32 const std::string
& extension_id
,
33 content::BrowserContext
* browser_context
)
34 : extension_id_(extension_id
), browser_context_(browser_context
) {}
36 BluetoothApiPairingDelegate::~BluetoothApiPairingDelegate() {}
38 void BluetoothApiPairingDelegate::RequestPinCode(
39 device::BluetoothDevice
* device
) {
40 bt_private::PairingEvent event
;
42 device
, bt_private::PAIRING_EVENT_TYPE_REQUESTPINCODE
, &event
);
43 DispatchPairingEvent(event
);
46 void BluetoothApiPairingDelegate::RequestPasskey(
47 device::BluetoothDevice
* device
) {
48 bt_private::PairingEvent event
;
50 device
, bt_private::PAIRING_EVENT_TYPE_REQUESTPASSKEY
, &event
);
51 DispatchPairingEvent(event
);
54 void BluetoothApiPairingDelegate::DisplayPinCode(
55 device::BluetoothDevice
* device
,
56 const std::string
& pincode
) {
57 bt_private::PairingEvent event
;
59 device
, bt_private::PAIRING_EVENT_TYPE_DISPLAYPINCODE
, &event
);
60 event
.pincode
.reset(new std::string(pincode
));
61 DispatchPairingEvent(event
);
64 void BluetoothApiPairingDelegate::DisplayPasskey(
65 device::BluetoothDevice
* device
,
67 bt_private::PairingEvent event
;
69 device
, bt_private::PAIRING_EVENT_TYPE_DISPLAYPASSKEY
, &event
);
70 event
.passkey
.reset(new int(passkey
));
71 DispatchPairingEvent(event
);
74 void BluetoothApiPairingDelegate::KeysEntered(device::BluetoothDevice
* device
,
76 bt_private::PairingEvent event
;
78 device
, bt_private::PAIRING_EVENT_TYPE_KEYSENTERED
, &event
);
79 event
.entered_key
.reset(new int(entered
));
80 DispatchPairingEvent(event
);
83 void BluetoothApiPairingDelegate::ConfirmPasskey(
84 device::BluetoothDevice
* device
,
86 bt_private::PairingEvent event
;
88 device
, bt_private::PAIRING_EVENT_TYPE_CONFIRMPASSKEY
, &event
);
89 event
.passkey
.reset(new int(passkey
));
90 DispatchPairingEvent(event
);
93 void BluetoothApiPairingDelegate::AuthorizePairing(
94 device::BluetoothDevice
* device
) {
95 bt_private::PairingEvent event
;
97 device
, bt_private::PAIRING_EVENT_TYPE_REQUESTAUTHORIZATION
, &event
);
98 DispatchPairingEvent(event
);
101 void BluetoothApiPairingDelegate::DispatchPairingEvent(
102 const bt_private::PairingEvent
& pairing_event
) {
103 scoped_ptr
<base::ListValue
> args
=
104 bt_private::OnPairing::Create(pairing_event
);
105 scoped_ptr
<Event
> event(
106 new Event(bt_private::OnPairing::kEventName
, args
.Pass()));
107 EventRouter::Get(browser_context_
)
108 ->DispatchEventToExtension(extension_id_
, event
.Pass());
111 } // namespace extensions