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.
5 #include "chrome/browser/storage_monitor/storage_info.h"
7 #include "base/logging.h"
11 // Prefix constants for different device id spaces.
12 const char kRemovableMassStorageWithDCIMPrefix
[] = "dcim:";
13 const char kRemovableMassStorageNoDCIMPrefix
[] = "nodcim:";
14 const char kFixedMassStoragePrefix
[] = "path:";
15 const char kMtpPtpPrefix
[] = "mtp:";
16 const char kMacImageCapturePrefix
[] = "ic:";
17 const char kITunesPrefix
[] = "itunes:";
18 const char kPicasaPrefix
[] = "picasa:";
19 const char kIPhotoPrefix
[] = "iphoto:";
23 StorageInfo::StorageInfo() : total_size_in_bytes_(0) {
26 StorageInfo::StorageInfo(const std::string
& device_id_in
,
27 const base::string16
& device_name
,
28 const base::FilePath::StringType
& device_location
,
29 const base::string16
& label
,
30 const base::string16
& vendor
,
31 const base::string16
& model
,
33 : device_id_(device_id_in
),
35 location_(device_location
),
36 storage_label_(label
),
39 total_size_in_bytes_(size_in_bytes
) {
42 StorageInfo::~StorageInfo() {
46 std::string
StorageInfo::MakeDeviceId(Type type
, const std::string
& unique_id
) {
47 DCHECK(!unique_id
.empty());
49 case REMOVABLE_MASS_STORAGE_WITH_DCIM
:
50 return std::string(kRemovableMassStorageWithDCIMPrefix
) + unique_id
;
51 case REMOVABLE_MASS_STORAGE_NO_DCIM
:
52 return std::string(kRemovableMassStorageNoDCIMPrefix
) + unique_id
;
53 case FIXED_MASS_STORAGE
:
54 return std::string(kFixedMassStoragePrefix
) + unique_id
;
56 return std::string(kMtpPtpPrefix
) + unique_id
;
57 case MAC_IMAGE_CAPTURE
:
58 return std::string(kMacImageCapturePrefix
) + unique_id
;
60 return std::string(kITunesPrefix
) + unique_id
;
62 return std::string(kPicasaPrefix
) + unique_id
;
64 return std::string(kIPhotoPrefix
) + unique_id
;
71 bool StorageInfo::CrackDeviceId(const std::string
& device_id
,
72 Type
* type
, std::string
* unique_id
) {
73 size_t prefix_length
= device_id
.find_first_of(':');
74 std::string prefix
= prefix_length
!= std::string::npos
75 ? device_id
.substr(0, prefix_length
+ 1)
79 if (prefix
== kRemovableMassStorageWithDCIMPrefix
) {
80 found_type
= REMOVABLE_MASS_STORAGE_WITH_DCIM
;
81 } else if (prefix
== kRemovableMassStorageNoDCIMPrefix
) {
82 found_type
= REMOVABLE_MASS_STORAGE_NO_DCIM
;
83 } else if (prefix
== kFixedMassStoragePrefix
) {
84 found_type
= FIXED_MASS_STORAGE
;
85 } else if (prefix
== kMtpPtpPrefix
) {
86 found_type
= MTP_OR_PTP
;
87 } else if (prefix
== kMacImageCapturePrefix
) {
88 found_type
= MAC_IMAGE_CAPTURE
;
89 } else if (prefix
== kITunesPrefix
) {
91 } else if (prefix
== kPicasaPrefix
) {
93 } else if (prefix
== kIPhotoPrefix
) {
103 *unique_id
= device_id
.substr(prefix_length
+ 1);
108 bool StorageInfo::IsMediaDevice(const std::string
& device_id
) {
110 return CrackDeviceId(device_id
, &type
, NULL
) &&
111 (type
== REMOVABLE_MASS_STORAGE_WITH_DCIM
|| type
== MTP_OR_PTP
||
112 type
== MAC_IMAGE_CAPTURE
);
116 bool StorageInfo::IsRemovableDevice(const std::string
& device_id
) {
118 return CrackDeviceId(device_id
, &type
, NULL
) &&
119 (type
== REMOVABLE_MASS_STORAGE_WITH_DCIM
||
120 type
== REMOVABLE_MASS_STORAGE_NO_DCIM
||
121 type
== MTP_OR_PTP
||
122 type
== MAC_IMAGE_CAPTURE
);
126 bool StorageInfo::IsMassStorageDevice(const std::string
& device_id
) {
128 return CrackDeviceId(device_id
, &type
, NULL
) &&
129 (type
== REMOVABLE_MASS_STORAGE_WITH_DCIM
||
130 type
== REMOVABLE_MASS_STORAGE_NO_DCIM
||
131 type
== FIXED_MASS_STORAGE
||
138 bool StorageInfo::IsITunesDevice(const std::string
& device_id
) {
140 return CrackDeviceId(device_id
, &type
, NULL
) && type
== ITUNES
;
144 bool StorageInfo::IsIPhotoDevice(const std::string
& device_id
) {
146 return CrackDeviceId(device_id
, &type
, NULL
) && type
== IPHOTO
;
150 bool StorageInfo::IsPicasaDevice(const std::string
& device_id
) {
152 return CrackDeviceId(device_id
, &type
, NULL
) && type
== PICASA
;