diff options
author | thestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-19 03:22:08 +0000 |
---|---|---|
committer | thestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-19 03:22:08 +0000 |
commit | a54c1c8ca724a74b0ba4d2f8ee20a8097be2201b (patch) | |
tree | 9825be5e95812a3ed44f6f34afc6c66f22929f34 | |
parent | f448770b1ad46c70b231e6b4b7944c27d5990220 (diff) | |
download | chromium_src-a54c1c8ca724a74b0ba4d2f8ee20a8097be2201b.zip chromium_src-a54c1c8ca724a74b0ba4d2f8ee20a8097be2201b.tar.gz chromium_src-a54c1c8ca724a74b0ba4d2f8ee20a8097be2201b.tar.bz2 |
Add a transient id to media gallery objects.
BUG=155779
Review URL: https://chromiumcodereview.appspot.com/11138031
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@162903 0039d316-1c4b-4281-b951-d872f2087c98
3 files changed, 50 insertions, 44 deletions
diff --git a/chrome/browser/extensions/api/media_galleries_private/media_galleries_private_event_router.cc b/chrome/browser/extensions/api/media_galleries_private/media_galleries_private_event_router.cc index bdd2032..ab13deb 100644 --- a/chrome/browser/extensions/api/media_galleries_private/media_galleries_private_event_router.cc +++ b/chrome/browser/extensions/api/media_galleries_private/media_galleries_private_event_router.cc @@ -29,43 +29,33 @@ class TransientDeviceIds { public: static TransientDeviceIds* GetInstance(); - // Returns the transient for a given |unique_id|. + // Returns the transient for a given |device_id|. // Returns an empty string on error. - std::string GetTransientIdForUniqueId(const std::string& unique_id) const { - UniqueIdToTransientIdMap::const_iterator it = id_map_.find(unique_id); - return (it == id_map_.end()) ? std::string() : - base::Uint64ToString(it->second); + std::string GetTransientIdForDeviceId(const std::string& device_id) const { + DeviceIdToTransientIdMap::const_iterator it = id_map_.find(device_id); + CHECK(it != id_map_.end()); + return base::Uint64ToString(it->second); } - bool DeviceAttached(const std::string& unique_id) { + void DeviceAttached(const std::string& device_id) { bool inserted = - id_map_.insert(std::make_pair(unique_id, transient_id_)).second; - if (!inserted) { - NOTREACHED(); - return false; + id_map_.insert(std::make_pair(device_id, transient_id_)).second; + if (inserted) { + // Inserted a device that has never been seen before. + ++transient_id_; } - ++transient_id_; - return true; - } - - bool DeviceDetached(const std::string& unique_id) { - if (id_map_.erase(unique_id) == 0) { - NOTREACHED(); - return false; - } - return true; } private: friend struct base::DefaultLazyInstanceTraits<TransientDeviceIds>; - typedef std::map<std::string, uint64_t> UniqueIdToTransientIdMap; + typedef std::map<std::string, uint64_t> DeviceIdToTransientIdMap; // Use GetInstance(). TransientDeviceIds() : transient_id_(0) {} ~TransientDeviceIds() {} - UniqueIdToTransientIdMap id_map_; + DeviceIdToTransientIdMap id_map_; uint64_t transient_id_; DISALLOW_COPY_AND_ASSIGN(TransientDeviceIds); @@ -87,6 +77,8 @@ using extensions::api::media_galleries_private::DeviceDetachmentDetails; MediaGalleriesPrivateEventRouter::MediaGalleriesPrivateEventRouter( Profile* profile) : profile_(profile) { + CHECK(profile_); + base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); if (system_monitor) { system_monitor->AddDevicesChangedObserver(this); @@ -107,24 +99,26 @@ MediaGalleriesPrivateEventRouter::~MediaGalleriesPrivateEventRouter() { system_monitor->RemoveDevicesChangedObserver(this); } +// static +std::string MediaGalleriesPrivateEventRouter::GetTransientIdForDeviceId( + const std::string& device_id) { + return TransientDeviceIds::GetInstance()->GetTransientIdForDeviceId( + device_id); +} + void MediaGalleriesPrivateEventRouter::OnRemovableStorageAttached( const std::string& id, const string16& name, const FilePath::StringType& location) { - TransientDeviceIds* device_ids = TransientDeviceIds::GetInstance(); - if (!device_ids->DeviceAttached(id)) - return; + TransientDeviceIds::GetInstance()->DeviceAttached(id); EventRouter* router = profile_->GetExtensionEventRouter(); if (!router->HasEventListener(kOnAttachEventName)) return; - std::string transient_id = device_ids->GetTransientIdForUniqueId(id); - CHECK(!transient_id.empty()); - DeviceAttachmentDetails details; details.device_name = UTF16ToUTF8(name); - details.device_id = transient_id; + details.device_id = GetTransientIdForDeviceId(id); scoped_ptr<base::ListValue> args(new base::ListValue()); args->Append(details.ToValue().release()); @@ -133,20 +127,12 @@ void MediaGalleriesPrivateEventRouter::OnRemovableStorageAttached( void MediaGalleriesPrivateEventRouter::OnRemovableStorageDetached( const std::string& id) { - TransientDeviceIds* device_ids = TransientDeviceIds::GetInstance(); - std::string transient_id = device_ids->GetTransientIdForUniqueId(id); - if (transient_id.empty()) { - NOTREACHED(); - return; - } - CHECK(device_ids->DeviceDetached(id)); - EventRouter* router = profile_->GetExtensionEventRouter(); if (!router->HasEventListener(kOnDetachEventName)) return; DeviceDetachmentDetails details; - details.device_id = transient_id; + details.device_id = GetTransientIdForDeviceId(id); scoped_ptr<base::ListValue> args(new ListValue()); args->Append(details.ToValue().release()); @@ -156,7 +142,7 @@ void MediaGalleriesPrivateEventRouter::OnRemovableStorageDetached( void MediaGalleriesPrivateEventRouter::DispatchEvent( const std::string& event_name, scoped_ptr<base::ListValue> event_args) { - EventRouter* router = profile_ ? profile_->GetExtensionEventRouter() : NULL; + EventRouter* router = profile_->GetExtensionEventRouter(); if (!router) return; router->DispatchEventToRenderers(event_name, event_args.Pass(), profile_, diff --git a/chrome/browser/extensions/api/media_galleries_private/media_galleries_private_event_router.h b/chrome/browser/extensions/api/media_galleries_private/media_galleries_private_event_router.h index 39f014b..fd5100b 100644 --- a/chrome/browser/extensions/api/media_galleries_private/media_galleries_private_event_router.h +++ b/chrome/browser/extensions/api/media_galleries_private/media_galleries_private_event_router.h @@ -27,6 +27,9 @@ class MediaGalleriesPrivateEventRouter explicit MediaGalleriesPrivateEventRouter(Profile* profile); virtual ~MediaGalleriesPrivateEventRouter(); + // Returns the transient id for a given |device_id|. + static std::string GetTransientIdForDeviceId(const std::string& device_id); + private: // base::SystemMonitor::DevicesChangedObserver implementation. virtual void OnRemovableStorageAttached( diff --git a/chrome/browser/media_gallery/media_file_system_registry.cc b/chrome/browser/media_gallery/media_file_system_registry.cc index c297a4d..99cc743 100644 --- a/chrome/browser/media_gallery/media_file_system_registry.cc +++ b/chrome/browser/media_gallery/media_file_system_registry.cc @@ -19,6 +19,7 @@ #include "base/system_monitor/system_monitor.h" #include "base/utf_string_conversions.h" #include "base/values.h" +#include "chrome/browser/extensions/api/media_galleries_private/media_galleries_private_event_router.h" #include "chrome/browser/media_gallery/media_galleries_preferences.h" #include "chrome/browser/media_gallery/media_galleries_preferences_factory.h" #include "chrome/browser/profiles/profile.h" @@ -66,10 +67,11 @@ struct InvalidatedGalleriesInfo { std::set<MediaGalleryPrefId> pref_ids; }; -// Make a JSON string out of |name| and |id|. The |id| makes the combined name -// unique. The JSON string should not contain any slashes. +// Make a JSON string out of |name|, |pref_id| and |device_id|. The IDs makes +// the combined name unique. The JSON string should not contain any slashes. std::string MakeJSONFileSystemName(const string16& name, - const MediaGalleryPrefId& id) { + const MediaGalleryPrefId& pref_id, + const std::string& device_id) { string16 sanitized_name; string16 separators = #if defined(FILE_PATH_USES_WIN_SEPARATORS) @@ -83,13 +85,26 @@ std::string MakeJSONFileSystemName(const string16& name, base::DictionaryValue dict_value; dict_value.SetWithoutPathExpansion( "name", Value::CreateStringValue(sanitized_name)); - dict_value.SetWithoutPathExpansion("id", Value::CreateIntegerValue(id)); + dict_value.SetWithoutPathExpansion("id", Value::CreateIntegerValue(pref_id)); + // |device_id| can be empty, in which case, just omit it. + if (!device_id.empty()) { + dict_value.SetWithoutPathExpansion("deviceId", + Value::CreateStringValue(device_id)); + } std::string json_string; base::JSONWriter::Write(&dict_value, &json_string); return json_string; } +std::string GetTransientIdForRemovableDeviceId(const std::string& device_id) { + using extensions::MediaGalleriesPrivateEventRouter; + + if (!MediaStorageUtil::IsRemovableDevice(device_id)) + return std::string(); + return MediaGalleriesPrivateEventRouter::GetTransientIdForDeviceId(device_id); +} + } // namespace MediaFileSystemInfo::MediaFileSystemInfo(const std::string& fs_name, @@ -333,7 +348,9 @@ class ExtensionGalleriesHost DCHECK(!fsid.empty()); MediaFileSystemInfo new_entry( - MakeJSONFileSystemName(gallery_info.display_name, pref_id), + MakeJSONFileSystemName(gallery_info.display_name, + pref_id, + GetTransientIdForRemovableDeviceId(device_id)), path, fsid); result.push_back(new_entry); |