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

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: Address code review feedback. 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 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 2c97ce09b90789489e02152fb14072b9eaf4d898..ae8c6f3c2739ffd0beff24b51899d8d2c4d78398 100644
--- a/chrome/browser/extensions/api/bluetooth/bluetooth_event_router.cc
+++ b/chrome/browser/extensions/api/bluetooth/bluetooth_event_router.cc
@@ -15,6 +15,7 @@
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h"
+#include "chrome/browser/extensions/extension_host.h"
#include "chrome/common/extensions/api/bluetooth.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
@@ -31,12 +32,6 @@ namespace extensions {
namespace bluetooth = api::bluetooth;
-// A struct storing a Bluetooth socket and the extension that added it.
-struct ExtensionBluetoothSocketRecord {
- std::string extension_id;
- scoped_refptr<device::BluetoothSocket> socket;
-};
-
// A struct storing a Bluetooth profile and the extension that added it.
struct ExtensionBluetoothProfileRecord {
std::string extension_id;
@@ -48,23 +43,23 @@ ExtensionBluetoothEventRouter::ExtensionBluetoothEventRouter(
: 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,
content::Source<content::BrowserContext>(browser_context_));
+ registrar_.Add(this,
+ chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
+ content::Source<content::BrowserContext>(browser_context_));
}
ExtensionBluetoothEventRouter::~ExtensionBluetoothEventRouter() {
+ 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.";
- socket_map_.clear();
-
for (BluetoothProfileMap::iterator iter = bluetooth_profile_map_.begin();
iter != bluetooth_profile_map_.end();
++iter) {
@@ -103,39 +98,18 @@ void ExtensionBluetoothEventRouter::OnListenerRemoved() {
MaybeReleaseAdapter();
}
-int ExtensionBluetoothEventRouter::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 ExtensionBluetoothEventRouter::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 ExtensionBluetoothEventRouter::AddProfile(
const std::string& 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 ExtensionBluetoothEventRouter::RemoveProfile(const std::string& 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;
@@ -145,6 +119,7 @@ void ExtensionBluetoothEventRouter::RemoveProfile(const std::string& uuid) {
}
bool ExtensionBluetoothEventRouter::HasProfile(const std::string& uuid) const {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
return bluetooth_profile_map_.find(uuid) != bluetooth_profile_map_.end();
}
@@ -194,6 +169,7 @@ void ExtensionBluetoothEventRouter::StopDiscoverySession(
device::BluetoothProfile* ExtensionBluetoothEventRouter::GetProfile(
const std::string& 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;
@@ -201,39 +177,9 @@ device::BluetoothProfile* ExtensionBluetoothEventRouter::GetProfile(
return NULL;
}
-scoped_refptr<device::BluetoothSocket>
-ExtensionBluetoothEventRouter::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 ExtensionBluetoothEventRouter::DispatchConnectionEvent(
- const std::string& extension_id,
- const std::string& 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;
- 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());
-}
-
void ExtensionBluetoothEventRouter::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;
@@ -243,6 +189,7 @@ void ExtensionBluetoothEventRouter::AdapterPresentChanged(
void ExtensionBluetoothEventRouter::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;
@@ -252,6 +199,7 @@ void ExtensionBluetoothEventRouter::AdapterPoweredChanged(
void ExtensionBluetoothEventRouter::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;
@@ -279,6 +227,7 @@ void ExtensionBluetoothEventRouter::AdapterDiscoveringChanged(
void ExtensionBluetoothEventRouter::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;
@@ -290,6 +239,7 @@ void ExtensionBluetoothEventRouter::DeviceAdded(
void ExtensionBluetoothEventRouter::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;
@@ -301,6 +251,7 @@ void ExtensionBluetoothEventRouter::DeviceChanged(
void ExtensionBluetoothEventRouter::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;
@@ -310,6 +261,7 @@ void ExtensionBluetoothEventRouter::DeviceRemoved(
}
void ExtensionBluetoothEventRouter::InitializeAdapterIfNeeded() {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (!adapter_.get()) {
GetAdapter(base::Bind(&ExtensionBluetoothEventRouter::InitializeAdapter,
weak_ptr_factory_.GetWeakPtr()));
@@ -332,7 +284,8 @@ void ExtensionBluetoothEventRouter::MaybeReleaseAdapter() {
}
void ExtensionBluetoothEventRouter::DispatchAdapterStateEvent() {
- bluetooth::AdapterState state;
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+ api::bluetooth::AdapterState state;
PopulateAdapterState(*adapter_.get(), &state);
scoped_ptr<base::ListValue> args =
@@ -359,6 +312,7 @@ void ExtensionBluetoothEventRouter::DispatchDeviceEvent(
void ExtensionBluetoothEventRouter::CleanUpForExtension(
const std::string& extension_id) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
// Remove all profiles added by the extension.
BluetoothProfileMap::iterator profile_iter = bluetooth_profile_map_.begin();
while (profile_iter != bluetooth_profile_map_.end()) {
@@ -371,17 +325,6 @@ void ExtensionBluetoothEventRouter::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);
@@ -408,6 +351,7 @@ void ExtensionBluetoothEventRouter::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: {
extensions::UnloadedExtensionInfo* info =
@@ -415,6 +359,11 @@ void ExtensionBluetoothEventRouter::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