diff options
author | vandebo@chromium.org <vandebo@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-29 23:42:22 +0000 |
---|---|---|
committer | vandebo@chromium.org <vandebo@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-29 23:42:22 +0000 |
commit | c2cab5528181d515e195ef5f31768e88cd15fbbd (patch) | |
tree | 3baa3c0f732a655d9e3fc4ad06ef46bdf411ba47 /base/system_monitor | |
parent | 95c796556ae6415b71b364c1bfd6257e8ca3a38e (diff) | |
download | chromium_src-c2cab5528181d515e195ef5f31768e88cd15fbbd.zip chromium_src-c2cab5528181d515e195ef5f31768e88cd15fbbd.tar.gz chromium_src-c2cab5528181d515e195ef5f31768e88cd15fbbd.tar.bz2 |
Add Media device notification to SystemMonitor and Mac impl
BUG=110400
TEST=run chrome with -v=1, attach and a mass storage media device and observe the log entries for the attach and detach events.
Review URL: https://chromiumcodereview.appspot.com/9363008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@124288 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/system_monitor')
-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 |
3 files changed, 80 insertions, 28 deletions
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 |