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.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::StringToLowerASCII(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().
138 bool MediaStorageUtil::GetDeviceInfoFromPath(const base::FilePath
& path
,
139 StorageInfo
* device_info
,
140 base::FilePath
* relative_path
) {
142 DCHECK(relative_path
);
144 if (!path
.IsAbsolute())
148 StorageMonitor
* monitor
= StorageMonitor::GetInstance();
149 bool found_device
= monitor
->GetStorageInfoForPath(path
, &info
);
151 if (found_device
&& StorageInfo::IsRemovableDevice(info
.device_id())) {
152 base::FilePath sub_folder_path
;
153 base::FilePath
device_path(info
.location());
154 if (path
!= device_path
) {
155 bool success
= device_path
.AppendRelativePath(path
, &sub_folder_path
);
160 *relative_path
= sub_folder_path
;
164 // On Posix systems, there's one root so any absolute path could be valid.
165 // TODO(gbillock): Delete this stanza? Posix systems should have the root
166 // volume information. If not, we should move the below into the
167 // right GetStorageInfoForPath implementations.
168 #if !defined(OS_POSIX)
173 // Handle non-removable devices. Note: this is just overwriting
174 // good values from StorageMonitor.
175 // TODO(gbillock): Make sure return values from that class are definitive,
176 // and don't do this here.
178 StorageInfo::MakeDeviceId(StorageInfo::FIXED_MASS_STORAGE
,
179 path
.AsUTF8Unsafe()));
181 *relative_path
= base::FilePath();
186 base::FilePath
MediaStorageUtil::FindDevicePathById(
187 const std::string
& device_id
) {
188 StorageInfo::Type type
;
189 std::string unique_id
;
190 if (!StorageInfo::CrackDeviceId(device_id
, &type
, &unique_id
))
191 return base::FilePath();
193 if (type
== StorageInfo::FIXED_MASS_STORAGE
||
194 type
== StorageInfo::ITUNES
||
195 type
== StorageInfo::IPHOTO
||
196 type
== StorageInfo::PICASA
) {
197 // For this type, the unique_id is the path.
198 return base::FilePath::FromUTF8Unsafe(unique_id
);
201 // For ImageCapture, the synthetic filesystem will be rooted at a fake
202 // top-level directory which is the device_id.
203 if (type
== StorageInfo::MAC_IMAGE_CAPTURE
) {
205 return base::FilePath(kRootPath
+ device_id
);
209 DCHECK(type
== StorageInfo::MTP_OR_PTP
||
210 type
== StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM
||
211 type
== StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM
);
212 return base::FilePath(FindRemovableStorageLocationById(device_id
));
216 void MediaStorageUtil::RecordDeviceInfoHistogram(
218 const std::string
& device_uuid
,
219 const base::string16
& device_label
) {
220 unsigned int event_number
= 0;
224 if (device_label
.empty())
227 if (device_uuid
.empty())
229 enum DeviceInfoHistogramBuckets event
=
230 static_cast<enum DeviceInfoHistogramBuckets
>(event_number
);
231 if (event
>= DEVICE_INFO_BUCKET_BOUNDARY
) {
235 UMA_HISTOGRAM_ENUMERATION("MediaDeviceNotifications.DeviceInfo", event
,
236 DEVICE_INFO_BUCKET_BOUNDARY
);
239 bool MediaStorageUtil::IsRemovableStorageAttached(const std::string
& id
) {
240 StorageInfoList devices
=
241 StorageMonitor::GetInstance()->GetAllAvailableStorages();
242 for (StorageInfoList::const_iterator it
= devices
.begin();
243 it
!= devices
.end(); ++it
) {
244 if (StorageInfo::IsRemovableDevice(id
) && it
->device_id() == id
)
250 } // namespace storage_monitor