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

Unified Diff: content/browser/bluetooth/bluetooth_adapter_factory_wrapper.cc

Issue 1922923002: bluetooth: Move requestDevice to mojo (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bluetooth-separate-tests-request-device
Patch Set: Remove debug log Created 4 years, 7 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/browser/bluetooth/bluetooth_adapter_factory_wrapper.cc
diff --git a/content/browser/bluetooth/bluetooth_adapter_factory_wrapper.cc b/content/browser/bluetooth/bluetooth_adapter_factory_wrapper.cc
new file mode 100644
index 0000000000000000000000000000000000000000..9f454f85a1b3382caf8132c4ca3b9df6d41b98c3
--- /dev/null
+++ b/content/browser/bluetooth/bluetooth_adapter_factory_wrapper.cc
@@ -0,0 +1,142 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/bluetooth/bluetooth_adapter_factory_wrapper.h"
+
+#include <stddef.h>
+
+#include <utility>
+
+#include "base/thread_task_runner_handle.h"
+#include "content/public/browser/browser_thread.h"
+#include "device/bluetooth/bluetooth_adapter_factory.h"
+
+using device::BluetoothAdapter;
+using device::BluetoothAdapterFactory;
+
+namespace {
+// TODO(ortuno): Once we have a chooser for scanning and a way to control that
+// chooser from tests we should delete this constant.
+// https://crbug.com/436280
+enum { kTestingScanDuration = 0 }; // No need to wait when testing.
+enum { kScanDuration = 10 };
+} // namespace
+
+namespace content {
+
+BluetoothAdapterFactoryWrapper::BluetoothAdapterFactoryWrapper()
+ : scan_duration_(kScanDuration), testing_(false), weak_ptr_factory_(this) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+}
+
+BluetoothAdapterFactoryWrapper::~BluetoothAdapterFactoryWrapper() {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ // All observers should have been removed already.
+ DCHECK(adapter_observers_.empty());
+ // Clear adapter.
+ set_adapter(scoped_refptr<device::BluetoothAdapter>());
+}
+
+bool BluetoothAdapterFactoryWrapper::IsBluetoothAdapterAvailable() {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ return BluetoothAdapterFactory::IsBluetoothAdapterAvailable() || testing_;
+}
+
+void BluetoothAdapterFactoryWrapper::AcquireAdapter(
+ device::BluetoothAdapter::Observer* observer,
+ const base::Closure& continuation) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ DCHECK(!GetAdapter(observer));
+
+ AddAdapterObserver(observer);
+ if (adapter_.get()) {
+ base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, continuation);
+ return;
+ }
+
+ DCHECK(BluetoothAdapterFactory::IsBluetoothAdapterAvailable());
+ BluetoothAdapterFactory::GetAdapter(
+ base::Bind(&BluetoothAdapterFactoryWrapper::OnGetAdapter,
+ weak_ptr_factory_.GetWeakPtr(), continuation));
+}
+
+void BluetoothAdapterFactoryWrapper::ReleaseAdapter(
+ device::BluetoothAdapter::Observer* observer) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ if (!HasAdapter(observer)) {
+ return;
+ }
+ RemoveAdapterObserver(observer);
+ if (adapter_observers_.empty())
+ set_adapter(scoped_refptr<device::BluetoothAdapter>());
+}
+
+BluetoothAdapter* BluetoothAdapterFactoryWrapper::GetAdapter(
+ device::BluetoothAdapter::Observer* observer) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ if (HasAdapter(observer)) {
+ return adapter_.get();
+ }
+ return nullptr;
+}
+
+void BluetoothAdapterFactoryWrapper::SetBluetoothAdapterForTesting(
+ scoped_refptr<device::BluetoothAdapter> mock_adapter) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ scan_duration_ = kTestingScanDuration;
+ testing_ = true;
+ set_adapter(std::move(mock_adapter));
+}
+
+void BluetoothAdapterFactoryWrapper::OnGetAdapter(
+ const base::Closure& continuation,
+ scoped_refptr<device::BluetoothAdapter> adapter) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ set_adapter(adapter);
+ continuation.Run();
+}
+
+bool BluetoothAdapterFactoryWrapper::HasAdapter(
+ device::BluetoothAdapter::Observer* observer) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ return ContainsKey(adapter_observers_, observer);
+}
+
+void BluetoothAdapterFactoryWrapper::AddAdapterObserver(
+ device::BluetoothAdapter::Observer* observer) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ auto iter = adapter_observers_.insert(observer);
+ DCHECK(iter.second);
+ if (adapter_) {
+ adapter_->AddObserver(observer);
+ }
+}
+
+void BluetoothAdapterFactoryWrapper::RemoveAdapterObserver(
+ device::BluetoothAdapter::Observer* observer) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ size_t removed = adapter_observers_.erase(observer);
+ DCHECK(removed);
+ if (adapter_) {
+ adapter_->RemoveObserver(observer);
+ }
+}
+
+void BluetoothAdapterFactoryWrapper::set_adapter(
+ scoped_refptr<device::BluetoothAdapter> adapter) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ if (adapter_.get()) {
+ for (device::BluetoothAdapter::Observer* observer : adapter_observers_) {
+ adapter_->RemoveObserver(observer);
+ }
+ }
+ adapter_ = adapter;
+ if (adapter_.get()) {
+ for (device::BluetoothAdapter::Observer* observer : adapter_observers_) {
+ adapter_->AddObserver(observer);
+ }
+ }
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698