Chromium Code Reviews| Index: chrome/browser/media_galleries/fileapi/itunes/itunes_file_util.cc |
| diff --git a/chrome/browser/media_galleries/fileapi/itunes/itunes_file_util.cc b/chrome/browser/media_galleries/fileapi/itunes/itunes_file_util.cc |
| index e167730ee290aec7966b77f15196ae04f902a546..fe339e94547ec8518e37fe5ba50f5cd7d93bc12e 100644 |
| --- a/chrome/browser/media_galleries/fileapi/itunes/itunes_file_util.cc |
| +++ b/chrome/browser/media_galleries/fileapi/itunes/itunes_file_util.cc |
| @@ -4,6 +4,9 @@ |
| #include "chrome/browser/media_galleries/fileapi/itunes/itunes_file_util.h" |
| +#include "base/file_util.h" |
| +#include "chrome/browser/media_galleries/fileapi/itunes_data_provider.h" |
| +#include "chrome/browser/media_galleries/imported_media_gallery_registry.h" |
| #include "webkit/browser/fileapi/file_system_file_util.h" |
| #include "webkit/browser/fileapi/file_system_operation_context.h" |
| #include "webkit/browser/fileapi/file_system_url.h" |
| @@ -11,15 +14,124 @@ |
| namespace itunes { |
| -ItunesFileUtil::ItunesFileUtil() {} |
| +namespace { |
| -ItunesFileUtil::~ItunesFileUtil() {} |
| +const base::FilePath::CharType kiTunesLibraryXML[] = |
| + FILE_PATH_LITERAL("iTunes Music Library.xml"); |
| +const base::FilePath::CharType kiTunesMediaDir[] = |
| + FILE_PATH_LITERAL("iTunes Media"); |
| +base::PlatformFileError MakeDirectoryFileInfo( |
| + base::PlatformFileInfo* file_info) { |
| + base::PlatformFileInfo result; |
| + result.is_directory = true; |
| + *file_info = result; |
| + return base::PLATFORM_FILE_OK; |
| +} |
| + |
| +fileapi::DirectoryEntry MakeDirectoryEntryFilePathType( |
|
Lei Zhang
2013/06/06 09:58:54
Feels like DirectoryEntry needs a ctor.
vandebo (ex-Chrome)
2013/06/06 19:49:18
Indeed, I think it just moved out of base, I will
|
| + const base::FilePath::StringType& name, |
| + int64 size, |
| + const base::Time& last_modified_time, |
| + bool is_directory) { |
| + fileapi::DirectoryEntry entry; |
| + entry.name = name; |
| + entry.is_directory = is_directory; |
| + entry.size = size; |
| + entry.last_modified_time = last_modified_time; |
| + return entry; |
| +} |
| + |
| +fileapi::DirectoryEntry MakeDirectoryEntryUTF8( |
| + const std::string& name, |
| + int64 size, |
| + const base::Time& last_modified_time, |
| + bool is_directory) { |
| +#if defined(OS_WIN) |
| + return MakeDirectoryEntryFilePathType(base::UTF8ToWide(name), size, |
| + last_modified_time, is_directory); |
| +#else |
| + return MakeDirectoryEntryFilePathType(name, size, last_modified_time, |
| + is_directory); |
| +#endif |
| +} |
| + |
| +} // namespace |
| + |
| +ItunesFileUtil::ItunesFileUtil() : weak_factory_(this) { |
| +} |
| + |
| +ItunesFileUtil::~ItunesFileUtil() { |
| +} |
| + |
| +void ItunesFileUtil::GetFileInfoOnTaskRunnerThread( |
| + fileapi::FileSystemOperationContext* context, |
| + const fileapi::FileSystemURL& url, |
| + const GetFileInfoCallback& callback) { |
| + GetDataProvider()->RefreshData( |
| + base::Bind(&ItunesFileUtil::GetFileInfoWithFreshDataProvider, |
| + weak_factory_.GetWeakPtr(), context, url, callback)); |
| +} |
| + |
| +void ItunesFileUtil::ReadDirectoryOnTaskRunnerThread( |
| + fileapi::FileSystemOperationContext* context, |
| + const fileapi::FileSystemURL& url, |
| + const ReadDirectoryCallback& callback) { |
| + GetDataProvider()->RefreshData( |
| + base::Bind(&ItunesFileUtil::ReadDirectoryWithFreshDataProvider, |
| + weak_factory_.GetWeakPtr(), context, url, callback)); |
| +} |
| + |
| +// Contents of the iTunes media gallery: |
| +// / - root directory |
| +// /iTunes Music Library.xml - library xml file |
| +// /iTunes Media/<Artist>/<Album>/<Track> - tracks |
| +// |
| base::PlatformFileError ItunesFileUtil::GetFileInfoSync( |
| fileapi::FileSystemOperationContext* context, |
| const fileapi::FileSystemURL& url, |
| base::PlatformFileInfo* file_info, |
| base::FilePath* platform_path) { |
| + std::vector<base::FilePath::StringType> components; |
| + url.path().NormalizePathSeparators().GetComponents(&components); |
| + |
| + if (components.size() == 0) |
| + return MakeDirectoryFileInfo(file_info); |
| + |
| + if (components.size() == 1 && components[0] == kiTunesLibraryXML) { |
| + return NativeMediaFileUtil::GetFileInfoSync(context, url, file_info, |
| + platform_path); |
| + } |
| + |
| + if (components[0] != kiTunesMediaDir || components.size() > 4) |
| + return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| + |
| + switch (components.size()) { |
| + case 1: |
| + return MakeDirectoryFileInfo(file_info); |
|
Lei Zhang
2013/06/06 09:58:54
Don't you need to fill in the directory name for |
vandebo (ex-Chrome)
2013/06/06 19:49:18
base::PlatformFileInfo doesn't have a name field.
|
| + |
| + case 2: |
| + if (GetDataProvider()->KnownArtist(components[1])) |
| + return MakeDirectoryFileInfo(file_info); |
| + break; |
| + |
| + case 3: |
| + if (GetDataProvider()->KnownAlbum(components[1], components[2])) |
| + return MakeDirectoryFileInfo(file_info); |
| + break; |
| + |
| + case 4: |
| + if (GetDataProvider()->GetTrackLocation(components[1], components[2], |
| + components[3], NULL)) { |
| + return NativeMediaFileUtil::GetFileInfoSync(context, url, file_info, |
| + platform_path); |
| + } |
| + break; |
| + |
| + default: |
| + NOTREACHED(); |
| + } |
| + |
| return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| } |
| @@ -27,22 +139,116 @@ base::PlatformFileError ItunesFileUtil::ReadDirectorySync( |
| fileapi::FileSystemOperationContext* context, |
| const fileapi::FileSystemURL& url, |
| EntryList* file_list) { |
| - DCHECK(context); |
| - DCHECK(file_list); |
| DCHECK(file_list->empty()); |
| - return base::PLATFORM_FILE_OK; |
| + std::vector<base::FilePath::StringType> components; |
| + url.path().NormalizePathSeparators().GetComponents(&components); |
| + |
| + if (components.size() == 0) { |
| + base::PlatformFileInfo xml_info; |
| + if (!file_util::GetFileInfo(GetDataProvider()->library_path(), &xml_info)) |
| + return base::PLATFORM_FILE_ERROR_IO; |
| + file_list->push_back(MakeDirectoryEntryFilePathType( |
| + kiTunesLibraryXML, xml_info.size, xml_info.last_modified, false)); |
| + file_list->push_back(MakeDirectoryEntryFilePathType( |
| + kiTunesMediaDir, 0, base::Time(), true)); |
| + return base::PLATFORM_FILE_OK; |
| + } |
| + |
| + if (components.size() == 1 && components[0] == kiTunesLibraryXML) |
| + return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; |
| + |
| + if (components[0] != kiTunesMediaDir || components.size() > 4) |
| + return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| + |
| + if (components.size() == 1) { |
| + std::set<ITunesDataProvider::ArtistName> artists = |
| + GetDataProvider()->GetArtists(); |
| + std::set<ITunesDataProvider::ArtistName>::const_iterator it; |
| + for (it = artists.begin(); it != artists.end(); ++it) |
| + file_list->push_back(MakeDirectoryEntryUTF8(*it, 0, base::Time(), true)); |
| + return base::PLATFORM_FILE_OK; |
| + } |
| + |
| + if (components.size() == 2) { |
| + std::set<ITunesDataProvider::AlbumName> albums = |
| + GetDataProvider()->GetAlbums(components[1]); |
| + if (albums.size() == 0) |
| + return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| + std::set<ITunesDataProvider::AlbumName>::const_iterator it; |
| + for (it = albums.begin(); it != albums.end(); ++it) |
| + file_list->push_back(MakeDirectoryEntryUTF8(*it, 0, base::Time(), true)); |
| + return base::PLATFORM_FILE_OK; |
| + } |
| + |
| + if (components.size() == 3) { |
| + std::map<ITunesDataProvider::TrackName, base::FilePath> tracks = |
| + GetDataProvider()->GetTracks(components[1], components[2]); |
| + if (tracks.size() == 0) |
| + return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| + std::map<ITunesDataProvider::TrackName, base::FilePath>::const_iterator it; |
| + for (it = tracks.begin(); it != tracks.end(); ++it) { |
| + base::PlatformFileInfo file_info; |
| + if (file_util::GetFileInfo(it->second, &file_info)) |
| + file_list->push_back(MakeDirectoryEntryUTF8(it->first, file_info.size, |
|
Lei Zhang
2013/06/06 09:58:54
nit: needs {} around this block.
vandebo (ex-Chrome)
2013/06/06 19:49:18
Done.
|
| + file_info.last_modified, |
| + false)); |
| + } |
| + return base::PLATFORM_FILE_OK; |
| + } |
| + |
| + // At this point, the only choice is one of two errors, but figuring out |
| + // which one is required. |
| + DCHECK_EQ(4UL, components.size()); |
| + if (GetDataProvider()->GetTrackLocation(components[1], components[2], |
| + components[3], NULL)) { |
| + return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; |
| + } |
| + return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| } |
| base::PlatformFileError ItunesFileUtil::GetLocalFilePath( |
| fileapi::FileSystemOperationContext* context, |
| const fileapi::FileSystemURL& url, |
| base::FilePath* local_file_path) { |
| - DCHECK(local_file_path); |
| - DCHECK(url.is_valid()); |
| + // Should only get here for files, i.e. the xml file and tracks. |
| + std::vector<base::FilePath::StringType> components; |
| + url.path().NormalizePathSeparators().GetComponents(&components); |
| + |
| + if (components.size() == 1 && components[0] == kiTunesLibraryXML) { |
| + *local_file_path = GetDataProvider()->library_path(); |
| + return base::PLATFORM_FILE_OK; |
| + } |
| + |
| + if (components[0] != kiTunesMediaDir || components.size() != 4) |
| + return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| + |
| + if (GetDataProvider()->GetTrackLocation(components[1], components[2], |
| + components[3], local_file_path)) { |
| + return base::PLATFORM_FILE_OK; |
| + } |
| + |
| + return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| +} |
| + |
| +void ItunesFileUtil::GetFileInfoWithFreshDataProvider( |
|
Lei Zhang
2013/06/06 09:58:54
Do you even need this and ReadDirectoryWithFreshDa
vandebo (ex-Chrome)
2013/06/06 19:49:18
Yes, bind wouldn't bind to a protected method of a
|
| + fileapi::FileSystemOperationContext* context, |
| + const fileapi::FileSystemURL& url, |
| + const GetFileInfoCallback& callback) { |
| + NativeMediaFileUtil::GetFileInfoOnTaskRunnerThread(context, url, callback); |
| +} |
| + |
| +void ItunesFileUtil::ReadDirectoryWithFreshDataProvider( |
| + fileapi::FileSystemOperationContext* context, |
| + const fileapi::FileSystemURL& url, |
| + const ReadDirectoryCallback& callback) { |
| + NativeMediaFileUtil::ReadDirectoryOnTaskRunnerThread(context, url, callback); |
| +} |
| - NOTREACHED(); |
| - return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
| +ITunesDataProvider* ItunesFileUtil::GetDataProvider() { |
| + chrome::ImportedMediaGalleryRegistry* imported_registry = |
|
Lei Zhang
2013/06/06 09:58:54
save this value instead of calling GetInstance() e
vandebo (ex-Chrome)
2013/06/06 19:49:18
Done.
|
| + chrome::ImportedMediaGalleryRegistry::GetInstance(); |
| + return imported_registry->itunes_data_provider(); |
| } |
| } // namespace itunes |