Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / storage_monitor / media_storage_util.cc
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/storage_monitor/media_storage_util.h"
6
7 #include <vector>
8
9 #include "base/callback.h"
10 #include "base/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 "chrome/browser/storage_monitor/removable_device_constants.h"
16 #include "chrome/browser/storage_monitor/storage_monitor.h"
17 #include "content/public/browser/browser_thread.h"
18
19 using content::BrowserThread;
20
21 namespace {
22
23 // MediaDeviceNotification.DeviceInfo histogram values.
24 enum DeviceInfoHistogramBuckets {
25   MASS_STORAGE_DEVICE_NAME_AND_UUID_AVAILABLE,
26   MASS_STORAGE_DEVICE_UUID_MISSING,
27   MASS_STORAGE_DEVICE_NAME_MISSING,
28   MASS_STORAGE_DEVICE_NAME_AND_UUID_MISSING,
29   MTP_STORAGE_DEVICE_NAME_AND_UUID_AVAILABLE,
30   MTP_STORAGE_DEVICE_UUID_MISSING,
31   MTP_STORAGE_DEVICE_NAME_MISSING,
32   MTP_STORAGE_DEVICE_NAME_AND_UUID_MISSING,
33   DEVICE_INFO_BUCKET_BOUNDARY
34 };
35
36 #if !defined(OS_WIN)
37 const char kRootPath[] = "/";
38 #endif
39
40 typedef std::vector<StorageInfo> StorageInfoList;
41
42 base::FilePath::StringType FindRemovableStorageLocationById(
43     const std::string& device_id) {
44   StorageInfoList devices =
45       StorageMonitor::GetInstance()->GetAllAvailableStorages();
46   for (StorageInfoList::const_iterator it = devices.begin();
47        it != devices.end(); ++it) {
48     if (it->device_id() == device_id
49         && StorageInfo::IsRemovableDevice(device_id))
50       return it->location();
51   }
52   return base::FilePath::StringType();
53 }
54
55 void FilterAttachedDevicesOnFileThread(MediaStorageUtil::DeviceIdSet* devices) {
56   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
57   MediaStorageUtil::DeviceIdSet missing_devices;
58
59   for (MediaStorageUtil::DeviceIdSet::const_iterator it = devices->begin();
60        it != devices->end();
61        ++it) {
62     StorageInfo::Type type;
63     std::string unique_id;
64     if (!StorageInfo::CrackDeviceId(*it, &type, &unique_id)) {
65       missing_devices.insert(*it);
66       continue;
67     }
68
69     if (type == StorageInfo::FIXED_MASS_STORAGE ||
70         type == StorageInfo::ITUNES ||
71         type == StorageInfo::IPHOTO ||
72         type == StorageInfo::PICASA) {
73       if (!base::PathExists(base::FilePath::FromUTF8Unsafe(unique_id)))
74         missing_devices.insert(*it);
75       continue;
76     }
77
78     if (!MediaStorageUtil::IsRemovableStorageAttached(*it))
79       missing_devices.insert(*it);
80   }
81
82   for (MediaStorageUtil::DeviceIdSet::const_iterator it =
83            missing_devices.begin();
84        it != missing_devices.end();
85        ++it) {
86     devices->erase(*it);
87   }
88 }
89
90 }  // namespace
91
92 // static
93 bool MediaStorageUtil::HasDcim(const base::FilePath& mount_point) {
94   DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
95
96   base::FilePath::StringType dcim_dir(kDCIMDirectoryName);
97   if (!base::DirectoryExists(mount_point.Append(dcim_dir))) {
98     // Check for lowercase 'dcim' as well.
99     base::FilePath dcim_path_lower(
100         mount_point.Append(StringToLowerASCII(dcim_dir)));
101     if (!base::DirectoryExists(dcim_path_lower))
102       return false;
103   }
104   return true;
105 }
106
107 // static
108 bool MediaStorageUtil::CanCreateFileSystem(const std::string& device_id,
109                                            const base::FilePath& path) {
110   StorageInfo::Type type;
111   if (!StorageInfo::CrackDeviceId(device_id, &type, NULL))
112     return false;
113
114   if (type == StorageInfo::MAC_IMAGE_CAPTURE)
115     return true;
116
117   return path.IsAbsolute() && !path.ReferencesParent();
118 }
119
120 // static
121 void MediaStorageUtil::FilterAttachedDevices(DeviceIdSet* devices,
122                                              const base::Closure& done) {
123   if (BrowserThread::CurrentlyOn(BrowserThread::FILE)) {
124     FilterAttachedDevicesOnFileThread(devices);
125     done.Run();
126     return;
127   }
128   BrowserThread::PostTaskAndReply(BrowserThread::FILE,
129                                   FROM_HERE,
130                                   base::Bind(&FilterAttachedDevicesOnFileThread,
131                                              devices),
132                                   done);
133 }
134
135 // TODO(kmadhusu) Write unit tests for GetDeviceInfoFromPath().
136 bool MediaStorageUtil::GetDeviceInfoFromPath(const base::FilePath& path,
137                                              StorageInfo* device_info,
138                                              base::FilePath* relative_path) {
139   DCHECK(device_info);
140   DCHECK(relative_path);
141
142   if (!path.IsAbsolute())
143     return false;
144
145   StorageInfo info;
146   StorageMonitor* monitor = StorageMonitor::GetInstance();
147   bool found_device = monitor->GetStorageInfoForPath(path, &info);
148
149   if (found_device && StorageInfo::IsRemovableDevice(info.device_id())) {
150     base::FilePath sub_folder_path;
151     base::FilePath device_path(info.location());
152     if (path != device_path) {
153       bool success = device_path.AppendRelativePath(path, &sub_folder_path);
154       DCHECK(success);
155     }
156
157     *device_info = info;
158     *relative_path = sub_folder_path;
159     return true;
160   }
161
162   // On Posix systems, there's one root so any absolute path could be valid.
163   // TODO(gbillock): Delete this stanza? Posix systems should have the root
164   // volume information. If not, we should move the below into the
165   // right GetStorageInfoForPath implementations.
166 #if !defined(OS_POSIX)
167   if (!found_device)
168     return false;
169 #endif
170
171   // Handle non-removable devices. Note: this is just overwriting
172   // good values from StorageMonitor.
173   // TODO(gbillock): Make sure return values from that class are definitive,
174   // and don't do this here.
175   info.set_device_id(
176       StorageInfo::MakeDeviceId(StorageInfo::FIXED_MASS_STORAGE,
177                                 path.AsUTF8Unsafe()));
178   *device_info = info;
179   *relative_path = base::FilePath();
180   return true;
181 }
182
183 // static
184 base::FilePath MediaStorageUtil::FindDevicePathById(
185     const std::string& device_id) {
186   StorageInfo::Type type;
187   std::string unique_id;
188   if (!StorageInfo::CrackDeviceId(device_id, &type, &unique_id))
189     return base::FilePath();
190
191   if (type == StorageInfo::FIXED_MASS_STORAGE ||
192       type == StorageInfo::ITUNES ||
193       type == StorageInfo::IPHOTO ||
194       type == StorageInfo::PICASA) {
195     // For this type, the unique_id is the path.
196     return base::FilePath::FromUTF8Unsafe(unique_id);
197   }
198
199   // For ImageCapture, the synthetic filesystem will be rooted at a fake
200   // top-level directory which is the device_id.
201   if (type == StorageInfo::MAC_IMAGE_CAPTURE) {
202 #if !defined(OS_WIN)
203     return base::FilePath(kRootPath + device_id);
204 #endif
205   }
206
207   DCHECK(type == StorageInfo::MTP_OR_PTP ||
208          type == StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM ||
209          type == StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM);
210   return base::FilePath(FindRemovableStorageLocationById(device_id));
211 }
212
213 // static
214 void MediaStorageUtil::RecordDeviceInfoHistogram(
215     bool mass_storage,
216     const std::string& device_uuid,
217     const base::string16& device_label) {
218   unsigned int event_number = 0;
219   if (!mass_storage)
220     event_number = 4;
221
222   if (device_label.empty())
223     event_number += 2;
224
225   if (device_uuid.empty())
226     event_number += 1;
227   enum DeviceInfoHistogramBuckets event =
228       static_cast<enum DeviceInfoHistogramBuckets>(event_number);
229   if (event >= DEVICE_INFO_BUCKET_BOUNDARY) {
230     NOTREACHED();
231     return;
232   }
233   UMA_HISTOGRAM_ENUMERATION("MediaDeviceNotifications.DeviceInfo", event,
234                             DEVICE_INFO_BUCKET_BOUNDARY);
235 }
236
237 bool MediaStorageUtil::IsRemovableStorageAttached(const std::string& id) {
238   StorageInfoList devices =
239       StorageMonitor::GetInstance()->GetAllAvailableStorages();
240   for (StorageInfoList::const_iterator it = devices.begin();
241        it != devices.end(); ++it) {
242     if (StorageInfo::IsRemovableDevice(id) && it->device_id() == id)
243       return true;
244   }
245   return false;
246 }