Rename GetIconID to GetIconId
[chromium-blink-merge.git] / components / storage_monitor / media_storage_util.cc
blobb9f321a7c2b4bf7df2333b8bef86c5b30c0abe58
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"
7 #include <vector>
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 {
23 namespace {
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
38 #if !defined(OS_WIN)
39 const char kRootPath[] = "/";
40 #endif
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();
62 it != devices->end();
63 ++it) {
64 StorageInfo::Type type;
65 std::string unique_id;
66 if (!StorageInfo::CrackDeviceId(*it, &type, &unique_id)) {
67 missing_devices.insert(*it);
68 continue;
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);
77 continue;
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();
87 ++it) {
88 devices->erase(*it);
92 } // namespace
94 // static
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))
104 return false;
106 return true;
109 // static
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))
114 return false;
116 if (type == StorageInfo::MAC_IMAGE_CAPTURE)
117 return true;
119 return !path.empty() && path.IsAbsolute() && !path.ReferencesParent();
122 // static
123 void MediaStorageUtil::FilterAttachedDevices(DeviceIdSet* devices,
124 const base::Closure& done) {
125 if (BrowserThread::CurrentlyOn(BrowserThread::FILE)) {
126 FilterAttachedDevicesOnFileThread(devices);
127 done.Run();
128 return;
130 BrowserThread::PostTaskAndReply(BrowserThread::FILE,
131 FROM_HERE,
132 base::Bind(&FilterAttachedDevicesOnFileThread,
133 devices),
134 done);
137 // TODO(kmadhusu) Write unit tests for GetDeviceInfoFromPath().
138 // static
139 bool MediaStorageUtil::GetDeviceInfoFromPath(const base::FilePath& path,
140 StorageInfo* device_info,
141 base::FilePath* relative_path) {
142 DCHECK(device_info);
143 DCHECK(relative_path);
145 if (!path.IsAbsolute())
146 return false;
148 StorageInfo info;
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);
157 DCHECK(success);
160 *device_info = info;
161 *relative_path = sub_folder_path;
162 return true;
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)
170 if (!found_device)
171 return false;
172 #endif
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.
178 info.set_device_id(
179 StorageInfo::MakeDeviceId(StorageInfo::FIXED_MASS_STORAGE,
180 path.AsUTF8Unsafe()));
181 *device_info = info;
182 *relative_path = base::FilePath();
183 return true;
186 // static
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) {
205 #if !defined(OS_WIN)
206 return base::FilePath(kRootPath + device_id);
207 #endif
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));
216 // static
217 void MediaStorageUtil::RecordDeviceInfoHistogram(
218 bool mass_storage,
219 const std::string& device_uuid,
220 const base::string16& device_label) {
221 unsigned int event_number = 0;
222 if (!mass_storage)
223 event_number = 4;
225 if (device_label.empty())
226 event_number += 2;
228 if (device_uuid.empty())
229 event_number += 1;
230 enum DeviceInfoHistogramBuckets event =
231 static_cast<enum DeviceInfoHistogramBuckets>(event_number);
232 if (event >= DEVICE_INFO_BUCKET_BOUNDARY) {
233 NOTREACHED();
234 return;
236 UMA_HISTOGRAM_ENUMERATION("MediaDeviceNotifications.DeviceInfo", event,
237 DEVICE_INFO_BUCKET_BOUNDARY);
240 // static
241 bool MediaStorageUtil::IsRemovableStorageAttached(const std::string& id) {
242 StorageMonitor* monitor = StorageMonitor::GetInstance();
243 if (!monitor)
244 return false;
246 StorageInfoList devices = monitor->GetAllAvailableStorages();
247 for (const auto& device : devices) {
248 if (StorageInfo::IsRemovableDevice(id) && device.device_id() == id)
249 return true;
251 return false;
254 } // namespace storage_monitor