diff options
author | vandebo@chromium.org <vandebo@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-02 23:13:40 +0000 |
---|---|---|
committer | vandebo@chromium.org <vandebo@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-02 23:13:40 +0000 |
commit | df6c4198e717be00adaeadd89293ef20bc551ebc (patch) | |
tree | 4c03a73da2c74168a55da21e0f324fa422f312ad /base | |
parent | 7e2c66d4bc593360a26cb95d551f34ab8138fd0d (diff) | |
download | chromium_src-df6c4198e717be00adaeadd89293ef20bc551ebc.zip chromium_src-df6c4198e717be00adaeadd89293ef20bc551ebc.tar.gz chromium_src-df6c4198e717be00adaeadd89293ef20bc551ebc.tar.bz2 |
Add media device attach notification mechanism.
Split out from https://chromiumcodereview.appspot.com/9363008/
BUG=110400
TEST=NONE
Review URL: http://codereview.chromium.org/9580018
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@124763 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/base.gyp | 2 | ||||
-rw-r--r-- | base/system_monitor/system_monitor.cc | 27 | ||||
-rw-r--r-- | base/system_monitor/system_monitor.h | 27 | ||||
-rw-r--r-- | base/system_monitor/system_monitor_unittest.cc | 54 | ||||
-rw-r--r-- | base/test/mock_devices_changed_observer.cc | 17 | ||||
-rw-r--r-- | base/test/mock_devices_changed_observer.h | 36 |
6 files changed, 135 insertions, 28 deletions
diff --git a/base/base.gyp b/base/base.gyp index c13a93e..1a1caee 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -409,6 +409,8 @@ 'perftimer.cc', 'test/mock_chrome_application_mac.h', 'test/mock_chrome_application_mac.mm', + 'test/mock_devices_changed_observer.cc', + 'test/mock_devices_changed_observer.h', 'test/mock_time_provider.cc', 'test/mock_time_provider.h', 'test/multiprocess_test.cc', diff --git a/base/system_monitor/system_monitor.cc b/base/system_monitor/system_monitor.cc index 28fc70b..afca3fc 100644 --- a/base/system_monitor/system_monitor.cc +++ b/base/system_monitor/system_monitor.cc @@ -1,9 +1,10 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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 "base/system_monitor/system_monitor.h" +#include "base/file_path.h" #include "base/logging.h" #include "base/message_loop.h" #include "base/time.h" @@ -83,6 +84,16 @@ void SystemMonitor::ProcessDevicesChanged() { NotifyDevicesChanged(); } +void SystemMonitor::ProcessMediaDeviceAttached(const DeviceIdType& id, + const std::string& name, + const FilePath& path) { + NotifyMediaDeviceAttached(id, name, path); +} + +void SystemMonitor::ProcessMediaDeviceDetached(const DeviceIdType& id) { + NotifyMediaDeviceDetached(id); +} + void SystemMonitor::AddPowerObserver(PowerObserver* obs) { power_observer_list_->AddObserver(obs); } @@ -105,6 +116,20 @@ void SystemMonitor::NotifyDevicesChanged() { &DevicesChangedObserver::OnDevicesChanged); } +void SystemMonitor::NotifyMediaDeviceAttached(const DeviceIdType& id, + const std::string& name, + const FilePath& path) { + DVLOG(1) << "MediaDeviceAttached with name " << name << " and id " << id; + devices_changed_observer_list_->Notify( + &DevicesChangedObserver::OnMediaDeviceAttached, id, name, path); +} + +void SystemMonitor::NotifyMediaDeviceDetached(const DeviceIdType& id) { + DVLOG(1) << "MediaDeviceDetached for id " << id; + devices_changed_observer_list_->Notify( + &DevicesChangedObserver::OnMediaDeviceDetached, id); +} + void SystemMonitor::NotifyPowerStateChange() { DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") << " battery"; diff --git a/base/system_monitor/system_monitor.h b/base/system_monitor/system_monitor.h index 7684523..3f73b15 100644 --- a/base/system_monitor/system_monitor.h +++ b/base/system_monitor/system_monitor.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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. @@ -6,6 +6,8 @@ #define BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ #pragma once +#include <string> + #include "base/base_export.h" #include "base/basictypes.h" #include "build/build_config.h" @@ -28,6 +30,8 @@ #include <IOKit/IOMessage.h> #endif // OS_MACOSX +class FilePath; + namespace base { // Class for monitoring various system-related subsystems @@ -42,6 +46,8 @@ class BASE_EXPORT SystemMonitor { RESUME_EVENT // The system is being resumed. }; + typedef unsigned int DeviceIdType; + // Create SystemMonitor. Only one SystemMonitor instance per application // is allowed. SystemMonitor(); @@ -93,8 +99,19 @@ class BASE_EXPORT SystemMonitor { class BASE_EXPORT DevicesChangedObserver { public: // Notification that the devices connected to the system have changed. + // This is only implemented on Windows currently. virtual void OnDevicesChanged() {} + // When a media device is attached or detached, one of these two events + // is triggered. + // TODO(vandebo) Pass an appropriate device identifier or way to interact + // with the devices instead of FilePath. + virtual void OnMediaDeviceAttached(const DeviceIdType& id, + const std::string& name, + const FilePath& path) {} + + virtual void OnMediaDeviceDetached(const DeviceIdType& id) {} + protected: virtual ~DevicesChangedObserver() {} }; @@ -123,6 +140,10 @@ class BASE_EXPORT SystemMonitor { // Cross-platform handling of a device change event. void ProcessDevicesChanged(); + void ProcessMediaDeviceAttached(const DeviceIdType& id, + const std::string& name, + const FilePath& path); + void ProcessMediaDeviceDetached(const DeviceIdType& id); private: #if defined(OS_MACOSX) @@ -141,6 +162,10 @@ class BASE_EXPORT SystemMonitor { // Functions to trigger notifications. void NotifyDevicesChanged(); + void NotifyMediaDeviceAttached(const DeviceIdType& id, + const std::string& name, + const FilePath& path); + void NotifyMediaDeviceDetached(const DeviceIdType& id); void NotifyPowerStateChange(); void NotifySuspend(); void NotifyResume(); diff --git a/base/system_monitor/system_monitor_unittest.cc b/base/system_monitor/system_monitor_unittest.cc index 1d5d6af..eaed974 100644 --- a/base/system_monitor/system_monitor_unittest.cc +++ b/base/system_monitor/system_monitor_unittest.cc @@ -1,8 +1,11 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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 "base/file_path.h" +#include "base/test/mock_devices_changed_observer.h" #include "base/system_monitor/system_monitor.h" +#include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" namespace base { @@ -90,26 +93,6 @@ TEST(SystemMonitor, PowerNotifications) { EXPECT_EQ(test[0].resumes(), 1); } -class DevicesChangedTest : public SystemMonitor::DevicesChangedObserver { - public: - DevicesChangedTest() - : changes_(0) { - } - - // DevicesChangedObserver callbacks. - virtual void OnDevicesChanged() OVERRIDE { - changes_++; - } - - // Test status counts. - int changes() const { return changes_; } - - private: - int changes_; // Count of OnDevicesChanged notifications. - - DISALLOW_COPY_AND_ASSIGN(DevicesChangedTest); -}; - TEST(SystemMonitor, DeviceChangeNotifications) { const int kObservers = 5; @@ -120,19 +103,38 @@ TEST(SystemMonitor, DeviceChangeNotifications) { SystemMonitor::AllocateSystemIOPorts(); #endif + testing::Sequence mock_sequencer[kObservers]; SystemMonitor system_monitor; - DevicesChangedTest test[kObservers]; - for (int index = 0; index < kObservers; ++index) - system_monitor.AddDevicesChangedObserver(&test[index]); + MockDevicesChangedObserver observers[kObservers]; + for (int index = 0; index < kObservers; ++index) { + system_monitor.AddDevicesChangedObserver(&observers[index]); + + EXPECT_CALL(observers[index], OnDevicesChanged()) + .Times(3) + .InSequence(mock_sequencer[index]); + EXPECT_CALL(observers[index], OnMediaDeviceAttached(1, "media device", + testing::_)) + .InSequence(mock_sequencer[index]); + EXPECT_CALL(observers[index], OnMediaDeviceDetached(1)) + .InSequence(mock_sequencer[index]); + EXPECT_CALL(observers[index], OnMediaDeviceDetached(2)) + .InSequence(mock_sequencer[index]); + } system_monitor.ProcessDevicesChanged(); loop.RunAllPending(); - EXPECT_EQ(1, test[0].changes()); system_monitor.ProcessDevicesChanged(); system_monitor.ProcessDevicesChanged(); loop.RunAllPending(); - EXPECT_EQ(3, test[0].changes()); + + system_monitor.ProcessMediaDeviceAttached( + 1, "media device", FilePath(FILE_PATH_LITERAL("path"))); + loop.RunAllPending(); + + system_monitor.ProcessMediaDeviceDetached(1); + system_monitor.ProcessMediaDeviceDetached(2); + loop.RunAllPending(); } } // namespace base diff --git a/base/test/mock_devices_changed_observer.cc b/base/test/mock_devices_changed_observer.cc new file mode 100644 index 0000000..25ff613 --- /dev/null +++ b/base/test/mock_devices_changed_observer.cc @@ -0,0 +1,17 @@ +// Copyright (c) 2012 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 "base/test/mock_devices_changed_observer.h" + +#include "base/file_path.h" + +namespace base { + +MockDevicesChangedObserver::MockDevicesChangedObserver() { +} + +MockDevicesChangedObserver::~MockDevicesChangedObserver() { +} + +} // namespace base diff --git a/base/test/mock_devices_changed_observer.h b/base/test/mock_devices_changed_observer.h new file mode 100644 index 0000000..9964f06 --- /dev/null +++ b/base/test/mock_devices_changed_observer.h @@ -0,0 +1,36 @@ +// Copyright (c) 2012 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. + +#ifndef BASE_TEST_MOCK_DEVICES_CHANGED_OBSERVER_H_ +#define BASE_TEST_MOCK_DEVICES_CHANGED_OBSERVER_H_ + +#include <string> + +#include "base/system_monitor/system_monitor.h" +#include "testing/gmock/include/gmock/gmock.h" + +class FilePath; + +namespace base { + +class MockDevicesChangedObserver + : public base::SystemMonitor::DevicesChangedObserver { + public: + MockDevicesChangedObserver(); + ~MockDevicesChangedObserver(); + + MOCK_METHOD0(OnDevicesChanged, void()); + MOCK_METHOD3(OnMediaDeviceAttached, + void(const base::SystemMonitor::DeviceIdType& id, + const std::string& name, + const FilePath& path)); + MOCK_METHOD1(OnMediaDeviceDetached, + void(const base::SystemMonitor::DeviceIdType& id)); + + DISALLOW_COPY_AND_ASSIGN(MockDevicesChangedObserver); +}; + +} // namespace base + +#endif // BASE_TEST_MOCK_DEVICES_CHANGED_OBSERVER_H_ |