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

Side by Side Diff: chrome/browser/chromeos/mtp/mtp_dev_manager_observer.cc

Issue 10894045: ChromeOS: Implement MediaTransferProtocolManager observer. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: '' 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/chromeos/mtp/mtp_dev_manager_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{
Lei Zhang 2012/08/29 23:11:15 nit: missing space
kmadhusu 2012/08/30 02:07:50 Done.
20
21 using base::SystemMonitor;
Lei Zhang 2012/08/29 23:11:15 nit: You don't really need these. They're only use
kmadhusu 2012/08/30 02:07:50 Done.
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* mtp_device_manager() {
Lei Zhang 2012/08/29 23:11:15 nit: foo_function() usually means "this function i
kmadhusu 2012/08/30 02:07:50 Done.
31 MediaTransferProtocolManager* mtp_dev_mgr =
32 MediaTransferProtocolManager::GetInstance();
33 DCHECK(mtp_dev_mgr);
34 return mtp_dev_mgr;
35 }
36
37 // Helper function to get the device storage details such as device id, label
38 // and location. On success and fills in |id|, |label| and |location|.
39 void GetDeviceInfo(const std::string& storage_name,
40 std::string* id,
41 string16* label,
42 std::string* location) {
43 DCHECK(!storage_name.empty());
44 StorageInfo storage_info;
45 mtp_device_manager()->GetStorageInfo(storage_name, &storage_info);
46
47 if (id) {
48 std::string unique_id;
Lei Zhang 2012/08/29 23:11:15 If you move this and the label block into their ow
kmadhusu 2012/08/30 02:07:50 Done.
49 const std::string& vendor_id = base::UintToString(storage_info.vendor_id());
Lei Zhang 2012/08/29 23:11:15 UintToString return values should not be treated a
kmadhusu 2012/08/30 02:07:50 Done.
50 const std::string& model_id = base::UintToString(storage_info.product_id());
51 const std::string& volume_id = storage_info.volume_identifier();
52 if (!vendor_id.empty() || !model_id.empty() || !volume_id.empty()) {
53 unique_id = base::StringPrintf("%s%s%s%s%s%s",
54 chrome::kVendorModelSerialPrefix,
55 vendor_id.c_str(),
56 chrome::kNonSpaceDelim,
57 model_id.c_str(),
58 chrome::kNonSpaceDelim,
59 volume_id.c_str());
60 } else {
61 unique_id = std::string(chrome::kFSUniqueIdPrefix) + storage_name;
62 }
63 *id = MediaStorageUtil::MakeDeviceId(MediaStorageUtil::MTP_OR_PTP,
64 unique_id);
65 }
66
67 if (label) {
68 std::string device_label;
69 const std::string& vendor_name = storage_info.vendor();
70 if (!vendor_name.empty())
71 device_label = vendor_name;
72
73 const std::string& product_name = storage_info.product();
74 if (!product_name.empty()) {
75 if (!device_label.empty())
76 device_label += chrome::kSpaceDelim;
77 device_label += product_name;
78 }
79 *label = UTF8ToUTF16(device_label);
80 }
81
82 // Construct a dummy device path using the storage name. This is only used
83 // for registering device media file system.
84 // E.g.: /usb:2,2:12345
85 if (location)
86 *location = std::string(kRootPath) + storage_name;
87 }
88
89 } // namespace
90
91 MtpDevManagerObserver::MtpDevManagerObserver()
92 : get_storage_info_func_(&GetDeviceInfo) {
Lei Zhang 2012/08/29 23:11:15 GetDeviceInfo -> GetStorageInfo or get_storage_inf
kmadhusu 2012/08/30 02:07:50 GetDeviceInfo => GetStorageInfo
93 mtp_device_manager()->AddObserver(this);
94 EnumerateStorages();
95 }
96
97 MtpDevManagerObserver::MtpDevManagerObserver(
98 GetStorageInfoFunc get_storage_info_func)
99 : get_storage_info_func_(get_storage_info_func) {
100 }
101
102 MtpDevManagerObserver::~MtpDevManagerObserver() {
103 if (mtp_device_manager())
104 mtp_device_manager()->RemoveObserver(this);
105 }
106
107 // MediaTransferProtocolManager::Observer override.
108 void MtpDevManagerObserver::StorageChanged(bool is_attached,
109 const std::string& storage_name) {
110 DCHECK(!storage_name.empty());
111
112 SystemMonitor* system_monitor = SystemMonitor::Get();
113 DCHECK(system_monitor);
114
115 // New storage is attached.
116 if (is_attached) {
117 std::string device_id;
118 string16 device_name;
119 std::string location;
120 get_storage_info_func_(storage_name, &device_id, &device_name, &location);
121
122 // Keep track of device id and device name to see how often we receive
123 // empty values.
124 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.mtp_device_uuid_available",
125 !device_id.empty());
126 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.mtp_device_name_available",
127 !device_name.empty());
128
129 if (device_id.empty() || device_name.empty())
130 return;
131
132 DCHECK(!ContainsKey(storage_map_, storage_name));
133 storage_map_[storage_name] = device_id;
134 system_monitor->ProcessRemovableStorageAttached(device_id, device_name,
135 location);
136 } else {
137 // Existing storage is detached.
138 StorageNameAndIdMap::iterator it = storage_map_.find(storage_name);
139 if (it == storage_map_.end())
140 return;
141 system_monitor->ProcessRemovableStorageDetached(it->second);
142 storage_map_.erase(it);
143 }
144 }
145
146 void MtpDevManagerObserver::EnumerateStorages() {
147 typedef std::vector<std::string> StorageList;
148 StorageList storages = mtp_device_manager()->GetStorages();
149 for (StorageList::const_iterator storage_iter = storages.begin();
150 storage_iter != storages.end(); ++storage_iter) {
151 StorageChanged(true, *storage_iter);
152 }
153 }
154
155 } // namespace mtp
156 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698