Chromium Code Reviews| Index: chrome/browser/media_galleries/fileapi/itunes_data_provider.cc |
| diff --git a/chrome/browser/media_galleries/fileapi/itunes_data_provider.cc b/chrome/browser/media_galleries/fileapi/itunes_data_provider.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b5c3a8a3f9cc5fe601a1540dddea389dd721a556 |
| --- /dev/null |
| +++ b/chrome/browser/media_galleries/fileapi/itunes_data_provider.cc |
| @@ -0,0 +1,213 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/media_galleries/fileapi/itunes_data_provider.h" |
| + |
| +#include "base/format_macros.h" |
| +#include "base/logging.h" |
| +#include "base/platform_file.h" |
| +#include "base/stl_util.h" |
| +#include "base/stringprintf.h" |
| +#include "base/threading/thread_restrictions.h" |
| +#include "chrome/browser/media_galleries/fileapi/itunes_library_parser.h" |
| + |
| +namespace itunes { |
| + |
| +namespace { |
| + |
| +// A "reasonable" artificial limit. |
| +const int64 kMaxLibraryFileSize = 50 * 1024 * 1024; |
|
Lei Zhang
2013/06/06 09:58:54
If you search, you will find users reporting havin
vandebo (ex-Chrome)
2013/06/06 19:49:18
Done.
|
| + |
| +std::string ReadFile(const base::FilePath& path) { |
| + base::ThreadRestrictions::AssertIOAllowed(); |
| + |
| + base::PlatformFile file = base::CreatePlatformFile( |
| + path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, NULL, NULL); |
| + if (file == base::kInvalidPlatformFileValue) |
| + return std::string(); |
| + |
| + base::PlatformFileInfo file_info; |
| + if (!base::GetPlatformFileInfo(file, &file_info) || |
| + file_info.size > kMaxLibraryFileSize) { |
| + base::ClosePlatformFile(file); |
| + return std::string(); |
| + } |
| + |
| + std::string result(file_info.size, 0); |
| + if (base::ReadPlatformFile(file, 0, string_as_array(&result), |
| + file_info.size) != file_info.size) { |
| + result.clear(); |
| + } |
| + |
| + base::ClosePlatformFile(file); |
| + return result; |
| +} |
| + |
| +} // namespace |
| + |
| +ITunesDataProvider::ITunesDataProvider(const base::FilePath& library_path) |
| + : library_path_(library_path), |
| + needs_refresh_(true) { |
| +} |
| + |
| +ITunesDataProvider::~ITunesDataProvider() {} |
| + |
| +// TODO(vandebo): add a file watch that resets |needs_refresh_| when the |
| +// file changes. |
| +void ITunesDataProvider::RefreshData(const base::Closure& ready_callback) { |
| + if (!needs_refresh_) { |
|
Lei Zhang
2013/06/06 09:58:54
If you write this the other way around, it's only
vandebo (ex-Chrome)
2013/06/06 19:49:18
Done.
|
| + ready_callback.Run(); |
| + return; |
| + } |
| + |
| + ParseLibrary(); |
| + needs_refresh_ = false; |
| + ready_callback.Run(); |
| +} |
| + |
| +base::FilePath ITunesDataProvider::library_path() const { |
| + return library_path_; |
| +} |
| + |
| +bool ITunesDataProvider::KnownArtist(const ArtistName& artist) const { |
| + DCHECK(!needs_refresh_); |
| + return ContainsKey(library_, artist); |
| +} |
| + |
| +bool ITunesDataProvider::KnownAlbum(const ArtistName& artist, |
| + const AlbumName& album) const { |
| + DCHECK(!needs_refresh_); |
| + Library::const_iterator library_it = library_.find(artist); |
| + if (library_it == library_.end()) |
| + return false; |
| + return ContainsKey(library_it->second, album); |
| + |
| +} |
| + |
| +bool ITunesDataProvider::GetTrackLocation(const ArtistName& artist, |
| + const AlbumName& album, |
| + const TrackName& track, |
| + base::FilePath* result) const { |
| + DCHECK(!needs_refresh_); |
| + Library::const_iterator library_it = library_.find(artist); |
| + if (library_it == library_.end()) |
| + return false; |
| + |
| + Artist::const_iterator artist_it = library_it->second.find(album); |
| + if (artist_it == library_it->second.end()) |
| + return false; |
| + |
| + Album::const_iterator album_it = artist_it->second.find(track); |
| + if (album_it == artist_it->second.end()) |
| + return false; |
| + |
| + if (result) |
| + *result = album_it->second; |
| + return true; |
| +} |
| + |
| +std::set<ITunesDataProvider::ArtistName> |
| +ITunesDataProvider::GetArtists() const { |
| + DCHECK(!needs_refresh_); |
| + std::set<ArtistName> result; |
| + Library::const_iterator it; |
| + for (it = library_.begin(); it != library_.end(); ++it) { |
| + result.insert(it->first); |
| + } |
| + return result; |
| +} |
| + |
| +std::set<ITunesDataProvider::AlbumName> ITunesDataProvider::GetAlbums( |
| + const ArtistName& artist) const { |
| + DCHECK(!needs_refresh_); |
| + std::set<AlbumName> result; |
| + Library::const_iterator artist_lookup = library_.find(artist); |
| + if (artist_lookup == library_.end()) |
| + return result; |
| + |
| + const Artist& artist_entry = artist_lookup->second; |
| + Artist::const_iterator it; |
| + for (it = artist_entry.begin(); it != artist_entry.end(); ++it) { |
| + result.insert(it->first); |
| + } |
| + return result; |
| +} |
| + |
| +std::map<ITunesDataProvider::TrackName, base::FilePath> |
| +ITunesDataProvider::GetTracks(const ArtistName& artist, |
| + const AlbumName& album) const { |
| + DCHECK(!needs_refresh_); |
| + std::map<TrackName, base::FilePath> empty_result; |
| + Library::const_iterator artist_lookup = library_.find(artist); |
| + if (artist_lookup == library_.end()) |
| + return empty_result; |
| + |
| + Artist::const_iterator album_lookup = artist_lookup->second.find(album); |
| + if (album_lookup == artist_lookup->second.end()) |
| + return empty_result; |
| + |
| + return album_lookup->second; |
| +} |
| + |
| +// static |
| +ITunesDataProvider::Album ITunesDataProvider::MakeUniqueTrackNames( |
| + const ITunesLibraryParser::Album& album) { |
| + // TODO(vandebo): It would be nice to ensure that names returned from here |
| + // are stable, but aside from persisting every name returned, it's not |
| + // obvious how to do that (without including the track id in every name). |
| + typedef std::set<const ITunesLibraryParser::Track*> TrackRefs; |
|
Lei Zhang
2013/06/06 09:58:54
What about a std::multimap instead of these two st
vandebo (ex-Chrome)
2013/06/06 19:49:18
multimap is an interesting idea, but to me, it loo
|
| + typedef std::map<TrackName, TrackRefs> AlbumInfo; |
| + |
| + Album result; |
| + AlbumInfo duped_tracks; |
| + |
| + ITunesLibraryParser::Album::const_iterator album_it; |
| + for (album_it = album.begin(); album_it != album.end(); ++album_it) { |
|
Lei Zhang
2013/06/06 09:58:54
a "const ITunesLibraryParser::Track& track = *albu
vandebo (ex-Chrome)
2013/06/06 19:49:18
Done.
|
| + std::string name = album_it->location.BaseName().AsUTF8Unsafe(); |
| + duped_tracks[name].insert(&*album_it); |
| + } |
| + |
| + for (AlbumInfo::const_iterator name_it = duped_tracks.begin(); |
| + name_it != duped_tracks.end(); |
| + ++name_it) { |
| + if (name_it->second.size() == 1) { |
|
Lei Zhang
2013/06/06 09:58:54
name_it->second can also use a const ref alias ins
vandebo (ex-Chrome)
2013/06/06 19:49:18
Done.
|
| + result[name_it->first] = (*name_it->second.begin())->location; |
| + } else { |
| + for (TrackRefs::const_iterator track_it = name_it->second.begin(); |
| + track_it != name_it->second.end(); ++track_it) { |
| + std::string id = |
| + base::StringPrintf(" (%" PRId64 ")", (*track_it)->id); |
| + base::FilePath unique_name = |
| + (*track_it)->location.BaseName().InsertBeforeExtensionASCII(id); |
| + result[unique_name.AsUTF8Unsafe()] = (*track_it)->location; |
| + } |
| + } |
| + } |
| + |
| + return result; |
| +} |
| + |
| +void ITunesDataProvider::ParseLibrary() { |
| + std::string xml = ReadFile(library_path_); |
| + |
| + library_.clear(); |
| + ITunesLibraryParser parser; |
| + if (!parser.Parse(xml)) |
| + return; |
| + |
| + ITunesLibraryParser::Library::const_iterator artist_it; |
| + ITunesLibraryParser::Albums::const_iterator album_it; |
| + for (artist_it = parser.library().begin(); |
| + artist_it != parser.library().end(); |
| + ++artist_it) { |
| + for (album_it = artist_it->second.begin(); |
| + album_it != artist_it->second.end(); |
| + ++album_it) { |
| + library_[artist_it->first][album_it->first] = |
| + ITunesDataProvider::MakeUniqueTrackNames(album_it->second); |
| + } |
| + } |
| +}; |
| + |
| +} // namespace itunes |