diff options
author | jpawlowski <jpawlowski@chromium.org> | 2015-04-16 16:58:44 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-04-16 23:59:02 +0000 |
commit | b51933daebb2453735df35a21519d7c4439c25fc (patch) | |
tree | 782b8874ea631830c6783857b7b480ffe732fcd5 | |
parent | 76302d920e57d6d0c368d827a78817aecea541ba (diff) | |
download | chromium_src-b51933daebb2453735df35a21519d7c4439c25fc.zip chromium_src-b51933daebb2453735df35a21519d7c4439c25fc.tar.gz chromium_src-b51933daebb2453735df35a21519d7c4439c25fc.tar.bz2 |
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}
5 files changed, 133 insertions, 8 deletions
diff --git a/device/bluetooth/test/mock_bluetooth_adapter.cc b/device/bluetooth/test/mock_bluetooth_adapter.cc index 1ee00ee..a021bfd 100644 --- a/device/bluetooth/test/mock_bluetooth_adapter.cc +++ b/device/bluetooth/test/mock_bluetooth_adapter.cc @@ -41,4 +41,12 @@ void MockBluetoothAdapter::SetDiscoveryFilter( 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 diff --git a/device/bluetooth/test/mock_bluetooth_adapter.h b/device/bluetooth/test/mock_bluetooth_adapter.h index d338256..3abe858 100644 --- a/device/bluetooth/test/mock_bluetooth_adapter.h +++ b/device/bluetooth/test/mock_bluetooth_adapter.h @@ -61,6 +61,10 @@ class MockBluetoothAdapter : public BluetoothAdapter { MOCK_METHOD2(StartDiscoverySession, void(const DiscoverySessionCallback& 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_METHOD1(GetDevice, BluetoothDevice*(const std::string& address)); MOCK_CONST_METHOD1(GetDevice, @@ -86,6 +90,11 @@ class MockBluetoothAdapter : public BluetoothAdapter { const AcquiredCallback& callback, const BluetoothAudioSink::ErrorCallback& error_callback)); + void StartDiscoverySessionWithFilter( + scoped_ptr<BluetoothDiscoveryFilter> discovery_filter, + const DiscoverySessionCallback& callback, + const ErrorCallback& error_callback); + protected: void DeleteOnCorrectThread() const override; void AddDiscoverySession(BluetoothDiscoveryFilter* discovery_filter, diff --git a/extensions/browser/api/bluetooth/bluetooth_event_router.cc b/extensions/browser/api/bluetooth/bluetooth_event_router.cc index 1064d0b..d7f4844 100644 --- a/extensions/browser/api/bluetooth/bluetooth_event_router.cc +++ b/extensions/browser/api/bluetooth/bluetooth_event_router.cc @@ -90,11 +90,24 @@ void BluetoothEventRouter::StartDiscoverySession( error_callback.Run(); 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( base::Bind(&BluetoothEventRouter::OnStartDiscoverySession, - weak_ptr_factory_.GetWeakPtr(), - extension_id, - callback), + weak_ptr_factory_.GetWeakPtr(), extension_id, callback), error_callback); } @@ -118,6 +131,33 @@ void BluetoothEventRouter::StopDiscoverySession( 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( const std::string& extension_id) { return ContainsKey(pairing_delegate_map_, extension_id) @@ -312,6 +352,13 @@ void BluetoothEventRouter::CleanUpForExtension( DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 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. DiscoverySessionMap::iterator session_iter = discovery_session_map_.find(extension_id); @@ -322,11 +369,14 @@ void BluetoothEventRouter::CleanUpForExtension( } void BluetoothEventRouter::CleanUpAllExtensions() { - for (DiscoverySessionMap::iterator it = discovery_session_map_.begin(); - it != discovery_session_map_.end(); - ++it) { - delete it->second; - } + for (auto& it : pre_set_filter_map_) + delete it.second; + + pre_set_filter_map_.clear(); + + for (auto& it : discovery_session_map_) + delete it.second; + discovery_session_map_.clear(); PairingDelegateMap::iterator pairing_iter = pairing_delegate_map_.begin(); @@ -347,6 +397,12 @@ void BluetoothEventRouter::OnStartDiscoverySession( callback.Run(); } +void BluetoothEventRouter::OnSetDiscoveryFilter(const std::string& extension_id, + const base::Closure& callback) { + DVLOG(1) << "Successfully set DiscoveryFilter."; + callback.Run(); +} + void BluetoothEventRouter::Observe( int type, const content::NotificationSource& source, diff --git a/extensions/browser/api/bluetooth/bluetooth_event_router.h b/extensions/browser/api/bluetooth/bluetooth_event_router.h index 60db922..6b544e6 100644 --- a/extensions/browser/api/bluetooth/bluetooth_event_router.h +++ b/extensions/browser/api/bluetooth/bluetooth_event_router.h @@ -68,6 +68,17 @@ class BluetoothEventRouter : public device::BluetoothAdapter::Observer, const base::Closure& 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. void OnListenerAdded(); @@ -133,6 +144,9 @@ class BluetoothEventRouter : public device::BluetoothAdapter::Observer, const base::Closure& callback, scoped_ptr<device::BluetoothDiscoverySession> discovery_session); + void OnSetDiscoveryFilter(const std::string& extension_id, + const base::Closure& callback); + content::BrowserContext* browser_context_; scoped_refptr<device::BluetoothAdapter> adapter_; @@ -143,6 +157,12 @@ class BluetoothEventRouter : public device::BluetoothAdapter::Observer, DiscoverySessionMap; 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. typedef std::map<std::string, BluetoothApiPairingDelegate*> PairingDelegateMap; diff --git a/extensions/browser/api/bluetooth/bluetooth_event_router_unittest.cc b/extensions/browser/api/bluetooth/bluetooth_event_router_unittest.cc index 7f5a412..f8aba44 100644 --- a/extensions/browser/api/bluetooth/bluetooth_event_router_unittest.cc +++ b/extensions/browser/api/bluetooth/bluetooth_event_router_unittest.cc @@ -27,6 +27,9 @@ const char kTestExtensionId[] = "test extension id"; const device::BluetoothUUID kAudioProfileUuid("1234"); const device::BluetoothUUID kHealthProfileUuid("4321"); +MATCHER_P(IsFilterEqual, a, "") { + return arg.Equals(*a); +} } // namespace namespace extensions { @@ -91,4 +94,33 @@ TEST_F(BluetoothEventRouterTest, UnloadExtension) { 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 |