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

Unified Diff: content/renderer/bluetooth/bluetooth_dispatcher.cc

Issue 1775953004: bluetooth: Move writeValue to mojo (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@my-origin
Patch Set: Moar cleanup Created 4 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: content/renderer/bluetooth/bluetooth_dispatcher.cc
diff --git a/content/renderer/bluetooth/bluetooth_dispatcher.cc b/content/renderer/bluetooth/bluetooth_dispatcher.cc
index 5c4e81bcd48e485c028caa6046b00c7efd04a1ed..34f70fea1a297197a029e6aace243b6043d8bb02 100644
--- a/content/renderer/bluetooth/bluetooth_dispatcher.cc
+++ b/content/renderer/bluetooth/bluetooth_dispatcher.cc
@@ -10,9 +10,13 @@
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/thread_task_runner_handle.h"
+#include "content/child/mojo/type_converters.h"
#include "content/child/thread_safe_sender.h"
#include "content/common/bluetooth/bluetooth_messages.h"
+#include "content/public/common/service_registry.h"
+#include "content/renderer/bluetooth/bluetooth_type_converters.h"
#include "device/bluetooth/bluetooth_uuid.h"
+#include "mojo/public/cpp/bindings/array.h"
#include "third_party/WebKit/public/platform/WebPassOwnPtr.h"
#include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevice.h"
#include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothError.h"
@@ -22,7 +26,6 @@
#include "third_party/WebKit/public/platform/modules/bluetooth/WebRequestDeviceOptions.h"
using blink::WebBluetoothDevice;
-using blink::WebBluetoothError;
using blink::WebBluetoothRemoteGATTCharacteristicInit;
using blink::WebBluetoothRemoteGATTServerConnectCallbacks;
using blink::WebBluetoothRemoteGATTService;
@@ -76,17 +79,6 @@ struct BluetoothCharacteristicsRequest {
scoped_ptr<blink::WebBluetoothGetCharacteristicsCallbacks> callbacks;
};
-// Struct that holds a pending WriteValue request.
-struct BluetoothWriteValueRequest {
- BluetoothWriteValueRequest(const blink::WebVector<uint8_t>& value,
- blink::WebBluetoothWriteValueCallbacks* callbacks)
- : value(value), callbacks(callbacks) {}
- ~BluetoothWriteValueRequest() {}
-
- const blink::WebVector<uint8_t> value;
- scoped_ptr<blink::WebBluetoothWriteValueCallbacks> callbacks;
-};
-
// Struct that holds a pending Start/StopNotifications request.
struct BluetoothNotificationsRequest {
BluetoothNotificationsRequest(
@@ -144,8 +136,9 @@ WebBluetoothDevice::VendorIDSource GetWebVendorIdSource(
} // namespace
-BluetoothDispatcher::BluetoothDispatcher(ThreadSafeSender* sender)
- : thread_safe_sender_(sender) {
+BluetoothDispatcher::BluetoothDispatcher(ThreadSafeSender* sender,
+ ServiceRegistry* service_registry)
+ : thread_safe_sender_(sender), service_registry_(service_registry) {
g_dispatcher_tls.Pointer()->Set(static_cast<void*>(this));
}
@@ -154,7 +147,8 @@ BluetoothDispatcher::~BluetoothDispatcher() {
}
BluetoothDispatcher* BluetoothDispatcher::GetOrCreateThreadSpecificInstance(
- ThreadSafeSender* thread_safe_sender) {
+ ThreadSafeSender* thread_safe_sender,
+ ServiceRegistry* service_registry) {
if (g_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) {
NOTREACHED() << "Re-instantiating TLS BluetoothDispatcher.";
g_dispatcher_tls.Pointer()->Set(NULL);
@@ -162,7 +156,8 @@ BluetoothDispatcher* BluetoothDispatcher::GetOrCreateThreadSpecificInstance(
if (g_dispatcher_tls.Pointer()->Get())
return static_cast<BluetoothDispatcher*>(g_dispatcher_tls.Pointer()->Get());
- BluetoothDispatcher* dispatcher = new BluetoothDispatcher(thread_safe_sender);
+ BluetoothDispatcher* dispatcher =
+ new BluetoothDispatcher(thread_safe_sender, service_registry);
if (CurrentWorkerId())
WorkerThread::AddObserver(dispatcher);
return dispatcher;
@@ -198,10 +193,6 @@ void BluetoothDispatcher::OnMessageReceived(const IPC::Message& msg) {
OnReadValueSuccess);
IPC_MESSAGE_HANDLER(BluetoothMsg_ReadCharacteristicValueError,
OnReadValueError);
- IPC_MESSAGE_HANDLER(BluetoothMsg_WriteCharacteristicValueSuccess,
- OnWriteValueSuccess);
- IPC_MESSAGE_HANDLER(BluetoothMsg_WriteCharacteristicValueError,
- OnWriteValueError);
IPC_MESSAGE_HANDLER(BluetoothMsg_StartNotificationsSuccess,
OnStartNotificationsSuccess)
IPC_MESSAGE_HANDLER(BluetoothMsg_StartNotificationsError,
@@ -311,12 +302,22 @@ void BluetoothDispatcher::writeValue(
const blink::WebString& characteristic_instance_id,
const blink::WebVector<uint8_t>& value,
blink::WebBluetoothWriteValueCallbacks* callbacks) {
- int request_id = pending_write_value_requests_.Add(
- new BluetoothWriteValueRequest(value, callbacks));
- Send(new BluetoothHostMsg_WriteValue(
- CurrentWorkerId(), request_id, frame_routing_id,
- characteristic_instance_id.utf8(),
- std::vector<uint8_t>(value.begin(), value.end())));
+ GetBluetoothService()->RemoteCharacteristicWriteValue(
+ frame_routing_id, mojo::String::From(characteristic_instance_id),
+ mojo::Array<uint8_t>::From(value),
+ base::Bind(&BluetoothDispatcher::OnWriteValue, base::Unretained(this),
ortuno 2016/03/09 01:21:23 This is safe because if the class gets destroyed t
Ken Rockot(use gerrit already) 2016/03/09 19:31:26 Correct, the callback will never be invoked if the
+ value, base::Passed(make_scoped_ptr(callbacks))));
+}
+
+void BluetoothDispatcher::OnWriteValue(
+ const blink::WebVector<uint8_t>& value,
+ scoped_ptr<blink::WebBluetoothWriteValueCallbacks> callbacks,
+ blink::mojom::WebBluetoothError error) {
+ if (error == blink::mojom::WebBluetoothError::SUCCESS) {
ortuno 2016/03/09 01:21:23 It would be nice if there was a way we could do th
Ken Rockot(use gerrit already) 2016/03/09 19:31:26 Can you elaborate on what a more ideal situation w
ortuno 2016/03/16 16:29:45 Jeffrey started a thread about it a while ago: htt
+ callbacks->onSuccess(value);
+ } else {
+ callbacks->onError(error);
+ }
}
void BluetoothDispatcher::startNotifications(
@@ -629,9 +630,10 @@ void BluetoothDispatcher::OnRequestDeviceSuccess(
void BluetoothDispatcher::OnRequestDeviceError(int thread_id,
int request_id,
- WebBluetoothError error) {
+ blink::WebBluetoothError error) {
DCHECK(pending_requests_.Lookup(request_id)) << request_id;
- pending_requests_.Lookup(request_id)->onError(WebBluetoothError(error));
+ pending_requests_.Lookup(request_id)
+ ->onError(blink::WebBluetoothError(error));
pending_requests_.Remove(request_id);
}
@@ -642,12 +644,13 @@ void BluetoothDispatcher::OnGATTServerConnectSuccess(int thread_id,
pending_connect_requests_.Remove(request_id);
}
-void BluetoothDispatcher::OnGATTServerConnectError(int thread_id,
- int request_id,
- WebBluetoothError error) {
+void BluetoothDispatcher::OnGATTServerConnectError(
+ int thread_id,
+ int request_id,
+ blink::WebBluetoothError error) {
DCHECK(pending_connect_requests_.Lookup(request_id)) << request_id;
pending_connect_requests_.Lookup(request_id)
- ->onError(WebBluetoothError(error));
+ ->onError(blink::WebBluetoothError(error));
pending_connect_requests_.Remove(request_id);
}
@@ -665,13 +668,14 @@ void BluetoothDispatcher::OnGetPrimaryServiceSuccess(
pending_primary_service_requests_.Remove(request_id);
}
-void BluetoothDispatcher::OnGetPrimaryServiceError(int thread_id,
- int request_id,
- WebBluetoothError error) {
+void BluetoothDispatcher::OnGetPrimaryServiceError(
+ int thread_id,
+ int request_id,
+ blink::WebBluetoothError error) {
DCHECK(pending_primary_service_requests_.Lookup(request_id)) << request_id;
pending_primary_service_requests_.Lookup(request_id)
- ->callbacks->onError(WebBluetoothError(error));
+ ->callbacks->onError(blink::WebBluetoothError(error));
pending_primary_service_requests_.Remove(request_id);
}
@@ -693,13 +697,14 @@ void BluetoothDispatcher::OnGetCharacteristicSuccess(
pending_characteristic_requests_.Remove(request_id);
}
-void BluetoothDispatcher::OnGetCharacteristicError(int thread_id,
- int request_id,
- WebBluetoothError error) {
+void BluetoothDispatcher::OnGetCharacteristicError(
+ int thread_id,
+ int request_id,
+ blink::WebBluetoothError error) {
DCHECK(pending_characteristic_requests_.Lookup(request_id)) << request_id;
pending_characteristic_requests_.Lookup(request_id)
- ->callbacks->onError(WebBluetoothError(error));
+ ->callbacks->onError(blink::WebBluetoothError(error));
pending_characteristic_requests_.Remove(request_id);
}
@@ -732,13 +737,14 @@ void BluetoothDispatcher::OnGetCharacteristicsSuccess(
pending_characteristics_requests_.Remove(request_id);
}
-void BluetoothDispatcher::OnGetCharacteristicsError(int thread_id,
- int request_id,
- WebBluetoothError error) {
+void BluetoothDispatcher::OnGetCharacteristicsError(
+ int thread_id,
+ int request_id,
+ blink::WebBluetoothError error) {
DCHECK(pending_characteristics_requests_.Lookup(request_id)) << request_id;
pending_characteristics_requests_.Lookup(request_id)
- ->callbacks->onError(WebBluetoothError(error));
+ ->callbacks->onError(blink::WebBluetoothError(error));
pending_characteristics_requests_.Remove(request_id);
}
@@ -758,37 +764,15 @@ void BluetoothDispatcher::OnReadValueSuccess(
void BluetoothDispatcher::OnReadValueError(int thread_id,
int request_id,
- WebBluetoothError error) {
+ blink::WebBluetoothError error) {
DCHECK(pending_read_value_requests_.Lookup(request_id)) << request_id;
pending_read_value_requests_.Lookup(request_id)
- ->onError(WebBluetoothError(error));
+ ->onError(blink::WebBluetoothError(error));
pending_read_value_requests_.Remove(request_id);
}
-void BluetoothDispatcher::OnWriteValueSuccess(int thread_id, int request_id) {
- DCHECK(pending_write_value_requests_.Lookup(request_id)) << request_id;
-
- BluetoothWriteValueRequest* request =
- pending_write_value_requests_.Lookup(request_id);
- request->callbacks->onSuccess(request->value);
-
- pending_write_value_requests_.Remove(request_id);
-}
-
-void BluetoothDispatcher::OnWriteValueError(int thread_id,
- int request_id,
- WebBluetoothError error) {
- DCHECK(pending_write_value_requests_.Lookup(request_id)) << request_id;
-
- BluetoothWriteValueRequest* request =
- pending_write_value_requests_.Lookup(request_id);
- request->callbacks->onError(WebBluetoothError(error));
-
- pending_write_value_requests_.Remove(request_id);
-}
-
void BluetoothDispatcher::OnStartNotificationsSuccess(int thread_id,
int request_id) {
DCHECK(pending_notifications_requests_.Lookup(request_id)) << request_id;
@@ -825,9 +809,10 @@ void BluetoothDispatcher::OnStartNotificationsSuccess(int thread_id,
PopNotificationRequestQueueAndProcessNext(request_id);
}
-void BluetoothDispatcher::OnStartNotificationsError(int thread_id,
- int request_id,
- WebBluetoothError error) {
+void BluetoothDispatcher::OnStartNotificationsError(
+ int thread_id,
+ int request_id,
+ blink::WebBluetoothError error) {
DCHECK(pending_notifications_requests_.Lookup(request_id)) << request_id;
BluetoothNotificationsRequest* request =
@@ -876,4 +861,12 @@ void BluetoothDispatcher::OnCharacteristicValueChanged(
}
}
+mojom::BluetoothServicePtr& BluetoothDispatcher::GetBluetoothService() {
+ if (!bluetooth_service_) {
+ service_registry_->ConnectToRemoteService(
+ mojo::GetProxy(&bluetooth_service_));
+ }
+ return bluetooth_service_;
+}
+
} // namespace content

Powered by Google App Engine
This is Rietveld 408576698