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/bluetooth_event_router.h" | 5 #include "chrome/browser/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" |
(...skipping 11 matching lines...) Expand all Loading... | |
22 | 22 |
23 namespace extensions { | 23 namespace extensions { |
24 | 24 |
25 ExtensionBluetoothEventRouter::ExtensionBluetoothEventRouter(Profile* profile) | 25 ExtensionBluetoothEventRouter::ExtensionBluetoothEventRouter(Profile* profile) |
26 : send_discovery_events_(false), | 26 : send_discovery_events_(false), |
27 responsible_for_discovery_(false), | 27 responsible_for_discovery_(false), |
28 profile_(profile), | 28 profile_(profile), |
29 adapter_(device::BluetoothAdapterFactory::DefaultAdapter()), | 29 adapter_(device::BluetoothAdapterFactory::DefaultAdapter()), |
30 next_socket_id_(1) { | 30 next_socket_id_(1) { |
31 DCHECK(profile_); | 31 DCHECK(profile_); |
32 if (adapter_.get()) | 32 } |
33 adapter_->AddObserver(this); | 33 |
34 ExtensionBluetoothEventRouter::ExtensionBluetoothEventRouter( | |
35 Profile* profile, device::BluetoothAdapter* adapter) | |
36 : send_discovery_events_(false), | |
37 responsible_for_discovery_(false), | |
38 profile_(profile), | |
39 adapter_(adapter), | |
40 next_socket_id_(1) { | |
41 DCHECK(profile_); | |
34 } | 42 } |
35 | 43 |
36 ExtensionBluetoothEventRouter::~ExtensionBluetoothEventRouter() { | 44 ExtensionBluetoothEventRouter::~ExtensionBluetoothEventRouter() { |
37 if (adapter_.get()) | 45 if (adapter_.get()) |
38 adapter_->RemoveObserver(this); | 46 adapter_->RemoveObserver(this); |
39 | 47 |
40 socket_map_.clear(); | 48 socket_map_.clear(); |
41 } | 49 } |
42 | 50 |
51 device::BluetoothAdapter* ExtensionBluetoothEventRouter::GetMutableAdapter() { | |
52 if (adapter_.get()) { | |
53 adapter_->AddObserver(this); | |
miket_OOO
2012/11/06 02:04:09
Braces not needed.
I might answer this question f
youngki
2012/11/06 16:18:54
Done.
| |
54 } | |
55 return adapter_.get(); | |
56 } | |
57 | |
43 int ExtensionBluetoothEventRouter::RegisterSocket( | 58 int ExtensionBluetoothEventRouter::RegisterSocket( |
44 scoped_refptr<device::BluetoothSocket> socket) { | 59 scoped_refptr<device::BluetoothSocket> socket) { |
45 // If there is a socket registered with the same fd, just return it's id | 60 // If there is a socket registered with the same fd, just return it's id |
46 for (SocketMap::const_iterator i = socket_map_.begin(); | 61 for (SocketMap::const_iterator i = socket_map_.begin(); |
47 i != socket_map_.end(); ++i) { | 62 i != socket_map_.end(); ++i) { |
48 if (i->second->fd() == socket->fd()) { | 63 if (i->second->fd() == socket->fd()) { |
49 return i->first; | 64 return i->first; |
50 } | 65 } |
51 } | 66 } |
52 int return_id = next_socket_id_++; | 67 int return_id = next_socket_id_++; |
53 socket_map_[return_id] = socket; | 68 socket_map_[return_id] = socket; |
54 return return_id; | 69 return return_id; |
55 } | 70 } |
56 | 71 |
57 bool ExtensionBluetoothEventRouter::ReleaseSocket(int id) { | 72 bool ExtensionBluetoothEventRouter::ReleaseSocket(int id) { |
58 SocketMap::iterator socket_entry = socket_map_.find(id); | 73 SocketMap::iterator socket_entry = socket_map_.find(id); |
59 if (socket_entry == socket_map_.end()) | 74 if (socket_entry == socket_map_.end()) |
60 return false; | 75 return false; |
61 socket_map_.erase(socket_entry); | 76 socket_map_.erase(socket_entry); |
62 return true; | 77 return true; |
63 } | 78 } |
64 | 79 |
80 bool ExtensionBluetoothEventRouter::IsBluetoothEvent( | |
81 const std::string& event_name) { | |
miket_OOO
2012/11/06 02:04:09
this method could be const.
youngki
2012/11/06 16:18:54
This method is actually static, so no need to be c
| |
82 return | |
83 event_name == extensions::event_names::kBluetoothOnAvailabilityChanged || | |
84 event_name == extensions::event_names::kBluetoothOnDiscoveringChanged || | |
85 event_name == extensions::event_names::kBluetoothOnPowerChanged; | |
86 } | |
87 | |
88 void ExtensionBluetoothEventRouter::OnEventListenerAdded( | |
89 const std::string& event_name) { | |
90 if (IsBluetoothEvent(event_name) && adapter_.get()) { | |
91 adapter_->AddObserver(this); | |
miket_OOO
2012/11/06 02:04:09
Would it be correct, or more elegant, to call GetM
youngki
2012/11/06 16:18:54
Hm.. Although they do the same thing, we are not t
| |
92 } | |
93 } | |
94 | |
65 scoped_refptr<device::BluetoothSocket> | 95 scoped_refptr<device::BluetoothSocket> |
66 ExtensionBluetoothEventRouter::GetSocket(int id) { | 96 ExtensionBluetoothEventRouter::GetSocket(int id) { |
67 SocketMap::iterator socket_entry = socket_map_.find(id); | 97 SocketMap::iterator socket_entry = socket_map_.find(id); |
68 if (socket_entry == socket_map_.end()) | 98 if (socket_entry == socket_map_.end()) |
69 return NULL; | 99 return NULL; |
70 return socket_entry->second; | 100 return socket_entry->second; |
71 } | 101 } |
72 | 102 |
73 void ExtensionBluetoothEventRouter::SetResponsibleForDiscovery( | 103 void ExtensionBluetoothEventRouter::SetResponsibleForDiscovery( |
74 bool responsible) { | 104 bool responsible) { |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 | 196 |
167 void ExtensionBluetoothEventRouter::DispatchBooleanValueEvent( | 197 void ExtensionBluetoothEventRouter::DispatchBooleanValueEvent( |
168 const char* event_name, bool value) { | 198 const char* event_name, bool value) { |
169 scoped_ptr<ListValue> args(new ListValue()); | 199 scoped_ptr<ListValue> args(new ListValue()); |
170 args->Append(Value::CreateBooleanValue(value)); | 200 args->Append(Value::CreateBooleanValue(value)); |
171 extensions::ExtensionSystem::Get(profile_)->event_router()-> | 201 extensions::ExtensionSystem::Get(profile_)->event_router()-> |
172 DispatchEventToRenderers(event_name, args.Pass(), NULL, GURL()); | 202 DispatchEventToRenderers(event_name, args.Pass(), NULL, GURL()); |
173 } | 203 } |
174 | 204 |
175 } // namespace extensions | 205 } // namespace extensions |
OLD | NEW |