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/storage_info.h"
7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "components/storage_monitor/media_storage_util.h"
10 #include "ui/base/l10n/l10n_util.h"
11 #include "ui/base/text/bytes_formatting.h"
15 // Prefix constants for different device id spaces.
16 const char kRemovableMassStorageWithDCIMPrefix
[] = "dcim:";
17 const char kRemovableMassStorageNoDCIMPrefix
[] = "nodcim:";
18 const char kFixedMassStoragePrefix
[] = "path:";
19 const char kMtpPtpPrefix
[] = "mtp:";
20 const char kMacImageCapturePrefix
[] = "ic:";
21 const char kITunesPrefix
[] = "itunes:";
22 const char kPicasaPrefix
[] = "picasa:";
23 const char kIPhotoPrefix
[] = "iphoto:";
25 base::string16
GetDisplayNameForDevice(uint64 storage_size_in_bytes
,
26 const base::string16
& name
) {
27 DCHECK(!name
.empty());
28 return (storage_size_in_bytes
== 0) ?
30 ui::FormatBytes(storage_size_in_bytes
) + base::ASCIIToUTF16(" ") + name
;
33 base::string16
GetFullProductName(const base::string16
& vendor_name
,
34 const base::string16
& model_name
) {
35 if (vendor_name
.empty() && model_name
.empty())
36 return base::string16();
38 base::string16 product_name
;
39 if (vendor_name
.empty())
40 product_name
= model_name
;
41 else if (model_name
.empty())
42 product_name
= vendor_name
;
43 else if (!vendor_name
.empty() && !model_name
.empty())
44 product_name
= vendor_name
+ base::UTF8ToUTF16(", ") + model_name
;
51 namespace storage_monitor
{
53 StorageInfo::StorageInfo() : total_size_in_bytes_(0) {
56 StorageInfo::StorageInfo(const std::string
& device_id_in
,
57 const base::FilePath::StringType
& device_location
,
58 const base::string16
& label
,
59 const base::string16
& vendor
,
60 const base::string16
& model
,
62 : device_id_(device_id_in
),
63 location_(device_location
),
64 storage_label_(label
),
67 total_size_in_bytes_(size_in_bytes
) {
70 StorageInfo::~StorageInfo() {
74 std::string
StorageInfo::MakeDeviceId(Type type
, const std::string
& unique_id
) {
75 DCHECK(!unique_id
.empty());
77 case REMOVABLE_MASS_STORAGE_WITH_DCIM
:
78 return std::string(kRemovableMassStorageWithDCIMPrefix
) + unique_id
;
79 case REMOVABLE_MASS_STORAGE_NO_DCIM
:
80 return std::string(kRemovableMassStorageNoDCIMPrefix
) + unique_id
;
81 case FIXED_MASS_STORAGE
:
82 return std::string(kFixedMassStoragePrefix
) + unique_id
;
84 return std::string(kMtpPtpPrefix
) + unique_id
;
85 case MAC_IMAGE_CAPTURE
:
86 return std::string(kMacImageCapturePrefix
) + unique_id
;
88 return std::string(kITunesPrefix
) + unique_id
;
90 return std::string(kPicasaPrefix
) + unique_id
;
92 return std::string(kIPhotoPrefix
) + unique_id
;
99 bool StorageInfo::CrackDeviceId(const std::string
& device_id
,
100 Type
* type
, std::string
* unique_id
) {
101 size_t prefix_length
= device_id
.find_first_of(':');
102 std::string prefix
= prefix_length
!= std::string::npos
103 ? device_id
.substr(0, prefix_length
+ 1)
107 if (prefix
== kRemovableMassStorageWithDCIMPrefix
) {
108 found_type
= REMOVABLE_MASS_STORAGE_WITH_DCIM
;
109 } else if (prefix
== kRemovableMassStorageNoDCIMPrefix
) {
110 found_type
= REMOVABLE_MASS_STORAGE_NO_DCIM
;
111 } else if (prefix
== kFixedMassStoragePrefix
) {
112 found_type
= FIXED_MASS_STORAGE
;
113 } else if (prefix
== kMtpPtpPrefix
) {
114 found_type
= MTP_OR_PTP
;
115 } else if (prefix
== kMacImageCapturePrefix
) {
116 found_type
= MAC_IMAGE_CAPTURE
;
117 } else if (prefix
== kITunesPrefix
) {
119 } else if (prefix
== kPicasaPrefix
) {
121 } else if (prefix
== kIPhotoPrefix
) {
131 *unique_id
= device_id
.substr(prefix_length
+ 1);
136 bool StorageInfo::IsMediaDevice(const std::string
& device_id
) {
138 return CrackDeviceId(device_id
, &type
, NULL
) &&
139 (type
== REMOVABLE_MASS_STORAGE_WITH_DCIM
|| type
== MTP_OR_PTP
||
140 type
== MAC_IMAGE_CAPTURE
);
144 bool StorageInfo::IsRemovableDevice(const std::string
& device_id
) {
146 return CrackDeviceId(device_id
, &type
, NULL
) &&
147 (type
== REMOVABLE_MASS_STORAGE_WITH_DCIM
||
148 type
== REMOVABLE_MASS_STORAGE_NO_DCIM
||
149 type
== MTP_OR_PTP
||
150 type
== MAC_IMAGE_CAPTURE
);
154 bool StorageInfo::IsMassStorageDevice(const std::string
& device_id
) {
156 return CrackDeviceId(device_id
, &type
, NULL
) &&
157 (type
== REMOVABLE_MASS_STORAGE_WITH_DCIM
||
158 type
== REMOVABLE_MASS_STORAGE_NO_DCIM
||
159 type
== FIXED_MASS_STORAGE
||
166 bool StorageInfo::IsITunesDevice(const std::string
& device_id
) {
168 return CrackDeviceId(device_id
, &type
, NULL
) && type
== ITUNES
;
172 bool StorageInfo::IsIPhotoDevice(const std::string
& device_id
) {
174 return CrackDeviceId(device_id
, &type
, NULL
) && type
== IPHOTO
;
178 bool StorageInfo::IsPicasaDevice(const std::string
& device_id
) {
180 return CrackDeviceId(device_id
, &type
, NULL
) && type
== PICASA
;
184 bool StorageInfo::IsMTPDevice(const std::string
& device_id
) {
186 return CrackDeviceId(device_id
, &type
, NULL
) && type
== MTP_OR_PTP
;
189 base::string16
StorageInfo::GetDisplayName(bool with_size
) const {
190 return GetDisplayNameWithOverride(base::string16(), with_size
);
193 base::string16
StorageInfo::GetDisplayNameWithOverride(
194 const base::string16
& override_display_name
, bool with_size
) const {
195 if (!IsRemovableDevice(device_id_
)) {
196 if (!storage_label_
.empty())
197 return storage_label_
;
198 return base::FilePath(location_
).LossyDisplayName();
201 base::string16 name
= override_display_name
;
203 name
= storage_label_
;
205 name
= GetFullProductName(vendor_name_
, model_name_
);
207 name
= base::ASCIIToUTF16("Unlabeled device");
210 name
= GetDisplayNameForDevice(total_size_in_bytes_
, name
);
214 } // namespace storage_monitor