summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authorvandebo@chromium.org <vandebo@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-06-13 23:31:15 +0000
committervandebo@chromium.org <vandebo@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-06-13 23:31:15 +0000
commitdca96dc9115911f8c3961543639d698111f7e5d1 (patch)
tree085bc19c9c1604064d86bc54b93dfd8177109ba5 /chrome
parent3e3fb37de017ebb5fba23248f979822448ca574f (diff)
downloadchromium_src-dca96dc9115911f8c3961543639d698111f7e5d1.zip
chromium_src-dca96dc9115911f8c3961543639d698111f7e5d1.tar.gz
chromium_src-dca96dc9115911f8c3961543639d698111f7e5d1.tar.bz2
Watch iTunes library file for changes in order to update data provider.
BUG=234837 Review URL: https://chromiumcodereview.appspot.com/16836010 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@206211 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/browser/media_galleries/fileapi/itunes_data_provider.cc34
-rw-r--r--chrome/browser/media_galleries/fileapi/itunes_data_provider.h9
2 files changed, 34 insertions, 9 deletions
diff --git a/chrome/browser/media_galleries/fileapi/itunes_data_provider.cc b/chrome/browser/media_galleries/fileapi/itunes_data_provider.cc
index 1f13b26..9709b66 100644
--- a/chrome/browser/media_galleries/fileapi/itunes_data_provider.cc
+++ b/chrome/browser/media_galleries/fileapi/itunes_data_provider.cc
@@ -4,6 +4,7 @@
#include "chrome/browser/media_galleries/fileapi/itunes_data_provider.h"
+#include "base/bind.h"
#include "base/callback.h"
#include "base/format_macros.h"
#include "base/logging.h"
@@ -94,14 +95,20 @@ ITunesDataProvider::Album MakeUniqueTrackNames(
ITunesDataProvider::ITunesDataProvider(const base::FilePath& library_path)
: library_path_(library_path),
needs_refresh_(true),
- is_valid_(false) {
+ is_valid_(false),
+ weak_factory_(this) {
DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread());
+ DCHECK(!library_path_.empty());
+ bool ret = library_watcher_.Watch(
+ library_path_, false /*recursive*/,
+ base::Bind(&ITunesDataProvider::OnLibraryChanged,
+ weak_factory_.GetWeakPtr()));
+ if (!ret)
+ LOG(ERROR) << "Adding watch for " << library_path_.value() << " failed";
}
ITunesDataProvider::~ITunesDataProvider() {}
-// TODO(vandebo): add a file watch that resets |needs_refresh_| when the
-// file changes.
void ITunesDataProvider::RefreshData(
const base::Callback<void(bool)>& ready_callback) {
DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread());
@@ -118,14 +125,14 @@ const base::FilePath& ITunesDataProvider::library_path() const {
bool ITunesDataProvider::KnownArtist(const ArtistName& artist) const {
DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread());
- DCHECK(!needs_refresh_);
+ DCHECK(is_valid_);
return ContainsKey(library_, artist);
}
bool ITunesDataProvider::KnownAlbum(const ArtistName& artist,
const AlbumName& album) const {
DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread());
- DCHECK(!needs_refresh_);
+ DCHECK(is_valid_);
Library::const_iterator library_it = library_.find(artist);
if (library_it == library_.end())
return false;
@@ -137,7 +144,7 @@ base::FilePath ITunesDataProvider::GetTrackLocation(
const ArtistName& artist, const AlbumName& album,
const TrackName& track) const {
DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread());
- DCHECK(!needs_refresh_);
+ DCHECK(is_valid_);
Library::const_iterator library_it = library_.find(artist);
if (library_it == library_.end())
return base::FilePath();
@@ -155,7 +162,7 @@ base::FilePath ITunesDataProvider::GetTrackLocation(
std::set<ITunesDataProvider::ArtistName>
ITunesDataProvider::GetArtistNames() const {
DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread());
- DCHECK(!needs_refresh_);
+ DCHECK(is_valid_);
std::set<ArtistName> result;
Library::const_iterator it;
for (it = library_.begin(); it != library_.end(); ++it) {
@@ -167,7 +174,7 @@ ITunesDataProvider::GetArtistNames() const {
std::set<ITunesDataProvider::AlbumName> ITunesDataProvider::GetAlbumNames(
const ArtistName& artist) const {
DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread());
- DCHECK(!needs_refresh_);
+ DCHECK(is_valid_);
std::set<AlbumName> result;
Library::const_iterator artist_lookup = library_.find(artist);
if (artist_lookup == library_.end())
@@ -184,7 +191,7 @@ std::set<ITunesDataProvider::AlbumName> ITunesDataProvider::GetAlbumNames(
ITunesDataProvider::Album ITunesDataProvider::GetAlbum(
const ArtistName& artist, const AlbumName& album) const {
DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread());
- DCHECK(!needs_refresh_);
+ DCHECK(is_valid_);
Album empty_result;
Library::const_iterator artist_lookup = library_.find(artist);
if (artist_lookup == library_.end())
@@ -221,4 +228,13 @@ bool ITunesDataProvider::ParseLibrary() {
return true;
}
+void ITunesDataProvider::OnLibraryChanged(const base::FilePath& path,
+ bool error) {
+ DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread());
+ DCHECK_EQ(library_path_.value(), path.value());
+ if (error)
+ LOG(ERROR) << "Error watching " << library_path_.value();
+ needs_refresh_ = true;
+}
+
} // namespace itunes
diff --git a/chrome/browser/media_galleries/fileapi/itunes_data_provider.h b/chrome/browser/media_galleries/fileapi/itunes_data_provider.h
index 5a313be..26d7e14 100644
--- a/chrome/browser/media_galleries/fileapi/itunes_data_provider.h
+++ b/chrome/browser/media_galleries/fileapi/itunes_data_provider.h
@@ -12,6 +12,8 @@
#include "base/basictypes.h"
#include "base/callback_forward.h"
#include "base/files/file_path.h"
+#include "base/files/file_path_watcher.h"
+#include "base/memory/weak_ptr.h"
#include "chrome/browser/media_galleries/fileapi/itunes_library_parser.h"
namespace itunes {
@@ -64,6 +66,8 @@ class ITunesDataProvider {
bool ParseLibrary();
+ void OnLibraryChanged(const base::FilePath& path, bool error);
+
// Path to the library XML file.
const base::FilePath library_path_;
@@ -77,6 +81,11 @@ class ITunesDataProvider {
// reading or parsing the XML file fails.
bool is_valid_;
+ // A watcher on the library xml file.
+ base::FilePathWatcher library_watcher_;
+
+ base::WeakPtrFactory<ITunesDataProvider> weak_factory_;
+
DISALLOW_COPY_AND_ASSIGN(ITunesDataProvider);
};