Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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/system_monitor/chromeos/media_transfer_protocol_device_ observer.h" | |
| 6 | |
| 7 #include "base/metrics/histogram.h" | |
| 8 #include "base/stl_util.h" | |
| 9 #include "base/string_number_conversions.h" | |
| 10 #include "base/string_split.h" | |
| 11 #include "base/system_monitor/system_monitor.h" | |
| 12 #include "base/utf_string_conversions.h" | |
| 13 #include "chrome/browser/chromeos/mtp/media_transfer_protocol_manager.h" | |
| 14 #include "chrome/browser/system_monitor/removable_device_constants.h" | |
| 15 #include "chrome/browser/system_monitor/media_storage_util.h" | |
| 16 #include "chromeos/dbus/mtp_storage_info.pb.h" | |
| 17 | |
| 18 namespace chromeos { | |
| 19 namespace mtp { | |
| 20 | |
| 21 using chrome::MediaStorageUtil; | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 // Device root path constant. | |
| 26 const char kRootPath[] = "/"; | |
| 27 | |
| 28 // Helper function to get an instance of MediaTransferProtocolManager. | |
| 29 MediaTransferProtocolManager* GetMediaTransferProtocolManager() { | |
| 30 MediaTransferProtocolManager* mtp_dev_mgr = | |
| 31 MediaTransferProtocolManager::GetInstance(); | |
| 32 return mtp_dev_mgr; | |
| 33 } | |
| 34 | |
| 35 // Helper function to get device id from storage information. | |
| 36 std::string GetDeviceIdFromStorageInfo(const MtpStorageInfo& storage_info, | |
|
Lei Zhang
2012/09/10 18:18:01
|storage_info| is not used anymore. Function name
kmadhusu
2012/09/11 01:50:19
oops. Fixed. Thanks for catching this.
| |
| 37 const std::string& storage_name) { | |
| 38 std::string unique_id(chrome::kFSUniqueIdPrefix + storage_name); | |
| 39 return MediaStorageUtil::MakeDeviceId(MediaStorageUtil::MTP_OR_PTP, | |
| 40 unique_id); | |
| 41 } | |
| 42 | |
| 43 // Helper function to get device label from storage information. | |
| 44 string16 GetDeviceLabelFromStorageInfo(const MtpStorageInfo& storage_info) { | |
| 45 std::string device_label; | |
| 46 const std::string& vendor_name = storage_info.vendor(); | |
| 47 device_label = vendor_name; | |
| 48 | |
| 49 const std::string& product_name = storage_info.product(); | |
| 50 if (!product_name.empty()) { | |
| 51 if (!device_label.empty()) | |
| 52 device_label += chrome::kSpaceDelim; | |
| 53 device_label += product_name; | |
| 54 } | |
| 55 return UTF8ToUTF16(device_label); | |
| 56 } | |
| 57 | |
| 58 // Helper function to get the device storage details such as device id, label | |
| 59 // and location. On success and fills in |id|, |label| and |location|. | |
| 60 void GetStorageInfo(const std::string& storage_name, | |
| 61 std::string* id, | |
| 62 string16* label, | |
| 63 std::string* location) { | |
| 64 DCHECK(!storage_name.empty()); | |
| 65 MediaTransferProtocolManager* mtp_dev_mgr = GetMediaTransferProtocolManager(); | |
| 66 DCHECK(mtp_dev_mgr); | |
| 67 const MtpStorageInfo* storage_info = | |
| 68 mtp_dev_mgr->GetStorageInfo(storage_name); | |
| 69 | |
| 70 if (!storage_info) | |
| 71 return; | |
| 72 | |
| 73 if (id) | |
| 74 *id = GetDeviceIdFromStorageInfo(*storage_info, storage_name); | |
| 75 | |
| 76 if (label) | |
| 77 *label = GetDeviceLabelFromStorageInfo(*storage_info); | |
| 78 | |
| 79 // Construct a dummy device path using the storage name. This is only used | |
| 80 // for registering device media file system. | |
| 81 // E.g.: /usb:2,2:12345 | |
| 82 if (location) | |
| 83 *location = kRootPath + storage_name; | |
| 84 } | |
| 85 | |
| 86 } // namespace | |
| 87 | |
| 88 MediaTransferProtocolDeviceObserver::MediaTransferProtocolDeviceObserver() | |
| 89 : get_storage_info_func_(&GetStorageInfo) { | |
| 90 MediaTransferProtocolManager* mtp_dev_mgr = GetMediaTransferProtocolManager(); | |
| 91 if (mtp_dev_mgr) | |
| 92 mtp_dev_mgr->AddObserver(this); | |
| 93 EnumerateStorages(); | |
| 94 } | |
| 95 | |
| 96 MediaTransferProtocolDeviceObserver::MediaTransferProtocolDeviceObserver( | |
| 97 GetStorageInfoFunc get_storage_info_func) | |
| 98 : get_storage_info_func_(get_storage_info_func) { | |
|
Lei Zhang
2012/09/10 18:18:01
It's weird how this doesn't call AddObserver(), wh
kmadhusu
2012/09/11 01:50:19
Added DCHECK and a comment in the function body.
| |
| 99 } | |
| 100 | |
| 101 MediaTransferProtocolDeviceObserver::~MediaTransferProtocolDeviceObserver() { | |
| 102 MediaTransferProtocolManager* mtp_dev_mgr = GetMediaTransferProtocolManager(); | |
| 103 if (mtp_dev_mgr) | |
| 104 mtp_dev_mgr->RemoveObserver(this); | |
| 105 } | |
| 106 | |
| 107 // MediaTransferProtocolManager::Observer override. | |
| 108 void MediaTransferProtocolDeviceObserver::StorageChanged( | |
| 109 bool is_attached, | |
| 110 const std::string& storage_name) { | |
| 111 DCHECK(!storage_name.empty()); | |
| 112 | |
| 113 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); | |
| 114 DCHECK(system_monitor); | |
| 115 | |
| 116 // New storage is attached. | |
| 117 if (is_attached) { | |
| 118 std::string device_id; | |
| 119 string16 device_name; | |
| 120 std::string location; | |
| 121 get_storage_info_func_(storage_name, &device_id, &device_name, &location); | |
| 122 | |
| 123 // Keep track of device id and device name to see how often we receive | |
| 124 // empty values. | |
| 125 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.mtp_device_uuid_available", | |
| 126 !device_id.empty()); | |
| 127 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.mtp_device_name_available", | |
| 128 !device_name.empty()); | |
| 129 | |
| 130 if (device_id.empty() || device_name.empty()) | |
| 131 return; | |
| 132 | |
| 133 DCHECK(!ContainsKey(storage_map_, storage_name)); | |
| 134 storage_map_[storage_name] = device_id; | |
| 135 system_monitor->ProcessRemovableStorageAttached(device_id, device_name, | |
| 136 location); | |
| 137 } else { | |
| 138 // Existing storage is detached. | |
| 139 StorageNameAndIdMap::iterator it = storage_map_.find(storage_name); | |
| 140 if (it == storage_map_.end()) | |
| 141 return; | |
| 142 system_monitor->ProcessRemovableStorageDetached(it->second); | |
| 143 storage_map_.erase(it); | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 void MediaTransferProtocolDeviceObserver::EnumerateStorages() { | |
| 148 typedef std::vector<std::string> StorageList; | |
| 149 MediaTransferProtocolManager* mtp_dev_mgr = GetMediaTransferProtocolManager(); | |
| 150 DCHECK(mtp_dev_mgr); | |
| 151 StorageList storages = mtp_dev_mgr->GetStorages(); | |
| 152 for (StorageList::const_iterator storage_iter = storages.begin(); | |
| 153 storage_iter != storages.end(); ++storage_iter) { | |
| 154 StorageChanged(true, *storage_iter); | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 } // namespace mtp | |
| 159 } // namespace chromeos | |
| OLD | NEW |