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

Unified Diff: chrome/browser/extensions/api/bluetooth/bluetooth_event_router.cc

Issue 180163009: chrome.bluetooth API improvements. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix ChromeOS Full build. Created 6 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/bluetooth/bluetooth_event_router.cc
diff --git a/chrome/browser/extensions/api/bluetooth/bluetooth_event_router.cc b/chrome/browser/extensions/api/bluetooth/bluetooth_event_router.cc
index 41bba4c9899b1812ec4095c081b8b00691c7e8aa..a440c49237d68793621224824d455a293c2504af 100644
--- a/chrome/browser/extensions/api/bluetooth/bluetooth_event_router.cc
+++ b/chrome/browser/extensions/api/bluetooth/bluetooth_event_router.cc
@@ -30,6 +30,7 @@
#include "device/bluetooth/bluetooth_profile.h"
#include "device/bluetooth/bluetooth_socket.h"
#include "extensions/browser/event_router.h"
+#include "extensions/browser/extension_host.h"
#include "extensions/browser/extension_system.h"
namespace extensions {
@@ -37,12 +38,6 @@ namespace extensions {
namespace bluetooth = api::bluetooth;
namespace bt_private = api::bluetooth_private;
-// A struct storing a Bluetooth socket and the extension that added it.
-struct BluetoothEventRouter::ExtensionBluetoothSocketRecord {
- std::string extension_id;
- scoped_refptr<device::BluetoothSocket> socket;
-};
-
// A struct storing a Bluetooth profile and the extension that added it.
struct BluetoothEventRouter::ExtensionBluetoothProfileRecord {
std::string extension_id;
@@ -53,21 +48,23 @@ BluetoothEventRouter::BluetoothEventRouter(content::BrowserContext* context)
: browser_context_(context),
adapter_(NULL),
num_event_listeners_(0),
- next_socket_id_(1),
weak_ptr_factory_(this) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
DCHECK(browser_context_);
registrar_.Add(this,
chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
content::Source<content::BrowserContext>(browser_context_));
+ registrar_.Add(this,
+ chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
+ content::Source<content::BrowserContext>(browser_context_));
}
BluetoothEventRouter::~BluetoothEventRouter() {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (adapter_.get()) {
adapter_->RemoveObserver(this);
adapter_ = NULL;
}
- DLOG_IF(WARNING, socket_map_.size() != 0)
- << "Bluetooth sockets are still open.";
CleanUpAllExtensions();
}
@@ -86,39 +83,18 @@ void BluetoothEventRouter::GetAdapter(
device::BluetoothAdapterFactory::GetAdapter(callback);
}
-int BluetoothEventRouter::RegisterSocket(
- const std::string& extension_id,
- scoped_refptr<device::BluetoothSocket> socket) {
- // If there is a socket registered with the same fd, just return it's id
- for (SocketMap::const_iterator i = socket_map_.begin();
- i != socket_map_.end(); ++i) {
- if (i->second.socket.get() == socket.get())
- return i->first;
- }
- int return_id = next_socket_id_++;
- ExtensionBluetoothSocketRecord record = { extension_id, socket };
- socket_map_[return_id] = record;
- return return_id;
-}
-
-bool BluetoothEventRouter::ReleaseSocket(int id) {
- SocketMap::iterator socket_entry = socket_map_.find(id);
- if (socket_entry == socket_map_.end())
- return false;
- socket_map_.erase(socket_entry);
- return true;
-}
-
void BluetoothEventRouter::AddProfile(
const device::BluetoothUUID& uuid,
const std::string& extension_id,
device::BluetoothProfile* bluetooth_profile) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
DCHECK(!HasProfile(uuid));
ExtensionBluetoothProfileRecord record = { extension_id, bluetooth_profile };
bluetooth_profile_map_[uuid] = record;
}
void BluetoothEventRouter::RemoveProfile(const device::BluetoothUUID& uuid) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
BluetoothProfileMap::iterator iter = bluetooth_profile_map_.find(uuid);
if (iter != bluetooth_profile_map_.end()) {
device::BluetoothProfile* bluetooth_profile = iter->second.profile;
@@ -128,6 +104,7 @@ void BluetoothEventRouter::RemoveProfile(const device::BluetoothUUID& uuid) {
}
bool BluetoothEventRouter::HasProfile(const device::BluetoothUUID& uuid) const {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
return bluetooth_profile_map_.find(uuid) != bluetooth_profile_map_.end();
}
@@ -177,6 +154,7 @@ void BluetoothEventRouter::StopDiscoverySession(
device::BluetoothProfile* BluetoothEventRouter::GetProfile(
const device::BluetoothUUID& uuid) const {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
BluetoothProfileMap::const_iterator iter = bluetooth_profile_map_.find(uuid);
if (iter != bluetooth_profile_map_.end())
return iter->second.profile;
@@ -184,36 +162,6 @@ device::BluetoothProfile* BluetoothEventRouter::GetProfile(
return NULL;
}
-scoped_refptr<device::BluetoothSocket> BluetoothEventRouter::GetSocket(int id) {
- SocketMap::iterator socket_entry = socket_map_.find(id);
- if (socket_entry == socket_map_.end())
- return NULL;
- return socket_entry->second.socket;
-}
-
-void BluetoothEventRouter::DispatchConnectionEvent(
- const std::string& extension_id,
- const device::BluetoothUUID& uuid,
- const device::BluetoothDevice* device,
- scoped_refptr<device::BluetoothSocket> socket) {
- if (!HasProfile(uuid))
- return;
-
- int socket_id = RegisterSocket(extension_id, socket);
- bluetooth::Socket result_socket;
- bluetooth::BluetoothDeviceToApiDevice(*device, &result_socket.device);
- result_socket.profile.uuid = uuid.canonical_value();
- result_socket.id = socket_id;
-
- scoped_ptr<base::ListValue> args =
- bluetooth::OnConnection::Create(result_socket);
- scoped_ptr<Event> event(new Event(
- bluetooth::OnConnection::kEventName, args.Pass()));
- ExtensionSystem::Get(browser_context_)
- ->event_router()
- ->DispatchEventToExtension(extension_id, event.Pass());
-}
-
BluetoothApiPairingDelegate* BluetoothEventRouter::GetPairingDelegate(
const std::string& extension_id) {
return ContainsKey(pairing_delegate_map_, extension_id)
@@ -281,6 +229,7 @@ void BluetoothEventRouter::RemovePairingDelegate(
void BluetoothEventRouter::AdapterPresentChanged(
device::BluetoothAdapter* adapter,
bool present) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (adapter != adapter_.get()) {
DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress();
return;
@@ -291,6 +240,7 @@ void BluetoothEventRouter::AdapterPresentChanged(
void BluetoothEventRouter::AdapterPoweredChanged(
device::BluetoothAdapter* adapter,
bool has_power) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (adapter != adapter_.get()) {
DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress();
return;
@@ -301,6 +251,7 @@ void BluetoothEventRouter::AdapterPoweredChanged(
void BluetoothEventRouter::AdapterDiscoveringChanged(
device::BluetoothAdapter* adapter,
bool discovering) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (adapter != adapter_.get()) {
DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress();
return;
@@ -328,6 +279,7 @@ void BluetoothEventRouter::AdapterDiscoveringChanged(
void BluetoothEventRouter::DeviceAdded(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (adapter != adapter_.get()) {
DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress();
return;
@@ -338,6 +290,7 @@ void BluetoothEventRouter::DeviceAdded(device::BluetoothAdapter* adapter,
void BluetoothEventRouter::DeviceChanged(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (adapter != adapter_.get()) {
DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress();
return;
@@ -348,6 +301,7 @@ void BluetoothEventRouter::DeviceChanged(device::BluetoothAdapter* adapter,
void BluetoothEventRouter::DeviceRemoved(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (adapter != adapter_.get()) {
DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress();
return;
@@ -358,6 +312,7 @@ void BluetoothEventRouter::DeviceRemoved(device::BluetoothAdapter* adapter,
void BluetoothEventRouter::OnListenerAdded() {
num_event_listeners_++;
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (!adapter_.get()) {
GetAdapter(base::Bind(&BluetoothEventRouter::OnAdapterInitialized,
weak_ptr_factory_.GetWeakPtr(),
@@ -372,7 +327,8 @@ void BluetoothEventRouter::OnListenerRemoved() {
}
void BluetoothEventRouter::DispatchAdapterStateEvent() {
- bluetooth::AdapterState state;
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+ api::bluetooth::AdapterState state;
PopulateAdapterState(*adapter_.get(), &state);
scoped_ptr<base::ListValue> args =
@@ -399,6 +355,7 @@ void BluetoothEventRouter::DispatchDeviceEvent(
void BluetoothEventRouter::CleanUpForExtension(
const std::string& extension_id) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
RemovePairingDelegate(extension_id);
// Remove all profiles added by the extension.
@@ -413,17 +370,6 @@ void BluetoothEventRouter::CleanUpForExtension(
}
}
- // Remove all sockets opened by the extension.
- SocketMap::iterator socket_iter = socket_map_.begin();
- while (socket_iter != socket_map_.end()) {
- int socket_id = socket_iter->first;
- ExtensionBluetoothSocketRecord record = socket_iter->second;
- socket_iter++;
- if (record.extension_id == extension_id) {
- ReleaseSocket(socket_id);
- }
- }
-
// Remove any discovery session initiated by the extension.
DiscoverySessionMap::iterator session_iter =
discovery_session_map_.find(extension_id);
@@ -448,10 +394,6 @@ void BluetoothEventRouter::CleanUpAllExtensions() {
}
discovery_session_map_.clear();
- SocketMap::iterator socket_iter = socket_map_.begin();
- while (socket_iter != socket_map_.end())
- ReleaseSocket(socket_iter++->first);
-
PairingDelegateMap::iterator pairing_iter = pairing_delegate_map_.begin();
while (pairing_iter != pairing_delegate_map_.end())
RemovePairingDelegate(pairing_iter++->first);
@@ -474,6 +416,7 @@ void BluetoothEventRouter::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
switch (type) {
case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: {
extensions::UnloadedExtensionInfo* info =
@@ -481,6 +424,11 @@ void BluetoothEventRouter::Observe(
CleanUpForExtension(info->extension->id());
break;
}
+ case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
+ ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
+ CleanUpForExtension(host->extension_id());
+ break;
+ }
}
}

Powered by Google App Engine
This is Rietveld 408576698