Chromium Code Reviews| 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/itunes_file_util.h" | 5 #include "chrome/browser/media_galleries/fileapi/itunes/itunes_file_util.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | |
| 8 #include "base/strings/utf_string_conversions.h" | |
| 9 #include "chrome/browser/media_galleries/fileapi/itunes_data_provider.h" | |
| 10 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h" | |
| 7 #include "webkit/browser/fileapi/file_system_file_util.h" | 11 #include "webkit/browser/fileapi/file_system_file_util.h" |
| 8 #include "webkit/browser/fileapi/file_system_operation_context.h" | 12 #include "webkit/browser/fileapi/file_system_operation_context.h" |
| 9 #include "webkit/browser/fileapi/file_system_url.h" | 13 #include "webkit/browser/fileapi/file_system_url.h" |
| 10 #include "webkit/common/fileapi/file_system_util.h" | 14 #include "webkit/common/fileapi/file_system_util.h" |
| 11 | 15 |
| 12 namespace itunes { | 16 namespace itunes { |
| 13 | 17 |
| 14 ItunesFileUtil::ItunesFileUtil() {} | 18 namespace { |
| 15 | 19 |
| 16 ItunesFileUtil::~ItunesFileUtil() {} | 20 const char kiTunesLibraryXML[] = "iTunes Music Library.xml"; |
| 21 const char kiTunesMediaDir[] = "iTunes Media"; | |
| 17 | 22 |
| 23 base::PlatformFileError MakeDirectoryFileInfo( | |
| 24 base::PlatformFileInfo* file_info) { | |
| 25 base::PlatformFileInfo result; | |
| 26 result.is_directory = true; | |
| 27 *file_info = result; | |
| 28 return base::PLATFORM_FILE_OK; | |
| 29 } | |
| 30 | |
| 31 // Get path components in UTF8. | |
| 32 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
| |
| 33 std::vector<base::FilePath::StringType> stringtype_components; | |
| 34 path.GetComponents(&stringtype_components); | |
| 35 | |
| 36 std::vector<std::string> utf8_components; | |
| 37 for (size_t i = 0; i < stringtype_components.size(); ++i) { | |
| 38 // This is the same as FilePath::AsUTF8Unsafe(). | |
| 39 #if defined(OS_WIN) | |
| 40 utf8_components.push_back(base::WideToUTF8(stringtype_components[i])); | |
| 41 #elif defined(OS_MACOSX) | |
| 42 utf8_components.push_back(stringtype_components[i]); | |
| 43 #endif | |
| 44 } | |
| 45 | |
| 46 return utf8_components; | |
| 47 } | |
| 48 | |
| 49 fileapi::DirectoryEntry MakeDirectoryEntry( | |
|
tommycli
2013/06/10 17:33:25
Ditto.
| |
| 50 const std::string& name, | |
| 51 int64 size, | |
| 52 const base::Time& last_modified_time, | |
| 53 bool is_directory) { | |
| 54 fileapi::DirectoryEntry entry; | |
| 55 #if defined(OS_WIN) | |
| 56 entry.name = base::UTF8ToWide(name); | |
| 57 #else | |
| 58 entry.name = name; | |
| 59 #endif | |
| 60 entry.is_directory = is_directory; | |
| 61 entry.size = size; | |
| 62 entry.last_modified_time = last_modified_time; | |
| 63 return entry; | |
| 64 } | |
| 65 | |
| 66 } // namespace | |
| 67 | |
| 68 ItunesFileUtil::ItunesFileUtil() | |
| 69 : weak_factory_(this), | |
| 70 imported_registry_(NULL) { | |
| 71 } | |
| 72 | |
| 73 ItunesFileUtil::~ItunesFileUtil() { | |
| 74 } | |
| 75 | |
| 76 void ItunesFileUtil::GetFileInfoOnTaskRunnerThread( | |
| 77 fileapi::FileSystemOperationContext* context, | |
| 78 const fileapi::FileSystemURL& url, | |
| 79 const GetFileInfoCallback& callback) { | |
| 80 GetDataProvider()->RefreshData( | |
| 81 base::Bind(&ItunesFileUtil::GetFileInfoWithFreshDataProvider, | |
| 82 weak_factory_.GetWeakPtr(), context, url, callback)); | |
| 83 } | |
| 84 | |
| 85 void ItunesFileUtil::ReadDirectoryOnTaskRunnerThread( | |
| 86 fileapi::FileSystemOperationContext* context, | |
| 87 const fileapi::FileSystemURL& url, | |
| 88 const ReadDirectoryCallback& callback) { | |
| 89 GetDataProvider()->RefreshData( | |
| 90 base::Bind(&ItunesFileUtil::ReadDirectoryWithFreshDataProvider, | |
| 91 weak_factory_.GetWeakPtr(), context, url, callback)); | |
| 92 } | |
| 93 | |
| 94 // Contents of the iTunes media gallery: | |
| 95 // / - root directory | |
| 96 // /iTunes Music Library.xml - library xml file | |
| 97 // /iTunes Media/<Artist>/<Album>/<Track> - tracks | |
| 98 // | |
| 18 base::PlatformFileError ItunesFileUtil::GetFileInfoSync( | 99 base::PlatformFileError ItunesFileUtil::GetFileInfoSync( |
| 19 fileapi::FileSystemOperationContext* context, | 100 fileapi::FileSystemOperationContext* context, |
| 20 const fileapi::FileSystemURL& url, | 101 const fileapi::FileSystemURL& url, |
| 21 base::PlatformFileInfo* file_info, | 102 base::PlatformFileInfo* file_info, |
| 22 base::FilePath* platform_path) { | 103 base::FilePath* platform_path) { |
| 104 std::vector<std::string> components = | |
| 105 GetComponents(url.path().NormalizePathSeparators()); | |
| 106 | |
| 107 if (components.size() == 0) | |
| 108 return MakeDirectoryFileInfo(file_info); | |
| 109 | |
| 110 if (components.size() == 1 && components[0] == kiTunesLibraryXML) { | |
| 111 return NativeMediaFileUtil::GetFileInfoSync(context, url, file_info, | |
| 112 platform_path); | |
| 113 } | |
| 114 | |
| 115 if (components[0] != kiTunesMediaDir || components.size() > 4) | |
| 116 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 117 | |
| 118 switch (components.size()) { | |
| 119 case 1: | |
| 120 return MakeDirectoryFileInfo(file_info); | |
| 121 | |
| 122 case 2: | |
| 123 if (GetDataProvider()->KnownArtist(components[1])) | |
| 124 return MakeDirectoryFileInfo(file_info); | |
| 125 break; | |
| 126 | |
| 127 case 3: | |
| 128 if (GetDataProvider()->KnownAlbum(components[1], components[2])) | |
| 129 return MakeDirectoryFileInfo(file_info); | |
| 130 break; | |
| 131 | |
| 132 case 4: { | |
| 133 base::FilePath location = | |
| 134 GetDataProvider()->GetTrackLocation(components[1], components[2], | |
| 135 components[3]); | |
| 136 if (!location.empty()) { | |
| 137 return NativeMediaFileUtil::GetFileInfoSync(context, url, file_info, | |
| 138 platform_path); | |
| 139 } | |
| 140 break; | |
| 141 } | |
| 142 | |
| 143 default: | |
| 144 NOTREACHED(); | |
| 145 } | |
| 146 | |
| 23 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 147 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 24 } | 148 } |
| 25 | 149 |
| 26 base::PlatformFileError ItunesFileUtil::ReadDirectorySync( | 150 base::PlatformFileError ItunesFileUtil::ReadDirectorySync( |
| 27 fileapi::FileSystemOperationContext* context, | 151 fileapi::FileSystemOperationContext* context, |
| 28 const fileapi::FileSystemURL& url, | 152 const fileapi::FileSystemURL& url, |
| 29 EntryList* file_list) { | 153 EntryList* file_list) { |
| 30 DCHECK(context); | |
| 31 DCHECK(file_list); | |
| 32 DCHECK(file_list->empty()); | 154 DCHECK(file_list->empty()); |
| 155 std::vector<std::string> components = | |
| 156 GetComponents(url.path().NormalizePathSeparators()); | |
| 33 | 157 |
| 34 return base::PLATFORM_FILE_OK; | 158 if (components.size() == 0) { |
| 159 base::PlatformFileInfo xml_info; | |
| 160 if (!file_util::GetFileInfo(GetDataProvider()->library_path(), &xml_info)) | |
| 161 return base::PLATFORM_FILE_ERROR_IO; | |
| 162 file_list->push_back(MakeDirectoryEntry(kiTunesLibraryXML, xml_info.size, | |
| 163 xml_info.last_modified, false)); | |
| 164 file_list->push_back(MakeDirectoryEntry(kiTunesMediaDir, 0, base::Time(), | |
| 165 true)); | |
| 166 return base::PLATFORM_FILE_OK; | |
| 167 } | |
| 168 | |
| 169 if (components.size() == 1 && components[0] == kiTunesLibraryXML) | |
| 170 return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; | |
| 171 | |
| 172 if (components[0] != kiTunesMediaDir || components.size() > 4) | |
| 173 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 174 | |
| 175 if (components.size() == 1) { | |
| 176 std::set<ITunesDataProvider::ArtistName> artists = | |
| 177 GetDataProvider()->GetArtistNames(); | |
| 178 std::set<ITunesDataProvider::ArtistName>::const_iterator it; | |
| 179 for (it = artists.begin(); it != artists.end(); ++it) | |
| 180 file_list->push_back(MakeDirectoryEntry(*it, 0, base::Time(), true)); | |
| 181 return base::PLATFORM_FILE_OK; | |
| 182 } | |
| 183 | |
| 184 if (components.size() == 2) { | |
| 185 std::set<ITunesDataProvider::AlbumName> albums = | |
| 186 GetDataProvider()->GetAlbumNames(components[1]); | |
| 187 if (albums.size() == 0) | |
| 188 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 189 std::set<ITunesDataProvider::AlbumName>::const_iterator it; | |
| 190 for (it = albums.begin(); it != albums.end(); ++it) | |
| 191 file_list->push_back(MakeDirectoryEntry(*it, 0, base::Time(), true)); | |
| 192 return base::PLATFORM_FILE_OK; | |
| 193 } | |
| 194 | |
| 195 if (components.size() == 3) { | |
| 196 ITunesDataProvider::Album album = | |
| 197 GetDataProvider()->GetAlbum(components[1], components[2]); | |
| 198 if (album.size() == 0) | |
| 199 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 200 ITunesDataProvider::Album::const_iterator it; | |
| 201 for (it = album.begin(); it != album.end(); ++it) { | |
| 202 base::PlatformFileInfo file_info; | |
| 203 if (file_util::GetFileInfo(it->second, &file_info)) { | |
| 204 file_list->push_back(MakeDirectoryEntry(it->first, file_info.size, | |
| 205 file_info.last_modified, | |
| 206 false)); | |
| 207 } | |
| 208 } | |
| 209 return base::PLATFORM_FILE_OK; | |
| 210 } | |
| 211 | |
| 212 // At this point, the only choice is one of two errors, but figuring out | |
| 213 // which one is required. | |
| 214 DCHECK_EQ(4UL, components.size()); | |
| 215 base::FilePath location; | |
| 216 location = GetDataProvider()->GetTrackLocation(components[1], components[2], | |
| 217 components[3]); | |
| 218 if (!location.empty()) | |
| 219 return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; | |
| 220 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 35 } | 221 } |
| 36 | 222 |
| 37 base::PlatformFileError ItunesFileUtil::GetLocalFilePath( | 223 base::PlatformFileError ItunesFileUtil::GetLocalFilePath( |
| 38 fileapi::FileSystemOperationContext* context, | 224 fileapi::FileSystemOperationContext* context, |
| 39 const fileapi::FileSystemURL& url, | 225 const fileapi::FileSystemURL& url, |
| 40 base::FilePath* local_file_path) { | 226 base::FilePath* local_file_path) { |
| 41 DCHECK(local_file_path); | 227 // Should only get here for files, i.e. the xml file and tracks. |
| 42 DCHECK(url.is_valid()); | 228 std::vector<std::string> components = |
| 229 GetComponents(url.path().NormalizePathSeparators()); | |
| 43 | 230 |
| 44 NOTREACHED(); | 231 if (components.size() == 1 && components[0] == kiTunesLibraryXML) { |
| 45 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 232 *local_file_path = GetDataProvider()->library_path(); |
| 233 return base::PLATFORM_FILE_OK; | |
| 234 } | |
| 235 | |
| 236 if (components[0] != kiTunesMediaDir || components.size() != 4) | |
| 237 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 238 | |
| 239 *local_file_path = GetDataProvider()->GetTrackLocation(components[1], | |
| 240 components[2], | |
| 241 components[3]); | |
| 242 if (!local_file_path->empty()) | |
| 243 return base::PLATFORM_FILE_OK; | |
| 244 | |
| 245 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 246 } | |
| 247 | |
| 248 void ItunesFileUtil::GetFileInfoWithFreshDataProvider( | |
| 249 fileapi::FileSystemOperationContext* context, | |
| 250 const fileapi::FileSystemURL& url, | |
| 251 const GetFileInfoCallback& callback) { | |
| 252 NativeMediaFileUtil::GetFileInfoOnTaskRunnerThread(context, url, callback); | |
| 253 } | |
| 254 | |
| 255 void ItunesFileUtil::ReadDirectoryWithFreshDataProvider( | |
| 256 fileapi::FileSystemOperationContext* context, | |
| 257 const fileapi::FileSystemURL& url, | |
| 258 const ReadDirectoryCallback& callback) { | |
| 259 NativeMediaFileUtil::ReadDirectoryOnTaskRunnerThread(context, url, callback); | |
| 260 } | |
| 261 | |
| 262 ITunesDataProvider* ItunesFileUtil::GetDataProvider() { | |
| 263 if (!imported_registry_) | |
| 264 imported_registry_ = chrome::ImportedMediaGalleryRegistry::GetInstance(); | |
| 265 return imported_registry_->ITunesDataProvider(); | |
| 46 } | 266 } |
| 47 | 267 |
| 48 } // namespace itunes | 268 } // namespace itunes |
| OLD | NEW |