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 chrome {
     10 
     11 namespace {
     12 
     13 // Prefix constants for different device id spaces.
     14 const char kRemovableMassStorageWithDCIMPrefix[] = "dcim:";
     15 const char kRemovableMassStorageNoDCIMPrefix[] = "nodcim:";
     16 const char kFixedMassStoragePrefix[] = "path:";
     17 const char kMtpPtpPrefix[] = "mtp:";
     18 const char kMacImageCapturePrefix[] = "ic:";
     19 const char kITunesPrefix[] = "itunes:";
     20 const char kPicasaPrefix[] = "picasa:";
     21 
     22 }  // namespace
     23 
     24 StorageInfo::StorageInfo() : total_size_in_bytes_(0) {
     25 }
     26 
     27 StorageInfo::StorageInfo(const std::string& device_id_in,
     28                          const string16& device_name,
     29                          const base::FilePath::StringType& device_location,
     30                          const string16& label,
     31                          const string16& vendor,
     32                          const string16& model,
     33                          uint64 size_in_bytes)
     34     : device_id_(device_id_in),
     35       name_(device_name),
     36       location_(device_location),
     37       storage_label_(label),
     38       vendor_name_(vendor),
     39       model_name_(model),
     40       total_size_in_bytes_(size_in_bytes) {
     41 }
     42 
     43 StorageInfo::~StorageInfo() {
     44 }
     45 
     46 // static
     47 std::string StorageInfo::MakeDeviceId(Type type, const std::string& unique_id) {
     48   DCHECK(!unique_id.empty());
     49   switch (type) {
     50     case REMOVABLE_MASS_STORAGE_WITH_DCIM:
     51       return std::string(kRemovableMassStorageWithDCIMPrefix) + unique_id;
     52     case REMOVABLE_MASS_STORAGE_NO_DCIM:
     53       return std::string(kRemovableMassStorageNoDCIMPrefix) + unique_id;
     54     case FIXED_MASS_STORAGE:
     55       return std::string(kFixedMassStoragePrefix) + unique_id;
     56     case MTP_OR_PTP:
     57       return std::string(kMtpPtpPrefix) + unique_id;
     58     case MAC_IMAGE_CAPTURE:
     59       return std::string(kMacImageCapturePrefix) + unique_id;
     60     case ITUNES:
     61       return std::string(kITunesPrefix) + unique_id;
     62     case PICASA:
     63       return std::string(kPicasaPrefix) + unique_id;
     64   }
     65   NOTREACHED();
     66   return std::string();
     67 }
     68 
     69 // static
     70 bool StorageInfo::CrackDeviceId(const std::string& device_id,
     71                                 Type* type, std::string* unique_id) {
     72   size_t prefix_length = device_id.find_first_of(':');
     73   std::string prefix = prefix_length != std::string::npos
     74                            ? device_id.substr(0, prefix_length + 1)
     75                            : std::string();
     76 
     77   Type found_type;
     78   if (prefix == kRemovableMassStorageWithDCIMPrefix) {
     79     found_type = REMOVABLE_MASS_STORAGE_WITH_DCIM;
     80   } else if (prefix == kRemovableMassStorageNoDCIMPrefix) {
     81     found_type = REMOVABLE_MASS_STORAGE_NO_DCIM;
     82   } else if (prefix == kFixedMassStoragePrefix) {
     83     found_type = FIXED_MASS_STORAGE;
     84   } else if (prefix == kMtpPtpPrefix) {
     85     found_type = MTP_OR_PTP;
     86   } else if (prefix == kMacImageCapturePrefix) {
     87     found_type = MAC_IMAGE_CAPTURE;
     88   } else if (prefix == kITunesPrefix) {
     89     found_type = ITUNES;
     90   } else if (prefix == kPicasaPrefix) {
     91     found_type = PICASA;
     92   } else {
     93     NOTREACHED();
     94     return false;
     95   }
     96   if (type)
     97     *type = found_type;
     98 
     99   if (unique_id)
    100     *unique_id = device_id.substr(prefix_length + 1);
    101   return true;
    102 }
    103 
    104 // static
    105 bool StorageInfo::IsMediaDevice(const std::string& device_id) {
    106   Type type;
    107   return CrackDeviceId(device_id, &type, NULL) &&
    108       (type == REMOVABLE_MASS_STORAGE_WITH_DCIM || type == MTP_OR_PTP ||
    109        type == MAC_IMAGE_CAPTURE);
    110 }
    111 
    112 // static
    113 bool StorageInfo::IsRemovableDevice(const std::string& device_id) {
    114   Type type;
    115   return CrackDeviceId(device_id, &type, NULL) &&
    116          (type == REMOVABLE_MASS_STORAGE_WITH_DCIM ||
    117           type == REMOVABLE_MASS_STORAGE_NO_DCIM ||
    118           type == MTP_OR_PTP ||
    119           type == MAC_IMAGE_CAPTURE);
    120 }
    121 
    122 // static
    123 bool StorageInfo::IsMassStorageDevice(const std::string& device_id) {
    124   Type type;
    125   return CrackDeviceId(device_id, &type, NULL) &&
    126          (type == REMOVABLE_MASS_STORAGE_WITH_DCIM ||
    127           type == REMOVABLE_MASS_STORAGE_NO_DCIM ||
    128           type == FIXED_MASS_STORAGE ||
    129           type == ITUNES ||
    130           type == PICASA);
    131 }
    132 
    133 // static
    134 bool StorageInfo::IsITunesDevice(const std::string& device_id) {
    135   Type type;
    136   return CrackDeviceId(device_id, &type, NULL) && type == ITUNES;
    137 }
    138 
    139 // static
    140 bool StorageInfo::IsPicasaDevice(const std::string& device_id) {
    141   Type type;
    142   return CrackDeviceId(device_id, &type, NULL) && type == PICASA;
    143 }
    144 
    145 }  // namespace chrome
    146