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

Side by Side Diff: chrome/browser/chromeos/extensions/bluetooth_event_router.cc

Issue 10536159: Bluetooth Extension API: Add a discovery API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remove chromeos/bluetooth code Created 8 years, 6 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 | Annotate | Revision Log
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/chromeos/extensions/bluetooth_event_router.h" 5 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h"
6 6
7 #include <map> 7 #include <map>
8 8
9 #include "base/json/json_writer.h" 9 #include "base/json/json_writer.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
11 #include "base/utf_string_conversions.h"
11 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" 12 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h"
13 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h"
12 #include "chrome/browser/chromeos/bluetooth/bluetooth_socket.h" 14 #include "chrome/browser/chromeos/bluetooth/bluetooth_socket.h"
13 #include "chrome/browser/extensions/extension_event_names.h" 15 #include "chrome/browser/extensions/extension_event_names.h"
14 #include "chrome/browser/extensions/extension_event_router.h" 16 #include "chrome/browser/extensions/extension_event_router.h"
17 #include "chrome/common/extensions/api/experimental_bluetooth.h"
15 18
16 namespace chromeos { 19 namespace chromeos {
17 20
18 ExtensionBluetoothEventRouter::ExtensionBluetoothEventRouter(Profile* profile) 21 ExtensionBluetoothEventRouter::ExtensionBluetoothEventRouter(Profile* profile)
19 : profile_(profile), 22 : profile_(profile),
20 adapter_(chromeos::BluetoothAdapter::CreateDefaultAdapter()), 23 adapter_(chromeos::BluetoothAdapter::CreateDefaultAdapter()),
21 next_socket_id_(1) { 24 next_socket_id_(1) {
22 DCHECK(profile_); 25 DCHECK(profile_);
23 DCHECK(adapter_.get()); 26 DCHECK(adapter_.get());
24 adapter_->AddObserver(this); 27 adapter_->AddObserver(this);
(...skipping 27 matching lines...) Expand all
52 } 55 }
53 56
54 scoped_refptr<BluetoothSocket> ExtensionBluetoothEventRouter::GetSocket( 57 scoped_refptr<BluetoothSocket> ExtensionBluetoothEventRouter::GetSocket(
55 int id) { 58 int id) {
56 SocketMap::iterator socket_entry = socket_map_.find(id); 59 SocketMap::iterator socket_entry = socket_map_.find(id);
57 if (socket_entry == socket_map_.end()) 60 if (socket_entry == socket_map_.end())
58 return NULL; 61 return NULL;
59 return socket_entry->second; 62 return socket_entry->second;
60 } 63 }
61 64
65 void ExtensionBluetoothEventRouter::SetSendDiscoveryEvents(bool should_send) {
66 send_discovery_events_ = should_send;
67 }
68
62 void ExtensionBluetoothEventRouter::AdapterPresentChanged( 69 void ExtensionBluetoothEventRouter::AdapterPresentChanged(
63 chromeos::BluetoothAdapter* adapter, bool present) { 70 chromeos::BluetoothAdapter* adapter, bool present) {
64 DCHECK(adapter == adapter_.get()); 71 DCHECK(adapter == adapter_.get());
65 DispatchEvent(extension_event_names::kBluetoothOnAvailabilityChanged, 72 DispatchBooleanValueEvent(
73 extension_event_names::kBluetoothOnAvailabilityChanged,
66 present); 74 present);
67 } 75 }
68 76
69 void ExtensionBluetoothEventRouter::AdapterPoweredChanged( 77 void ExtensionBluetoothEventRouter::AdapterPoweredChanged(
70 chromeos::BluetoothAdapter* adapter, bool has_power) { 78 chromeos::BluetoothAdapter* adapter, bool has_power) {
71 DCHECK(adapter == adapter_.get()); 79 DCHECK(adapter == adapter_.get());
72 DispatchEvent(extension_event_names::kBluetoothOnPowerChanged, has_power); 80 DispatchBooleanValueEvent(
81 extension_event_names::kBluetoothOnPowerChanged,
82 has_power);
73 } 83 }
74 84
75 void ExtensionBluetoothEventRouter::DispatchEvent( 85 void ExtensionBluetoothEventRouter::DeviceAdded(
86 chromeos::BluetoothAdapter* adapter, chromeos::BluetoothDevice* device) {
87 if (!send_discovery_events_)
88 return;
89
90 DCHECK(adapter == adapter_.get());
91
92 extensions::api::experimental_bluetooth::Device extension_device;
93 extension_device.name = UTF16ToUTF8(device->GetName());
94 extension_device.address = device->address();
95
96 ListValue args;
97 args.Append(extension_device.ToValue().release());
98 std::string json_args;
99 base::JSONWriter::Write(&args, &json_args);
100
101 profile_->GetExtensionEventRouter()->DispatchEventToRenderers(
asargent_no_longer_on_chrome 2012/06/14 18:23:55 If I have 2 platform apps installed that both have
102 extension_event_names::kBluetoothOnDeviceDiscovered,
103 json_args,
104 NULL,
105 GURL());
106 }
107
108 void ExtensionBluetoothEventRouter::DispatchBooleanValueEvent(
76 const char* event_name, bool value) { 109 const char* event_name, bool value) {
77 ListValue args; 110 ListValue args;
78 args.Append(Value::CreateBooleanValue(value)); 111 args.Append(Value::CreateBooleanValue(value));
79 std::string json_args; 112 std::string json_args;
80 base::JSONWriter::Write(&args, &json_args); 113 base::JSONWriter::Write(&args, &json_args);
81 114
82 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( 115 profile_->GetExtensionEventRouter()->DispatchEventToRenderers(
83 event_name, json_args, NULL, GURL()); 116 event_name, json_args, NULL, GURL());
84 } 117 }
85 118
86 } // namespace chromeos 119 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698