| 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 "extensions/browser/api/bluetooth/bluetooth_api.h" | 5 #include "extensions/browser/api/bluetooth/bluetooth_api.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 namespace GetDevices = extensions::core_api::bluetooth::GetDevices; | 28 namespace GetDevices = extensions::core_api::bluetooth::GetDevices; |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 const char kInvalidDevice[] = "Invalid device"; | 32 const char kInvalidDevice[] = "Invalid device"; |
| 33 const char kStartDiscoveryFailed[] = "Starting discovery failed"; | 33 const char kStartDiscoveryFailed[] = "Starting discovery failed"; |
| 34 const char kStopDiscoveryFailed[] = "Failed to stop discovery"; | 34 const char kStopDiscoveryFailed[] = "Failed to stop discovery"; |
| 35 | 35 |
| 36 extensions::BluetoothEventRouter* GetEventRouter(BrowserContext* context) { | 36 extensions::BluetoothEventRouter* GetEventRouter(BrowserContext* context) { |
| 37 // Note: |context| is valid on UI thread only. | 37 // Note: |context| is valid on UI thread only. |
| 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 38 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 39 return extensions::BluetoothAPI::Get(context)->event_router(); | 39 return extensions::BluetoothAPI::Get(context)->event_router(); |
| 40 } | 40 } |
| 41 | 41 |
| 42 } // namespace | 42 } // namespace |
| 43 | 43 |
| 44 namespace extensions { | 44 namespace extensions { |
| 45 | 45 |
| 46 static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothAPI> > | 46 static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothAPI> > |
| 47 g_factory = LAZY_INSTANCE_INITIALIZER; | 47 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 48 | 48 |
| 49 // static | 49 // static |
| 50 BrowserContextKeyedAPIFactory<BluetoothAPI>* | 50 BrowserContextKeyedAPIFactory<BluetoothAPI>* |
| 51 BluetoothAPI::GetFactoryInstance() { | 51 BluetoothAPI::GetFactoryInstance() { |
| 52 return g_factory.Pointer(); | 52 return g_factory.Pointer(); |
| 53 } | 53 } |
| 54 | 54 |
| 55 // static | 55 // static |
| 56 BluetoothAPI* BluetoothAPI::Get(BrowserContext* context) { | 56 BluetoothAPI* BluetoothAPI::Get(BrowserContext* context) { |
| 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 57 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 58 return GetFactoryInstance()->Get(context); | 58 return GetFactoryInstance()->Get(context); |
| 59 } | 59 } |
| 60 | 60 |
| 61 BluetoothAPI::BluetoothAPI(content::BrowserContext* context) | 61 BluetoothAPI::BluetoothAPI(content::BrowserContext* context) |
| 62 : browser_context_(context) { | 62 : browser_context_(context) { |
| 63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 63 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 64 EventRouter* event_router = EventRouter::Get(browser_context_); | 64 EventRouter* event_router = EventRouter::Get(browser_context_); |
| 65 event_router->RegisterObserver(this, | 65 event_router->RegisterObserver(this, |
| 66 bluetooth::OnAdapterStateChanged::kEventName); | 66 bluetooth::OnAdapterStateChanged::kEventName); |
| 67 event_router->RegisterObserver(this, bluetooth::OnDeviceAdded::kEventName); | 67 event_router->RegisterObserver(this, bluetooth::OnDeviceAdded::kEventName); |
| 68 event_router->RegisterObserver(this, bluetooth::OnDeviceChanged::kEventName); | 68 event_router->RegisterObserver(this, bluetooth::OnDeviceChanged::kEventName); |
| 69 event_router->RegisterObserver(this, bluetooth::OnDeviceRemoved::kEventName); | 69 event_router->RegisterObserver(this, bluetooth::OnDeviceRemoved::kEventName); |
| 70 } | 70 } |
| 71 | 71 |
| 72 BluetoothAPI::~BluetoothAPI() {} | 72 BluetoothAPI::~BluetoothAPI() {} |
| 73 | 73 |
| 74 BluetoothEventRouter* BluetoothAPI::event_router() { | 74 BluetoothEventRouter* BluetoothAPI::event_router() { |
| 75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 75 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 76 if (!event_router_) { | 76 if (!event_router_) { |
| 77 event_router_.reset(new BluetoothEventRouter(browser_context_)); | 77 event_router_.reset(new BluetoothEventRouter(browser_context_)); |
| 78 } | 78 } |
| 79 return event_router_.get(); | 79 return event_router_.get(); |
| 80 } | 80 } |
| 81 | 81 |
| 82 void BluetoothAPI::Shutdown() { | 82 void BluetoothAPI::Shutdown() { |
| 83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 83 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 84 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 84 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
| 85 } | 85 } |
| 86 | 86 |
| 87 void BluetoothAPI::OnListenerAdded(const EventListenerInfo& details) { | 87 void BluetoothAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 88 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 89 if (event_router()->IsBluetoothSupported()) | 89 if (event_router()->IsBluetoothSupported()) |
| 90 event_router()->OnListenerAdded(); | 90 event_router()->OnListenerAdded(); |
| 91 } | 91 } |
| 92 | 92 |
| 93 void BluetoothAPI::OnListenerRemoved(const EventListenerInfo& details) { | 93 void BluetoothAPI::OnListenerRemoved(const EventListenerInfo& details) { |
| 94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 94 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 95 if (event_router()->IsBluetoothSupported()) | 95 if (event_router()->IsBluetoothSupported()) |
| 96 event_router()->OnListenerRemoved(); | 96 event_router()->OnListenerRemoved(); |
| 97 } | 97 } |
| 98 | 98 |
| 99 namespace core_api { | 99 namespace core_api { |
| 100 | 100 |
| 101 BluetoothGetAdapterStateFunction::~BluetoothGetAdapterStateFunction() {} | 101 BluetoothGetAdapterStateFunction::~BluetoothGetAdapterStateFunction() {} |
| 102 | 102 |
| 103 bool BluetoothGetAdapterStateFunction::DoWork( | 103 bool BluetoothGetAdapterStateFunction::DoWork( |
| 104 scoped_refptr<BluetoothAdapter> adapter) { | 104 scoped_refptr<BluetoothAdapter> adapter) { |
| 105 bluetooth::AdapterState state; | 105 bluetooth::AdapterState state; |
| 106 PopulateAdapterState(*adapter.get(), &state); | 106 PopulateAdapterState(*adapter.get(), &state); |
| 107 results_ = bluetooth::GetAdapterState::Results::Create(state); | 107 results_ = bluetooth::GetAdapterState::Results::Create(state); |
| 108 SendResponse(true); | 108 SendResponse(true); |
| 109 return true; | 109 return true; |
| 110 } | 110 } |
| 111 | 111 |
| 112 BluetoothGetDevicesFunction::~BluetoothGetDevicesFunction() {} | 112 BluetoothGetDevicesFunction::~BluetoothGetDevicesFunction() {} |
| 113 | 113 |
| 114 bool BluetoothGetDevicesFunction::DoWork( | 114 bool BluetoothGetDevicesFunction::DoWork( |
| 115 scoped_refptr<BluetoothAdapter> adapter) { | 115 scoped_refptr<BluetoothAdapter> adapter) { |
| 116 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 116 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 117 | 117 |
| 118 base::ListValue* device_list = new base::ListValue; | 118 base::ListValue* device_list = new base::ListValue; |
| 119 SetResult(device_list); | 119 SetResult(device_list); |
| 120 | 120 |
| 121 BluetoothAdapter::DeviceList devices = adapter->GetDevices(); | 121 BluetoothAdapter::DeviceList devices = adapter->GetDevices(); |
| 122 for (BluetoothAdapter::DeviceList::const_iterator iter = devices.begin(); | 122 for (BluetoothAdapter::DeviceList::const_iterator iter = devices.begin(); |
| 123 iter != devices.end(); | 123 iter != devices.end(); |
| 124 ++iter) { | 124 ++iter) { |
| 125 const BluetoothDevice* device = *iter; | 125 const BluetoothDevice* device = *iter; |
| 126 DCHECK(device); | 126 DCHECK(device); |
| 127 | 127 |
| 128 bluetooth::Device extension_device; | 128 bluetooth::Device extension_device; |
| 129 bluetooth::BluetoothDeviceToApiDevice(*device, &extension_device); | 129 bluetooth::BluetoothDeviceToApiDevice(*device, &extension_device); |
| 130 | 130 |
| 131 device_list->Append(extension_device.ToValue().release()); | 131 device_list->Append(extension_device.ToValue().release()); |
| 132 } | 132 } |
| 133 | 133 |
| 134 SendResponse(true); | 134 SendResponse(true); |
| 135 | 135 |
| 136 return true; | 136 return true; |
| 137 } | 137 } |
| 138 | 138 |
| 139 BluetoothGetDeviceFunction::~BluetoothGetDeviceFunction() {} | 139 BluetoothGetDeviceFunction::~BluetoothGetDeviceFunction() {} |
| 140 | 140 |
| 141 bool BluetoothGetDeviceFunction::DoWork( | 141 bool BluetoothGetDeviceFunction::DoWork( |
| 142 scoped_refptr<BluetoothAdapter> adapter) { | 142 scoped_refptr<BluetoothAdapter> adapter) { |
| 143 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 143 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 144 | 144 |
| 145 scoped_ptr<GetDevice::Params> params(GetDevice::Params::Create(*args_)); | 145 scoped_ptr<GetDevice::Params> params(GetDevice::Params::Create(*args_)); |
| 146 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 146 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
| 147 | 147 |
| 148 BluetoothDevice* device = adapter->GetDevice(params->device_address); | 148 BluetoothDevice* device = adapter->GetDevice(params->device_address); |
| 149 if (device) { | 149 if (device) { |
| 150 bluetooth::Device extension_device; | 150 bluetooth::Device extension_device; |
| 151 bluetooth::BluetoothDeviceToApiDevice(*device, &extension_device); | 151 bluetooth::BluetoothDeviceToApiDevice(*device, &extension_device); |
| 152 SetResult(extension_device.ToValue().release()); | 152 SetResult(extension_device.ToValue().release()); |
| 153 SendResponse(true); | 153 SendResponse(true); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 adapter.get(), | 194 adapter.get(), |
| 195 extension_id(), | 195 extension_id(), |
| 196 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), | 196 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), |
| 197 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); | 197 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); |
| 198 | 198 |
| 199 return true; | 199 return true; |
| 200 } | 200 } |
| 201 | 201 |
| 202 } // namespace core_api | 202 } // namespace core_api |
| 203 } // namespace extensions | 203 } // namespace extensions |
| OLD | NEW |