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

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: Addressed review comments. 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..54b917bf2f672c0d1c05283ead4d60035acba24f 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"
@@ -19,13 +19,25 @@
namespace chromeos {
namespace mtp {
+using base::SystemMonitor;
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 +110,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 +120,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,23 +137,62 @@ MediaTransferProtocolDeviceObserverCros::
: get_storage_info_func_(get_storage_info_func) {
// In unit tests, we don't have a media transfer protocol manager.
DCHECK(!MediaTransferProtocolManager::GetInstance());
+
vandebo (ex-Chrome) 2012/09/16 23:03:19 nit: extra line
kmadhusu 2012/09/17 21:47:55 Done.
+ DCHECK(!g_mtp_device_observer);
+ g_mtp_device_observer = this;
}
MediaTransferProtocolDeviceObserverCros::
~MediaTransferProtocolDeviceObserverCros() {
+ if (g_mtp_device_observer) {
vandebo (ex-Chrome) 2012/09/16 23:03:19 No if - this is set in the constructor.
kmadhusu 2012/09/17 21:47:55 Done.
+ 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;
+}
+
+bool MediaTransferProtocolDeviceObserverCros::GetStorageInfoForPath(
+ const FilePath& path,
+ SystemMonitor::RemovableStorageInfo* storage_info) const {
+ if (!path.IsAbsolute())
+ return false;
+
+ std::vector<FilePath::StringType> path_components;
+ path.GetComponents(&path_components);
+ if (path_components.size() < 2)
+ return false;
+
+ // First and second component of the path specifies the device location.
+ // E.g.: If |path| is "/usb:2,2:65537/DCIM/Folder_a", "/usb:2,2:65537" is the
+ // device location.
+ StorageLocationToInfoMap::const_iterator info_it =
+ storage_map_.find(path_components[0] + path_components[1]);
+ if (info_it == storage_map_.end())
+ return false;
+
+ if (storage_info)
+ *storage_info = info_it->second;
+ return true;
+}
+
// MediaTransferProtocolManager::Observer override.
void MediaTransferProtocolDeviceObserverCros::StorageChanged(
bool is_attached,
const std::string& storage_name) {
DCHECK(!storage_name.empty());
- base::SystemMonitor* system_monitor = base::SystemMonitor::Get();
+ SystemMonitor* system_monitor = SystemMonitor::Get();
DCHECK(system_monitor);
// New storage is attached.
@@ -160,16 +212,20 @@ 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));
+
+ SystemMonitor::RemovableStorageInfo storage_info(device_id, device_name,
+ location);
+ 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);
+ 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.device_id);
storage_map_.erase(it);
}
}

Powered by Google App Engine
This is Rietveld 408576698