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/chromeos/mtp/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/stringprintf.h" | |
12 #include "base/system_monitor/system_monitor.h" | |
13 #include "base/utf_string_conversions.h" | |
14 #include "chrome/browser/chromeos/mtp/media_transfer_protocol_manager.h" | |
15 #include "chrome/browser/media_gallery/media_gallery_constants.h" | |
16 #include "chrome/browser/media_gallery/media_storage_util.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 DCHECK(mtp_dev_mgr); | |
33 return mtp_dev_mgr; | |
34 } | |
35 | |
36 // Helper function to get device id from storage information. | |
37 std::string GetDeviceIdFromStorageInfo(const StorageInfo& storage_info, | |
38 const std::string& storage_name) { | |
39 std::string unique_id; | |
40 const std::string vendor_id = base::UintToString(storage_info.vendor_id()); | |
41 const std::string model_id = base::UintToString(storage_info.product_id()); | |
42 const std::string& volume_id = storage_info.volume_identifier(); | |
43 if (!vendor_id.empty() || !model_id.empty() || !volume_id.empty()) { | |
44 unique_id = base::StringPrintf("%s%s%s%s%s%s", | |
45 chrome::kVendorModelSerialPrefix, | |
46 vendor_id.c_str(), | |
47 chrome::kNonSpaceDelim, | |
48 model_id.c_str(), | |
49 chrome::kNonSpaceDelim, | |
50 volume_id.c_str()); | |
51 } else { | |
52 unique_id = std::string(chrome::kFSUniqueIdPrefix) + storage_name; | |
53 } | |
54 return MediaStorageUtil::MakeDeviceId(MediaStorageUtil::MTP_OR_PTP, | |
55 unique_id); | |
56 } | |
57 | |
58 // Helper functio to get device label from storage information. | |
Lei Zhang
2012/08/30 02:30:20
nit: typo
kmadhusu
2012/08/30 03:12:58
Done.
| |
59 string16 GetDeviceLabelFromStorageInfo(const StorageInfo& storage_info) { | |
60 std::string device_label; | |
61 const std::string& vendor_name = storage_info.vendor(); | |
62 if (!vendor_name.empty()) | |
Lei Zhang
2012/08/30 02:30:20
You can get rid of this check. You'll do it again
kmadhusu
2012/08/30 03:12:58
Done.
| |
63 device_label = vendor_name; | |
64 | |
65 const std::string& product_name = storage_info.product(); | |
66 if (!product_name.empty()) { | |
67 if (!device_label.empty()) | |
68 device_label += chrome::kSpaceDelim; | |
69 device_label += product_name; | |
70 } | |
71 return UTF8ToUTF16(device_label); | |
72 } | |
73 | |
74 // Helper function to get the device storage details such as device id, label | |
75 // and location. On success and fills in |id|, |label| and |location|. | |
76 void GetStorageInfo(const std::string& storage_name, | |
77 std::string* id, | |
78 string16* label, | |
79 std::string* location) { | |
80 DCHECK(!storage_name.empty()); | |
81 StorageInfo storage_info; | |
82 GetMediaTransferProtocolManager()->GetStorageInfo(storage_name, | |
83 &storage_info); | |
84 | |
85 if (id) | |
86 *id = GetDeviceIdFromStorageInfo(storage_info, storage_name); | |
87 | |
88 if (label) | |
89 *label = GetDeviceLabelFromStorageInfo(storage_info); | |
90 | |
91 // Construct a dummy device path using the storage name. This is only used | |
92 // for registering device media file system. | |
93 // E.g.: /usb:2,2:12345 | |
94 if (location) | |
95 *location = std::string(kRootPath) + storage_name; | |
Lei Zhang
2012/08/30 02:30:20
You probably don't need to construct a string for
kmadhusu
2012/08/30 03:12:58
Done.
| |
96 } | |
97 | |
98 } // namespace | |
99 | |
100 MediaTransferProtocolDeviceObserver::MediaTransferProtocolDeviceObserver() | |
101 : get_storage_info_func_(&GetStorageInfo) { | |
102 GetMediaTransferProtocolManager()->AddObserver(this); | |
103 EnumerateStorages(); | |
104 } | |
105 | |
106 MediaTransferProtocolDeviceObserver::MediaTransferProtocolDeviceObserver( | |
107 GetStorageInfoFunc get_storage_info_func) | |
108 : get_storage_info_func_(get_storage_info_func) { | |
109 } | |
110 | |
111 MediaTransferProtocolDeviceObserver::~MediaTransferProtocolDeviceObserver() { | |
112 if (GetMediaTransferProtocolManager()) | |
113 GetMediaTransferProtocolManager()->RemoveObserver(this); | |
114 } | |
115 | |
116 // MediaTransferProtocolManager::Observer override. | |
117 void MediaTransferProtocolDeviceObserver::StorageChanged( | |
118 bool is_attached, | |
119 const std::string& storage_name) { | |
120 DCHECK(!storage_name.empty()); | |
121 | |
122 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); | |
123 DCHECK(system_monitor); | |
124 | |
125 // New storage is attached. | |
126 if (is_attached) { | |
127 std::string device_id; | |
128 string16 device_name; | |
129 std::string location; | |
130 get_storage_info_func_(storage_name, &device_id, &device_name, &location); | |
131 | |
132 // Keep track of device id and device name to see how often we receive | |
133 // empty values. | |
134 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.mtp_device_uuid_available", | |
135 !device_id.empty()); | |
136 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.mtp_device_name_available", | |
137 !device_name.empty()); | |
138 | |
139 if (device_id.empty() || device_name.empty()) | |
Lei Zhang
2012/08/30 02:30:20
So we are assuming all mtp/ptp devices will always
kmadhusu
2012/08/30 03:12:58
We are not assuming that. If they don't have valid
| |
140 return; | |
141 | |
142 DCHECK(!ContainsKey(storage_map_, storage_name)); | |
143 storage_map_[storage_name] = device_id; | |
144 system_monitor->ProcessRemovableStorageAttached(device_id, device_name, | |
145 location); | |
146 } else { | |
147 // Existing storage is detached. | |
148 StorageNameAndIdMap::iterator it = storage_map_.find(storage_name); | |
149 if (it == storage_map_.end()) | |
150 return; | |
Lei Zhang
2012/08/30 02:30:20
Should this ever happen?
kmadhusu
2012/08/30 03:12:58
Very unlikely. Just to be on the safer side, I had
| |
151 system_monitor->ProcessRemovableStorageDetached(it->second); | |
152 storage_map_.erase(it); | |
153 } | |
154 } | |
155 | |
156 void MediaTransferProtocolDeviceObserver::EnumerateStorages() { | |
157 typedef std::vector<std::string> StorageList; | |
158 StorageList storages = GetMediaTransferProtocolManager()->GetStorages(); | |
159 for (StorageList::const_iterator storage_iter = storages.begin(); | |
160 storage_iter != storages.end(); ++storage_iter) { | |
161 StorageChanged(true, *storage_iter); | |
162 } | |
163 } | |
164 | |
165 } // namespace mtp | |
166 } // namespace chromeos | |
OLD | NEW |