Home | History | Annotate | Download | only in storage_monitor
      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/storage_info.h"
      6 
      7 #include "base/logging.h"
      8 
      9 namespace {
     10 
     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:";
     20 
     21 }  // namespace
     22 
     23 StorageInfo::StorageInfo() : total_size_in_bytes_(0) {
     24 }
     25 
     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,
     32                          uint64 size_in_bytes)
     33     : device_id_(device_id_in),
     34       name_(device_name),
     35       location_(device_location),
     36       storage_label_(label),
     37       vendor_name_(vendor),
     38       model_name_(model),
     39       total_size_in_bytes_(size_in_bytes) {
     40 }
     41 
     42 StorageInfo::~StorageInfo() {
     43 }
     44 
     45 // static
     46 std::string StorageInfo::MakeDeviceId(Type type, const std::string& unique_id) {
     47   DCHECK(!unique_id.empty());
     48   switch (type) {
     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;
     55     case MTP_OR_PTP:
     56       return std::string(kMtpPtpPrefix) + unique_id;
     57     case MAC_IMAGE_CAPTURE:
     58       return std::string(kMacImageCapturePrefix) + unique_id;
     59     case ITUNES:
     60       return std::string(kITunesPrefix) + unique_id;
     61     case PICASA:
     62       return std::string(kPicasaPrefix) + unique_id;
     63     case IPHOTO:
     64       return std::string(kIPhotoPrefix) + unique_id;
     65   }
     66   NOTREACHED();
     67   return std::string();
     68 }
     69 
     70 // static
     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)
     76                            : std::string();
     77 
     78   Type found_type;
     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) {
     90     found_type = ITUNES;
     91   } else if (prefix == kPicasaPrefix) {
     92     found_type = PICASA;
     93   } else if (prefix == kIPhotoPrefix) {
     94     found_type = IPHOTO;
     95   } else {
     96     NOTREACHED();
     97     return false;
     98   }
     99   if (type)
    100     *type = found_type;
    101 
    102   if (unique_id)
    103     *unique_id = device_id.substr(prefix_length + 1);
    104   return true;
    105 }
    106 
    107 // static
    108 bool StorageInfo::IsMediaDevice(const std::string& device_id) {
    109   Type type;
    110   return CrackDeviceId(device_id, &type, NULL) &&
    111       (type == REMOVABLE_MASS_STORAGE_WITH_DCIM || type == MTP_OR_PTP ||
    112        type == MAC_IMAGE_CAPTURE);
    113 }
    114 
    115 // static
    116 bool StorageInfo::IsRemovableDevice(const std::string& device_id) {
    117   Type type;
    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);
    123 }
    124 
    125 // static
    126 bool StorageInfo::IsMassStorageDevice(const std::string& device_id) {
    127   Type type;
    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 ||
    132           type == ITUNES ||
    133           type == IPHOTO ||
    134           type == PICASA);
    135 }
    136 
    137 // static
    138 bool StorageInfo::IsITunesDevice(const std::string& device_id) {
    139   Type type;
    140   return CrackDeviceId(device_id, &type, NULL) && type == ITUNES;
    141 }
    142 
    143 // static
    144 bool StorageInfo::IsIPhotoDevice(const std::string& device_id) {
    145   Type type;
    146   return CrackDeviceId(device_id, &type, NULL) && type == IPHOTO;
    147 }
    148 
    149 // static
    150 bool StorageInfo::IsPicasaDevice(const std::string& device_id) {
    151   Type type;
    152   return CrackDeviceId(device_id, &type, NULL) && type == PICASA;
    153 }
    154