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

Side by Side Diff: chrome/browser/system_monitor/chromeos/media_transfer_protocol_device_observer.cc

Issue 10894045: ChromeOS: Implement MediaTransferProtocolManager observer. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Moved media_transfer_protocol_device_observer to chrome/browser/system_monitor 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/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/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/system_monitor/removable_device_constants.h"
16 #include "chrome/browser/system_monitor/media_storage_util.h"
17 #include "chromeos/dbus/mtp_storage_info.pb.h"
18
19 namespace chromeos {
20 namespace mtp {
21
22 using chrome::MediaStorageUtil;
23
24 namespace {
25
26 // Device root path constant.
27 const char kRootPath[] = "/";
28
29 // Helper function to get an instance of MediaTransferProtocolManager.
30 MediaTransferProtocolManager* GetMediaTransferProtocolManager() {
31 MediaTransferProtocolManager* mtp_dev_mgr =
32 MediaTransferProtocolManager::GetInstance();
33 return mtp_dev_mgr;
34 }
35
36 // Helper function to get device id from storage information.
37 std::string GetDeviceIdFromStorageInfo(const MtpStorageInfo& storage_info,
38 const std::string& storage_name) {
39 std::string unique_id(chrome::kFSUniqueIdPrefix + storage_name);
kmadhusu 2012/09/08 03:15:14 This will help us to distinguish the storage parti
40 return MediaStorageUtil::MakeDeviceId(MediaStorageUtil::MTP_OR_PTP,
41 unique_id);
42 }
43
44 // Helper function to get device label from storage information.
45 string16 GetDeviceLabelFromStorageInfo(const MtpStorageInfo& storage_info) {
46 std::string device_label;
47 const std::string& vendor_name = storage_info.vendor();
48 device_label = vendor_name;
49
50 const std::string& product_name = storage_info.product();
51 if (!product_name.empty()) {
52 if (!device_label.empty())
53 device_label += chrome::kSpaceDelim;
54 device_label += product_name;
55 }
56 return UTF8ToUTF16(device_label);
57 }
58
59 // Helper function to get the device storage details such as device id, label
60 // and location. On success and fills in |id|, |label| and |location|.
61 void GetStorageInfo(const std::string& storage_name,
62 std::string* id,
63 string16* label,
64 std::string* location) {
65 DCHECK(!storage_name.empty());
66 MediaTransferProtocolManager* mtp_dev_mgr = GetMediaTransferProtocolManager();
67 DCHECK(mtp_dev_mgr);
68 const MtpStorageInfo* storage_info =
69 mtp_dev_mgr->GetStorageInfo(storage_name);
70
71 if (!storage_info)
72 return;
73
74 if (id)
75 *id = GetDeviceIdFromStorageInfo(*storage_info, storage_name);
76
77 if (label)
78 *label = GetDeviceLabelFromStorageInfo(*storage_info);
79
80 // Construct a dummy device path using the storage name. This is only used
81 // for registering device media file system.
82 // E.g.: /usb:2,2:12345
83 if (location)
84 *location = kRootPath + storage_name;
85 }
86
87 } // namespace
88
89 MediaTransferProtocolDeviceObserver::MediaTransferProtocolDeviceObserver()
90 : get_storage_info_func_(&GetStorageInfo) {
91 MediaTransferProtocolManager* mtp_dev_mgr = GetMediaTransferProtocolManager();
92 if (mtp_dev_mgr)
93 mtp_dev_mgr->AddObserver(this);
94 EnumerateStorages();
95 }
96
97 MediaTransferProtocolDeviceObserver::MediaTransferProtocolDeviceObserver(
98 GetStorageInfoFunc get_storage_info_func)
99 : get_storage_info_func_(get_storage_info_func) {
100 }
101
102 MediaTransferProtocolDeviceObserver::~MediaTransferProtocolDeviceObserver() {
103 MediaTransferProtocolManager* mtp_dev_mgr = GetMediaTransferProtocolManager();
104 if (mtp_dev_mgr)
105 mtp_dev_mgr->RemoveObserver(this);
106 }
107
108 // MediaTransferProtocolManager::Observer override.
109 void MediaTransferProtocolDeviceObserver::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.mtp_device_uuid_available",
127 !device_id.empty());
128 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.mtp_device_name_available",
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 StorageNameAndIdMap::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 MediaTransferProtocolDeviceObserver::EnumerateStorages() {
149 typedef std::vector<std::string> StorageList;
150 MediaTransferProtocolManager* mtp_dev_mgr = GetMediaTransferProtocolManager();
151 DCHECK(mtp_dev_mgr);
152 StorageList storages = mtp_dev_mgr->GetStorages();
153 for (StorageList::const_iterator storage_iter = storages.begin();
154 storage_iter != storages.end(); ++storage_iter) {
155 StorageChanged(true, *storage_iter);
156 }
157 }
158
159 } // namespace mtp
160 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698