• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_pairing_delegate.h"
6 
7 #include "base/memory/scoped_ptr.h"
8 #include "base/values.h"
9 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h"
10 #include "chrome/common/extensions/api/bluetooth_private.h"
11 #include "content/public/browser/browser_context.h"
12 #include "device/bluetooth/bluetooth_device.h"
13 #include "extensions/browser/event_router.h"
14 #include "extensions/browser/extension_system.h"
15 
16 namespace extensions {
17 
18 namespace bt_private = api::bluetooth_private;
19 
20 namespace {
21 
PopulatePairingEvent(const device::BluetoothDevice * device,bt_private::PairingEventType type,bt_private::PairingEvent * out)22 void PopulatePairingEvent(const device::BluetoothDevice* device,
23                           bt_private::PairingEventType type,
24                           bt_private::PairingEvent* out) {
25   api::bluetooth::BluetoothDeviceToApiDevice(*device, &out->device);
26   out->pairing = type;
27 }
28 
29 }  // namespace
30 
BluetoothApiPairingDelegate(const std::string & extension_id,content::BrowserContext * browser_context)31 BluetoothApiPairingDelegate::BluetoothApiPairingDelegate(
32     const std::string& extension_id,
33     content::BrowserContext* browser_context)
34     : extension_id_(extension_id), browser_context_(browser_context) {}
35 
~BluetoothApiPairingDelegate()36 BluetoothApiPairingDelegate::~BluetoothApiPairingDelegate() {}
37 
RequestPinCode(device::BluetoothDevice * device)38 void BluetoothApiPairingDelegate::RequestPinCode(
39     device::BluetoothDevice* device) {
40   bt_private::PairingEvent event;
41   PopulatePairingEvent(
42       device, bt_private::PAIRING_EVENT_TYPE_REQUESTPINCODE, &event);
43   DispatchPairingEvent(event);
44 }
45 
RequestPasskey(device::BluetoothDevice * device)46 void BluetoothApiPairingDelegate::RequestPasskey(
47     device::BluetoothDevice* device) {
48   bt_private::PairingEvent event;
49   PopulatePairingEvent(
50       device, bt_private::PAIRING_EVENT_TYPE_REQUESTPASSKEY, &event);
51   DispatchPairingEvent(event);
52 }
53 
DisplayPinCode(device::BluetoothDevice * device,const std::string & pincode)54 void BluetoothApiPairingDelegate::DisplayPinCode(
55     device::BluetoothDevice* device,
56     const std::string& pincode) {
57   bt_private::PairingEvent event;
58   PopulatePairingEvent(
59       device, bt_private::PAIRING_EVENT_TYPE_DISPLAYPINCODE, &event);
60   event.pincode.reset(new std::string(pincode));
61   DispatchPairingEvent(event);
62 }
63 
DisplayPasskey(device::BluetoothDevice * device,uint32 passkey)64 void BluetoothApiPairingDelegate::DisplayPasskey(
65     device::BluetoothDevice* device,
66     uint32 passkey) {
67   bt_private::PairingEvent event;
68   PopulatePairingEvent(
69       device, bt_private::PAIRING_EVENT_TYPE_DISPLAYPASSKEY, &event);
70   event.passkey.reset(new int(passkey));
71   DispatchPairingEvent(event);
72 }
73 
KeysEntered(device::BluetoothDevice * device,uint32 entered)74 void BluetoothApiPairingDelegate::KeysEntered(device::BluetoothDevice* device,
75                                               uint32 entered) {
76   bt_private::PairingEvent event;
77   PopulatePairingEvent(
78       device, bt_private::PAIRING_EVENT_TYPE_KEYSENTERED, &event);
79   event.entered_key.reset(new int(entered));
80   DispatchPairingEvent(event);
81 }
82 
ConfirmPasskey(device::BluetoothDevice * device,uint32 passkey)83 void BluetoothApiPairingDelegate::ConfirmPasskey(
84     device::BluetoothDevice* device,
85     uint32 passkey) {
86   bt_private::PairingEvent event;
87   PopulatePairingEvent(
88       device, bt_private::PAIRING_EVENT_TYPE_CONFIRMPASSKEY, &event);
89   event.passkey.reset(new int(passkey));
90   DispatchPairingEvent(event);
91 }
92 
AuthorizePairing(device::BluetoothDevice * device)93 void BluetoothApiPairingDelegate::AuthorizePairing(
94     device::BluetoothDevice* device) {
95   bt_private::PairingEvent event;
96   PopulatePairingEvent(
97       device, bt_private::PAIRING_EVENT_TYPE_REQUESTAUTHORIZATION, &event);
98   DispatchPairingEvent(event);
99 }
100 
DispatchPairingEvent(const bt_private::PairingEvent & pairing_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());
109 }
110 
111 }  // namespace extensions
112