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_data_provider.h" | 5 #include "chrome/browser/media_galleries/fileapi/itunes_data_provider.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
12 #include "base/location.h" | 12 #include "base/location.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/platform_file.h" | 14 #include "base/platform_file.h" |
15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
16 #include "base/strings/string_util.h" | |
16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
17 #include "base/threading/thread_restrictions.h" | 18 #include "base/threading/thread_restrictions.h" |
18 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" | 19 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" |
19 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h" | 20 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h" |
20 #include "chrome/common/media_galleries/itunes_library.h" | 21 #include "chrome/common/media_galleries/itunes_library.h" |
21 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
22 #include "third_party/icu/source/common/unicode/locid.h" | 23 #include "third_party/icu/source/common/unicode/locid.h" |
23 #include "webkit/browser/fileapi/native_file_util.h" | 24 #include "webkit/browser/fileapi/native_file_util.h" |
24 | 25 |
25 using chrome::MediaFileSystemBackend; | 26 using chrome::MediaFileSystemBackend; |
26 | 27 |
27 namespace itunes { | 28 namespace itunes { |
28 | 29 |
29 namespace { | 30 namespace { |
30 | 31 |
31 typedef base::Callback<void(scoped_ptr<base::FilePathWatcher> watcher)> | 32 typedef base::Callback<void(scoped_ptr<base::FilePathWatcher> watcher)> |
32 FileWatchStartedCallback; | 33 FileWatchStartedCallback; |
33 | 34 |
35 // Colon and slash are not allowed in filenames, replace them with underscore. | |
36 std::string EscapeBadCharacters(const std::string& input) { | |
37 std::string result; | |
38 ReplaceChars(input, ":/", "_", &result); | |
39 return result; | |
40 } | |
41 | |
34 ITunesDataProvider::Album MakeUniqueTrackNames(const parser::Album& album) { | 42 ITunesDataProvider::Album MakeUniqueTrackNames(const parser::Album& album) { |
35 // TODO(vandebo): It would be nice to ensure that names returned from here | 43 // TODO(vandebo): It would be nice to ensure that names returned from here |
36 // are stable, but aside from persisting every name returned, it's not | 44 // are stable, but aside from persisting every name returned, it's not |
37 // obvious how to do that (without including the track id in every name). | 45 // obvious how to do that (without including the track id in every name). |
38 typedef std::set<const parser::Track*> TrackRefs; | 46 typedef std::set<const parser::Track*> TrackRefs; |
39 typedef std::map<ITunesDataProvider::TrackName, TrackRefs> AlbumInfo; | 47 typedef std::map<ITunesDataProvider::TrackName, TrackRefs> AlbumInfo; |
40 | 48 |
41 ITunesDataProvider::Album result; | 49 ITunesDataProvider::Album result; |
42 AlbumInfo duped_tracks; | 50 AlbumInfo duped_tracks; |
43 | 51 |
44 parser::Album::const_iterator album_it; | 52 parser::Album::const_iterator album_it; |
45 for (album_it = album.begin(); album_it != album.end(); ++album_it) { | 53 for (album_it = album.begin(); album_it != album.end(); ++album_it) { |
46 const parser::Track& track = *album_it; | 54 const parser::Track& track = *album_it; |
47 std::string name = track.location.BaseName().AsUTF8Unsafe(); | 55 std::string name = |
56 EscapeBadCharacters(track.location.BaseName().AsUTF8Unsafe()); | |
48 duped_tracks[name].insert(&track); | 57 duped_tracks[name].insert(&track); |
49 } | 58 } |
50 | 59 |
51 for (AlbumInfo::const_iterator name_it = duped_tracks.begin(); | 60 for (AlbumInfo::const_iterator name_it = duped_tracks.begin(); |
52 name_it != duped_tracks.end(); | 61 name_it != duped_tracks.end(); |
53 ++name_it) { | 62 ++name_it) { |
54 const TrackRefs& track_refs = name_it->second; | 63 const TrackRefs& track_refs = name_it->second; |
55 if (track_refs.size() == 1) { | 64 if (track_refs.size() == 1) { |
56 result[name_it->first] = (*track_refs.begin())->location; | 65 result[name_it->first] = (*track_refs.begin())->location; |
57 } else { | 66 } else { |
58 for (TrackRefs::const_iterator track_it = track_refs.begin(); | 67 for (TrackRefs::const_iterator track_it = track_refs.begin(); |
59 track_it != track_refs.end(); | 68 track_it != track_refs.end(); |
60 ++track_it) { | 69 ++track_it) { |
61 std::string id = | 70 std::string id = |
62 base::StringPrintf(" (%" PRId64 ")", (*track_it)->id); | 71 base::StringPrintf(" (%" PRId64 ")", (*track_it)->id); |
63 base::FilePath unique_name = | 72 std::string track_name = |
64 (*track_it)->location.BaseName().InsertBeforeExtensionASCII(id); | 73 EscapeBadCharacters((*track_it)->location |
Lei Zhang
2013/08/29 23:28:38
Can we use more temp vars so this is easier to rea
vandebo (ex-Chrome)
2013/08/30 03:48:32
Done.
| |
65 result[unique_name.AsUTF8Unsafe()] = (*track_it)->location; | 74 .BaseName() |
75 .InsertBeforeExtensionASCII(id) | |
76 .AsUTF8Unsafe()); | |
77 result[track_name] = (*track_it)->location; | |
66 } | 78 } |
67 } | 79 } |
68 } | 80 } |
69 | 81 |
70 return result; | 82 return result; |
71 } | 83 } |
72 | 84 |
73 // Bounces |path| and |error| to |callback| from the FILE thread to the media | 85 // Bounces |path| and |error| to |callback| from the FILE thread to the media |
74 // task runner. | 86 // task runner. |
75 void OnLibraryChanged(const base::FilePathWatcher::Callback& callback, | 87 void OnLibraryChanged(const base::FilePathWatcher::Callback& callback, |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
343 void ITunesDataProvider::OnLibraryParsed(const ReadyCallback& ready_callback, | 355 void ITunesDataProvider::OnLibraryParsed(const ReadyCallback& ready_callback, |
344 bool result, | 356 bool result, |
345 const parser::Library& library) { | 357 const parser::Library& library) { |
346 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 358 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
347 is_valid_ = result; | 359 is_valid_ = result; |
348 if (is_valid_) { | 360 if (is_valid_) { |
349 library_.clear(); | 361 library_.clear(); |
350 for (parser::Library::const_iterator artist_it = library.begin(); | 362 for (parser::Library::const_iterator artist_it = library.begin(); |
351 artist_it != library.end(); | 363 artist_it != library.end(); |
352 ++artist_it) { | 364 ++artist_it) { |
365 std::string artist_name = EscapeBadCharacters(artist_it->first); | |
353 for (parser::Albums::const_iterator album_it = artist_it->second.begin(); | 366 for (parser::Albums::const_iterator album_it = artist_it->second.begin(); |
354 album_it != artist_it->second.end(); | 367 album_it != artist_it->second.end(); |
355 ++album_it) { | 368 ++album_it) { |
356 library_[artist_it->first][album_it->first] = | 369 std::string album_name = EscapeBadCharacters(album_it->first); |
370 library_[artist_name][album_name] = | |
357 MakeUniqueTrackNames(album_it->second); | 371 MakeUniqueTrackNames(album_it->second); |
358 } | 372 } |
359 } | 373 } |
360 } | 374 } |
361 ready_callback.Run(is_valid_); | 375 ready_callback.Run(is_valid_); |
362 } | 376 } |
363 | 377 |
364 } // namespace itunes | 378 } // namespace itunes |
OLD | NEW |