Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: chrome/browser/system_monitor/media_transfer_protocol_device_observer_chromeos.cc

Issue 10894045: ChromeOS: Implement MediaTransferProtocolManager observer. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/media_transfer_protocol_device_observer_ chromeos.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 device id from storage name.
29 std::string GetDeviceIdFromStorageName(const std::string& storage_name) {
30 std::string unique_id(chrome::kFSUniqueIdPrefix + storage_name);
31 return MediaStorageUtil::MakeDeviceId(MediaStorageUtil::MTP_OR_PTP,
32 unique_id);
33 }
34
35 // Helper function to get device label from storage information.
36 string16 GetDeviceLabelFromStorageInfo(const MtpStorageInfo& storage_info) {
37 std::string device_label;
38 const std::string& vendor_name = storage_info.vendor();
39 device_label = vendor_name;
40
41 const std::string& product_name = storage_info.product();
42 if (!product_name.empty()) {
43 if (!device_label.empty())
44 device_label += chrome::kSpaceDelim;
45 device_label += product_name;
46 }
47 return UTF8ToUTF16(device_label);
48 }
49
50 // Helper function to get the device storage details such as device id, label
51 // and location. On success and fills in |id|, |label| and |location|.
52 void GetStorageInfo(const std::string& storage_name,
53 std::string* id,
54 string16* label,
55 std::string* location) {
56 DCHECK(!storage_name.empty());
57 MediaTransferProtocolManager* mtp_manager =
58 MediaTransferProtocolManager::GetInstance();
59 DCHECK(mtp_manager);
60 const MtpStorageInfo* storage_info =
61 mtp_manager->GetStorageInfo(storage_name);
62
63 if (!storage_info)
64 return;
65
66 if (id)
67 *id = GetDeviceIdFromStorageName(storage_name);
68
69 if (label)
70 *label = GetDeviceLabelFromStorageInfo(*storage_info);
71
72 // Construct a dummy device path using the storage name. This is only used
73 // for registering device media file system.
74 // E.g.: /usb:2,2:12345
75 if (location)
76 *location = kRootPath + storage_name;
77 }
78
79 } // namespace
80
81 MediaTransferProtocolDeviceObserverCros::
82 MediaTransferProtocolDeviceObserverCros()
83 : get_storage_info_func_(&GetStorageInfo) {
84 MediaTransferProtocolManager* mtp_manager =
85 MediaTransferProtocolManager::GetInstance();
86 if (mtp_manager)
87 mtp_manager->AddObserver(this);
88 EnumerateStorages();
89 }
90
91 // This constructor is only used by unit tests.
92 MediaTransferProtocolDeviceObserverCros::
93 MediaTransferProtocolDeviceObserverCros(
94 GetStorageInfoFunc get_storage_info_func)
95 : get_storage_info_func_(get_storage_info_func) {
96 // In unit tests, we don't have a media transfer protocol manager.
97 DCHECK(!MediaTransferProtocolManager::GetInstance());
98 }
99
100 MediaTransferProtocolDeviceObserverCros::
101 ~MediaTransferProtocolDeviceObserverCros() {
102 MediaTransferProtocolManager* mtp_manager =
103 MediaTransferProtocolManager::GetInstance();
104 if (mtp_manager)
105 mtp_manager->RemoveObserver(this);
106 }
107
108 // MediaTransferProtocolManager::Observer override.
109 void MediaTransferProtocolDeviceObserverCros::StorageChanged(
110 bool is_attached,
111 const std::string& storage_name) {
112 DCHECK(!storage_name.empty());
113
114 base::SystemMonitor* system_monitor = base::SystemMonitor::Get();
115 DCHECK(system_monitor);
116
117 // New storage is attached.
118 if (is_attached) {
119 std::string device_id;
120 string16 device_name;
121 std::string location;
122 get_storage_info_func_(storage_name, &device_id, &device_name, &location);
123
124 // Keep track of device id and device name to see how often we receive
125 // empty values.
126 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.MTPDeviceUUIDAvailable",
127 !device_id.empty());
128 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.MTPDeviceNameAvailable",
129 !device_name.empty());
130
131 if (device_id.empty() || device_name.empty())
132 return;
133
134 DCHECK(!ContainsKey(storage_map_, storage_name));
135 storage_map_[storage_name] = device_id;
136 system_monitor->ProcessRemovableStorageAttached(device_id, device_name,
137 location);
138 } else {
139 // Existing storage is detached.
140 StorageNameToIdMap::iterator it = storage_map_.find(storage_name);
141 if (it == storage_map_.end())
142 return;
143 system_monitor->ProcessRemovableStorageDetached(it->second);
144 storage_map_.erase(it);
145 }
146 }
147
148 void MediaTransferProtocolDeviceObserverCros::EnumerateStorages() {
149 typedef std::vector<std::string> StorageList;
150 MediaTransferProtocolManager* mtp_manager =
151 MediaTransferProtocolManager::GetInstance();
152 DCHECK(mtp_manager);
153 StorageList storages = mtp_manager->GetStorages();
154 for (StorageList::const_iterator storage_iter = storages.begin();
155 storage_iter != storages.end(); ++storage_iter) {
156 StorageChanged(true, *storage_iter);
157 }
158 }
159
160 } // namespace mtp
161 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698