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

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: Fix nit 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 an instance of MediaTransferProtocolManager.
29 MediaTransferProtocolManager* GetMediaTransferProtocolManager() {
30 MediaTransferProtocolManager* mtp_dev_mgr =
31 MediaTransferProtocolManager::GetInstance();
tbarzic 2012/09/11 22:03:05 I don't see the point of this function.. why not j
kmadhusu 2012/09/11 23:30:53 Removed the helper function and called GetInstance
32 return mtp_dev_mgr;
33 }
34
35 // Helper function to get device id from storage name.
36 std::string GetDeviceIdFromStorageName(const std::string& storage_name) {
37 std::string unique_id(chrome::kFSUniqueIdPrefix + storage_name);
38 return MediaStorageUtil::MakeDeviceId(MediaStorageUtil::MTP_OR_PTP,
39 unique_id);
40 }
41
42 // Helper function to get device label from storage information.
43 string16 GetDeviceLabelFromStorageInfo(const MtpStorageInfo& storage_info) {
44 std::string device_label;
45 const std::string& vendor_name = storage_info.vendor();
46 device_label = vendor_name;
47
48 const std::string& product_name = storage_info.product();
49 if (!product_name.empty()) {
50 if (!device_label.empty())
51 device_label += chrome::kSpaceDelim;
52 device_label += product_name;
53 }
54 return UTF8ToUTF16(device_label);
55 }
56
57 // Helper function to get the device storage details such as device id, label
58 // and location. On success and fills in |id|, |label| and |location|.
59 void GetStorageInfo(const std::string& storage_name,
60 std::string* id,
61 string16* label,
62 std::string* location) {
63 DCHECK(!storage_name.empty());
64 MediaTransferProtocolManager* mtp_dev_mgr = GetMediaTransferProtocolManager();
tbarzic 2012/09/11 22:03:05 it's not obvious what |mtp_dev_mgr| means (especia
kmadhusu 2012/09/11 23:30:53 Done.
65 DCHECK(mtp_dev_mgr);
66 const MtpStorageInfo* storage_info =
67 mtp_dev_mgr->GetStorageInfo(storage_name);
68
69 if (!storage_info)
70 return;
71
72 if (id)
73 *id = GetDeviceIdFromStorageName(storage_name);
74
75 if (label)
76 *label = GetDeviceLabelFromStorageInfo(*storage_info);
77
78 // Construct a dummy device path using the storage name. This is only used
79 // for registering device media file system.
80 // E.g.: /usb:2,2:12345
81 if (location)
82 *location = kRootPath + storage_name;
83 }
84
85 } // namespace
86
87 MediaTransferProtocolDeviceObserverCros::
88 MediaTransferProtocolDeviceObserverCros()
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 // This constructor is only used by unit tests.
97 MediaTransferProtocolDeviceObserverCros::
98 MediaTransferProtocolDeviceObserverCros(
99 GetStorageInfoFunc get_storage_info_func)
100 : get_storage_info_func_(get_storage_info_func) {
101 // In unit tests, we don't have a media transfer protocol manager.
102 DCHECK(!GetMediaTransferProtocolManager());
103 }
104
105 MediaTransferProtocolDeviceObserverCros::
106 ~MediaTransferProtocolDeviceObserverCros() {
107 MediaTransferProtocolManager* mtp_dev_mgr = GetMediaTransferProtocolManager();
108 if (mtp_dev_mgr)
109 mtp_dev_mgr->RemoveObserver(this);
110 }
111
112 // MediaTransferProtocolManager::Observer override.
113 void MediaTransferProtocolDeviceObserverCros::StorageChanged(
114 bool is_attached,
115 const std::string& storage_name) {
116 DCHECK(!storage_name.empty());
117
118 base::SystemMonitor* system_monitor = base::SystemMonitor::Get();
119 DCHECK(system_monitor);
120
121 // New storage is attached.
122 if (is_attached) {
123 std::string device_id;
124 string16 device_name;
125 std::string location;
126 get_storage_info_func_(storage_name, &device_id, &device_name, &location);
127
128 // Keep track of device id and device name to see how often we receive
129 // empty values.
130 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.MTPDeviceUUIDAvailable",
131 !device_id.empty());
132 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.MTPDeviceNameAvailable",
133 !device_name.empty());
134
135 if (device_id.empty() || device_name.empty())
136 return;
137
138 DCHECK(!ContainsKey(storage_map_, storage_name));
139 storage_map_[storage_name] = device_id;
140 system_monitor->ProcessRemovableStorageAttached(device_id, device_name,
141 location);
142 } else {
143 // Existing storage is detached.
144 StorageNameAndIdMap::iterator it = storage_map_.find(storage_name);
145 if (it == storage_map_.end())
146 return;
147 system_monitor->ProcessRemovableStorageDetached(it->second);
148 storage_map_.erase(it);
149 }
150 }
151
152 void MediaTransferProtocolDeviceObserverCros::EnumerateStorages() {
153 typedef std::vector<std::string> StorageList;
154 MediaTransferProtocolManager* mtp_dev_mgr = GetMediaTransferProtocolManager();
155 DCHECK(mtp_dev_mgr);
156 StorageList storages = mtp_dev_mgr->GetStorages();
157 for (StorageList::const_iterator storage_iter = storages.begin();
158 storage_iter != storages.end(); ++storage_iter) {
159 StorageChanged(true, *storage_iter);
160 }
161 }
162
163 } // namespace mtp
164 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698