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/utility/media_galleries/itunes_library_parser.h" | 5 #include "chrome/utility/media_galleries/itunes_library_parser.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "chrome/utility/media_galleries/iapps_xml_utils.h" | 13 #include "chrome/utility/media_galleries/iapps_xml_utils.h" |
14 #include "third_party/libxml/chromium/libxml_utils.h" | 14 #include "third_party/libxml/chromium/libxml_utils.h" |
15 #include "url/gurl.h" | 15 #include "url/gurl.h" |
16 #include "url/url_canon.h" | 16 #include "url/url_canon.h" |
17 #include "url/url_util.h" | 17 #include "url/url_util.h" |
18 | 18 |
19 namespace itunes { | 19 namespace itunes { |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 struct TrackInfo { | 23 struct TrackInfo { |
24 uint64 id; | 24 uint64 id; |
25 base::FilePath location; | 25 base::FilePath location; |
26 std::string artist; | 26 std::string artist; |
27 std::string album; | 27 std::string album; |
28 }; | 28 }; |
29 | 29 |
30 // Walk through a dictionary filling in |result| with track information. Return | 30 class TrackInfoXmlDictReader : public iapps::XmlDictReader { |
31 // true if at least the id and location where found (artist and album may be | 31 public: |
32 // empty). In either case, the cursor is advanced out of the dictionary. | 32 TrackInfoXmlDictReader(XmlReader* reader, TrackInfo* track_info) : |
33 bool GetTrackInfoFromDict(XmlReader* reader, TrackInfo* result) { | 33 iapps::XmlDictReader(reader), track_info_(track_info) {} |
34 DCHECK(result); | |
35 if (reader->NodeName() != "dict") | |
36 return false; | |
37 | 34 |
38 int dict_content_depth = reader->Depth() + 1; | 35 virtual bool ShouldLoop() OVERRIDE { |
39 // Advance past the dict node and into the body of the dictionary. | 36 return !(Found("Track ID") && Found("Location") && |
40 if (!reader->Read()) | 37 Found("Album Artist") && Found("Album")); |
41 return false; | 38 } |
42 | 39 |
43 bool found_id = false; | 40 virtual bool HandleKeyImpl(const std::string& key) OVERRIDE { |
44 bool found_location = false; | 41 if (key == "Track ID") { |
45 bool found_artist = false; | 42 return iapps::ReadInteger(reader_, &track_info_->id); |
46 bool found_album_artist = false; | 43 } else if (key == "Location") { |
47 bool found_album = false; | |
48 while (reader->Depth() >= dict_content_depth && | |
49 !(found_id && found_location && found_album_artist && found_album)) { | |
50 if (!iapps::SeekToNodeAtCurrentDepth(reader, "key")) | |
51 break; | |
52 std::string found_key; | |
53 if (!reader->ReadElementContent(&found_key)) | |
54 break; | |
55 DCHECK_EQ(dict_content_depth, reader->Depth()); | |
56 | |
57 if (found_key == "Track ID") { | |
58 if (found_id) | |
59 break; | |
60 if (!iapps::ReadInteger(reader, &result->id)) | |
61 break; | |
62 found_id = true; | |
63 } else if (found_key == "Location") { | |
64 if (found_location) | |
65 break; | |
66 std::string value; | 44 std::string value; |
67 if (!iapps::ReadString(reader, &value)) | 45 if (!iapps::ReadString(reader_, &value)) |
68 break; | 46 return false; |
69 GURL url(value); | 47 GURL url(value); |
70 if (!url.SchemeIsFile()) | 48 if (!url.SchemeIsFile()) |
71 break; | 49 return false; |
72 url::RawCanonOutputW<1024> decoded_location; | 50 url::RawCanonOutputW<1024> decoded_location; |
73 url::DecodeURLEscapeSequences(url.path().c_str() + 1, // Strip /. | 51 url::DecodeURLEscapeSequences(url.path().c_str() + 1, // Strip /. |
74 url.path().length() - 1, | 52 url.path().length() - 1, |
75 &decoded_location); | 53 &decoded_location); |
76 #if defined(OS_WIN) | 54 #if defined(OS_WIN) |
77 base::string16 location(decoded_location.data(), | 55 base::string16 location(decoded_location.data(), |
78 decoded_location.length()); | 56 decoded_location.length()); |
79 #else | 57 #else |
80 base::string16 location16(decoded_location.data(), | 58 base::string16 location16(decoded_location.data(), |
81 decoded_location.length()); | 59 decoded_location.length()); |
82 std::string location = "/" + base::UTF16ToUTF8(location16); | 60 std::string location = "/" + base::UTF16ToUTF8(location16); |
83 #endif | 61 #endif |
84 result->location = base::FilePath(location); | 62 track_info_->location = base::FilePath(location); |
85 found_location = true; | 63 } else if (key == "Artist") { |
86 } else if (found_key == "Artist") { | 64 if (Found("Album Artist")) |
87 if (found_artist || found_album_artist) | 65 return false; |
88 break; | 66 return iapps::ReadString(reader_, &track_info_->artist); |
89 if (!iapps::ReadString(reader, &result->artist)) | 67 } else if (key == "Album Artist") { |
90 break; | 68 track_info_->artist.clear(); |
91 found_artist = true; | 69 return iapps::ReadString(reader_, &track_info_->artist); |
92 } else if (found_key == "Album Artist") { | 70 } else if (key == "Album") { |
93 if (found_album_artist) | 71 return iapps::ReadString(reader_, &track_info_->album); |
94 break; | 72 } else if (!SkipToNext()) { |
95 result->artist.clear(); | 73 return false; |
96 if (!iapps::ReadString(reader, &result->artist)) | |
97 break; | |
98 found_album_artist = true; | |
99 } else if (found_key == "Album") { | |
100 if (found_album) | |
101 break; | |
102 if (!iapps::ReadString(reader, &result->album)) | |
103 break; | |
104 found_album = true; | |
105 } else { | |
106 if (!iapps::SkipToNextElement(reader)) | |
107 break; | |
108 if (!reader->Next()) | |
109 break; | |
110 } | 74 } |
| 75 return true; |
111 } | 76 } |
112 | 77 |
113 // Seek to the end of the dictionary | 78 virtual bool FinishedOk() OVERRIDE { |
114 while (reader->Depth() >= dict_content_depth) | 79 return Found("Track ID") && Found("Location"); |
115 reader->Next(); | 80 } |
116 | 81 |
117 return found_id && found_location; | 82 private: |
| 83 TrackInfo* track_info_; |
| 84 }; |
| 85 |
| 86 // Walk through a dictionary filling in |result| with track information. Return |
| 87 // true if at least the id and location where found (artist and album may be |
| 88 // empty). In either case, the cursor is advanced out of the dictionary. |
| 89 bool GetTrackInfoFromDict(XmlReader* reader, TrackInfo* result) { |
| 90 DCHECK(result); |
| 91 TrackInfoXmlDictReader dict_reader(reader, result); |
| 92 return dict_reader.Read(); |
118 } | 93 } |
119 | 94 |
120 } // namespace | 95 } // namespace |
121 | 96 |
122 ITunesLibraryParser::ITunesLibraryParser() {} | 97 ITunesLibraryParser::ITunesLibraryParser() {} |
123 ITunesLibraryParser::~ITunesLibraryParser() {} | 98 ITunesLibraryParser::~ITunesLibraryParser() {} |
124 | 99 |
125 bool ITunesLibraryParser::Parse(const std::string& library_xml) { | 100 bool ITunesLibraryParser::Parse(const std::string& library_xml) { |
126 XmlReader reader; | 101 XmlReader reader; |
127 | 102 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 track_found = true; | 155 track_found = true; |
181 } else { | 156 } else { |
182 no_errors = false; | 157 no_errors = false; |
183 } | 158 } |
184 } | 159 } |
185 | 160 |
186 return track_found || no_errors; | 161 return track_found || no_errors; |
187 } | 162 } |
188 | 163 |
189 } // namespace itunes | 164 } // namespace itunes |
OLD | NEW |