Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2106)

Unified Diff: chrome/browser/system_monitor/removable_device_notifications_mac.mm

Issue 10919185: Implement RemovableDeviceNotifications for Mac (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix threading Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/system_monitor/removable_device_notifications_mac.mm
diff --git a/chrome/browser/system_monitor/removable_device_notifications_mac.mm b/chrome/browser/system_monitor/removable_device_notifications_mac.mm
new file mode 100644
index 0000000000000000000000000000000000000000..688021bbff97b1b11633f6cb5512a008bd8368c0
--- /dev/null
+++ b/chrome/browser/system_monitor/removable_device_notifications_mac.mm
@@ -0,0 +1,137 @@
+// 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 "chrome/browser/system_monitor/removable_device_notifications_mac.h"
+
+#include "base/system_monitor/system_monitor.h"
+#include "content/public/browser/browser_thread.h"
+
+namespace chrome {
+
+namespace {
+
+void GetDiskInfoAndUpdateOnFileThead(
vandebo (ex-Chrome) 2012/09/10 18:45:03 nit: Thead -> Thread
sail 2012/09/10 21:58:43 Done.
+ const base::WeakPtr<RemovableDeviceNotificationsMac>& notifications,
+ base::mac::ScopedCFTypeRef<CFDictionaryRef> dict,
+ bool should_remove) {
+ DiskInfoMac info = DiskInfoMac::BuildDiskInfoOnFileThread(dict);
+ if (info.device_id().empty())
+ return;
+
+ content::BrowserThread::PostTask(
+ content::BrowserThread::UI,
+ FROM_HERE,
+ base::Bind(&RemovableDeviceNotificationsMac::UpdateDisk,
+ notifications,
+ info,
+ should_remove));
+}
+
+void GetDiskInfoAndUpdate(
+ const base::WeakPtr<RemovableDeviceNotificationsMac>& notifications,
+ DADiskRef disk,
+ bool should_remove) {
+ base::mac::ScopedCFTypeRef<CFDictionaryRef> dict(DADiskCopyDescription(disk));
+ content::BrowserThread::PostTask(
+ content::BrowserThread::FILE,
+ FROM_HERE,
+ base::Bind(GetDiskInfoAndUpdateOnFileThead,
+ notifications,
+ dict,
+ should_remove));
+}
+
+} // namespace
+
+RemovableDeviceNotificationsMac::RemovableDeviceNotificationsMac() {
+ session_.reset(DASessionCreate(NULL));
+ DASessionScheduleWithRunLoop(
vandebo (ex-Chrome) 2012/09/10 18:45:03 I assume this means you have to register and recei
sail 2012/09/10 21:58:43 I think we can register on any thread that has a C
+ session_, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
+
+ DARegisterDiskAppearedCallback(
vandebo (ex-Chrome) 2012/09/10 18:45:03 Does this send notifications for already attached
sail 2012/09/10 21:58:43 Yep! Added comment too.
+ session_,
+ kDADiskDescriptionMatchVolumeMountable,
+ DiskAppearedCallback,
+ this);
+ DARegisterDiskDisappearedCallback(
+ session_,
+ kDADiskDescriptionMatchVolumeMountable,
+ DiskDisappearedCallback,
+ this);
+ DARegisterDiskDescriptionChangedCallback(
+ session_,
+ kDADiskDescriptionMatchVolumeMountable,
+ kDADiskDescriptionWatchVolumePath,
+ DiskDescriptionChangedCallback,
+ this);
+}
+
+RemovableDeviceNotificationsMac::~RemovableDeviceNotificationsMac() {
+ DASessionUnscheduleFromRunLoop(
+ session_, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
+}
+
+void RemovableDeviceNotificationsMac::UpdateDisk(
+ const DiskInfoMac& info,
+ bool should_remove) {
+ std::map<std::string, DiskInfoMac>::iterator it =
+ disk_info_map_.find(info.device_id());
+ if (it != disk_info_map_.end()) {
+ if (ShouldPostNotificationForDisk(it->second)) {
vandebo (ex-Chrome) 2012/09/10 18:45:03 DCHECK(should_remove) ?
sail 2012/09/10 21:58:43 I'm trying to handle the update case here. For ex
+ base::SystemMonitor::Get()->ProcessRemovableStorageDetached(
+ it->second.device_id());
+ }
+ }
+
+ if (should_remove) {
vandebo (ex-Chrome) 2012/09/10 18:45:03 looks like line 80 and 87 could be merged?
sail 2012/09/10 21:58:43 see above.
+ if (it != disk_info_map_.end())
+ disk_info_map_.erase(it);
+ } else {
+ disk_info_map_[info.device_id()] = info;
+ if (ShouldPostNotificationForDisk(info)) {
+ base::SystemMonitor::Get()->ProcessRemovableStorageAttached(
+ info.device_id(), info.display_name(), info.mount_point().value());
+ }
+ }
+}
+
+// static
+void RemovableDeviceNotificationsMac::DiskAppearedCallback(
+ DADiskRef disk,
+ void* context) {
+ RemovableDeviceNotificationsMac* notifications =
+ static_cast<RemovableDeviceNotificationsMac*>(context);
+ GetDiskInfoAndUpdate(notifications->AsWeakPtr(), disk, false);
+}
+
+// static
+void RemovableDeviceNotificationsMac::DiskDisappearedCallback(
+ DADiskRef disk,
+ void* context) {
+ RemovableDeviceNotificationsMac* notifications =
+ static_cast<RemovableDeviceNotificationsMac*>(context);
+ GetDiskInfoAndUpdate(notifications->AsWeakPtr(), disk, true);
+}
+
+// static
+void RemovableDeviceNotificationsMac::DiskDescriptionChangedCallback(
+ DADiskRef disk,
+ CFArrayRef keys,
+ void *context) {
+ RemovableDeviceNotificationsMac* notifications =
+ static_cast<RemovableDeviceNotificationsMac*>(context);
+ GetDiskInfoAndUpdate(notifications->AsWeakPtr(), disk, false);
+}
+
+bool RemovableDeviceNotificationsMac::ShouldPostNotificationForDisk(
+ const DiskInfoMac& info) const {
+ return !info.bsd_name().empty() &&
+ !info.device_id().empty() &&
+ !info.display_name().empty() &&
+ !info.mount_point().empty() &&
+ (info.type() == MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM ||
+ info.type() == MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM);
Nico 2012/09/10 07:02:34 the other file mentioned FIXED_MASS_FOO. that's no
vandebo (ex-Chrome) 2012/09/10 18:45:03 SystemMonitor only notifies about removable storag
sail 2012/09/10 21:58:43 Yea, we only post notifications about removable st
sail 2012/09/10 21:58:43 I didn't understand this comment. Did you mean th
vandebo (ex-Chrome) 2012/09/10 22:53:05 No, I was replying to Nico's comment.
+}
+
+} // namespace chrome

Powered by Google App Engine
This is Rietveld 408576698