Skip to content

Commit

Permalink
Add Media device notification to SystemMonitor and Mac impl
Browse files Browse the repository at this point in the history
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
  • Loading branch information
vandebo@chromium.org committed Feb 29, 2012
1 parent 95c7965 commit c2cab55
Show file tree
Hide file tree
Showing 12 changed files with 300 additions and 28 deletions.
2 changes: 2 additions & 0 deletions base/base.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,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',
Expand Down
2 changes: 2 additions & 0 deletions base/mac/foundation_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ TYPE_NAME_FOR_CF_TYPE_DECL(CFNull);
TYPE_NAME_FOR_CF_TYPE_DECL(CFNumber);
TYPE_NAME_FOR_CF_TYPE_DECL(CFSet);
TYPE_NAME_FOR_CF_TYPE_DECL(CFString);
TYPE_NAME_FOR_CF_TYPE_DECL(CFURL);

#undef TYPE_NAME_FOR_CF_TYPE_DECL

Expand Down Expand Up @@ -244,6 +245,7 @@ CF_CAST_DECL(CFNull);
CF_CAST_DECL(CFNumber);
CF_CAST_DECL(CFSet);
CF_CAST_DECL(CFString);
CF_CAST_DECL(CFURL);

#undef CF_CAST_DEFN

Expand Down
2 changes: 2 additions & 0 deletions base/mac/foundation_util.mm
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ FilePath GetAppBundlePath(const FilePath& exec_name) {
TYPE_NAME_FOR_CF_TYPE_DEFN(CFNumber);
TYPE_NAME_FOR_CF_TYPE_DEFN(CFSet);
TYPE_NAME_FOR_CF_TYPE_DEFN(CFString);
TYPE_NAME_FOR_CF_TYPE_DEFN(CFURL);

#undef TYPE_NAME_FOR_CF_TYPE_DEFN

Expand Down Expand Up @@ -311,6 +312,7 @@ void SetBaseBundleID(const char* new_base_bundle_id) {
CF_CAST_DEFN(CFNumber);
CF_CAST_DEFN(CFSet);
CF_CAST_DEFN(CFString);
CF_CAST_DEFN(CFURL);

#undef CF_CAST_DEFN

Expand Down
27 changes: 26 additions & 1 deletion base/system_monitor/system_monitor.cc
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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);
}
Expand All @@ -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";
Expand Down
27 changes: 26 additions & 1 deletion base/system_monitor/system_monitor.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// 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.

#ifndef BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_
#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"
Expand All @@ -28,6 +30,8 @@
#include <IOKit/IOMessage.h>
#endif // OS_MACOSX

class FilePath;

namespace base {

// Class for monitoring various system-related subsystems
Expand All @@ -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();
Expand Down Expand Up @@ -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() {}
};
Expand Down Expand Up @@ -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)
Expand All @@ -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();
Expand Down
54 changes: 28 additions & 26 deletions base/system_monitor/system_monitor_unittest.cc
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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;

Expand All @@ -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
17 changes: 17 additions & 0 deletions base/test/mock_devices_changed_observer.cc
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions base/test/mock_devices_changed_observer.h
Original file line number Diff line number Diff line change
@@ -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_
6 changes: 6 additions & 0 deletions content/browser/browser_main_loop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
#include "net/base/winsock_init.h"
#endif

#if defined(OS_MACOSX)
#include "content/browser/mac/media_device_notifications.h"
#endif

#if defined(OS_LINUX) || defined(OS_OPENBSD)
#include <glib-object.h>
#endif
Expand Down Expand Up @@ -327,6 +331,8 @@ void BrowserMainLoop::MainMessageLoopStart() {

#if defined(OS_WIN)
system_message_window_.reset(new SystemMessageWindowWin);
#elif defined(OS_MACOSX)
StartMediaDeviceNotifications();
#endif

// Prior to any processing happening on the io thread, we create the
Expand Down
15 changes: 15 additions & 0 deletions content/browser/mac/media_device_notifications.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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 CONTENT_BROWSER_MAC_MEDIA_DEVICE_NOTIFICATIONS_H_
#define CONTENT_BROWSER_MAC_MEDIA_DEVICE_NOTIFICATIONS_H_
#pragma once

namespace content {

void StartMediaDeviceNotifications();

} // namespace content

#endif // CONTENT_BROWSER_MAC_MEDIA_DEVICE_NOTIFICATIONS_H_
Loading

0 comments on commit c2cab55

Please sign in to comment.