Commit b51933da authored by jpawlowski's avatar jpawlowski Committed by Commit bot

Expose SetDiscoveryFilter from BluetoothEventRouter

This patch makes it possible to control BluetoothDiscoveryFilter assigned to discovery sessions through BluetoothEventRouter router.

BUG=407773
R=armansito@chromium.org

Review URL: https://codereview.chromium.org/1083163002

Cr-Commit-Position: refs/heads/master@{#325557}
parent 76302d92
...@@ -41,4 +41,12 @@ void MockBluetoothAdapter::SetDiscoveryFilter( ...@@ -41,4 +41,12 @@ void MockBluetoothAdapter::SetDiscoveryFilter(
const ErrorCallback& error_callback) { const ErrorCallback& error_callback) {
} }
void MockBluetoothAdapter::StartDiscoverySessionWithFilter(
scoped_ptr<BluetoothDiscoveryFilter> discovery_filter,
const DiscoverySessionCallback& callback,
const ErrorCallback& error_callback) {
StartDiscoverySessionWithFilterRaw(discovery_filter.get(), callback,
error_callback);
}
} // namespace device } // namespace device
...@@ -61,6 +61,10 @@ class MockBluetoothAdapter : public BluetoothAdapter { ...@@ -61,6 +61,10 @@ class MockBluetoothAdapter : public BluetoothAdapter {
MOCK_METHOD2(StartDiscoverySession, MOCK_METHOD2(StartDiscoverySession,
void(const DiscoverySessionCallback& callback, void(const DiscoverySessionCallback& callback,
const ErrorCallback& error_callback)); const ErrorCallback& error_callback));
MOCK_METHOD3(StartDiscoverySessionWithFilterRaw,
void(const BluetoothDiscoveryFilter*,
const DiscoverySessionCallback& callback,
const ErrorCallback& error_callback));
MOCK_CONST_METHOD0(GetDevices, BluetoothAdapter::ConstDeviceList()); MOCK_CONST_METHOD0(GetDevices, BluetoothAdapter::ConstDeviceList());
MOCK_METHOD1(GetDevice, BluetoothDevice*(const std::string& address)); MOCK_METHOD1(GetDevice, BluetoothDevice*(const std::string& address));
MOCK_CONST_METHOD1(GetDevice, MOCK_CONST_METHOD1(GetDevice,
...@@ -86,6 +90,11 @@ class MockBluetoothAdapter : public BluetoothAdapter { ...@@ -86,6 +90,11 @@ class MockBluetoothAdapter : public BluetoothAdapter {
const AcquiredCallback& callback, const AcquiredCallback& callback,
const BluetoothAudioSink::ErrorCallback& error_callback)); const BluetoothAudioSink::ErrorCallback& error_callback));
void StartDiscoverySessionWithFilter(
scoped_ptr<BluetoothDiscoveryFilter> discovery_filter,
const DiscoverySessionCallback& callback,
const ErrorCallback& error_callback);
protected: protected:
void DeleteOnCorrectThread() const override; void DeleteOnCorrectThread() const override;
void AddDiscoverySession(BluetoothDiscoveryFilter* discovery_filter, void AddDiscoverySession(BluetoothDiscoveryFilter* discovery_filter,
......
...@@ -90,11 +90,24 @@ void BluetoothEventRouter::StartDiscoverySession( ...@@ -90,11 +90,24 @@ void BluetoothEventRouter::StartDiscoverySession(
error_callback.Run(); error_callback.Run();
return; return;
} }
// Check whether user pre set discovery filter by calling SetDiscoveryFilter
// before. If the user has set a discovery filter then start a filtered
// discovery session, otherwise start a regular session
PreSetFilterMap::iterator pre_set_iter =
pre_set_filter_map_.find(extension_id);
if (pre_set_iter != pre_set_filter_map_.end()) {
adapter->StartDiscoverySessionWithFilter(
scoped_ptr<device::BluetoothDiscoveryFilter>(pre_set_iter->second),
base::Bind(&BluetoothEventRouter::OnStartDiscoverySession,
weak_ptr_factory_.GetWeakPtr(), extension_id, callback),
error_callback);
pre_set_filter_map_.erase(pre_set_iter);
return;
}
adapter->StartDiscoverySession( adapter->StartDiscoverySession(
base::Bind(&BluetoothEventRouter::OnStartDiscoverySession, base::Bind(&BluetoothEventRouter::OnStartDiscoverySession,
weak_ptr_factory_.GetWeakPtr(), weak_ptr_factory_.GetWeakPtr(), extension_id, callback),
extension_id,
callback),
error_callback); error_callback);
} }
...@@ -118,6 +131,33 @@ void BluetoothEventRouter::StopDiscoverySession( ...@@ -118,6 +131,33 @@ void BluetoothEventRouter::StopDiscoverySession(
session->Stop(callback, error_callback); session->Stop(callback, error_callback);
} }
void BluetoothEventRouter::SetDiscoveryFilter(
scoped_ptr<device::BluetoothDiscoveryFilter> discovery_filter,
device::BluetoothAdapter* adapter,
const std::string& extension_id,
const base::Closure& callback,
const base::Closure& error_callback) {
DVLOG(1) << "SetDiscoveryFilter";
if (adapter != adapter_.get()) {
error_callback.Run();
return;
}
DiscoverySessionMap::iterator iter =
discovery_session_map_.find(extension_id);
if (iter == discovery_session_map_.end() || !iter->second->IsActive()) {
DVLOG(1) << "No active discovery session exists for extension, so caching "
"filter for later use.";
pre_set_filter_map_[extension_id] = discovery_filter.release();
callback.Run();
return;
}
// extension is already running discovery, update it's discovery filter
iter->second->SetDiscoveryFilter(discovery_filter.Pass(), callback,
error_callback);
}
BluetoothApiPairingDelegate* BluetoothEventRouter::GetPairingDelegate( BluetoothApiPairingDelegate* BluetoothEventRouter::GetPairingDelegate(
const std::string& extension_id) { const std::string& extension_id) {
return ContainsKey(pairing_delegate_map_, extension_id) return ContainsKey(pairing_delegate_map_, extension_id)
...@@ -312,6 +352,13 @@ void BluetoothEventRouter::CleanUpForExtension( ...@@ -312,6 +352,13 @@ void BluetoothEventRouter::CleanUpForExtension(
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
RemovePairingDelegate(extension_id); RemovePairingDelegate(extension_id);
PreSetFilterMap::iterator pre_set_iter =
pre_set_filter_map_.find(extension_id);
if (pre_set_iter != pre_set_filter_map_.end()) {
delete pre_set_iter->second;
pre_set_filter_map_.erase(pre_set_iter);
}
// Remove any discovery session initiated by the extension. // Remove any discovery session initiated by the extension.
DiscoverySessionMap::iterator session_iter = DiscoverySessionMap::iterator session_iter =
discovery_session_map_.find(extension_id); discovery_session_map_.find(extension_id);
...@@ -322,11 +369,14 @@ void BluetoothEventRouter::CleanUpForExtension( ...@@ -322,11 +369,14 @@ void BluetoothEventRouter::CleanUpForExtension(
} }
void BluetoothEventRouter::CleanUpAllExtensions() { void BluetoothEventRouter::CleanUpAllExtensions() {
for (DiscoverySessionMap::iterator it = discovery_session_map_.begin(); for (auto& it : pre_set_filter_map_)
it != discovery_session_map_.end(); delete it.second;
++it) {
delete it->second; pre_set_filter_map_.clear();
}
for (auto& it : discovery_session_map_)
delete it.second;
discovery_session_map_.clear(); discovery_session_map_.clear();
PairingDelegateMap::iterator pairing_iter = pairing_delegate_map_.begin(); PairingDelegateMap::iterator pairing_iter = pairing_delegate_map_.begin();
...@@ -347,6 +397,12 @@ void BluetoothEventRouter::OnStartDiscoverySession( ...@@ -347,6 +397,12 @@ void BluetoothEventRouter::OnStartDiscoverySession(
callback.Run(); callback.Run();
} }
void BluetoothEventRouter::OnSetDiscoveryFilter(const std::string& extension_id,
const base::Closure& callback) {
DVLOG(1) << "Successfully set DiscoveryFilter.";
callback.Run();
}
void BluetoothEventRouter::Observe( void BluetoothEventRouter::Observe(
int type, int type,
const content::NotificationSource& source, const content::NotificationSource& source,
......
...@@ -68,6 +68,17 @@ class BluetoothEventRouter : public device::BluetoothAdapter::Observer, ...@@ -68,6 +68,17 @@ class BluetoothEventRouter : public device::BluetoothAdapter::Observer,
const base::Closure& callback, const base::Closure& callback,
const base::Closure& error_callback); const base::Closure& error_callback);
// Requests that the filter associated with discovery session that belongs
// to the extension with id |extension_id| be set to |discovery_filter|.
// Callback is called, if the filter was successfully updated.
// |error_callback| is called, if filter update failed.
void SetDiscoveryFilter(
scoped_ptr<device::BluetoothDiscoveryFilter> discovery_filter,
device::BluetoothAdapter* adapter,
const std::string& extension_id,
const base::Closure& callback,
const base::Closure& error_callback);
// Called when a bluetooth event listener is added. // Called when a bluetooth event listener is added.
void OnListenerAdded(); void OnListenerAdded();
...@@ -133,6 +144,9 @@ class BluetoothEventRouter : public device::BluetoothAdapter::Observer, ...@@ -133,6 +144,9 @@ class BluetoothEventRouter : public device::BluetoothAdapter::Observer,
const base::Closure& callback, const base::Closure& callback,
scoped_ptr<device::BluetoothDiscoverySession> discovery_session); scoped_ptr<device::BluetoothDiscoverySession> discovery_session);
void OnSetDiscoveryFilter(const std::string& extension_id,
const base::Closure& callback);
content::BrowserContext* browser_context_; content::BrowserContext* browser_context_;
scoped_refptr<device::BluetoothAdapter> adapter_; scoped_refptr<device::BluetoothAdapter> adapter_;
...@@ -143,6 +157,12 @@ class BluetoothEventRouter : public device::BluetoothAdapter::Observer, ...@@ -143,6 +157,12 @@ class BluetoothEventRouter : public device::BluetoothAdapter::Observer,
DiscoverySessionMap; DiscoverySessionMap;
DiscoverySessionMap discovery_session_map_; DiscoverySessionMap discovery_session_map_;
typedef std::map<std::string, device::BluetoothDiscoveryFilter*>
PreSetFilterMap;
// Maps an extension id to it's pre-set discovery filter.
PreSetFilterMap pre_set_filter_map_;
// Maps an extension id to its pairing delegate. // Maps an extension id to its pairing delegate.
typedef std::map<std::string, BluetoothApiPairingDelegate*> typedef std::map<std::string, BluetoothApiPairingDelegate*>
PairingDelegateMap; PairingDelegateMap;
......
...@@ -27,6 +27,9 @@ const char kTestExtensionId[] = "test extension id"; ...@@ -27,6 +27,9 @@ const char kTestExtensionId[] = "test extension id";
const device::BluetoothUUID kAudioProfileUuid("1234"); const device::BluetoothUUID kAudioProfileUuid("1234");
const device::BluetoothUUID kHealthProfileUuid("4321"); const device::BluetoothUUID kHealthProfileUuid("4321");
MATCHER_P(IsFilterEqual, a, "") {
return arg.Equals(*a);
}
} // namespace } // namespace
namespace extensions { namespace extensions {
...@@ -91,4 +94,33 @@ TEST_F(BluetoothEventRouterTest, UnloadExtension) { ...@@ -91,4 +94,33 @@ TEST_F(BluetoothEventRouterTest, UnloadExtension) {
EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
} }
// This test check that calling SetDiscoveryFilter before StartDiscoverySession
// for given extension will start session with proper filter.
TEST_F(BluetoothEventRouterTest, SetDiscoveryFilter) {
scoped_ptr<device::BluetoothDiscoveryFilter> discovery_filter(
new device::BluetoothDiscoveryFilter(
device::BluetoothDiscoveryFilter::Transport::TRANSPORT_LE));
discovery_filter->SetRSSI(-80);
discovery_filter->AddUUID(device::BluetoothUUID("1000"));
device::BluetoothDiscoveryFilter df(
device::BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
df.CopyFrom(*discovery_filter);
router_->SetDiscoveryFilter(discovery_filter.Pass(), mock_adapter_,
kTestExtensionId, base::Bind(&base::DoNothing),
base::Bind(&base::DoNothing));
EXPECT_CALL(*mock_adapter_, StartDiscoverySessionWithFilterRaw(
testing::Pointee(IsFilterEqual(&df)),
testing::_, testing::_)).Times(1);
router_->StartDiscoverySession(mock_adapter_, kTestExtensionId,
base::Bind(&base::DoNothing),
base::Bind(&base::DoNothing));
EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
}
} // namespace extensions } // namespace extensions
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment