summaryrefslogtreecommitdiffstats
path: root/content/browser/media_device_notifications_linux.h
blob: cd4124560ba0b16379b559a25c470f3a94012cc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// 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_MEDIA_DEVICE_NOTIFICATIONS_LINUX_H_
#define CONTENT_BROWSER_MEDIA_DEVICE_NOTIFICATIONS_LINUX_H_
#pragma once

#include <map>
#include <set>
#include <string>
#include <utility>

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/file_path.h"
#include "base/files/file_path_watcher.h"
#include "base/memory/ref_counted.h"
#include "base/system_monitor/system_monitor.h"
#include "content/common/content_export.h"

namespace content {

class CONTENT_EXPORT MediaDeviceNotificationsLinux
    : public base::RefCountedThreadSafe<MediaDeviceNotificationsLinux> {
 public:
  explicit MediaDeviceNotificationsLinux(const FilePath& path);

  // Must be called for MediaDeviceNotificationsLinux to work.
  void Init();

  void OnFilePathChanged(const FilePath& path);

 private:
  friend class base::RefCountedThreadSafe<MediaDeviceNotificationsLinux>;

  typedef std::string MountDevice;
  typedef std::string MountPoint;
  typedef std::pair<MountDevice,
                    base::SystemMonitor::DeviceIdType> MountDeviceAndId;
  typedef std::map<MountPoint, MountDeviceAndId> MountMap;

  // A simple pass-through class. MediaDeviceNotificationsLinux cannot directly
  // inherit from FilePathWatcher::Delegate due to multiple inheritance.
  class WatcherDelegate : public base::files::FilePathWatcher::Delegate {
   public:
    explicit WatcherDelegate(MediaDeviceNotificationsLinux* notifier);

    // base::files::FilePathWatcher::Delegate implementation.
    virtual void OnFilePathChanged(const FilePath& path) OVERRIDE;

   private:
    friend class base::RefCountedThreadSafe<WatcherDelegate>;

    virtual ~WatcherDelegate();
    MediaDeviceNotificationsLinux* notifier_;

    DISALLOW_COPY_AND_ASSIGN(WatcherDelegate);
  };

  virtual ~MediaDeviceNotificationsLinux();

  void InitOnFileThread();

  // Parse the mtab file and find all changes.
  void UpdateMtab();

  // Read the mtab file entries into |mtab|.
  void ReadMtab(MountMap* mtab);

  // For a new device mounted at |mount_point|, see if it is a media device by
  // checking for the existence of a DCIM directory.
  // If it is a media device, return true, otherwise return false.
  // Mac OS X behaves similarly, but this is not the only heuristic it uses.
  // TODO(vandebo) Try to figure out how Mac OS X decides this.
  bool IsMediaDevice(const MountPoint& mount_point);

  // Add a media device with a given device and mount device. Assign it a device
  // id as well.
  void AddNewDevice(const MountDevice& mount_device,
                    const MountPoint& mount_point,
                    base::SystemMonitor::DeviceIdType* device_id);

  // Remove a media device with a given device id.
  void RemoveOldDevice(const base::SystemMonitor::DeviceIdType& device_id);

  // Whether Init() has been called or not.
  bool initialized_;

  // Mtab file that lists the mount points.
  const FilePath mtab_path_;
  // Watcher for |mtab_path_|.
  base::files::FilePathWatcher file_watcher_;
  // Delegate to receive watcher notifications.
  scoped_refptr<WatcherDelegate> watcher_delegate_;

  // Mapping of relevent mount points and their corresponding mount devices.
  // Keep in mind on Linux, a device can be mounted at multiple mount points,
  // and multiple devices can be mounted at a mount point.
  MountMap mtab_;

  // The lowest available device id number.
  base::SystemMonitor::DeviceIdType current_device_id_;

  // Set of known file systems that we care about.
  std::set<std::string> known_file_systems_;

  DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinux);
};

}  // namespace content

#endif  // CONTENT_BROWSER_MEDIA_DEVICE_NOTIFICATIONS_LINUX_H_