[M120 Migration][VD] Enable direct rendering for TVPlus
[platform/framework/web/chromium-efl.git] / components / storage_monitor / storage_info_utils.cc
1 // Copyright 2018 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/storage_info_utils.h"
6
7 #include <string>
8
9 #include "base/files/file_path.h"
10 #include "base/strings/strcat.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_piece.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "components/storage_monitor/removable_device_constants.h"
16 #include "components/storage_monitor/storage_info.h"
17
18 namespace storage_monitor {
19
20 namespace {
21
22 // Device root path constant.
23 const char kRootPath[] = "/";
24
25 // Returns the storage identifier of the device from the given |storage_name|.
26 // E.g. If the |storage_name| is "usb:2,2:65537", the storage identifier is
27 // "65537".
28 std::string GetStorageIdFromStorageName(const std::string& storage_name) {
29   std::vector<base::StringPiece> name_parts = base::SplitStringPiece(
30       storage_name, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
31   return name_parts.size() == 3 ? std::string(name_parts[2]) : std::string();
32 }
33
34 // Returns the |data_store_id| string in the required format.
35 // If the |data_store_id| is 65537, this function returns " (65537)".
36 std::string GetFormattedIdString(const std::string& data_store_id) {
37   return ("(" + data_store_id + ")");
38 }
39
40 }  // namespace
41
42 // Constructs and returns the location of the device using the |storage_name|.
43 std::string GetDeviceLocationFromStorageName(const std::string& storage_name) {
44   // Construct a dummy device path using the storage name. This is only used
45   // for registering device media file system.
46   // E.g.: If the |storage_name| is "usb:2,2:12345" then "/usb:2,2:12345" is the
47   // device location.
48   DCHECK(!storage_name.empty());
49   return kRootPath + storage_name;
50 }
51
52 // Returns a unique device id from the given |storage_info|.
53 std::string GetDeviceIdFromStorageInfo(
54     const device::mojom::MtpStorageInfo& storage_info) {
55   // We'll add a storage_id suffix to our return value. The function name says
56   // "device" but storage_monitor ultimately wants to identify a unit of
57   // logical storage, not necessarily identify a physical device.
58   //
59   // MTP devices (e.g. phones) can have multiple physical storages (e.g. disks)
60   // and each physical storage can have multiple logical storages.
61   //
62   // The storage_id returned by GetStorageIdFromStorageName often looks like
63   // "65537", the string form of 0x0001_0001 that encodes a pair of uint16_t
64   // identifiers for physical and logical storage. Zero values are reserved.
65   // See the MTP spec (MTPforUSB-IFv1.1.pdf) section 5.2.1 Storage IDs.
66   const std::string storage_id =
67       GetStorageIdFromStorageName(storage_info.storage_name);
68   if (storage_id.empty()) {
69     return std::string();
70   }
71
72   // The serial number (and the storage_id) should form a unique id.
73   if (!storage_info.serial_number.empty()) {
74     return StorageInfo::MakeDeviceId(
75         StorageInfo::MTP_OR_PTP,
76         base::StrCat({storage_info.serial_number, ":", storage_id}));
77   }
78
79   // If no serial number is available, fall back to a combination of vendor,
80   // model and volume (and the storage_id). This isn't always unique: attaching
81   // two "Google Pixel" phones to a Chromebook at the same time can produce the
82   // same (vendor, model, volume, storage) 4-tuple. The collision can cause
83   // problems with other code that assumes "device ids" are unique. See
84   // https://crbug.com/1184941
85   //
86   // Nonetheless, it's better than nothing. It also matches how Chrome OS
87   // behaved (not using the serial number) for some years up until 2022.
88   const std::string vendor_id = base::NumberToString(storage_info.vendor_id);
89   const std::string model_id = base::NumberToString(storage_info.product_id);
90   return StorageInfo::MakeDeviceId(
91       StorageInfo::MTP_OR_PTP,
92       base::StrCat({kVendorModelVolumeStoragePrefix, vendor_id, ":", model_id,
93                     ":", storage_info.volume_identifier, ":", storage_id}));
94 }
95
96 // Helper function to get device label from storage information.
97 std::u16string GetDeviceLabelFromStorageInfo(
98     const device::mojom::MtpStorageInfo& storage_info) {
99   std::string device_label;
100   const std::string& vendor_name = storage_info.vendor;
101   device_label = vendor_name;
102
103   const std::string& product_name = storage_info.product;
104   if (!product_name.empty()) {
105     if (!device_label.empty())
106       device_label += " ";
107     device_label += product_name;
108   }
109
110   // Add the data store id to the device label.
111   if (!device_label.empty()) {
112     const std::string& volume_id = storage_info.volume_identifier;
113     if (!volume_id.empty()) {
114       device_label += GetFormattedIdString(volume_id);
115     } else {
116       const std::string data_store_id =
117           GetStorageIdFromStorageName(storage_info.storage_name);
118       if (!data_store_id.empty())
119         device_label += GetFormattedIdString(data_store_id);
120     }
121   }
122   return base::UTF8ToUTF16(device_label);
123 }
124
125 }  // namespace storage_monitor