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