Add ICU message format support
[chromium-blink-merge.git] / extensions / browser / api / bluetooth / bluetooth_api.cc
blob4570f4ed6826b5a263b68465e3813e744d4a4299
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
3 // found in the LICENSE file.
5 #include "extensions/browser/api/bluetooth/bluetooth_api.h"
7 #include <string>
9 #include "base/bind_helpers.h"
10 #include "base/lazy_instance.h"
11 #include "base/memory/ref_counted.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "device/bluetooth/bluetooth_adapter.h"
14 #include "device/bluetooth/bluetooth_device.h"
15 #include "extensions/browser/api/bluetooth/bluetooth_api_utils.h"
16 #include "extensions/browser/api/bluetooth/bluetooth_event_router.h"
17 #include "extensions/browser/event_router.h"
18 #include "extensions/common/api/bluetooth.h"
20 using content::BrowserContext;
21 using content::BrowserThread;
23 using device::BluetoothAdapter;
24 using device::BluetoothDevice;
26 namespace bluetooth = extensions::api::bluetooth;
27 namespace GetDevice = extensions::api::bluetooth::GetDevice;
28 namespace GetDevices = extensions::api::bluetooth::GetDevices;
30 namespace {
32 const char kInvalidDevice[] = "Invalid device";
33 const char kStartDiscoveryFailed[] = "Starting discovery failed";
34 const char kStopDiscoveryFailed[] = "Failed to stop discovery";
36 extensions::BluetoothEventRouter* GetEventRouter(BrowserContext* context) {
37 // Note: |context| is valid on UI thread only.
38 DCHECK_CURRENTLY_ON(BrowserThread::UI);
39 return extensions::BluetoothAPI::Get(context)->event_router();
42 } // namespace
44 namespace extensions {
46 static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothAPI> >
47 g_factory = LAZY_INSTANCE_INITIALIZER;
49 // static
50 BrowserContextKeyedAPIFactory<BluetoothAPI>*
51 BluetoothAPI::GetFactoryInstance() {
52 return g_factory.Pointer();
55 // static
56 BluetoothAPI* BluetoothAPI::Get(BrowserContext* context) {
57 DCHECK_CURRENTLY_ON(BrowserThread::UI);
58 return GetFactoryInstance()->Get(context);
61 BluetoothAPI::BluetoothAPI(content::BrowserContext* context)
62 : browser_context_(context) {
63 DCHECK_CURRENTLY_ON(BrowserThread::UI);
64 EventRouter* event_router = EventRouter::Get(browser_context_);
65 event_router->RegisterObserver(this,
66 bluetooth::OnAdapterStateChanged::kEventName);
67 event_router->RegisterObserver(this, bluetooth::OnDeviceAdded::kEventName);
68 event_router->RegisterObserver(this, bluetooth::OnDeviceChanged::kEventName);
69 event_router->RegisterObserver(this, bluetooth::OnDeviceRemoved::kEventName);
72 BluetoothAPI::~BluetoothAPI() {}
74 BluetoothEventRouter* BluetoothAPI::event_router() {
75 DCHECK_CURRENTLY_ON(BrowserThread::UI);
76 if (!event_router_) {
77 event_router_.reset(new BluetoothEventRouter(browser_context_));
79 return event_router_.get();
82 void BluetoothAPI::Shutdown() {
83 DCHECK_CURRENTLY_ON(BrowserThread::UI);
84 EventRouter::Get(browser_context_)->UnregisterObserver(this);
87 void BluetoothAPI::OnListenerAdded(const EventListenerInfo& details) {
88 DCHECK_CURRENTLY_ON(BrowserThread::UI);
89 if (event_router()->IsBluetoothSupported())
90 event_router()->OnListenerAdded();
93 void BluetoothAPI::OnListenerRemoved(const EventListenerInfo& details) {
94 DCHECK_CURRENTLY_ON(BrowserThread::UI);
95 if (event_router()->IsBluetoothSupported())
96 event_router()->OnListenerRemoved();
99 namespace api {
101 BluetoothGetAdapterStateFunction::~BluetoothGetAdapterStateFunction() {}
103 bool BluetoothGetAdapterStateFunction::DoWork(
104 scoped_refptr<BluetoothAdapter> adapter) {
105 bluetooth::AdapterState state;
106 PopulateAdapterState(*adapter.get(), &state);
107 results_ = bluetooth::GetAdapterState::Results::Create(state);
108 SendResponse(true);
109 return true;
112 BluetoothGetDevicesFunction::~BluetoothGetDevicesFunction() {}
114 bool BluetoothGetDevicesFunction::DoWork(
115 scoped_refptr<BluetoothAdapter> adapter) {
116 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
118 base::ListValue* device_list = new base::ListValue;
119 SetResult(device_list);
121 BluetoothAdapter::DeviceList devices = adapter->GetDevices();
122 for (BluetoothAdapter::DeviceList::const_iterator iter = devices.begin();
123 iter != devices.end();
124 ++iter) {
125 const BluetoothDevice* device = *iter;
126 DCHECK(device);
128 bluetooth::Device extension_device;
129 bluetooth::BluetoothDeviceToApiDevice(*device, &extension_device);
131 device_list->Append(extension_device.ToValue().release());
134 SendResponse(true);
136 return true;
139 BluetoothGetDeviceFunction::~BluetoothGetDeviceFunction() {}
141 bool BluetoothGetDeviceFunction::DoWork(
142 scoped_refptr<BluetoothAdapter> adapter) {
143 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
145 scoped_ptr<GetDevice::Params> params(GetDevice::Params::Create(*args_));
146 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
148 BluetoothDevice* device = adapter->GetDevice(params->device_address);
149 if (device) {
150 bluetooth::Device extension_device;
151 bluetooth::BluetoothDeviceToApiDevice(*device, &extension_device);
152 SetResult(extension_device.ToValue().release());
153 SendResponse(true);
154 } else {
155 SetError(kInvalidDevice);
156 SendResponse(false);
159 return false;
162 void BluetoothStartDiscoveryFunction::OnSuccessCallback() {
163 SendResponse(true);
166 void BluetoothStartDiscoveryFunction::OnErrorCallback() {
167 SetError(kStartDiscoveryFailed);
168 SendResponse(false);
171 bool BluetoothStartDiscoveryFunction::DoWork(
172 scoped_refptr<BluetoothAdapter> adapter) {
173 GetEventRouter(browser_context())->StartDiscoverySession(
174 adapter.get(),
175 extension_id(),
176 base::Bind(&BluetoothStartDiscoveryFunction::OnSuccessCallback, this),
177 base::Bind(&BluetoothStartDiscoveryFunction::OnErrorCallback, this));
179 return true;
182 void BluetoothStopDiscoveryFunction::OnSuccessCallback() {
183 SendResponse(true);
186 void BluetoothStopDiscoveryFunction::OnErrorCallback() {
187 SetError(kStopDiscoveryFailed);
188 SendResponse(false);
191 bool BluetoothStopDiscoveryFunction::DoWork(
192 scoped_refptr<BluetoothAdapter> adapter) {
193 GetEventRouter(browser_context())->StopDiscoverySession(
194 adapter.get(),
195 extension_id(),
196 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this),
197 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this));
199 return true;
202 } // namespace api
203 } // namespace extensions