OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h" | 5 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h" |
6 | 6 |
7 #if defined(OS_CHROMEOS) | 7 #if defined(OS_CHROMEOS) |
8 #include <errno.h> | 8 #include <errno.h> |
9 #endif | 9 #endif |
10 | 10 |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_factory.h" | |
14 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" | 15 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" |
15 #include "chrome/browser/extensions/bluetooth_event_router.h" | 16 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" |
16 #include "chrome/browser/extensions/event_names.h" | 17 #include "chrome/browser/extensions/event_names.h" |
17 #include "chrome/browser/extensions/event_router.h" | 18 #include "chrome/browser/extensions/event_router.h" |
18 #include "chrome/browser/extensions/extension_service.h" | |
19 #include "chrome/browser/extensions/extension_system.h" | 19 #include "chrome/browser/extensions/extension_system.h" |
20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/common/extensions/api/bluetooth.h" | 21 #include "chrome/common/extensions/api/bluetooth.h" |
22 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
23 #include "device/bluetooth/bluetooth_adapter.h" | 23 #include "device/bluetooth/bluetooth_adapter.h" |
24 #include "device/bluetooth/bluetooth_device.h" | 24 #include "device/bluetooth/bluetooth_device.h" |
25 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" | 25 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" |
26 #include "device/bluetooth/bluetooth_service_record.h" | 26 #include "device/bluetooth/bluetooth_service_record.h" |
27 #include "device/bluetooth/bluetooth_socket.h" | 27 #include "device/bluetooth/bluetooth_socket.h" |
28 #include "device/bluetooth/bluetooth_utils.h" | 28 #include "device/bluetooth/bluetooth_utils.h" |
29 | 29 |
30 #if defined(OS_CHROMEOS) | 30 #if defined(OS_CHROMEOS) |
31 #include "base/safe_strerror_posix.h" | 31 #include "base/safe_strerror_posix.h" |
32 #endif | 32 #endif |
33 | 33 |
34 using device::BluetoothAdapter; | 34 using device::BluetoothAdapter; |
35 using device::BluetoothDevice; | 35 using device::BluetoothDevice; |
36 using device::BluetoothServiceRecord; | 36 using device::BluetoothServiceRecord; |
37 using device::BluetoothSocket; | 37 using device::BluetoothSocket; |
38 | 38 |
39 namespace { | 39 namespace { |
40 | 40 |
41 extensions::ExtensionBluetoothEventRouter* GetEventRouter(Profile* profile) { | 41 extensions::ExtensionBluetoothEventRouter* GetEventRouter(Profile* profile) { |
42 return profile->GetExtensionService()->bluetooth_event_router(); | 42 return extensions::BluetoothAPI::Get(profile)->bluetooth_event_router(); |
43 } | 43 } |
44 | 44 |
45 const BluetoothAdapter* GetAdapter(Profile* profile) { | 45 const BluetoothAdapter* GetAdapter(Profile* profile) { |
46 return GetEventRouter(profile)->adapter(); | 46 return GetEventRouter(profile)->adapter(); |
47 } | 47 } |
48 | 48 |
49 BluetoothAdapter* GetMutableAdapter(Profile* profile) { | 49 BluetoothAdapter* GetMutableAdapter(Profile* profile) { |
50 BluetoothAdapter* adapter = GetEventRouter(profile)->GetMutableAdapter(); | 50 BluetoothAdapter* adapter = GetEventRouter(profile)->GetMutableAdapter(); |
51 return adapter; | 51 return adapter; |
52 } | 52 } |
(...skipping 25 matching lines...) Expand all Loading... | |
78 namespace Connect = extensions::api::bluetooth::Connect; | 78 namespace Connect = extensions::api::bluetooth::Connect; |
79 namespace Disconnect = extensions::api::bluetooth::Disconnect; | 79 namespace Disconnect = extensions::api::bluetooth::Disconnect; |
80 namespace GetDevices = extensions::api::bluetooth::GetDevices; | 80 namespace GetDevices = extensions::api::bluetooth::GetDevices; |
81 namespace GetServices = extensions::api::bluetooth::GetServices; | 81 namespace GetServices = extensions::api::bluetooth::GetServices; |
82 namespace Read = extensions::api::bluetooth::Read; | 82 namespace Read = extensions::api::bluetooth::Read; |
83 namespace SetOutOfBandPairingData = | 83 namespace SetOutOfBandPairingData = |
84 extensions::api::bluetooth::SetOutOfBandPairingData; | 84 extensions::api::bluetooth::SetOutOfBandPairingData; |
85 namespace Write = extensions::api::bluetooth::Write; | 85 namespace Write = extensions::api::bluetooth::Write; |
86 | 86 |
87 namespace extensions { | 87 namespace extensions { |
88 | |
89 BluetoothAPI::BluetoothAPI(Profile* profile) : profile_(profile) { | |
90 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( | |
91 this, extensions::event_names::kBluetoothOnAvailabilityChanged); | |
92 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( | |
93 this, extensions::event_names::kBluetoothOnPowerChanged); | |
94 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( | |
95 this, extensions::event_names::kBluetoothOnDiscoveringChanged); | |
96 } | |
97 | |
98 BluetoothAPI::~BluetoothAPI() { | |
99 } | |
100 | |
101 void BluetoothAPI::Shutdown() { | |
102 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); | |
103 } | |
104 | |
105 // static | |
106 BluetoothAPI* BluetoothAPI::Get(Profile* profile) { | |
107 return BluetoothAPIFactory::GetForProfile(profile); | |
108 } | |
109 | |
110 ExtensionBluetoothEventRouter* BluetoothAPI::bluetooth_event_router() { | |
111 if (!bluetooth_event_router_) { | |
bryeung
2012/11/12 20:44:39
no braces
youngki
2012/11/13 01:14:13
Done.
| |
112 bluetooth_event_router_.reset(new ExtensionBluetoothEventRouter(profile_)); | |
113 } | |
114 return bluetooth_event_router_.get(); | |
115 } | |
116 | |
117 void BluetoothAPI::OnListenerAdded(const std::string& event_name) { | |
118 NOTIMPLEMENTED(); | |
Yoyo Zhou
2012/11/12 20:03:56
I don't understand. Can you explain what this is f
bryeung
2012/11/12 20:44:39
Seems like this CL should handle the lifetime mana
youngki
2012/11/13 01:14:13
Okay I combined https://chromiumcodereview.appspot
| |
119 } | |
120 | |
121 void BluetoothAPI::OnListenerRemoved(const std::string& event_name) { | |
122 NOTIMPLEMENTED(); | |
123 } | |
124 | |
88 namespace api { | 125 namespace api { |
89 | 126 |
90 bool BluetoothIsAvailableFunction::RunImpl() { | 127 bool BluetoothIsAvailableFunction::RunImpl() { |
91 if (!IsBluetoothSupported(profile())) { | 128 if (!IsBluetoothSupported(profile())) { |
92 SetError(kPlatformNotSupported); | 129 SetError(kPlatformNotSupported); |
93 return false; | 130 return false; |
94 } | 131 } |
95 | 132 |
96 SetResult(Value::CreateBooleanValue(GetAdapter(profile())->IsPresent())); | 133 SetResult(Value::CreateBooleanValue(GetAdapter(profile())->IsPresent())); |
97 return true; | 134 return true; |
(...skipping 506 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
604 if (GetEventRouter(profile())->IsResponsibleForDiscovery()) { | 641 if (GetEventRouter(profile())->IsResponsibleForDiscovery()) { |
605 GetMutableAdapter(profile())->SetDiscovering(false, | 642 GetMutableAdapter(profile())->SetDiscovering(false, |
606 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), | 643 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), |
607 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); | 644 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); |
608 } | 645 } |
609 return true; | 646 return true; |
610 } | 647 } |
611 | 648 |
612 } // namespace api | 649 } // namespace api |
613 } // namespace extensions | 650 } // namespace extensions |
OLD | NEW |