Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: chrome/browser/extensions/api/bluetooth/bluetooth_event_router.cc

Issue 177113013: Bluetooth: add Device events, and cleanup JS API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@arman-patch
Patch Set: I suck at rebase Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_event_router.h" 5 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/json/json_writer.h" 11 #include "base/json/json_writer.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_vector.h" 14 #include "base/memory/scoped_vector.h"
15 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
16 #include "chrome/browser/chrome_notification_types.h" 16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" 17 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h"
18 #include "chrome/browser/extensions/event_names.h"
19 #include "chrome/common/extensions/api/bluetooth.h" 18 #include "chrome/common/extensions/api/bluetooth.h"
20 #include "content/public/browser/notification_details.h" 19 #include "content/public/browser/notification_details.h"
21 #include "content/public/browser/notification_source.h" 20 #include "content/public/browser/notification_source.h"
22 #include "device/bluetooth/bluetooth_adapter.h" 21 #include "device/bluetooth/bluetooth_adapter.h"
23 #include "device/bluetooth/bluetooth_adapter_factory.h" 22 #include "device/bluetooth/bluetooth_adapter_factory.h"
24 #include "device/bluetooth/bluetooth_device.h" 23 #include "device/bluetooth/bluetooth_device.h"
25 #include "device/bluetooth/bluetooth_discovery_session.h" 24 #include "device/bluetooth/bluetooth_discovery_session.h"
26 #include "device/bluetooth/bluetooth_profile.h" 25 #include "device/bluetooth/bluetooth_profile.h"
27 #include "device/bluetooth/bluetooth_socket.h" 26 #include "device/bluetooth/bluetooth_socket.h"
28 #include "extensions/browser/event_router.h" 27 #include "extensions/browser/event_router.h"
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 } 202 }
204 203
205 scoped_refptr<device::BluetoothSocket> 204 scoped_refptr<device::BluetoothSocket>
206 ExtensionBluetoothEventRouter::GetSocket(int id) { 205 ExtensionBluetoothEventRouter::GetSocket(int id) {
207 SocketMap::iterator socket_entry = socket_map_.find(id); 206 SocketMap::iterator socket_entry = socket_map_.find(id);
208 if (socket_entry == socket_map_.end()) 207 if (socket_entry == socket_map_.end())
209 return NULL; 208 return NULL;
210 return socket_entry->second.socket; 209 return socket_entry->second.socket;
211 } 210 }
212 211
213 void ExtensionBluetoothEventRouter::DispatchDeviceEvent(
214 const std::string& event_name, const bluetooth::Device& device) {
215 scoped_ptr<base::ListValue> args(new base::ListValue());
216 args->Append(device.ToValue().release());
217 scoped_ptr<Event> event(new Event(event_name, args.Pass()));
218 ExtensionSystem::Get(browser_context_)->event_router()->BroadcastEvent(
219 event.Pass());
220 }
221
222 void ExtensionBluetoothEventRouter::DispatchConnectionEvent( 212 void ExtensionBluetoothEventRouter::DispatchConnectionEvent(
223 const std::string& extension_id, 213 const std::string& extension_id,
224 const std::string& uuid, 214 const std::string& uuid,
225 const device::BluetoothDevice* device, 215 const device::BluetoothDevice* device,
226 scoped_refptr<device::BluetoothSocket> socket) { 216 scoped_refptr<device::BluetoothSocket> socket) {
227 if (!HasProfile(uuid)) 217 if (!HasProfile(uuid))
228 return; 218 return;
229 219
230 int socket_id = RegisterSocket(extension_id, socket); 220 int socket_id = RegisterSocket(extension_id, socket);
231 api::bluetooth::Socket result_socket; 221 bluetooth::Socket result_socket;
232 api::bluetooth::BluetoothDeviceToApiDevice(*device, &result_socket.device); 222 bluetooth::BluetoothDeviceToApiDevice(*device, &result_socket.device);
233 result_socket.profile.uuid = uuid; 223 result_socket.profile.uuid = uuid;
234 result_socket.id = socket_id; 224 result_socket.id = socket_id;
235 225
236 scoped_ptr<base::ListValue> args(new base::ListValue()); 226 scoped_ptr<base::ListValue> args =
237 args->Append(result_socket.ToValue().release()); 227 bluetooth::OnConnection::Create(result_socket);
238 scoped_ptr<Event> event(new Event( 228 scoped_ptr<Event> event(new Event(
239 bluetooth::OnConnection::kEventName, args.Pass())); 229 bluetooth::OnConnection::kEventName, args.Pass()));
240 ExtensionSystem::Get(browser_context_) 230 ExtensionSystem::Get(browser_context_)
241 ->event_router() 231 ->event_router()
242 ->DispatchEventToExtension(extension_id, event.Pass()); 232 ->DispatchEventToExtension(extension_id, event.Pass());
243 } 233 }
244 234
245 void ExtensionBluetoothEventRouter::AdapterPresentChanged( 235 void ExtensionBluetoothEventRouter::AdapterPresentChanged(
246 device::BluetoothAdapter* adapter, bool present) { 236 device::BluetoothAdapter* adapter, bool present) {
247 if (adapter != adapter_.get()) { 237 if (adapter != adapter_.get()) {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 } 277 }
288 278
289 void ExtensionBluetoothEventRouter::DeviceAdded( 279 void ExtensionBluetoothEventRouter::DeviceAdded(
290 device::BluetoothAdapter* adapter, 280 device::BluetoothAdapter* adapter,
291 device::BluetoothDevice* device) { 281 device::BluetoothDevice* device) {
292 if (adapter != adapter_.get()) { 282 if (adapter != adapter_.get()) {
293 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); 283 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress();
294 return; 284 return;
295 } 285 }
296 286
297 bluetooth::Device extension_device; 287 DispatchDeviceEvent(bluetooth::OnDeviceAdded::kEventName, device);
298 bluetooth::BluetoothDeviceToApiDevice(*device, &extension_device); 288 }
299 289
300 DispatchDeviceEvent(extensions::event_names::kBluetoothOnDeviceDiscovered, 290 void ExtensionBluetoothEventRouter::DeviceChanged(
301 extension_device); 291 device::BluetoothAdapter* adapter,
292 device::BluetoothDevice* device) {
293 if (adapter != adapter_.get()) {
294 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress();
295 return;
296 }
297
298 DispatchDeviceEvent(bluetooth::OnDeviceChanged::kEventName, device);
299 }
300
301 void ExtensionBluetoothEventRouter::DeviceRemoved(
302 device::BluetoothAdapter* adapter,
303 device::BluetoothDevice* device) {
304 if (adapter != adapter_.get()) {
305 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress();
306 return;
307 }
308
309 DispatchDeviceEvent(bluetooth::OnDeviceRemoved::kEventName, device);
302 } 310 }
303 311
304 void ExtensionBluetoothEventRouter::InitializeAdapterIfNeeded() { 312 void ExtensionBluetoothEventRouter::InitializeAdapterIfNeeded() {
305 if (!adapter_.get()) { 313 if (!adapter_.get()) {
306 GetAdapter(base::Bind(&ExtensionBluetoothEventRouter::InitializeAdapter, 314 GetAdapter(base::Bind(&ExtensionBluetoothEventRouter::InitializeAdapter,
307 weak_ptr_factory_.GetWeakPtr())); 315 weak_ptr_factory_.GetWeakPtr()));
308 } 316 }
309 } 317 }
310 318
311 void ExtensionBluetoothEventRouter::InitializeAdapter( 319 void ExtensionBluetoothEventRouter::InitializeAdapter(
312 scoped_refptr<device::BluetoothAdapter> adapter) { 320 scoped_refptr<device::BluetoothAdapter> adapter) {
313 if (!adapter_.get()) { 321 if (!adapter_.get()) {
314 adapter_ = adapter; 322 adapter_ = adapter;
315 adapter_->AddObserver(this); 323 adapter_->AddObserver(this);
316 } 324 }
317 } 325 }
318 326
319 void ExtensionBluetoothEventRouter::MaybeReleaseAdapter() { 327 void ExtensionBluetoothEventRouter::MaybeReleaseAdapter() {
320 if (adapter_.get() && num_event_listeners_ == 0) { 328 if (adapter_.get() && num_event_listeners_ == 0) {
321 adapter_->RemoveObserver(this); 329 adapter_->RemoveObserver(this);
322 adapter_ = NULL; 330 adapter_ = NULL;
323 } 331 }
324 } 332 }
325 333
326 void ExtensionBluetoothEventRouter::DispatchAdapterStateEvent() { 334 void ExtensionBluetoothEventRouter::DispatchAdapterStateEvent() {
327 api::bluetooth::AdapterState state; 335 bluetooth::AdapterState state;
328 PopulateAdapterState(*adapter_.get(), &state); 336 PopulateAdapterState(*adapter_.get(), &state);
329 337
330 scoped_ptr<base::ListValue> args(new base::ListValue()); 338 scoped_ptr<base::ListValue> args =
331 args->Append(state.ToValue().release()); 339 bluetooth::OnAdapterStateChanged::Create(state);
332 scoped_ptr<Event> event(new Event( 340 scoped_ptr<Event> event(new Event(
333 bluetooth::OnAdapterStateChanged::kEventName, 341 bluetooth::OnAdapterStateChanged::kEventName,
334 args.Pass())); 342 args.Pass()));
335 ExtensionSystem::Get(browser_context_)->event_router()->BroadcastEvent( 343 ExtensionSystem::Get(browser_context_)->event_router()->BroadcastEvent(
336 event.Pass()); 344 event.Pass());
337 } 345 }
338 346
347 void ExtensionBluetoothEventRouter::DispatchDeviceEvent(
348 const std::string& event_name,
349 device::BluetoothDevice* device) {
350 bluetooth::Device extension_device;
351 bluetooth::BluetoothDeviceToApiDevice(*device, &extension_device);
352
353 scoped_ptr<base::ListValue> args =
354 bluetooth::OnDeviceAdded::Create(extension_device);
355 scoped_ptr<Event> event(new Event(event_name, args.Pass()));
356 ExtensionSystem::Get(browser_context_)->event_router()->BroadcastEvent(
357 event.Pass());
358 }
359
339 void ExtensionBluetoothEventRouter::CleanUpForExtension( 360 void ExtensionBluetoothEventRouter::CleanUpForExtension(
340 const std::string& extension_id) { 361 const std::string& extension_id) {
341 // Remove all profiles added by the extension. 362 // Remove all profiles added by the extension.
342 BluetoothProfileMap::iterator profile_iter = bluetooth_profile_map_.begin(); 363 BluetoothProfileMap::iterator profile_iter = bluetooth_profile_map_.begin();
343 while (profile_iter != bluetooth_profile_map_.end()) { 364 while (profile_iter != bluetooth_profile_map_.end()) {
344 ExtensionBluetoothProfileRecord record = profile_iter->second; 365 ExtensionBluetoothProfileRecord record = profile_iter->second;
345 if (record.extension_id == extension_id) { 366 if (record.extension_id == extension_id) {
346 bluetooth_profile_map_.erase(profile_iter++); 367 bluetooth_profile_map_.erase(profile_iter++);
347 record.profile->Unregister(); 368 record.profile->Unregister();
348 } else { 369 } else {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { 412 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
392 extensions::UnloadedExtensionInfo* info = 413 extensions::UnloadedExtensionInfo* info =
393 content::Details<extensions::UnloadedExtensionInfo>(details).ptr(); 414 content::Details<extensions::UnloadedExtensionInfo>(details).ptr();
394 CleanUpForExtension(info->extension->id()); 415 CleanUpForExtension(info->extension->id());
395 break; 416 break;
396 } 417 }
397 } 418 }
398 } 419 }
399 420
400 } // namespace extensions 421 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h ('k') | chrome/browser/extensions/event_names.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698