diff options
author | thestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-31 11:58:10 +0000 |
---|---|---|
committer | thestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-31 11:58:10 +0000 |
commit | 023740a847c35a747af9ff30cb7d389cc5e9a690 (patch) | |
tree | 77c4f42e2658e15f8588ad5771b3fdda36acbd8b | |
parent | 66d87d76aed1cbb77da82309f2d70dd20f4b9937 (diff) | |
download | chromium_src-023740a847c35a747af9ff30cb7d389cc5e9a690.zip chromium_src-023740a847c35a747af9ff30cb7d389cc5e9a690.tar.gz chromium_src-023740a847c35a747af9ff30cb7d389cc5e9a690.tar.bz2 |
Media Galleries: Add MediaGalleriesPreferences::GalleryChangeObserver class.
This lets interested parties such as media gallery permission dialogs get
notifications for preferences changes.
BUG=158849
Review URL: https://chromiumcodereview.appspot.com/12114019
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@179871 0039d316-1c4b-4281-b951-d872f2087c98
3 files changed, 150 insertions, 29 deletions
diff --git a/chrome/browser/media_gallery/media_galleries_preferences.cc b/chrome/browser/media_gallery/media_galleries_preferences.cc index abc08f9..6839bd9 100644 --- a/chrome/browser/media_gallery/media_galleries_preferences.cc +++ b/chrome/browser/media_gallery/media_galleries_preferences.cc @@ -136,10 +136,12 @@ FilePath MediaGalleryPrefInfo::AbsolutePath() const { return base_path.Append(path); } +MediaGalleriesPreferences::GalleryChangeObserver::~GalleryChangeObserver() {} + MediaGalleriesPreferences::MediaGalleriesPreferences(Profile* profile) : profile_(profile) { AddDefaultGalleriesIfFreshProfile(); - InitFromPrefs(); + InitFromPrefs(false /*no notification*/); } MediaGalleriesPreferences::~MediaGalleriesPreferences() {} @@ -171,28 +173,46 @@ void MediaGalleriesPreferences::AddDefaultGalleriesIfFreshProfile() { } } -void MediaGalleriesPreferences::InitFromPrefs() { +void MediaGalleriesPreferences::InitFromPrefs(bool notify_observers) { known_galleries_.clear(); device_map_.clear(); PrefService* prefs = profile_->GetPrefs(); const ListValue* list = prefs->GetList( prefs::kMediaGalleriesRememberedGalleries); - if (!list) - return; + if (list) { + for (ListValue::const_iterator it = list->begin(); + it != list->end(); ++it) { + const DictionaryValue* dict = NULL; + if (!(*it)->GetAsDictionary(&dict)) + continue; + + MediaGalleryPrefInfo gallery_info; + if (!PopulateGalleryPrefInfoFromDictionary(*dict, &gallery_info)) + continue; + + known_galleries_[gallery_info.pref_id] = gallery_info; + device_map_[gallery_info.device_id].insert(gallery_info.pref_id); + } + } + if (notify_observers) + NotifyChangeObservers(); +} - for (ListValue::const_iterator it = list->begin(); it != list->end(); ++it) { - const DictionaryValue* dict = NULL; - if (!(*it)->GetAsDictionary(&dict)) - continue; +void MediaGalleriesPreferences::NotifyChangeObservers() { + FOR_EACH_OBSERVER(GalleryChangeObserver, + gallery_change_observers_, + OnGalleryChanged(this)); +} - MediaGalleryPrefInfo gallery_info; - if (!PopulateGalleryPrefInfoFromDictionary(*dict, &gallery_info)) - continue; +void MediaGalleriesPreferences::AddGalleryChangeObserver( + GalleryChangeObserver* observer) { + gallery_change_observers_.AddObserver(observer); +} - known_galleries_[gallery_info.pref_id] = gallery_info; - device_map_[gallery_info.device_id].insert(gallery_info.pref_id); - } +void MediaGalleriesPreferences::RemoveGalleryChangeObserver( + GalleryChangeObserver* observer) { + gallery_change_observers_.RemoveObserver(observer); } bool MediaGalleriesPreferences::LookUpGalleryByPath( @@ -281,7 +301,7 @@ MediaGalleryPrefId MediaGalleriesPreferences::AddGallery( } if (update_gallery_name) dict->SetString(kMediaGalleriesDisplayNameKey, display_name); - InitFromPrefs(); + InitFromPrefs(true /* notify observers */); break; } } @@ -304,7 +324,7 @@ MediaGalleryPrefId MediaGalleriesPreferences::AddGallery( ListPrefUpdate update(prefs, prefs::kMediaGalleriesRememberedGalleries); ListValue* list = update.Get(); list->Append(CreateGalleryPrefInfoDictionary(gallery_info)); - InitFromPrefs(); + InitFromPrefs(true /* notify observers */); return gallery_info.pref_id; } @@ -342,7 +362,7 @@ void MediaGalleriesPreferences::ForgetGalleryById(MediaGalleryPrefId pref_id) { } else { list->Erase(iter, NULL); } - InitFromPrefs(); + InitFromPrefs(true /* notify observers */); return; } } diff --git a/chrome/browser/media_gallery/media_galleries_preferences.h b/chrome/browser/media_gallery/media_galleries_preferences.h index 75c01f7..b712eba 100644 --- a/chrome/browser/media_gallery/media_galleries_preferences.h +++ b/chrome/browser/media_gallery/media_galleries_preferences.h @@ -11,6 +11,7 @@ #include "base/basictypes.h" #include "base/file_path.h" +#include "base/observer_list.h" #include "base/string16.h" #include "chrome/browser/profiles/profile_keyed_service.h" @@ -75,9 +76,20 @@ typedef std::set<MediaGalleryPrefId> MediaGalleryPrefIdSet; // user profile. class MediaGalleriesPreferences : public ProfileKeyedService { public: + class GalleryChangeObserver { + public: + virtual void OnGalleryChanged(MediaGalleriesPreferences* pref) {} + + protected: + virtual ~GalleryChangeObserver(); + }; + explicit MediaGalleriesPreferences(Profile* profile); virtual ~MediaGalleriesPreferences(); + void AddGalleryChangeObserver(GalleryChangeObserver* observer); + void RemoveGalleryChangeObserver(GalleryChangeObserver* observer); + // Lookup a media gallery and fill in information about it and return true. // If the media gallery does not already exist, fill in as much of the // MediaGalleryPrefInfo struct as we can and return false. @@ -90,12 +102,14 @@ class MediaGalleriesPreferences : public ProfileKeyedService { const std::string& device_id) const; // Teaches the registry about a new gallery. + // Returns the gallery's pref id. MediaGalleryPrefId AddGallery(const std::string& device_id, const string16& display_name, const FilePath& relative_path, bool user_added); // Teach the registry about a user added registry simply from the path. + // Returns the gallery's pref id. MediaGalleryPrefId AddGalleryByPath(const FilePath& path); // Removes the gallery identified by |id| from the store. @@ -130,7 +144,13 @@ class MediaGalleriesPreferences : public ProfileKeyedService { void AddDefaultGalleriesIfFreshProfile(); // Builds |known_galleries_| from the persistent store. - void InitFromPrefs(); + // Notifies GalleryChangeObservers if |notify_observers| is true. + void InitFromPrefs(bool notify_observers); + + // Notifies |gallery_change_observers_| about changes in |known_galleries_|. + void NotifyChangeObservers(); + + extensions::ExtensionPrefs* GetExtensionPrefs() const; // The profile that owns |this|. Profile* profile_; @@ -142,7 +162,7 @@ class MediaGalleriesPreferences : public ProfileKeyedService { // All pref ids in |device_map_| are also in |known_galleries_|. DeviceIdPrefIdsMap device_map_; - extensions::ExtensionPrefs* GetExtensionPrefs() const; + ObserverList<GalleryChangeObserver> gallery_change_observers_; DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPreferences); }; diff --git a/chrome/browser/media_gallery/media_galleries_preferences_unittest.cc b/chrome/browser/media_gallery/media_galleries_preferences_unittest.cc index 66e56b5..2ceceeb 100644 --- a/chrome/browser/media_gallery/media_galleries_preferences_unittest.cc +++ b/chrome/browser/media_gallery/media_galleries_preferences_unittest.cc @@ -49,6 +49,29 @@ class MediaStorageUtilTest : public MediaStorageUtil { } }; +class MockGalleryChangeObserver + : public MediaGalleriesPreferences::GalleryChangeObserver { + public: + explicit MockGalleryChangeObserver(MediaGalleriesPreferences* pref) + : pref_(pref), + notifications_(0) {} + virtual ~MockGalleryChangeObserver() {} + + int notifications() const { return notifications_;} + + private: + // MediaGalleriesPreferences::GalleryChangeObserver implementation. + virtual void OnGalleryChanged(MediaGalleriesPreferences* pref) OVERRIDE { + EXPECT_EQ(pref_, pref); + ++notifications_; + } + + MediaGalleriesPreferences* pref_; + int notifications_; + + DISALLOW_COPY_AND_ASSIGN(MockGalleryChangeObserver); +}; + class MediaGalleriesPreferencesTest : public testing::Test { public: typedef std::map<std::string /*device id*/, MediaGalleryPrefIdSet> @@ -117,7 +140,7 @@ class MediaGalleriesPreferencesTest : public testing::Test { for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin(); it != known_galleries.end(); ++it) { - VerifyGalleryInfo(&it->second, it->first); + VerifyGalleryInfo(it->second, it->first); } for (DeviceIdPrefIdsMap::const_iterator it = expected_device_map.begin(); @@ -142,16 +165,16 @@ class MediaGalleriesPreferencesTest : public testing::Test { EXPECT_EQ(0U, galleries_for_no.size()); } - void VerifyGalleryInfo(const MediaGalleryPrefInfo* actual, + void VerifyGalleryInfo(const MediaGalleryPrefInfo& actual, MediaGalleryPrefId expected_id) const { MediaGalleriesPrefInfoMap::const_iterator in_expectation = expected_galleries_.find(expected_id); - EXPECT_FALSE(in_expectation == expected_galleries_.end()); - EXPECT_EQ(in_expectation->second.pref_id, actual->pref_id); - EXPECT_EQ(in_expectation->second.display_name, actual->display_name); - EXPECT_EQ(in_expectation->second.device_id, actual->device_id); - EXPECT_EQ(in_expectation->second.path.value(), actual->path.value()); - EXPECT_EQ(in_expectation->second.type, actual->type); + ASSERT_FALSE(in_expectation == expected_galleries_.end()) << expected_id; + EXPECT_EQ(in_expectation->second.pref_id, actual.pref_id); + EXPECT_EQ(in_expectation->second.display_name, actual.display_name); + EXPECT_EQ(in_expectation->second.device_id, actual.device_id); + EXPECT_EQ(in_expectation->second.path.value(), actual.path.value()); + EXPECT_EQ(in_expectation->second.type, actual.type); } MediaGalleriesPreferences* gallery_prefs() { @@ -261,10 +284,10 @@ TEST_F(MediaGalleriesPreferencesTest, GalleryManagement) { MediaGalleryPrefInfo gallery_info; EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_auto"), &gallery_info)); - VerifyGalleryInfo(&gallery_info, auto_id); + VerifyGalleryInfo(gallery_info, auto_id); EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_user"), &gallery_info)); - VerifyGalleryInfo(&gallery_info, user_added_id); + VerifyGalleryInfo(gallery_info, user_added_id); path = MakePath("other"); EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(path, &gallery_info)); @@ -559,6 +582,64 @@ TEST_F(MediaGalleriesPreferencesTest, MultipleGalleriesPerDevices) { Verify(); } +TEST_F(MediaGalleriesPreferencesTest, GalleryChangeObserver) { + // Start with one observer. + MockGalleryChangeObserver observer1(gallery_prefs()); + gallery_prefs()->AddGalleryChangeObserver(&observer1); + + // Add a new auto detected gallery. + string16 device_name = ASCIIToUTF16("NewAutoGallery"); + FilePath path = MakePath("new_auto"); + std::string device_id; + FilePath relative_path; + MediaStorageUtil::GetDeviceInfoFromPath(path, &device_id, NULL, + &relative_path); + MediaGalleryPrefId auto_id = + gallery_prefs()->AddGallery(device_id, device_name, relative_path, + false /*auto*/); + EXPECT_EQ(default_galleries_count() + 1UL, auto_id); + AddGalleryExpectation(auto_id, device_name, device_id, relative_path, + MediaGalleryPrefInfo::kAutoDetected); + EXPECT_EQ(1, observer1.notifications()); + + // Add a second observer. + MockGalleryChangeObserver observer2(gallery_prefs()); + gallery_prefs()->AddGalleryChangeObserver(&observer2); + + // Add a new user added gallery. + path = MakePath("new_user"); + MediaStorageUtil::GetDeviceInfoFromPath(path, &device_id, NULL, + &relative_path); + device_name = ASCIIToUTF16("NewUserGallery"); + MediaGalleryPrefId user_added_id = + gallery_prefs()->AddGallery(device_id, device_name, relative_path, + true /*user*/); + AddGalleryExpectation(user_added_id, device_name, device_id, relative_path, + MediaGalleryPrefInfo::kUserAdded); + EXPECT_EQ(default_galleries_count() + 2UL, user_added_id); + EXPECT_EQ(2, observer1.notifications()); + EXPECT_EQ(1, observer2.notifications()); + + // Remove the first observer. + gallery_prefs()->RemoveGalleryChangeObserver(&observer1); + + // Remove an auto added gallery (i.e. make it blacklisted). + gallery_prefs()->ForgetGalleryById(auto_id); + expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed; + expected_galleries_for_all.erase(auto_id); + + EXPECT_EQ(2, observer1.notifications()); + EXPECT_EQ(2, observer2.notifications()); + + // Remove a user added gallery and it should go away. + gallery_prefs()->ForgetGalleryById(user_added_id); + expected_galleries_.erase(user_added_id); + expected_device_map[device_id].erase(user_added_id); + + EXPECT_EQ(2, observer1.notifications()); + EXPECT_EQ(3, observer2.notifications()); +} + } // namespace } // namespace chrome |