Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / media_galleries / fileapi / iapps_finder_impl_win.cc
blob168576d8861867769d665f575b7520c7af060d22
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.
5 #include "chrome/browser/media_galleries/fileapi/iapps_finder_impl.h"
7 #include <string>
9 #include "base/base_paths_win.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/logging.h"
13 #include "base/path_service.h"
14 #include "chrome/browser/media_galleries/fileapi/iapps_finder.h"
15 #include "chrome/browser/media_galleries/fileapi/safe_itunes_pref_parser_win.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "components/storage_monitor/storage_info.h"
18 #include "content/public/browser/browser_thread.h"
20 namespace iapps {
22 namespace {
24 // Try to read the iTunes preferences file from the default location and return
25 // its contents if found.
26 std::string GetPrefFileData() {
27 std::string xml_pref_data;
29 base::FilePath appdata_dir;
30 if (PathService::Get(base::DIR_APP_DATA, &appdata_dir)) {
31 base::FilePath pref_file = appdata_dir.AppendASCII("Apple Computer")
32 .AppendASCII("iTunes")
33 .AppendASCII("iTunesPrefs.xml");
34 base::ReadFileToString(pref_file, &xml_pref_data);
36 return xml_pref_data;
39 // Check the default location for a correctly named file.
40 void TryDefaultLocation(const iapps::IAppsFinderCallback& callback) {
41 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
43 base::FilePath music_dir;
44 if (!PathService::Get(chrome::DIR_USER_MUSIC, &music_dir)) {
45 callback.Run(std::string());
46 return;
48 base::FilePath library_file =
49 music_dir.AppendASCII("iTunes").AppendASCII("iTunes Music Library.xml");
51 if (!base::PathExists(library_file)) {
52 callback.Run(std::string());
53 return;
55 callback.Run(library_file.AsUTF8Unsafe());
58 // Check the location that parsing the preferences XML file found.
59 void FinishedParsingPrefXML(const iapps::IAppsFinderCallback& callback,
60 const base::FilePath& library_file) {
61 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
63 if (library_file.empty() || !base::PathExists(library_file)) {
64 TryDefaultLocation(callback);
65 return;
67 callback.Run(library_file.AsUTF8Unsafe());
70 void FindITunesLibraryOnFileThread(const iapps::IAppsFinderCallback& callback) {
71 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
73 std::string xml_pref_data = GetPrefFileData();
74 if (xml_pref_data.empty()) {
75 TryDefaultLocation(callback);
76 return;
79 scoped_refptr<itunes::SafeITunesPrefParserWin> parser =
80 new itunes::SafeITunesPrefParserWin(
81 xml_pref_data,
82 base::Bind(&FinishedParsingPrefXML, callback));
83 parser->Start();
86 } // namespace
88 // The Windows-specific ITunesFinder uses a utility process to parse the
89 // iTunes preferences XML file if it exists. If not or if the parsing fails,
90 // ITunesFinderWin will try a default location as well.
91 void FindITunesLibrary(const IAppsFinderCallback& callback) {
92 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
93 FindIAppsOnFileThread(storage_monitor::StorageInfo::ITUNES,
94 base::Bind(FindITunesLibraryOnFileThread), callback);
97 } // namespace iapps