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..0f9d839502ad176b4cd273c77a7ba0337f420d2d 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,10 @@ |
#include "chrome/browser/media_galleries/fileapi/itunes/itunes_file_util.h" |
+#include "base/file_util.h" |
+#include "base/strings/utf_string_conversions.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 +15,135 @@ |
namespace itunes { |
-ItunesFileUtil::ItunesFileUtil() {} |
+namespace { |
-ItunesFileUtil::~ItunesFileUtil() {} |
+const char kiTunesLibraryXML[] = "iTunes Music Library.xml"; |
+const char kiTunesMediaDir[] = "iTunes Media"; |
+base::PlatformFileError MakeDirectoryFileInfo( |
+ base::PlatformFileInfo* file_info) { |
+ base::PlatformFileInfo result; |
+ result.is_directory = true; |
+ *file_info = result; |
+ return base::PLATFORM_FILE_OK; |
+} |
+ |
+// Get path components in UTF8. |
+std::vector<std::string> GetComponents(const base::FilePath& path) { |
tommycli
2013/06/10 17:33:25
Given that this method also appears in PicasaFileU
vandebo (ex-Chrome)
2013/06/11 05:24:37
We should just create a file with helper functions
|
+ std::vector<base::FilePath::StringType> stringtype_components; |
+ path.GetComponents(&stringtype_components); |
+ |
+ std::vector<std::string> utf8_components; |
+ for (size_t i = 0; i < stringtype_components.size(); ++i) { |
+ // This is the same as FilePath::AsUTF8Unsafe(). |
+#if defined(OS_WIN) |
+ utf8_components.push_back(base::WideToUTF8(stringtype_components[i])); |
+#elif defined(OS_MACOSX) |
+ utf8_components.push_back(stringtype_components[i]); |
+#endif |
+ } |
+ |
+ return utf8_components; |
+} |
+ |
+fileapi::DirectoryEntry MakeDirectoryEntry( |
tommycli
2013/06/10 17:33:25
Ditto.
|
+ const std::string& name, |
+ int64 size, |
+ const base::Time& last_modified_time, |
+ bool is_directory) { |
+ fileapi::DirectoryEntry entry; |
+#if defined(OS_WIN) |
+ entry.name = base::UTF8ToWide(name); |
+#else |
+ entry.name = name; |
+#endif |
+ entry.is_directory = is_directory; |
+ entry.size = size; |
+ entry.last_modified_time = last_modified_time; |
+ return entry; |
+} |
+ |
+} // namespace |
+ |
+ItunesFileUtil::ItunesFileUtil() |
+ : weak_factory_(this), |
+ imported_registry_(NULL) { |
+} |
+ |
+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<std::string> components = |
+ GetComponents(url.path().NormalizePathSeparators()); |
+ |
+ 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); |
+ |
+ 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: { |
+ base::FilePath location = |
+ GetDataProvider()->GetTrackLocation(components[1], components[2], |
+ components[3]); |
+ if (!location.empty()) { |
+ return NativeMediaFileUtil::GetFileInfoSync(context, url, file_info, |
+ platform_path); |
+ } |
+ break; |
+ } |
+ |
+ default: |
+ NOTREACHED(); |
+ } |
+ |
return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
} |
@@ -27,22 +151,118 @@ base::PlatformFileError ItunesFileUtil::ReadDirectorySync( |
fileapi::FileSystemOperationContext* context, |
const fileapi::FileSystemURL& url, |
EntryList* file_list) { |
- DCHECK(context); |
- DCHECK(file_list); |
DCHECK(file_list->empty()); |
+ std::vector<std::string> components = |
+ GetComponents(url.path().NormalizePathSeparators()); |
- return base::PLATFORM_FILE_OK; |
+ 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(MakeDirectoryEntry(kiTunesLibraryXML, xml_info.size, |
+ xml_info.last_modified, false)); |
+ file_list->push_back(MakeDirectoryEntry(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()->GetArtistNames(); |
+ std::set<ITunesDataProvider::ArtistName>::const_iterator it; |
+ for (it = artists.begin(); it != artists.end(); ++it) |
+ file_list->push_back(MakeDirectoryEntry(*it, 0, base::Time(), true)); |
+ return base::PLATFORM_FILE_OK; |
+ } |
+ |
+ if (components.size() == 2) { |
+ std::set<ITunesDataProvider::AlbumName> albums = |
+ GetDataProvider()->GetAlbumNames(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(MakeDirectoryEntry(*it, 0, base::Time(), true)); |
+ return base::PLATFORM_FILE_OK; |
+ } |
+ |
+ if (components.size() == 3) { |
+ ITunesDataProvider::Album album = |
+ GetDataProvider()->GetAlbum(components[1], components[2]); |
+ if (album.size() == 0) |
+ return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
+ ITunesDataProvider::Album::const_iterator it; |
+ for (it = album.begin(); it != album.end(); ++it) { |
+ base::PlatformFileInfo file_info; |
+ if (file_util::GetFileInfo(it->second, &file_info)) { |
+ file_list->push_back(MakeDirectoryEntry(it->first, file_info.size, |
+ 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()); |
+ base::FilePath location; |
+ location = GetDataProvider()->GetTrackLocation(components[1], components[2], |
+ components[3]); |
+ if (!location.empty()) |
+ 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<std::string> components = |
+ GetComponents(url.path().NormalizePathSeparators()); |
+ |
+ 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; |
+ |
+ *local_file_path = GetDataProvider()->GetTrackLocation(components[1], |
+ components[2], |
+ components[3]); |
+ if (!local_file_path->empty()) |
+ return base::PLATFORM_FILE_OK; |
+ |
+ return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
+} |
+ |
+void ItunesFileUtil::GetFileInfoWithFreshDataProvider( |
+ 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() { |
+ if (!imported_registry_) |
+ imported_registry_ = chrome::ImportedMediaGalleryRegistry::GetInstance(); |
+ return imported_registry_->ITunesDataProvider(); |
} |
} // namespace itunes |