OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/media_device_notifications_linux.h" | 5 #include "content/browser/media_device_notifications_linux.h" |
6 | 6 |
7 #include <mntent.h> | 7 #include <mntent.h> |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "base/system_monitor/system_monitor.h" | 13 #include "base/system_monitor/system_monitor.h" |
14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
15 | 15 |
16 using base::SystemMonitor; | 16 using base::SystemMonitor; |
ajl
2012/03/23 22:47:39
If you don't use this outside your project namespa
Lei Zhang
2012/03/24 03:01:26
Done.
| |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 const char* const kDCIMDirName = "DCIM"; | 20 const char* const kDCIMDirName = "DCIM"; |
ajl
2012/03/23 22:47:39
There is a preference in google code to declare C
Lei Zhang
2012/03/24 03:01:26
Done.
vandebo (ex-Chrome)
2012/04/11 21:45:16
ajl: For my benefit can you explain the decision h
| |
21 | 21 |
22 // List of file systems we care about. | 22 // List of file systems we care about. |
23 const char* const kKnownFileSystems[] = { | 23 const char* const kKnownFileSystems[] = { |
24 "ext2", | 24 "ext2", |
25 "ext3", | 25 "ext3", |
26 "ext4", | 26 "ext4", |
27 "fat", | 27 "fat", |
28 "hfsplus", | 28 "hfsplus", |
29 "iso9660", | 29 "iso9660", |
30 "msdos", | 30 "msdos", |
31 "ntfs", | 31 "ntfs", |
32 "udf", | 32 "udf", |
33 "vfat", | 33 "vfat", |
34 }; | 34 }; |
35 | 35 |
36 } // namespace | 36 } // namespace |
37 | 37 |
38 namespace content { | 38 namespace content { |
39 | 39 |
40 MediaDeviceNotificationsLinux::MediaDeviceNotificationsLinux( | 40 MediaDeviceNotificationsLinux::MediaDeviceNotificationsLinux( |
41 const FilePath& path) | 41 const FilePath& path) |
42 : initialized_(false), | 42 : initialized_(false), |
43 mtab_path_(path), | 43 mtab_path_(path), |
44 current_device_id_(0U) { | 44 current_device_id_(0U) { |
45 CHECK(!path.empty()); | 45 CHECK(!path.empty()); |
46 | 46 |
47 // Put |kKnownFileSystems| in std::set to get O(log N) access time. | 47 // Put |kKnownFileSystems| in std::set to get O(log N) access time. |
48 for (size_t i = 0; i < arraysize(kKnownFileSystems); ++i) | 48 for (size_t i = 0; i < arraysize(kKnownFileSystems); ++i) |
49 known_file_systems_.insert(kKnownFileSystems[i]); | 49 known_file_systems_.insert(kKnownFileSystems[i]); |
ajl
2012/03/23 22:47:39
For this particular case, I think it would help re
Lei Zhang
2012/03/24 03:01:26
Done.
| |
50 } | 50 } |
51 | 51 |
52 MediaDeviceNotificationsLinux::~MediaDeviceNotificationsLinux() { | 52 MediaDeviceNotificationsLinux::~MediaDeviceNotificationsLinux() { |
53 } | 53 } |
54 | 54 |
55 void MediaDeviceNotificationsLinux::Init() { | 55 void MediaDeviceNotificationsLinux::Init() { |
56 BrowserThread::PostTask( | 56 BrowserThread::PostTask( |
57 BrowserThread::FILE, FROM_HERE, | 57 BrowserThread::FILE, FROM_HERE, |
58 base::Bind(&MediaDeviceNotificationsLinux::InitOnFileThread, this)); | 58 base::Bind(&MediaDeviceNotificationsLinux::InitOnFileThread, this)); |
59 } | 59 } |
60 | 60 |
61 void MediaDeviceNotificationsLinux::OnFilePathChanged(const FilePath& path) { | 61 void MediaDeviceNotificationsLinux::OnFilePathChanged(const FilePath& path) { |
62 if (path != mtab_path_) { | 62 if (path != mtab_path_) { |
63 NOTREACHED(); | 63 NOTREACHED(); |
ajl
2012/03/23 22:47:39
How about a comment explaining what this means?
Lei Zhang
2012/03/24 03:01:26
Done.
| |
64 return; | 64 return; |
65 } | 65 } |
66 | 66 |
67 UpdateMtab(); | 67 UpdateMtab(); |
68 } | 68 } |
69 | 69 |
70 void MediaDeviceNotificationsLinux::InitOnFileThread() { | 70 void MediaDeviceNotificationsLinux::InitOnFileThread() { |
71 DCHECK(!initialized_); | 71 DCHECK(!initialized_); |
72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
73 initialized_ = true; | 73 initialized_ = true; |
74 | 74 |
75 watcher_delegate_ = new WatcherDelegate(this); | 75 watcher_delegate_ = new WatcherDelegate(this); |
ajl
2012/03/23 22:47:39
Is there a method call (like scoped_ptr::reset())
Lei Zhang
2012/03/24 03:01:26
Unfortunately no. See http://src.chromium.org/view
| |
76 if (!file_watcher_.Watch(mtab_path_, watcher_delegate_)) { | 76 if (!file_watcher_.Watch(mtab_path_, watcher_delegate_)) { |
77 LOG(ERROR) << "Adding watch for " << mtab_path_.value() << " failed"; | 77 LOG(ERROR) << "Adding watch for " << mtab_path_.value() << " failed"; |
78 return; | 78 return; |
79 } | 79 } |
80 | 80 |
81 UpdateMtab(); | 81 UpdateMtab(); |
82 } | 82 } |
83 | 83 |
84 void MediaDeviceNotificationsLinux::UpdateMtab() { | 84 void MediaDeviceNotificationsLinux::UpdateMtab() { |
85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
86 | 86 |
87 MountMap new_mtab; | 87 MountMap new_mtab; |
88 ReadMtab(&new_mtab); | 88 ReadMtab(&new_mtab); |
89 | 89 |
90 // Check existing mtab entries for unaccounted mount points. | 90 // Check existing mtab entries for unaccounted mount points. |
91 // These mount points must have been removed in the new mtab. | 91 // These mount points must have been removed in the new mtab. |
92 MountMap::iterator it = mtab_.begin(); | 92 MountMap::iterator it = mtab_.begin(); |
93 while (it != mtab_.end()) { | 93 while (it != mtab_.end()) { |
94 const MountPoint& mount_point(it->first); | 94 const MountPoint& mount_point(it->first); |
95 // |mount_point| not in |new_mtab|. | 95 // |mount_point| not in |new_mtab|. |
96 if (new_mtab.find(mount_point) == new_mtab.end()) { | 96 if (new_mtab.find(mount_point) == new_mtab.end()) { |
97 const SystemMonitor::DeviceIdType& device_id(it->second.second); | 97 const SystemMonitor::DeviceIdType& device_id(it->second.second); |
98 RemoveOldDevice(device_id); | 98 RemoveOldDevice(device_id); |
99 mtab_.erase(it++); | 99 mtab_.erase(it++); |
100 continue; | 100 continue; |
ajl
2012/03/23 22:47:39
Do I understand correctly that the only thing this
Lei Zhang
2012/03/24 03:01:26
The code is a bit subtle. When I call map::erase()
| |
101 } | 101 } |
102 // Existing mount point. Ignore and deal in the next loop. | 102 // Existing mount point. Ignore and deal in the next loop. |
103 ++it; | 103 ++it; |
104 } | 104 } |
105 | 105 |
106 // Check new mtab entries against existing ones. | 106 // Check new mtab entries against existing ones. |
107 for (MountMap::iterator newiter = new_mtab.begin(); | 107 for (MountMap::iterator newiter = new_mtab.begin(); |
108 newiter != new_mtab.end(); | 108 newiter != new_mtab.end(); |
109 ++newiter) { | 109 ++newiter) { |
110 const MountPoint& mount_point(newiter->first); | 110 const MountPoint& mount_point(newiter->first); |
ajl
2012/03/23 22:47:39
It is true that the style guide allows using eithe
Lei Zhang
2012/03/24 03:01:26
Done.
| |
111 const MountDeviceAndId& mount_device_and_id(newiter->second); | 111 const MountDeviceAndId& mount_device_and_id(newiter->second); |
112 const MountDevice& mount_device(newiter->second.first); | 112 const MountDevice& mount_device(newiter->second.first); |
ajl
2012/03/23 22:47:39
Why not use the temporary you created on the previ
Lei Zhang
2012/03/24 03:01:26
Sure, but not for the next one. It's not const and
| |
113 SystemMonitor::DeviceIdType& id(newiter->second.second); | 113 SystemMonitor::DeviceIdType& id(newiter->second.second); |
114 MountMap::iterator olditer = mtab_.find(mount_point); | 114 MountMap::iterator olditer = mtab_.find(mount_point); |
115 // Check to see if it is a new mount point. | 115 // Check to see if it is a new mount point. |
116 if (olditer == mtab_.end()) { | 116 if (olditer == mtab_.end()) { |
117 if (IsMediaDevice(mount_point)) { | 117 if (IsMediaDevice(mount_point)) { |
118 AddNewDevice(mount_device, mount_point, &id); | 118 AddNewDevice(mount_device, mount_point, &id); |
119 mtab_[mount_point] = mount_device_and_id; | 119 mtab_[mount_point] = mount_device_and_id; |
ajl
2012/03/23 22:47:39
Can you use insert() here rather than the [] opera
Lei Zhang
2012/03/24 03:01:26
Done.
| |
120 } | 120 } |
121 continue; | 121 continue; |
122 } | 122 } |
123 | 123 |
124 // Existing mount point. Check to see if a new device is mounted there. | 124 // Existing mount point. Check to see if a new device is mounted there. |
125 MountDeviceAndId& old_mount_device_and_id(olditer->second); | 125 MountDeviceAndId& old_mount_device_and_id(olditer->second); |
126 if (mount_device == old_mount_device_and_id.first) | 126 if (mount_device == old_mount_device_and_id.first) |
127 continue; | 127 continue; |
128 | 128 |
129 // New device mounted. | 129 // New device mounted. |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
224 | 224 |
225 MediaDeviceNotificationsLinux::WatcherDelegate::~WatcherDelegate() { | 225 MediaDeviceNotificationsLinux::WatcherDelegate::~WatcherDelegate() { |
226 } | 226 } |
227 | 227 |
228 void MediaDeviceNotificationsLinux::WatcherDelegate::OnFilePathChanged( | 228 void MediaDeviceNotificationsLinux::WatcherDelegate::OnFilePathChanged( |
229 const FilePath& path) { | 229 const FilePath& path) { |
230 notifier_->OnFilePathChanged(path); | 230 notifier_->OnFilePathChanged(path); |
231 } | 231 } |
232 | 232 |
233 } // namespace content | 233 } // namespace content |
234 | |
OLD | NEW |