| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/media_galleries/fileapi/itunes_data_provider.h" | 5 #include "chrome/browser/media_galleries/fileapi/itunes_data_provider.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/platform_file.h" | 12 #include "base/platform_file.h" |
| 13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
| 16 #include "chrome/browser/media_galleries/fileapi/media_file_system_mount_point_p
rovider.h" | 16 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" |
| 17 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h" | 17 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h" |
| 18 #include "chrome/common/itunes_library.h" | 18 #include "chrome/common/itunes_library.h" |
| 19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 20 | 20 |
| 21 using chrome::MediaFileSystemMountPointProvider; | 21 using chrome::MediaFileSystemBackend; |
| 22 | 22 |
| 23 namespace itunes { | 23 namespace itunes { |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 typedef base::Callback<void(scoped_ptr<base::FilePathWatcher> watcher)> | 27 typedef base::Callback<void(scoped_ptr<base::FilePathWatcher> watcher)> |
| 28 FileWatchStartedCallback; | 28 FileWatchStartedCallback; |
| 29 | 29 |
| 30 ITunesDataProvider::Album MakeUniqueTrackNames(const parser::Album& album) { | 30 ITunesDataProvider::Album MakeUniqueTrackNames(const parser::Album& album) { |
| 31 // TODO(vandebo): It would be nice to ensure that names returned from here | 31 // TODO(vandebo): It would be nice to ensure that names returned from here |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 | 65 |
| 66 return result; | 66 return result; |
| 67 } | 67 } |
| 68 | 68 |
| 69 // Bounces |path| and |error| to |callback| from the FILE thread to the media | 69 // Bounces |path| and |error| to |callback| from the FILE thread to the media |
| 70 // task runner. | 70 // task runner. |
| 71 void OnLibraryChanged(const base::FilePathWatcher::Callback& callback, | 71 void OnLibraryChanged(const base::FilePathWatcher::Callback& callback, |
| 72 const base::FilePath& path, | 72 const base::FilePath& path, |
| 73 bool error) { | 73 bool error) { |
| 74 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 74 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 75 MediaFileSystemMountPointProvider::MediaTaskRunner()->PostTask( | 75 MediaFileSystemBackend::MediaTaskRunner()->PostTask( |
| 76 FROM_HERE, base::Bind(callback, path, error)); | 76 FROM_HERE, base::Bind(callback, path, error)); |
| 77 } | 77 } |
| 78 | 78 |
| 79 // The watch has to be started on the FILE thread, and the callback called by | 79 // The watch has to be started on the FILE thread, and the callback called by |
| 80 // the FilePathWatcher also needs to run on the FILE thread. | 80 // the FilePathWatcher also needs to run on the FILE thread. |
| 81 void StartLibraryWatchOnFileThread( | 81 void StartLibraryWatchOnFileThread( |
| 82 const base::FilePath& library_path, | 82 const base::FilePath& library_path, |
| 83 const FileWatchStartedCallback& watch_started_callback, | 83 const FileWatchStartedCallback& watch_started_callback, |
| 84 const base::FilePathWatcher::Callback& library_changed_callback) { | 84 const base::FilePathWatcher::Callback& library_changed_callback) { |
| 85 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 85 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 86 scoped_ptr<base::FilePathWatcher> watcher(new base::FilePathWatcher); | 86 scoped_ptr<base::FilePathWatcher> watcher(new base::FilePathWatcher); |
| 87 bool success = watcher->Watch( | 87 bool success = watcher->Watch( |
| 88 library_path, false /*recursive*/, | 88 library_path, false /*recursive*/, |
| 89 base::Bind(&OnLibraryChanged, library_changed_callback)); | 89 base::Bind(&OnLibraryChanged, library_changed_callback)); |
| 90 if (!success) | 90 if (!success) |
| 91 LOG(ERROR) << "Adding watch for " << library_path.value() << " failed"; | 91 LOG(ERROR) << "Adding watch for " << library_path.value() << " failed"; |
| 92 MediaFileSystemMountPointProvider::MediaTaskRunner()->PostTask( | 92 MediaFileSystemBackend::MediaTaskRunner()->PostTask( |
| 93 FROM_HERE, | 93 FROM_HERE, |
| 94 base::Bind(watch_started_callback, base::Passed(&watcher))); | 94 base::Bind(watch_started_callback, base::Passed(&watcher))); |
| 95 } | 95 } |
| 96 | 96 |
| 97 } // namespace | 97 } // namespace |
| 98 | 98 |
| 99 ITunesDataProvider::ITunesDataProvider(const base::FilePath& library_path) | 99 ITunesDataProvider::ITunesDataProvider(const base::FilePath& library_path) |
| 100 : library_path_(library_path), | 100 : library_path_(library_path), |
| 101 needs_refresh_(true), | 101 needs_refresh_(true), |
| 102 is_valid_(false) { | 102 is_valid_(false) { |
| 103 DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread()); | 103 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 104 DCHECK(!library_path_.empty()); | 104 DCHECK(!library_path_.empty()); |
| 105 | 105 |
| 106 content::BrowserThread::PostTask( | 106 content::BrowserThread::PostTask( |
| 107 content::BrowserThread::FILE, | 107 content::BrowserThread::FILE, |
| 108 FROM_HERE, | 108 FROM_HERE, |
| 109 base::Bind(StartLibraryWatchOnFileThread, | 109 base::Bind(StartLibraryWatchOnFileThread, |
| 110 library_path_, | 110 library_path_, |
| 111 base::Bind(&ITunesDataProvider::OnLibraryWatchStartedCallback), | 111 base::Bind(&ITunesDataProvider::OnLibraryWatchStartedCallback), |
| 112 base::Bind(&ITunesDataProvider::OnLibraryChangedCallback))); | 112 base::Bind(&ITunesDataProvider::OnLibraryChangedCallback))); |
| 113 } | 113 } |
| 114 | 114 |
| 115 ITunesDataProvider::~ITunesDataProvider() {} | 115 ITunesDataProvider::~ITunesDataProvider() {} |
| 116 | 116 |
| 117 // TODO(vandebo): add a file watch that resets |needs_refresh_| when the | 117 // TODO(vandebo): add a file watch that resets |needs_refresh_| when the |
| 118 // file changes. | 118 // file changes. |
| 119 void ITunesDataProvider::RefreshData(const ReadyCallback& ready_callback) { | 119 void ITunesDataProvider::RefreshData(const ReadyCallback& ready_callback) { |
| 120 DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread()); | 120 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 121 if (!needs_refresh_) { | 121 if (!needs_refresh_) { |
| 122 ready_callback.Run(is_valid_); | 122 ready_callback.Run(is_valid_); |
| 123 return; | 123 return; |
| 124 } | 124 } |
| 125 | 125 |
| 126 needs_refresh_ = false; | 126 needs_refresh_ = false; |
| 127 xml_parser_ = new SafeITunesLibraryParser( | 127 xml_parser_ = new SafeITunesLibraryParser( |
| 128 library_path_, | 128 library_path_, |
| 129 base::Bind(&ITunesDataProvider::OnLibraryParsedCallback, ready_callback)); | 129 base::Bind(&ITunesDataProvider::OnLibraryParsedCallback, ready_callback)); |
| 130 xml_parser_->Start(); | 130 xml_parser_->Start(); |
| 131 } | 131 } |
| 132 | 132 |
| 133 const base::FilePath& ITunesDataProvider::library_path() const { | 133 const base::FilePath& ITunesDataProvider::library_path() const { |
| 134 return library_path_; | 134 return library_path_; |
| 135 } | 135 } |
| 136 | 136 |
| 137 bool ITunesDataProvider::KnownArtist(const ArtistName& artist) const { | 137 bool ITunesDataProvider::KnownArtist(const ArtistName& artist) const { |
| 138 DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread()); | 138 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 139 DCHECK(is_valid_); | 139 DCHECK(is_valid_); |
| 140 return ContainsKey(library_, artist); | 140 return ContainsKey(library_, artist); |
| 141 } | 141 } |
| 142 | 142 |
| 143 bool ITunesDataProvider::KnownAlbum(const ArtistName& artist, | 143 bool ITunesDataProvider::KnownAlbum(const ArtistName& artist, |
| 144 const AlbumName& album) const { | 144 const AlbumName& album) const { |
| 145 DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread()); | 145 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 146 DCHECK(is_valid_); | 146 DCHECK(is_valid_); |
| 147 Library::const_iterator library_it = library_.find(artist); | 147 Library::const_iterator library_it = library_.find(artist); |
| 148 if (library_it == library_.end()) | 148 if (library_it == library_.end()) |
| 149 return false; | 149 return false; |
| 150 return ContainsKey(library_it->second, album); | 150 return ContainsKey(library_it->second, album); |
| 151 } | 151 } |
| 152 | 152 |
| 153 base::FilePath ITunesDataProvider::GetTrackLocation( | 153 base::FilePath ITunesDataProvider::GetTrackLocation( |
| 154 const ArtistName& artist, const AlbumName& album, | 154 const ArtistName& artist, const AlbumName& album, |
| 155 const TrackName& track) const { | 155 const TrackName& track) const { |
| 156 DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread()); | 156 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 157 DCHECK(is_valid_); | 157 DCHECK(is_valid_); |
| 158 Library::const_iterator library_it = library_.find(artist); | 158 Library::const_iterator library_it = library_.find(artist); |
| 159 if (library_it == library_.end()) | 159 if (library_it == library_.end()) |
| 160 return base::FilePath(); | 160 return base::FilePath(); |
| 161 | 161 |
| 162 Artist::const_iterator artist_it = library_it->second.find(album); | 162 Artist::const_iterator artist_it = library_it->second.find(album); |
| 163 if (artist_it == library_it->second.end()) | 163 if (artist_it == library_it->second.end()) |
| 164 return base::FilePath(); | 164 return base::FilePath(); |
| 165 | 165 |
| 166 Album::const_iterator album_it = artist_it->second.find(track); | 166 Album::const_iterator album_it = artist_it->second.find(track); |
| 167 if (album_it == artist_it->second.end()) | 167 if (album_it == artist_it->second.end()) |
| 168 return base::FilePath(); | 168 return base::FilePath(); |
| 169 return album_it->second; | 169 return album_it->second; |
| 170 } | 170 } |
| 171 | 171 |
| 172 std::set<ITunesDataProvider::ArtistName> | 172 std::set<ITunesDataProvider::ArtistName> |
| 173 ITunesDataProvider::GetArtistNames() const { | 173 ITunesDataProvider::GetArtistNames() const { |
| 174 DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread()); | 174 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 175 DCHECK(is_valid_); | 175 DCHECK(is_valid_); |
| 176 std::set<ArtistName> result; | 176 std::set<ArtistName> result; |
| 177 Library::const_iterator it; | 177 Library::const_iterator it; |
| 178 for (it = library_.begin(); it != library_.end(); ++it) { | 178 for (it = library_.begin(); it != library_.end(); ++it) { |
| 179 result.insert(it->first); | 179 result.insert(it->first); |
| 180 } | 180 } |
| 181 return result; | 181 return result; |
| 182 } | 182 } |
| 183 | 183 |
| 184 std::set<ITunesDataProvider::AlbumName> ITunesDataProvider::GetAlbumNames( | 184 std::set<ITunesDataProvider::AlbumName> ITunesDataProvider::GetAlbumNames( |
| 185 const ArtistName& artist) const { | 185 const ArtistName& artist) const { |
| 186 DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread()); | 186 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 187 DCHECK(is_valid_); | 187 DCHECK(is_valid_); |
| 188 std::set<AlbumName> result; | 188 std::set<AlbumName> result; |
| 189 Library::const_iterator artist_lookup = library_.find(artist); | 189 Library::const_iterator artist_lookup = library_.find(artist); |
| 190 if (artist_lookup == library_.end()) | 190 if (artist_lookup == library_.end()) |
| 191 return result; | 191 return result; |
| 192 | 192 |
| 193 const Artist& artist_entry = artist_lookup->second; | 193 const Artist& artist_entry = artist_lookup->second; |
| 194 Artist::const_iterator it; | 194 Artist::const_iterator it; |
| 195 for (it = artist_entry.begin(); it != artist_entry.end(); ++it) { | 195 for (it = artist_entry.begin(); it != artist_entry.end(); ++it) { |
| 196 result.insert(it->first); | 196 result.insert(it->first); |
| 197 } | 197 } |
| 198 return result; | 198 return result; |
| 199 } | 199 } |
| 200 | 200 |
| 201 ITunesDataProvider::Album ITunesDataProvider::GetAlbum( | 201 ITunesDataProvider::Album ITunesDataProvider::GetAlbum( |
| 202 const ArtistName& artist, const AlbumName& album) const { | 202 const ArtistName& artist, const AlbumName& album) const { |
| 203 DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread()); | 203 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 204 DCHECK(is_valid_); | 204 DCHECK(is_valid_); |
| 205 Album result; | 205 Album result; |
| 206 Library::const_iterator artist_lookup = library_.find(artist); | 206 Library::const_iterator artist_lookup = library_.find(artist); |
| 207 if (artist_lookup != library_.end()) { | 207 if (artist_lookup != library_.end()) { |
| 208 Artist::const_iterator album_lookup = artist_lookup->second.find(album); | 208 Artist::const_iterator album_lookup = artist_lookup->second.find(album); |
| 209 if (album_lookup != artist_lookup->second.end()) | 209 if (album_lookup != artist_lookup->second.end()) |
| 210 result = album_lookup->second; | 210 result = album_lookup->second; |
| 211 } | 211 } |
| 212 return result; | 212 return result; |
| 213 } | 213 } |
| 214 | 214 |
| 215 // static | 215 // static |
| 216 void ITunesDataProvider::OnLibraryWatchStartedCallback( | 216 void ITunesDataProvider::OnLibraryWatchStartedCallback( |
| 217 scoped_ptr<base::FilePathWatcher> library_watcher) { | 217 scoped_ptr<base::FilePathWatcher> library_watcher) { |
| 218 DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread()); | 218 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 219 ITunesDataProvider* provider = | 219 ITunesDataProvider* provider = |
| 220 chrome::ImportedMediaGalleryRegistry::ITunesDataProvider(); | 220 chrome::ImportedMediaGalleryRegistry::ITunesDataProvider(); |
| 221 if (provider) | 221 if (provider) |
| 222 provider->OnLibraryWatchStarted(library_watcher.Pass()); | 222 provider->OnLibraryWatchStarted(library_watcher.Pass()); |
| 223 } | 223 } |
| 224 | 224 |
| 225 // static | 225 // static |
| 226 void ITunesDataProvider::OnLibraryChangedCallback(const base::FilePath& path, | 226 void ITunesDataProvider::OnLibraryChangedCallback(const base::FilePath& path, |
| 227 bool error) { | 227 bool error) { |
| 228 DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread()); | 228 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 229 ITunesDataProvider* provider = | 229 ITunesDataProvider* provider = |
| 230 chrome::ImportedMediaGalleryRegistry::ITunesDataProvider(); | 230 chrome::ImportedMediaGalleryRegistry::ITunesDataProvider(); |
| 231 if (provider) | 231 if (provider) |
| 232 provider->OnLibraryChanged(path, error); | 232 provider->OnLibraryChanged(path, error); |
| 233 } | 233 } |
| 234 | 234 |
| 235 // static | 235 // static |
| 236 void ITunesDataProvider::OnLibraryParsedCallback( | 236 void ITunesDataProvider::OnLibraryParsedCallback( |
| 237 const ReadyCallback& ready_callback, | 237 const ReadyCallback& ready_callback, |
| 238 bool result, | 238 bool result, |
| 239 const parser::Library& library) { | 239 const parser::Library& library) { |
| 240 DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread()); | 240 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 241 ITunesDataProvider* provider = | 241 ITunesDataProvider* provider = |
| 242 chrome::ImportedMediaGalleryRegistry::ITunesDataProvider(); | 242 chrome::ImportedMediaGalleryRegistry::ITunesDataProvider(); |
| 243 if (!provider) { | 243 if (!provider) { |
| 244 ready_callback.Run(false); | 244 ready_callback.Run(false); |
| 245 return; | 245 return; |
| 246 } | 246 } |
| 247 provider->OnLibraryParsed(ready_callback, result, library); | 247 provider->OnLibraryParsed(ready_callback, result, library); |
| 248 } | 248 } |
| 249 | 249 |
| 250 void ITunesDataProvider::OnLibraryWatchStarted( | 250 void ITunesDataProvider::OnLibraryWatchStarted( |
| 251 scoped_ptr<base::FilePathWatcher> library_watcher) { | 251 scoped_ptr<base::FilePathWatcher> library_watcher) { |
| 252 DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread()); | 252 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 253 library_watcher_.reset(library_watcher.release()); | 253 library_watcher_.reset(library_watcher.release()); |
| 254 } | 254 } |
| 255 | 255 |
| 256 void ITunesDataProvider::OnLibraryChanged(const base::FilePath& path, | 256 void ITunesDataProvider::OnLibraryChanged(const base::FilePath& path, |
| 257 bool error) { | 257 bool error) { |
| 258 DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread()); | 258 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 259 DCHECK_EQ(library_path_.value(), path.value()); | 259 DCHECK_EQ(library_path_.value(), path.value()); |
| 260 if (error) | 260 if (error) |
| 261 LOG(ERROR) << "Error watching " << library_path_.value(); | 261 LOG(ERROR) << "Error watching " << library_path_.value(); |
| 262 needs_refresh_ = true; | 262 needs_refresh_ = true; |
| 263 } | 263 } |
| 264 | 264 |
| 265 void ITunesDataProvider::OnLibraryParsed(const ReadyCallback& ready_callback, | 265 void ITunesDataProvider::OnLibraryParsed(const ReadyCallback& ready_callback, |
| 266 bool result, | 266 bool result, |
| 267 const parser::Library& library) { | 267 const parser::Library& library) { |
| 268 DCHECK(MediaFileSystemMountPointProvider::CurrentlyOnMediaTaskRunnerThread()); | 268 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 269 is_valid_ = result; | 269 is_valid_ = result; |
| 270 if (is_valid_) { | 270 if (is_valid_) { |
| 271 library_.clear(); | 271 library_.clear(); |
| 272 for (parser::Library::const_iterator artist_it = library.begin(); | 272 for (parser::Library::const_iterator artist_it = library.begin(); |
| 273 artist_it != library.end(); | 273 artist_it != library.end(); |
| 274 ++artist_it) { | 274 ++artist_it) { |
| 275 for (parser::Albums::const_iterator album_it = artist_it->second.begin(); | 275 for (parser::Albums::const_iterator album_it = artist_it->second.begin(); |
| 276 album_it != artist_it->second.end(); | 276 album_it != artist_it->second.end(); |
| 277 ++album_it) { | 277 ++album_it) { |
| 278 library_[artist_it->first][album_it->first] = | 278 library_[artist_it->first][album_it->first] = |
| 279 MakeUniqueTrackNames(album_it->second); | 279 MakeUniqueTrackNames(album_it->second); |
| 280 } | 280 } |
| 281 } | 281 } |
| 282 } | 282 } |
| 283 ready_callback.Run(is_valid_); | 283 ready_callback.Run(is_valid_); |
| 284 } | 284 } |
| 285 | 285 |
| 286 } // namespace itunes | 286 } // namespace itunes |
| OLD | NEW |