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

Unified Diff: chrome/browser/chromeos/mtp/media_transfer_protocol_device_observer.cc

Issue 10894045: ChromeOS: Implement MediaTransferProtocolManager observer. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix nit Created 8 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/mtp/media_transfer_protocol_device_observer.cc
diff --git a/chrome/browser/chromeos/mtp/media_transfer_protocol_device_observer.cc b/chrome/browser/chromeos/mtp/media_transfer_protocol_device_observer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..883a7ba38fd5fcd45164901986e55502f2dc21f2
--- /dev/null
+++ b/chrome/browser/chromeos/mtp/media_transfer_protocol_device_observer.cc
@@ -0,0 +1,166 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/chromeos/mtp/media_transfer_protocol_device_observer.h"
+
+#include "base/metrics/histogram.h"
+#include "base/stl_util.h"
+#include "base/string_number_conversions.h"
+#include "base/string_split.h"
+#include "base/stringprintf.h"
+#include "base/system_monitor/system_monitor.h"
+#include "base/utf_string_conversions.h"
+#include "chrome/browser/chromeos/mtp/media_transfer_protocol_manager.h"
+#include "chrome/browser/media_gallery/media_gallery_constants.h"
+#include "chrome/browser/media_gallery/media_storage_util.h"
+
+namespace chromeos {
+namespace mtp {
+
+using chrome::MediaStorageUtil;
+
+namespace {
+
+// Device root path constant.
+const char kRootPath[] = "/";
+
+// Helper function to get an instance of MediaTransferProtocolManager.
+MediaTransferProtocolManager* GetMediaTransferProtocolManager() {
+ MediaTransferProtocolManager* mtp_dev_mgr =
+ MediaTransferProtocolManager::GetInstance();
+ DCHECK(mtp_dev_mgr);
+ return mtp_dev_mgr;
+}
+
+// Helper function to get device id from storage information.
+std::string GetDeviceIdFromStorageInfo(const StorageInfo& storage_info,
+ const std::string& storage_name) {
+ std::string unique_id;
+ const std::string vendor_id = base::UintToString(storage_info.vendor_id());
+ const std::string model_id = base::UintToString(storage_info.product_id());
+ const std::string& volume_id = storage_info.volume_identifier();
+ if (!vendor_id.empty() || !model_id.empty() || !volume_id.empty()) {
+ unique_id = base::StringPrintf("%s%s%s%s%s%s",
+ chrome::kVendorModelSerialPrefix,
+ vendor_id.c_str(),
+ chrome::kNonSpaceDelim,
+ model_id.c_str(),
+ chrome::kNonSpaceDelim,
+ volume_id.c_str());
+ } else {
+ unique_id = std::string(chrome::kFSUniqueIdPrefix) + storage_name;
+ }
+ return MediaStorageUtil::MakeDeviceId(MediaStorageUtil::MTP_OR_PTP,
+ unique_id);
+}
+
+// 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.
+string16 GetDeviceLabelFromStorageInfo(const StorageInfo& storage_info) {
+ std::string device_label;
+ const std::string& vendor_name = storage_info.vendor();
+ 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.
+ device_label = vendor_name;
+
+ const std::string& product_name = storage_info.product();
+ if (!product_name.empty()) {
+ if (!device_label.empty())
+ device_label += chrome::kSpaceDelim;
+ device_label += product_name;
+ }
+ return UTF8ToUTF16(device_label);
+}
+
+// Helper function to get the device storage details such as device id, label
+// and location. On success and fills in |id|, |label| and |location|.
+void GetStorageInfo(const std::string& storage_name,
+ std::string* id,
+ string16* label,
+ std::string* location) {
+ DCHECK(!storage_name.empty());
+ StorageInfo storage_info;
+ GetMediaTransferProtocolManager()->GetStorageInfo(storage_name,
+ &storage_info);
+
+ if (id)
+ *id = GetDeviceIdFromStorageInfo(storage_info, storage_name);
+
+ if (label)
+ *label = GetDeviceLabelFromStorageInfo(storage_info);
+
+ // Construct a dummy device path using the storage name. This is only used
+ // for registering device media file system.
+ // E.g.: /usb:2,2:12345
+ if (location)
+ *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.
+}
+
+} // namespace
+
+MediaTransferProtocolDeviceObserver::MediaTransferProtocolDeviceObserver()
+ : get_storage_info_func_(&GetStorageInfo) {
+ GetMediaTransferProtocolManager()->AddObserver(this);
+ EnumerateStorages();
+}
+
+MediaTransferProtocolDeviceObserver::MediaTransferProtocolDeviceObserver(
+ GetStorageInfoFunc get_storage_info_func)
+ : get_storage_info_func_(get_storage_info_func) {
+}
+
+MediaTransferProtocolDeviceObserver::~MediaTransferProtocolDeviceObserver() {
+ if (GetMediaTransferProtocolManager())
+ GetMediaTransferProtocolManager()->RemoveObserver(this);
+}
+
+// MediaTransferProtocolManager::Observer override.
+void MediaTransferProtocolDeviceObserver::StorageChanged(
+ bool is_attached,
+ const std::string& storage_name) {
+ DCHECK(!storage_name.empty());
+
+ base::SystemMonitor* system_monitor = base::SystemMonitor::Get();
+ DCHECK(system_monitor);
+
+ // New storage is attached.
+ if (is_attached) {
+ std::string device_id;
+ string16 device_name;
+ std::string location;
+ get_storage_info_func_(storage_name, &device_id, &device_name, &location);
+
+ // Keep track of device id and device name to see how often we receive
+ // empty values.
+ UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.mtp_device_uuid_available",
+ !device_id.empty());
+ UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.mtp_device_name_available",
+ !device_name.empty());
+
+ 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
+ return;
+
+ DCHECK(!ContainsKey(storage_map_, storage_name));
+ storage_map_[storage_name] = device_id;
+ system_monitor->ProcessRemovableStorageAttached(device_id, device_name,
+ location);
+ } else {
+ // Existing storage is detached.
+ StorageNameAndIdMap::iterator it = storage_map_.find(storage_name);
+ if (it == storage_map_.end())
+ 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
+ system_monitor->ProcessRemovableStorageDetached(it->second);
+ storage_map_.erase(it);
+ }
+}
+
+void MediaTransferProtocolDeviceObserver::EnumerateStorages() {
+ typedef std::vector<std::string> StorageList;
+ StorageList storages = GetMediaTransferProtocolManager()->GetStorages();
+ for (StorageList::const_iterator storage_iter = storages.begin();
+ storage_iter != storages.end(); ++storage_iter) {
+ StorageChanged(true, *storage_iter);
+ }
+}
+
+} // namespace mtp
+} // namespace chromeos

Powered by Google App Engine
This is Rietveld 408576698