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

Unified Diff: chrome/browser/system_monitor/media_transfer_protocol_device_observer_chromeos.cc

Issue 10918259: [Chrome OS]Implement MediaStorageUtil::GetDeviceInfoForPath function to support media gallery permi… (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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/system_monitor/media_transfer_protocol_device_observer_chromeos.cc
diff --git a/chrome/browser/system_monitor/media_transfer_protocol_device_observer_chromeos.cc b/chrome/browser/system_monitor/media_transfer_protocol_device_observer_chromeos.cc
index a9d6dfeffa3ef3c1e6d12dde620760e87716dca8..69d5863ea97738232c7980f066984c989d215a1a 100644
--- a/chrome/browser/system_monitor/media_transfer_protocol_device_observer_chromeos.cc
+++ b/chrome/browser/system_monitor/media_transfer_protocol_device_observer_chromeos.cc
@@ -4,12 +4,12 @@
#include "chrome/browser/system_monitor/media_transfer_protocol_device_observer_chromeos.h"
+#include "base/file_path.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/system_monitor/removable_device_constants.h"
@@ -23,9 +23,20 @@ using chrome::MediaStorageUtil;
namespace {
+static MediaTransferProtocolDeviceObserverCros* g_mtp_device_observer = NULL;
+
// Device root path constant.
const char kRootPath[] = "/";
+// Constructs and returns the location of the device using the |storage_name|.
+std::string GetDeviceLocationFromStorageName(const std::string& storage_name) {
+ // 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
+ DCHECK(!storage_name.empty());
+ return kRootPath + storage_name;
+}
+
// Returns the storage identifier of the device from the given |storage_name|.
// E.g. If the |storage_name| is "usb:2,2:65537", the storage identifier is
// "65537".
@@ -98,11 +109,9 @@ void GetStorageInfo(const std::string& 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 = kRootPath + storage_name;
+ *location = GetDeviceLocationFromStorageName(storage_name);
}
} // namespace
@@ -110,6 +119,9 @@ void GetStorageInfo(const std::string& storage_name,
MediaTransferProtocolDeviceObserverCros::
MediaTransferProtocolDeviceObserverCros()
: get_storage_info_func_(&GetStorageInfo) {
+ DCHECK(!g_mtp_device_observer);
+ g_mtp_device_observer = this;
+
MediaTransferProtocolManager* mtp_manager =
MediaTransferProtocolManager::GetInstance();
if (mtp_manager)
@@ -124,16 +136,61 @@ MediaTransferProtocolDeviceObserverCros::
: get_storage_info_func_(get_storage_info_func) {
// In unit tests, we don't have a media transfer protocol manager.
DCHECK(!MediaTransferProtocolManager::GetInstance());
+
+ // In unit tests, we don't call
+ // MediaTransferProtocolDeviceObserverCros::GetInstance(). so it is safe to
+ // keep this NULL;
+ DCHECK(!g_mtp_device_observer);
Lei Zhang 2012/09/16 00:17:05 this is weird, why not just set |g_mtp_device_obse
kmadhusu 2012/09/16 22:35:42 Done.
}
MediaTransferProtocolDeviceObserverCros::
~MediaTransferProtocolDeviceObserverCros() {
+ if (g_mtp_device_observer) {
+ DCHECK_EQ(this, g_mtp_device_observer);
+ g_mtp_device_observer = NULL;
+ }
+
MediaTransferProtocolManager* mtp_manager =
MediaTransferProtocolManager::GetInstance();
if (mtp_manager)
mtp_manager->RemoveObserver(this);
}
+// static
+MediaTransferProtocolDeviceObserverCros*
+MediaTransferProtocolDeviceObserverCros::GetInstance() {
+ DCHECK(g_mtp_device_observer != NULL);
+ return g_mtp_device_observer;
+}
+
+MediaTransferProtocolDeviceObserverCros::StorageInfo::StorageInfo() {
+}
+
+bool MediaTransferProtocolDeviceObserverCros::GetStorageInfoForPath(
+ const FilePath& path,
+ base::SystemMonitor::RemovableStorageInfo* storage_info) const {
+ if (!path.IsAbsolute())
+ return false;
+
+ FilePath current = path;
+ while (!ContainsKey(storage_map_, current.value()) &&
vandebo (ex-Chrome) 2012/09/16 00:31:44 Why do this? Isn't the path for a mtp device a fi
kmadhusu 2012/09/16 22:35:42 Fixed. The first component of the path is always t
+ current != current.DirName()) {
+ current = current.DirName();
+ }
+
+ StorageLocationToInfoMap::const_iterator info_it =
+ storage_map_.find(current.value());
+ if (info_it == storage_map_.end())
+ return false;
+
+ if (storage_info) {
+ storage_info->device_id = info_it->second.id;
+ storage_info->name = info_it->second.name;
+ storage_info->location = info_it->second.location;
+ }
+ return true;
+}
+
// MediaTransferProtocolManager::Observer override.
void MediaTransferProtocolDeviceObserverCros::StorageChanged(
bool is_attached,
@@ -160,16 +217,23 @@ void MediaTransferProtocolDeviceObserverCros::StorageChanged(
if (device_id.empty() || device_name.empty())
return;
- DCHECK(!ContainsKey(storage_map_, storage_name));
- storage_map_[storage_name] = device_id;
+ DCHECK(!ContainsKey(storage_map_, location));
+
+ StorageInfo storage_info;
+ storage_info.id = device_id;
+ storage_info.location = location;
+ storage_info.name = device_name;
+ storage_map_[location] = storage_info;
system_monitor->ProcessRemovableStorageAttached(device_id, device_name,
location);
} else {
// Existing storage is detached.
- StorageNameToIdMap::iterator it = storage_map_.find(storage_name);
+ std::string location;
+ StorageLocationToInfoMap::iterator it =
+ storage_map_.find(GetDeviceLocationFromStorageName(storage_name));
if (it == storage_map_.end())
return;
- system_monitor->ProcessRemovableStorageDetached(it->second);
+ system_monitor->ProcessRemovableStorageDetached(it->second.id);
storage_map_.erase(it);
}
}

Powered by Google App Engine
This is Rietveld 408576698