| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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_finder_win.h" | 5 #include "chrome/browser/media_galleries/fileapi/itunes_finder_win.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/base_paths_win.h" | 10 #include "base/base_paths_win.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 16 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 17 #include "chrome/browser/media_galleries/fileapi/itunes_xml_util.h" |
| 17 #include "chrome/common/chrome_paths.h" | 18 #include "chrome/common/chrome_paths.h" |
| 18 #include "third_party/libxml/chromium/libxml_utils.h" | 19 #include "third_party/libxml/chromium/libxml_utils.h" |
| 19 | 20 |
| 20 namespace itunes { | 21 namespace itunes { |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 // Traverse |reader| looking for a node named |name| at the current depth | |
| 25 // of |reader|. | |
| 26 bool SeekToNodeAtCurrentDepth(XmlReader* reader, const std::string& name) { | |
| 27 int depth = reader->Depth(); | |
| 28 do { | |
| 29 if (!reader->SkipToElement()) { | |
| 30 // SkipToElement returns false if the current node is an end element, | |
| 31 // try to advance to the next element and then try again. | |
| 32 if (!reader->Read() || !reader->SkipToElement()) | |
| 33 return false; | |
| 34 } | |
| 35 DCHECK_EQ(depth, reader->Depth()); | |
| 36 if (reader->NodeName() == name) | |
| 37 return true; | |
| 38 } while (reader->Next()); | |
| 39 | |
| 40 return false; | |
| 41 } | |
| 42 | |
| 43 // Search within the dict for |key|. | |
| 44 bool SeekInDict(XmlReader* reader, const std::string& key) { | |
| 45 DCHECK_EQ("dict", reader->NodeName()); | |
| 46 | |
| 47 int dict_content_depth = reader->Depth() + 1; | |
| 48 // Advance past the dict node and into the body of the dictionary. | |
| 49 if (!reader->Read()) | |
| 50 return false; | |
| 51 | |
| 52 while (reader->Depth() >= dict_content_depth) { | |
| 53 if (!SeekToNodeAtCurrentDepth(reader, "key")) | |
| 54 return false; | |
| 55 std::string found_key; | |
| 56 if (!reader->ReadElementContent(&found_key)) | |
| 57 return false; | |
| 58 DCHECK_EQ(dict_content_depth, reader->Depth()); | |
| 59 if (found_key == key) | |
| 60 return true; | |
| 61 } | |
| 62 return false; | |
| 63 } | |
| 64 | |
| 65 // Read the iTunes preferences from |pref_file| and then try to extract the | 25 // Read the iTunes preferences from |pref_file| and then try to extract the |
| 66 // library XML location from the XML file. Return it if found. The minimal | 26 // library XML location from the XML file. Return it if found. The minimal |
| 67 // valid snippet of XML is: | 27 // valid snippet of XML is: |
| 68 // | 28 // |
| 69 // <plist> | 29 // <plist> |
| 70 // <dict> | 30 // <dict> |
| 71 // <key>User Preferences</key> | 31 // <key>User Preferences</key> |
| 72 // <dict> | 32 // <dict> |
| 73 // <key>iTunes Library XML Location:1</key> | 33 // <key>iTunes Library XML Location:1</key> |
| 74 // <data>Base64 encoded w string path</data> | 34 // <data>Base64 encoded w string path</data> |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 | 127 |
| 168 if (library_file.empty()) { | 128 if (library_file.empty()) { |
| 169 PostResultToUIThread(std::string()); | 129 PostResultToUIThread(std::string()); |
| 170 return; | 130 return; |
| 171 } | 131 } |
| 172 | 132 |
| 173 PostResultToUIThread(library_file.AsUTF8Unsafe()); | 133 PostResultToUIThread(library_file.AsUTF8Unsafe()); |
| 174 } | 134 } |
| 175 | 135 |
| 176 } // namespace itunes | 136 } // namespace itunes |
| OLD | NEW |