1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/storage_monitor/media_storage_util.h"
9 #include "base/callback.h"
10 #include "base/files/file_util.h"
11 #include "base/logging.h"
12 #include "base/metrics/histogram_macros.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "components/storage_monitor/removable_device_constants.h"
16 #include "components/storage_monitor/storage_monitor.h"
17 #include "content/public/browser/browser_thread.h"
19 using content::BrowserThread
;
21 namespace storage_monitor
{
25 // MediaDeviceNotification.DeviceInfo histogram values.
26 enum DeviceInfoHistogramBuckets
{
27 MASS_STORAGE_DEVICE_NAME_AND_UUID_AVAILABLE
,
28 MASS_STORAGE_DEVICE_UUID_MISSING
,
29 MASS_STORAGE_DEVICE_NAME_MISSING
,
30 MASS_STORAGE_DEVICE_NAME_AND_UUID_MISSING
,
31 MTP_STORAGE_DEVICE_NAME_AND_UUID_AVAILABLE
,
32 MTP_STORAGE_DEVICE_UUID_MISSING
,
33 MTP_STORAGE_DEVICE_NAME_MISSING
,
34 MTP_STORAGE_DEVICE_NAME_AND_UUID_MISSING
,
35 DEVICE_INFO_BUCKET_BOUNDARY
39 const char kRootPath
[] = "/";
42 typedef std::vector
<StorageInfo
> StorageInfoList
;
44 base::FilePath::StringType
FindRemovableStorageLocationById(
45 const std::string
& device_id
) {
46 StorageInfoList devices
=
47 StorageMonitor::GetInstance()->GetAllAvailableStorages();
48 for (StorageInfoList::const_iterator it
= devices
.begin();
49 it
!= devices
.end(); ++it
) {
50 if (it
->device_id() == device_id
51 && StorageInfo::IsRemovableDevice(device_id
))
52 return it
->location();
54 return base::FilePath::StringType();
57 void FilterAttachedDevicesOnFileThread(MediaStorageUtil::DeviceIdSet
* devices
) {
58 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
59 MediaStorageUtil::DeviceIdSet missing_devices
;
61 for (MediaStorageUtil::DeviceIdSet::const_iterator it
= devices
->begin();
64 StorageInfo::Type type
;
65 std::string unique_id
;
66 if (!StorageInfo::CrackDeviceId(*it
, &type
, &unique_id
)) {
67 missing_devices
.insert(*it
);
71 if (type
== StorageInfo::FIXED_MASS_STORAGE
||
72 type
== StorageInfo::ITUNES
||
73 type
== StorageInfo::IPHOTO
||
74 type
== StorageInfo::PICASA
) {
75 if (!base::PathExists(base::FilePath::FromUTF8Unsafe(unique_id
)))
76 missing_devices
.insert(*it
);
80 if (!MediaStorageUtil::IsRemovableStorageAttached(*it
))
81 missing_devices
.insert(*it
);
84 for (MediaStorageUtil::DeviceIdSet::const_iterator it
=
85 missing_devices
.begin();
86 it
!= missing_devices
.end();
95 bool MediaStorageUtil::HasDcim(const base::FilePath
& mount_point
) {
96 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
98 base::FilePath::StringType
dcim_dir(kDCIMDirectoryName
);
99 if (!base::DirectoryExists(mount_point
.Append(dcim_dir
))) {
100 // Check for lowercase 'dcim' as well.
101 base::FilePath
dcim_path_lower(
102 mount_point
.Append(base::ToLowerASCII(dcim_dir
)));
103 if (!base::DirectoryExists(dcim_path_lower
))
110 bool MediaStorageUtil::CanCreateFileSystem(const std::string
& device_id
,
111 const base::FilePath
& path
) {
112 StorageInfo::Type type
;
113 if (!StorageInfo::CrackDeviceId(device_id
, &type
, NULL
))
116 if (type
== StorageInfo::MAC_IMAGE_CAPTURE
)
119 return !path
.empty() && path
.IsAbsolute() && !path
.ReferencesParent();
123 void MediaStorageUtil::FilterAttachedDevices(DeviceIdSet
* devices
,
124 const base::Closure
& done
) {
125 if (BrowserThread::CurrentlyOn(BrowserThread::FILE)) {
126 FilterAttachedDevicesOnFileThread(devices
);
130 BrowserThread::PostTaskAndReply(BrowserThread::FILE,
132 base::Bind(&FilterAttachedDevicesOnFileThread
,
137 // TODO(kmadhusu) Write unit tests for GetDeviceInfoFromPath().
139 bool MediaStorageUtil::GetDeviceInfoFromPath(const base::FilePath
& path
,
140 StorageInfo
* device_info
,
141 base::FilePath
* relative_path
) {
143 DCHECK(relative_path
);
145 if (!path
.IsAbsolute())
149 StorageMonitor
* monitor
= StorageMonitor::GetInstance();
150 bool found_device
= monitor
->GetStorageInfoForPath(path
, &info
);
152 if (found_device
&& StorageInfo::IsRemovableDevice(info
.device_id())) {
153 base::FilePath sub_folder_path
;
154 base::FilePath
device_path(info
.location());
155 if (path
!= device_path
) {
156 bool success
= device_path
.AppendRelativePath(path
, &sub_folder_path
);
161 *relative_path
= sub_folder_path
;
165 // On Posix systems, there's one root so any absolute path could be valid.
166 // TODO(gbillock): Delete this stanza? Posix systems should have the root
167 // volume information. If not, we should move the below into the
168 // right GetStorageInfoForPath implementations.
169 #if !defined(OS_POSIX)
174 // Handle non-removable devices. Note: this is just overwriting
175 // good values from StorageMonitor.
176 // TODO(gbillock): Make sure return values from that class are definitive,
177 // and don't do this here.
179 StorageInfo::MakeDeviceId(StorageInfo::FIXED_MASS_STORAGE
,
180 path
.AsUTF8Unsafe()));
182 *relative_path
= base::FilePath();
187 base::FilePath
MediaStorageUtil::FindDevicePathById(
188 const std::string
& device_id
) {
189 StorageInfo::Type type
;
190 std::string unique_id
;
191 if (!StorageInfo::CrackDeviceId(device_id
, &type
, &unique_id
))
192 return base::FilePath();
194 if (type
== StorageInfo::FIXED_MASS_STORAGE
||
195 type
== StorageInfo::ITUNES
||
196 type
== StorageInfo::IPHOTO
||
197 type
== StorageInfo::PICASA
) {
198 // For this type, the unique_id is the path.
199 return base::FilePath::FromUTF8Unsafe(unique_id
);
202 // For ImageCapture, the synthetic filesystem will be rooted at a fake
203 // top-level directory which is the device_id.
204 if (type
== StorageInfo::MAC_IMAGE_CAPTURE
) {
206 return base::FilePath(kRootPath
+ device_id
);
210 DCHECK(type
== StorageInfo::MTP_OR_PTP
||
211 type
== StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM
||
212 type
== StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM
);
213 return base::FilePath(FindRemovableStorageLocationById(device_id
));
217 void MediaStorageUtil::RecordDeviceInfoHistogram(
219 const std::string
& device_uuid
,
220 const base::string16
& device_label
) {
221 unsigned int event_number
= 0;
225 if (device_label
.empty())
228 if (device_uuid
.empty())
230 enum DeviceInfoHistogramBuckets event
=
231 static_cast<enum DeviceInfoHistogramBuckets
>(event_number
);
232 if (event
>= DEVICE_INFO_BUCKET_BOUNDARY
) {
236 UMA_HISTOGRAM_ENUMERATION("MediaDeviceNotifications.DeviceInfo", event
,
237 DEVICE_INFO_BUCKET_BOUNDARY
);
241 bool MediaStorageUtil::IsRemovableStorageAttached(const std::string
& id
) {
242 StorageMonitor
* monitor
= StorageMonitor::GetInstance();
246 StorageInfoList devices
= monitor
->GetAllAvailableStorages();
247 for (const auto& device
: devices
) {
248 if (StorageInfo::IsRemovableDevice(id
) && device
.device_id() == id
)
254 } // namespace storage_monitor