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

Unified Diff: device/bluetooth/bluetooth_adapter_factory_wrapper.cc

Issue 2059543002: bluetooth: Move FactoryWrapper to device and expose a function for testing in chooser controller (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@my-origin
Patch Set: Remove code from client interface Created 4 years, 5 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
« no previous file with comments | « device/bluetooth/bluetooth_adapter_factory_wrapper.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: device/bluetooth/bluetooth_adapter_factory_wrapper.cc
diff --git a/content/browser/bluetooth/bluetooth_adapter_factory_wrapper.cc b/device/bluetooth/bluetooth_adapter_factory_wrapper.cc
similarity index 55%
rename from content/browser/bluetooth/bluetooth_adapter_factory_wrapper.cc
rename to device/bluetooth/bluetooth_adapter_factory_wrapper.cc
index 5623d5177fc0740cb670a41ec8811c7655ae8744..50318965e7db8490fbc37b772d2fe84e664c3bd0 100644
--- a/content/browser/bluetooth/bluetooth_adapter_factory_wrapper.cc
+++ b/device/bluetooth/bluetooth_adapter_factory_wrapper.cc
@@ -2,54 +2,48 @@
// 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 "device/bluetooth/bluetooth_adapter_factory_wrapper.h"
#include <stddef.h>
#include <utility>
+#include "base/bind.h"
+#include "base/location.h"
#include "base/threading/thread_task_runner_handle.h"
-#include "base/time/time.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 {
+static base::LazyInstance<device::BluetoothAdapterFactoryWrapper>::Leaky
+ g_singleton = LAZY_INSTANCE_INITIALIZER;
-BluetoothAdapterFactoryWrapper::BluetoothAdapterFactoryWrapper()
- : scan_duration_(base::TimeDelta::FromSecondsD(kScanDuration)),
- testing_(false),
- weak_ptr_factory_(this) {
- DCHECK_CURRENTLY_ON(BrowserThread::UI);
-}
+} // namespace
+
+namespace device {
BluetoothAdapterFactoryWrapper::~BluetoothAdapterFactoryWrapper() {
- DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ DCHECK(thread_checker_.CalledOnValidThread());
// All observers should have been removed already.
DCHECK(adapter_observers_.empty());
// Clear adapter.
- set_adapter(scoped_refptr<device::BluetoothAdapter>());
+ set_adapter(scoped_refptr<BluetoothAdapter>());
+}
+
+// static
+BluetoothAdapterFactoryWrapper& BluetoothAdapterFactoryWrapper::Get() {
+ return g_singleton.Get();
}
bool BluetoothAdapterFactoryWrapper::IsBluetoothAdapterAvailable() {
- DCHECK_CURRENTLY_ON(BrowserThread::UI);
- return BluetoothAdapterFactory::IsBluetoothAdapterAvailable() || testing_;
+ DCHECK(thread_checker_.CalledOnValidThread());
+ return BluetoothAdapterFactory::IsBluetoothAdapterAvailable();
}
void BluetoothAdapterFactoryWrapper::AcquireAdapter(
- device::BluetoothAdapter::Observer* observer,
+ BluetoothAdapter::Observer* observer,
const AcquireAdapterCallback& callback) {
- DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!GetAdapter(observer));
AddAdapterObserver(observer);
@@ -66,19 +60,19 @@ void BluetoothAdapterFactoryWrapper::AcquireAdapter(
}
void BluetoothAdapterFactoryWrapper::ReleaseAdapter(
- device::BluetoothAdapter::Observer* observer) {
- DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ BluetoothAdapter::Observer* observer) {
+ DCHECK(thread_checker_.CalledOnValidThread());
if (!HasAdapter(observer)) {
return;
}
RemoveAdapterObserver(observer);
if (adapter_observers_.empty())
- set_adapter(scoped_refptr<device::BluetoothAdapter>());
+ set_adapter(scoped_refptr<BluetoothAdapter>());
}
BluetoothAdapter* BluetoothAdapterFactoryWrapper::GetAdapter(
- device::BluetoothAdapter::Observer* observer) {
- DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ BluetoothAdapter::Observer* observer) {
+ DCHECK(thread_checker_.CalledOnValidThread());
if (HasAdapter(observer)) {
return adapter_.get();
}
@@ -86,30 +80,36 @@ BluetoothAdapter* BluetoothAdapterFactoryWrapper::GetAdapter(
}
void BluetoothAdapterFactoryWrapper::SetBluetoothAdapterForTesting(
- scoped_refptr<device::BluetoothAdapter> mock_adapter) {
- DCHECK_CURRENTLY_ON(BrowserThread::UI);
- scan_duration_ = base::TimeDelta::FromSecondsD(kTestingScanDuration);
- testing_ = true;
+ scoped_refptr<BluetoothAdapter> mock_adapter) {
+ DCHECK(thread_checker_.CalledOnValidThread());
set_adapter(std::move(mock_adapter));
}
+BluetoothAdapterFactoryWrapper::BluetoothAdapterFactoryWrapper()
+ : weak_ptr_factory_(this) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+}
+
void BluetoothAdapterFactoryWrapper::OnGetAdapter(
const AcquireAdapterCallback& continuation,
- scoped_refptr<device::BluetoothAdapter> adapter) {
- DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ scoped_refptr<BluetoothAdapter> adapter) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
set_adapter(adapter);
continuation.Run(adapter_.get());
}
bool BluetoothAdapterFactoryWrapper::HasAdapter(
- device::BluetoothAdapter::Observer* observer) {
- DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ BluetoothAdapter::Observer* observer) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
return ContainsKey(adapter_observers_, observer);
}
void BluetoothAdapterFactoryWrapper::AddAdapterObserver(
- device::BluetoothAdapter::Observer* observer) {
- DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ BluetoothAdapter::Observer* observer) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
auto iter = adapter_observers_.insert(observer);
DCHECK(iter.second);
if (adapter_) {
@@ -118,8 +118,9 @@ void BluetoothAdapterFactoryWrapper::AddAdapterObserver(
}
void BluetoothAdapterFactoryWrapper::RemoveAdapterObserver(
- device::BluetoothAdapter::Observer* observer) {
- DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ BluetoothAdapter::Observer* observer) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
size_t removed = adapter_observers_.erase(observer);
DCHECK(removed);
if (adapter_) {
@@ -128,19 +129,20 @@ void BluetoothAdapterFactoryWrapper::RemoveAdapterObserver(
}
void BluetoothAdapterFactoryWrapper::set_adapter(
- scoped_refptr<device::BluetoothAdapter> adapter) {
- DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ scoped_refptr<BluetoothAdapter> adapter) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
if (adapter_.get()) {
- for (device::BluetoothAdapter::Observer* observer : adapter_observers_) {
+ for (BluetoothAdapter::Observer* observer : adapter_observers_) {
adapter_->RemoveObserver(observer);
}
}
adapter_ = adapter;
if (adapter_.get()) {
- for (device::BluetoothAdapter::Observer* observer : adapter_observers_) {
+ for (BluetoothAdapter::Observer* observer : adapter_observers_) {
adapter_->AddObserver(observer);
}
}
}
-} // namespace content
+} // namespace device
« no previous file with comments | « device/bluetooth/bluetooth_adapter_factory_wrapper.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698