[M120 Migration][VD] Enable direct rendering for TVPlus
[platform/framework/web/chromium-efl.git] / components / storage_monitor / media_storage_util.cc
1 // Copyright 2014 The Chromium Authors
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 "components/storage_monitor/media_storage_util.h"
6
7 #include <vector>
8
9 #include "base/check_op.h"
10 #include "base/files/file_util.h"
11 #include "base/functional/bind.h"
12 #include "base/functional/callback.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/task/task_traits.h"
16 #include "base/task/thread_pool.h"
17 #include "build/build_config.h"
18 #include "components/storage_monitor/removable_device_constants.h"
19 #include "components/storage_monitor/storage_monitor.h"
20 #include "content/public/browser/browser_thread.h"
21
22 namespace storage_monitor {
23
24 namespace {
25
26 #if !BUILDFLAG(IS_WIN)
27 const char kRootPath[] = "/";
28 #endif
29
30 typedef std::vector<StorageInfo> StorageInfoList;
31
32 base::FilePath::StringType FindRemovableStorageLocationById(
33     const std::string& device_id) {
34   StorageInfoList devices =
35       StorageMonitor::GetInstance()->GetAllAvailableStorages();
36   for (StorageInfoList::const_iterator it = devices.begin();
37        it != devices.end(); ++it) {
38     if (it->device_id() == device_id
39         && StorageInfo::IsRemovableDevice(device_id))
40       return it->location();
41   }
42   return base::FilePath::StringType();
43 }
44
45 void FilterAttachedDevicesOnBackgroundSequence(
46     MediaStorageUtil::DeviceIdSet* devices) {
47   MediaStorageUtil::DeviceIdSet missing_devices;
48
49   for (auto it = devices->begin(); it != devices->end(); ++it) {
50     StorageInfo::Type type;
51     std::string unique_id;
52     if (!StorageInfo::CrackDeviceId(*it, &type, &unique_id)) {
53       missing_devices.insert(*it);
54       continue;
55     }
56
57     if (type == StorageInfo::FIXED_MASS_STORAGE) {
58       if (!base::PathExists(base::FilePath::FromUTF8Unsafe(unique_id)))
59         missing_devices.insert(*it);
60       continue;
61     }
62
63     if (!MediaStorageUtil::IsRemovableStorageAttached(*it))
64       missing_devices.insert(*it);
65   }
66
67   for (auto it = missing_devices.begin(); it != missing_devices.end(); ++it) {
68     devices->erase(*it);
69   }
70 }
71
72 }  // namespace
73
74 // static
75 bool MediaStorageUtil::HasDcim(const base::FilePath& mount_point) {
76   base::FilePath::StringType dcim_dir(kDCIMDirectoryName);
77   if (!base::DirectoryExists(mount_point.Append(dcim_dir))) {
78     // Check for lowercase 'dcim' as well.
79     base::FilePath dcim_path_lower(
80         mount_point.Append(base::ToLowerASCII(dcim_dir)));
81     if (!base::DirectoryExists(dcim_path_lower))
82       return false;
83   }
84   return true;
85 }
86
87 // static
88 bool MediaStorageUtil::CanCreateFileSystem(const std::string& device_id,
89                                            const base::FilePath& path) {
90   StorageInfo::Type type;
91   if (!StorageInfo::CrackDeviceId(device_id, &type, nullptr))
92     return false;
93
94   if (type == StorageInfo::MAC_IMAGE_CAPTURE)
95     return true;
96
97   return !path.empty() && path.IsAbsolute() && !path.ReferencesParent();
98 }
99
100 // static
101 void MediaStorageUtil::FilterAttachedDevices(DeviceIdSet* devices,
102                                              base::OnceClosure done) {
103   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
104   base::ThreadPool::PostTaskAndReply(
105       FROM_HERE,
106       {base::TaskPriority::BEST_EFFORT, base::MayBlock(),
107        base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
108       base::BindOnce(&FilterAttachedDevicesOnBackgroundSequence, devices),
109       std::move(done));
110 }
111
112 // TODO(kmadhusu) Write unit tests for GetDeviceInfoFromPath().
113 // static
114 bool MediaStorageUtil::GetDeviceInfoFromPath(const base::FilePath& path,
115                                              StorageInfo* device_info,
116                                              base::FilePath* relative_path) {
117   DCHECK(device_info);
118   DCHECK(relative_path);
119
120   if (!path.IsAbsolute())
121     return false;
122
123   StorageInfo info;
124   StorageMonitor* monitor = StorageMonitor::GetInstance();
125   bool found_device = monitor->GetStorageInfoForPath(path, &info);
126
127   if (found_device && StorageInfo::IsRemovableDevice(info.device_id())) {
128     base::FilePath sub_folder_path;
129     base::FilePath device_path(info.location());
130     if (path != device_path) {
131       bool success = device_path.AppendRelativePath(path, &sub_folder_path);
132       DCHECK(success);
133     }
134
135     *device_info = info;
136     *relative_path = sub_folder_path;
137     return true;
138   }
139
140   // On Posix systems, there's one root so any absolute path could be valid.
141   // TODO(gbillock): Delete this stanza? Posix systems should have the root
142   // volume information. If not, we should move the below into the
143   // right GetStorageInfoForPath implementations.
144 #if !BUILDFLAG(IS_POSIX)
145   if (!found_device)
146     return false;
147 #endif
148
149   // Handle non-removable devices. Note: this is just overwriting
150   // good values from StorageMonitor.
151   // TODO(gbillock): Make sure return values from that class are definitive,
152   // and don't do this here.
153   info.set_device_id(
154       StorageInfo::MakeDeviceId(StorageInfo::FIXED_MASS_STORAGE,
155                                 path.AsUTF8Unsafe()));
156   *device_info = info;
157   *relative_path = base::FilePath();
158   return true;
159 }
160
161 // static
162 base::FilePath MediaStorageUtil::FindDevicePathById(
163     const std::string& device_id) {
164   StorageInfo::Type type;
165   std::string unique_id;
166   if (!StorageInfo::CrackDeviceId(device_id, &type, &unique_id))
167     return base::FilePath();
168
169   if (type == StorageInfo::FIXED_MASS_STORAGE) {
170     // For this type, the unique_id is the path.
171     return base::FilePath::FromUTF8Unsafe(unique_id);
172   }
173
174   // For ImageCapture, the synthetic filesystem will be rooted at a fake
175   // top-level directory which is the device_id.
176   if (type == StorageInfo::MAC_IMAGE_CAPTURE) {
177 #if !BUILDFLAG(IS_WIN)
178     return base::FilePath(kRootPath + device_id);
179 #endif
180   }
181
182   DCHECK(type == StorageInfo::MTP_OR_PTP ||
183          type == StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM ||
184          type == StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM);
185   return base::FilePath(FindRemovableStorageLocationById(device_id));
186 }
187
188 // static
189 bool MediaStorageUtil::IsRemovableStorageAttached(const std::string& id) {
190   StorageMonitor* monitor = StorageMonitor::GetInstance();
191   if (!monitor)
192     return false;
193
194   StorageInfoList devices = monitor->GetAllAvailableStorages();
195   for (const auto& device : devices) {
196     if (StorageInfo::IsRemovableDevice(id) && device.device_id() == id)
197       return true;
198   }
199   return false;
200 }
201
202 }  // namespace storage_monitor