Linux: Unify udev scopers.

Review URL: https://codereview.chromium.org/302093009

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@274658 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/components/storage_monitor.gypi b/components/storage_monitor.gypi
index 5b62656..b038303 100644
--- a/components/storage_monitor.gypi
+++ b/components/storage_monitor.gypi
@@ -60,12 +60,16 @@
         }],
         ['OS=="linux"', {
           'dependencies': [
-            '../build/linux/system.gyp:udev',
             '../device/media_transfer_protocol/media_transfer_protocol.gyp:device_media_transfer_protocol',
             '../device/media_transfer_protocol/media_transfer_protocol.gyp:mtp_file_entry_proto',
             '../device/media_transfer_protocol/media_transfer_protocol.gyp:mtp_storage_info_proto',
           ],
         }],
+        ['use_udev==1', {
+          'dependencies': [
+            '../device/udev_linux/udev.gyp:udev_linux',
+          ],
+        }],
         ['chromeos==1', {
           'sources!': [
             'storage_monitor/mtab_watcher_linux.cc',
diff --git a/components/storage_monitor/DEPS b/components/storage_monitor/DEPS
index 4410c12..e560e0b 100644
--- a/components/storage_monitor/DEPS
+++ b/components/storage_monitor/DEPS
@@ -3,5 +3,6 @@
   "+content/public/browser",
   "+content/public/test",
   "+device/media_transfer_protocol",
+  "+device/udev_linux",
   "+ui/base",
 ]
diff --git a/components/storage_monitor/storage_monitor_linux.cc b/components/storage_monitor/storage_monitor_linux.cc
index 4e79fd2..254fd5d 100644
--- a/components/storage_monitor/storage_monitor_linux.cc
+++ b/components/storage_monitor/storage_monitor_linux.cc
@@ -26,6 +26,7 @@
 #include "components/storage_monitor/storage_info.h"
 #include "components/storage_monitor/udev_util_linux.h"
 #include "device/media_transfer_protocol/media_transfer_protocol_manager.h"
+#include "device/udev_linux/udev.h"
 
 using content::BrowserThread;
 
@@ -124,7 +125,7 @@
 
   ScopedGetDeviceInfoResultRecorder results_recorder;
 
-  ScopedUdevObject udev_obj(udev_new());
+  device::ScopedUdevPtr udev_obj(udev_new());
   if (!udev_obj.get())
     return storage_info.Pass();
 
@@ -140,7 +141,7 @@
   else
     return storage_info.Pass();  // Not a supported type.
 
-  ScopedUdevDeviceObject device(
+  device::ScopedUdevDevicePtr device(
       udev_device_new_from_devnum(udev_obj.get(), device_type,
                                   device_stat.st_rdev));
   if (!device.get())
diff --git a/components/storage_monitor/udev_util_linux.cc b/components/storage_monitor/udev_util_linux.cc
index 142a54e..5a3a117 100644
--- a/components/storage_monitor/udev_util_linux.cc
+++ b/components/storage_monitor/udev_util_linux.cc
@@ -5,17 +5,10 @@
 #include "components/storage_monitor/udev_util_linux.h"
 
 #include "base/files/file_path.h"
+#include "device/udev_linux/udev.h"
 
 namespace storage_monitor {
 
-void UdevDeleter::operator()(struct udev* udev) {
-  udev_unref(udev);
-}
-
-void UdevDeviceDeleter::operator()(struct udev_device* device) {
-  udev_device_unref(device);
-}
-
 std::string GetUdevDevicePropertyValue(struct udev_device* udev_device,
                                        const char* key) {
   const char* value = udev_device_get_property_value(udev_device, key);
@@ -25,10 +18,10 @@
 bool GetUdevDevicePropertyValueByPath(const base::FilePath& device_path,
                                       const char* key,
                                       std::string* result) {
-  ScopedUdevObject udev(udev_new());
+  device::ScopedUdevPtr udev(udev_new());
   if (!udev.get())
     return false;
-  ScopedUdevDeviceObject device(udev_device_new_from_syspath(
+  device::ScopedUdevDevicePtr device(udev_device_new_from_syspath(
       udev.get(), device_path.value().c_str()));
   if (!device.get())
     return false;
diff --git a/components/storage_monitor/udev_util_linux.h b/components/storage_monitor/udev_util_linux.h
index 108816b..f00048b 100644
--- a/components/storage_monitor/udev_util_linux.h
+++ b/components/storage_monitor/udev_util_linux.h
@@ -17,19 +17,6 @@
 
 namespace storage_monitor {
 
-// Deleter for ScopedUdevObject.
-struct UdevDeleter {
-  void operator()(struct udev* udev);
-};
-typedef scoped_ptr<struct udev, UdevDeleter> ScopedUdevObject;
-
-// Deleter for ScopedUdevDeviceObject().
-struct UdevDeviceDeleter {
-  void operator()(struct udev_device* device);
-};
-typedef scoped_ptr<struct udev_device, UdevDeviceDeleter>
-    ScopedUdevDeviceObject;
-
 // Wrapper function for udev_device_get_property_value() that also checks for
 // valid but empty values.
 std::string GetUdevDevicePropertyValue(struct udev_device* udev_device,
diff --git a/device/hid/device_monitor_linux.h b/device/hid/device_monitor_linux.h
index bc5ac63..2f219dc 100644
--- a/device/hid/device_monitor_linux.h
+++ b/device/hid/device_monitor_linux.h
@@ -14,7 +14,7 @@
 #include "base/message_loop/message_pump_libevent.h"
 #include "base/observer_list.h"
 #include "base/threading/thread_checker.h"
-#include "device/hid/udev_common.h"
+#include "device/udev_linux/udev.h"
 
 struct udev_device;
 
diff --git a/device/hid/hid.gyp b/device/hid/hid.gyp
index 5925a318..a62a68f 100644
--- a/device/hid/hid.gyp
+++ b/device/hid/hid.gyp
@@ -14,9 +14,9 @@
         '../..',
       ],
       'conditions': [
-        ['OS=="linux"', {
+        ['use_udev==1', {
           'dependencies': [
-            '../../build/linux/system.gyp:udev',
+            '../udev_linux/udev.gyp:udev_linux',
           ],
         }],
       ],
@@ -51,7 +51,6 @@
         'hid_utils_mac.h',
         'input_service_linux.cc',
         'input_service_linux.h',
-        'udev_common.h'
       ],
     },
   ],
diff --git a/device/hid/hid_connection_linux.h b/device/hid/hid_connection_linux.h
index 1d1c140..108ad88 100644
--- a/device/hid/hid_connection_linux.h
+++ b/device/hid/hid_connection_linux.h
@@ -12,7 +12,6 @@
 #include "base/message_loop/message_pump_libevent.h"
 #include "device/hid/hid_connection.h"
 #include "device/hid/hid_device_info.h"
-#include "device/hid/udev_common.h"
 
 namespace device {
 
@@ -57,4 +56,4 @@
 
 }  // namespace device
 
-#endif  // DEVICE_HID_HID_CONNECTION_LINUX__
+#endif  // DEVICE_HID_HID_CONNECTION_LINUX_H_
diff --git a/device/hid/hid_service_linux.cc b/device/hid/hid_service_linux.cc
index 86342c92..5f9f0dc 100644
--- a/device/hid/hid_service_linux.cc
+++ b/device/hid/hid_service_linux.cc
@@ -21,7 +21,7 @@
 #include "device/hid/hid_device_info.h"
 #include "device/hid/hid_report_descriptor.h"
 #include "device/hid/hid_service_linux.h"
-#include "device/hid/udev_common.h"
+#include "device/udev_linux/udev.h"
 
 namespace device {
 
@@ -33,7 +33,7 @@
 const char kHIDName[] = "HID_NAME";
 const char kHIDUnique[] = "HID_UNIQ";
 
-} // namespace
+}  // namespace
 
 HidServiceLinux::HidServiceLinux() {
   DeviceMonitorLinux* monitor = DeviceMonitorLinux::GetInstance();
@@ -198,4 +198,4 @@
   return false;
 }
 
-} // namespace dev
+}  // namespace device
diff --git a/device/hid/udev_common.h b/device/hid/udev_common.h
deleted file mode 100644
index 6b9d5f8..0000000
--- a/device/hid/udev_common.h
+++ /dev/null
@@ -1,49 +0,0 @@
-// Copyright 2014 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 DEVICE_HID_UDEV_COMMON_H_
-#define DEVICE_HID_UDEV_COMMON_H_
-
-#include <libudev.h>
-
-#include "base/memory/scoped_ptr.h"
-
-namespace device {
-
-template <typename T, void func(T*)>
-void DiscardReturnType(T* arg) {
-  func(arg);
-}
-
-template <typename T, T* func(T*)>
-void DiscardReturnType(T* arg) {
-  func(arg);
-}
-
-template <typename T, void func(T*)>
-struct Deleter {
-  void operator()(T* enumerate) const {
-    if (enumerate != NULL)
-      func(enumerate);
-  }
-};
-
-typedef Deleter<udev, DiscardReturnType<udev, udev_unref> > UdevDeleter;
-typedef Deleter<udev_enumerate,
-                DiscardReturnType<udev_enumerate, udev_enumerate_unref> >
-    UdevEnumerateDeleter;
-typedef Deleter<udev_device, DiscardReturnType<udev_device, udev_device_unref> >
-    UdevDeviceDeleter;
-typedef Deleter<udev_monitor,
-                DiscardReturnType<udev_monitor, udev_monitor_unref> >
-    UdevMonitorDeleter;
-
-typedef scoped_ptr<udev, UdevDeleter> ScopedUdevPtr;
-typedef scoped_ptr<udev_enumerate, UdevEnumerateDeleter> ScopedUdevEnumeratePtr;
-typedef scoped_ptr<udev_device, UdevDeviceDeleter> ScopedUdevDevicePtr;
-typedef scoped_ptr<udev_monitor, UdevMonitorDeleter> ScopedUdevMonitorPtr;
-
-}  // namespace device
-
-#endif  // DEVICE_HID_UDEV_COMMON_H_
\ No newline at end of file
diff --git a/device/udev_linux/udev.cc b/device/udev_linux/udev.cc
new file mode 100644
index 0000000..e10074b
--- /dev/null
+++ b/device/udev_linux/udev.cc
@@ -0,0 +1,27 @@
+// Copyright 2014 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 <libudev.h>
+
+#include "device/udev_linux/udev.h"
+
+namespace device {
+
+void UdevDeleter::operator()(udev* dev) const {
+  udev_unref(dev);
+}
+
+void UdevEnumerateDeleter::operator()(udev_enumerate* enumerate) const {
+  udev_enumerate_unref(enumerate);
+}
+
+void UdevDeviceDeleter::operator()(udev_device* device) const {
+  udev_device_unref(device);
+}
+
+void UdevMonitorDeleter::operator()(udev_monitor* monitor) const {
+  udev_monitor_unref(monitor);
+}
+
+}  // namespace device
diff --git a/device/udev_linux/udev.gyp b/device/udev_linux/udev.gyp
new file mode 100644
index 0000000..6d142ac
--- /dev/null
+++ b/device/udev_linux/udev.gyp
@@ -0,0 +1,30 @@
+# Copyright 2014 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.
+
+{
+  'variables': {
+    'chromium_code': 1,
+  },
+  'conditions': [
+    ['use_udev==1', {
+      'targets': [
+        {
+          'target_name': 'udev_linux',
+          'type': 'static_library',
+          'dependencies': [
+            '../../base/base.gyp:base',
+            '../../build/linux/system.gyp:udev',
+          ],
+          'include_dirs': [
+            '../..',
+          ],
+          'sources': [
+            'udev.cc',
+            'udev.h',
+          ],
+        },
+      ],
+    }],
+  ]
+}
diff --git a/device/udev_linux/udev.h b/device/udev_linux/udev.h
new file mode 100644
index 0000000..cffb6723
--- /dev/null
+++ b/device/udev_linux/udev.h
@@ -0,0 +1,38 @@
+// Copyright 2014 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 DEVICE_UDEV_LINUX_UDEV_H_
+#define DEVICE_UDEV_LINUX_UDEV_H_
+
+#include <libudev.h>
+
+#include "base/memory/scoped_ptr.h"
+
+#if !defined(USE_UDEV)
+#error "USE_UDEV not defined"
+#endif
+
+namespace device {
+
+struct UdevDeleter {
+  void operator()(udev* dev) const;
+};
+struct UdevEnumerateDeleter {
+  void operator()(udev_enumerate* enumerate) const;
+};
+struct UdevDeviceDeleter {
+  void operator()(udev_device* device) const;
+};
+struct UdevMonitorDeleter {
+  void operator()(udev_monitor* monitor) const;
+};
+
+typedef scoped_ptr<udev, UdevDeleter> ScopedUdevPtr;
+typedef scoped_ptr<udev_enumerate, UdevEnumerateDeleter> ScopedUdevEnumeratePtr;
+typedef scoped_ptr<udev_device, UdevDeviceDeleter> ScopedUdevDevicePtr;
+typedef scoped_ptr<udev_monitor, UdevMonitorDeleter> ScopedUdevMonitorPtr;
+
+}  // namespace device
+
+#endif  // DEVICE_UDEV_LINUX_UDEV_H_
diff --git a/ui/events/ozone/DEPS b/ui/events/ozone/DEPS
new file mode 100644
index 0000000..911cdbe
--- /dev/null
+++ b/ui/events/ozone/DEPS
@@ -0,0 +1,3 @@
+include_rules = [
+  "+device/udev_linux",
+]
diff --git a/ui/events/ozone/device/device_manager.cc b/ui/events/ozone/device/device_manager.cc
index cd60d9d..bbd5d80 100644
--- a/ui/events/ozone/device/device_manager.cc
+++ b/ui/events/ozone/device/device_manager.cc
@@ -4,8 +4,11 @@
 
 #include "ui/events/ozone/device/device_manager.h"
 
-#include "ui/events/ozone/device/device_manager_manual.h"
+#if defined(USE_UDEV)
 #include "ui/events/ozone/device/udev/device_manager_udev.h"
+#else
+#include "ui/events/ozone/device/device_manager_manual.h"
+#endif
 
 namespace ui {
 
diff --git a/ui/events/ozone/device/udev/device_manager_udev.cc b/ui/events/ozone/device/udev/device_manager_udev.cc
index 47244a46..7f86bee 100644
--- a/ui/events/ozone/device/udev/device_manager_udev.cc
+++ b/ui/events/ozone/device/udev/device_manager_udev.cc
@@ -50,17 +50,17 @@
 }
 
 // Create libudev context.
-scoped_udev UdevCreate() {
+device::ScopedUdevPtr UdevCreate() {
   struct udev* udev = udev_new();
   if (udev) {
     udev_set_log_fn(udev, UdevLog);
     udev_set_log_priority(udev, SYS_LOG_DEBUG);
   }
-  return scoped_udev(udev);
+  return device::ScopedUdevPtr(udev);
 }
 
 // Start monitoring input device changes.
-scoped_udev_monitor UdevCreateMonitor(struct udev* udev) {
+device::ScopedUdevMonitorPtr UdevCreateMonitor(struct udev* udev) {
   struct udev_monitor* monitor = udev_monitor_new_from_netlink(udev, "udev");
   if (monitor) {
     for (size_t i = 0; i < arraysize(kSubsystems); ++i)
@@ -73,7 +73,7 @@
     LOG(ERROR) << "Failed to create udev monitor";
   }
 
-  return scoped_udev_monitor(monitor);
+  return device::ScopedUdevMonitorPtr(monitor);
 }
 
 }  // namespace
@@ -99,7 +99,7 @@
 void DeviceManagerUdev::ScanDevices(DeviceEventObserver* observer) {
   CreateMonitor();
 
-  scoped_udev_enumerate enumerate(udev_enumerate_new(udev_.get()));
+  device::ScopedUdevEnumeratePtr enumerate(udev_enumerate_new(udev_.get()));
   if (!enumerate)
     return;
 
@@ -112,7 +112,7 @@
   struct udev_list_entry* entry;
 
   udev_list_entry_foreach(entry, devices) {
-    scoped_udev_device device(udev_device_new_from_syspath(
+    device::ScopedUdevDevicePtr device(udev_device_new_from_syspath(
         udev_.get(), udev_list_entry_get_name(entry)));
     if (!device)
       continue;
@@ -136,7 +136,8 @@
   // to handle broken connections here.
   TRACE_EVENT1("ozone", "UdevDeviceChange", "socket", fd);
 
-  scoped_udev_device device(udev_monitor_receive_device(monitor_.get()));
+  device::ScopedUdevDevicePtr device(
+      udev_monitor_receive_device(monitor_.get()));
   if (!device)
     return;
 
diff --git a/ui/events/ozone/device/udev/device_manager_udev.h b/ui/events/ozone/device/udev/device_manager_udev.h
index 7c15e3b..8a7537a 100644
--- a/ui/events/ozone/device/udev/device_manager_udev.h
+++ b/ui/events/ozone/device/udev/device_manager_udev.h
@@ -7,8 +7,8 @@
 
 #include "base/message_loop/message_pump_libevent.h"
 #include "base/observer_list.h"
+#include "device/udev_linux/udev.h"
 #include "ui/events/ozone/device/device_manager.h"
-#include "ui/events/ozone/device/udev/scoped_udev.h"
 
 namespace ui {
 
@@ -36,8 +36,8 @@
   virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
   virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE;
 
-  scoped_udev udev_;
-  scoped_udev_monitor monitor_;
+  device::ScopedUdevPtr udev_;
+  device::ScopedUdevMonitorPtr monitor_;
 
   base::MessagePumpLibevent::FileDescriptorWatcher controller_;
 
diff --git a/ui/events/ozone/device/udev/scoped_udev.h b/ui/events/ozone/device/udev/scoped_udev.h
deleted file mode 100644
index 2d02bb34..0000000
--- a/ui/events/ozone/device/udev/scoped_udev.h
+++ /dev/null
@@ -1,50 +0,0 @@
-// Copyright 2014 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 UI_EVENTS_OZONE_DEVICE_UDEV_SCOPED_UDEV_H_
-#define UI_EVENTS_OZONE_DEVICE_UDEV_SCOPED_UDEV_H_
-
-#include <libudev.h>
-
-#include "base/memory/scoped_ptr.h"
-
-// This file enables management of udev object references using scoped_ptr<> by
-// associating a custom deleter that drops the reference rather than deleting.
-
-namespace ui {
-
-// Scoped struct udev.
-struct UdevDeleter {
-  void operator()(struct udev* udev) { udev_unref(udev); }
-};
-typedef scoped_ptr<struct udev, UdevDeleter> scoped_udev;
-
-// Scoped struct udev_device.
-struct UdevDeviceDeleter {
-  void operator()(struct udev_device* udev_device) {
-    udev_device_unref(udev_device);
-  }
-};
-typedef scoped_ptr<struct udev_device, UdevDeviceDeleter> scoped_udev_device;
-
-// Scoped struct udev_enumerate.
-struct UdevEnumerateDeleter {
-  void operator()(struct udev_enumerate* udev_enumerate) {
-    udev_enumerate_unref(udev_enumerate);
-  }
-};
-typedef scoped_ptr<struct udev_enumerate, UdevEnumerateDeleter>
-    scoped_udev_enumerate;
-
-// Scoped struct udev_monitor.
-struct UdevMonitorDeleter {
-  void operator()(struct udev_monitor* udev_monitor) {
-    udev_monitor_unref(udev_monitor);
-  }
-};
-typedef scoped_ptr<struct udev_monitor, UdevMonitorDeleter> scoped_udev_monitor;
-
-}  // namespace ui
-
-#endif  // UI_EVENTS_OZONE_DEVICE_UDEV_SCOPED_UDEV_H_
diff --git a/ui/events/ozone/events_ozone.gyp b/ui/events/ozone/events_ozone.gyp
index 9a4805a..b3dcf3c 100644
--- a/ui/events/ozone/events_ozone.gyp
+++ b/ui/events/ozone/events_ozone.gyp
@@ -25,7 +25,6 @@
       'device/device_manager_manual.h',
       'device/udev/device_manager_udev.cc',
       'device/udev/device_manager_udev.h',
-      'device/udev/scoped_udev.h',
       'event_factory_ozone.cc',
       'event_factory_ozone.h',
       'events_ozone_export.h',
@@ -38,7 +37,7 @@
       }],
       ['use_ozone_evdev==1 and use_udev==1', {
         'dependencies': [
-          '<(DEPTH)/build/linux/system.gyp:udev',
+          '<(DEPTH)/device/udev_linux/udev.gyp:udev_linux',
         ],
       }],
     ],