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
3// found in the LICENSE file.
4
5#include "chrome/utility/media_galleries/itunes_pref_parser_win.h"
6
7#include "base/base64.h"
8#include "base/strings/string_util.h"
9#include "chrome/utility/media_galleries/iapps_xml_utils.h"
10#include "third_party/libxml/chromium/libxml_utils.h"
11
12namespace itunes {
13
14base::FilePath::StringType FindLibraryLocationInPrefXml(
15    const std::string& pref_xml_data) {
16  XmlReader reader;
17  base::FilePath::StringType result;
18
19  if (!reader.Load(pref_xml_data))
20    return result;
21
22  // Find the plist node and then search within that tag.
23  if (!iapps::SeekToNodeAtCurrentDepth(&reader, "plist"))
24    return result;
25  if (!reader.Read())
26    return result;
27
28  if (!iapps::SeekToNodeAtCurrentDepth(&reader, "dict"))
29    return result;
30
31  if (!iapps::SeekInDict(&reader, "User Preferences"))
32    return result;
33
34  if (!iapps::SeekToNodeAtCurrentDepth(&reader, "dict"))
35    return result;
36
37  if (!iapps::SeekInDict(&reader, "iTunes Library XML Location:1"))
38    return result;
39
40  if (!iapps::SeekToNodeAtCurrentDepth(&reader, "data"))
41    return result;
42
43  std::string pref_value;
44  if (!reader.ReadElementContent(&pref_value))
45    return result;
46  // The data is a base64 encoded wchar_t*. Because Base64Decode uses
47  // std::strings, the result has to be casted to a wchar_t*.
48  std::string data;
49  if (!base::Base64Decode(base::CollapseWhitespaceASCII(pref_value, true),
50                          &data))
51    return result;
52  return base::FilePath::StringType(
53      reinterpret_cast<const wchar_t*>(data.data()), data.size() / 2);
54}
55
56}  // namespace itunes
57