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/browser/media_galleries/fileapi/iapps_finder_impl.h" 6 7#include <string> 8 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" 19 20namespace iapps { 21 22namespace { 23 24// Try to read the iTunes preferences file from the default location and return 25// its contents if found. 26std::string GetPrefFileData() { 27 std::string xml_pref_data; 28 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); 35 } 36 return xml_pref_data; 37} 38 39// Check the default location for a correctly named file. 40void TryDefaultLocation(const iapps::IAppsFinderCallback& callback) { 41 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); 42 43 base::FilePath music_dir; 44 if (!PathService::Get(chrome::DIR_USER_MUSIC, &music_dir)) { 45 callback.Run(std::string()); 46 return; 47 } 48 base::FilePath library_file = 49 music_dir.AppendASCII("iTunes").AppendASCII("iTunes Music Library.xml"); 50 51 if (!base::PathExists(library_file)) { 52 callback.Run(std::string()); 53 return; 54 } 55 callback.Run(library_file.AsUTF8Unsafe()); 56} 57 58// Check the location that parsing the preferences XML file found. 59void FinishedParsingPrefXML(const iapps::IAppsFinderCallback& callback, 60 const base::FilePath& library_file) { 61 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); 62 63 if (library_file.empty() || !base::PathExists(library_file)) { 64 TryDefaultLocation(callback); 65 return; 66 } 67 callback.Run(library_file.AsUTF8Unsafe()); 68} 69 70void FindITunesLibraryOnFileThread(const iapps::IAppsFinderCallback& callback) { 71 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); 72 73 std::string xml_pref_data = GetPrefFileData(); 74 if (xml_pref_data.empty()) { 75 TryDefaultLocation(callback); 76 return; 77 } 78 79 scoped_refptr<itunes::SafeITunesPrefParserWin> parser = 80 new itunes::SafeITunesPrefParserWin( 81 xml_pref_data, 82 base::Bind(&FinishedParsingPrefXML, callback)); 83 parser->Start(); 84} 85 86} // namespace 87 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. 91void FindITunesLibrary(const IAppsFinderCallback& callback) { 92 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 93 FindIAppsOnFileThread(storage_monitor::StorageInfo::ITUNES, 94 base::Bind(FindITunesLibraryOnFileThread), callback); 95} 96 97} // namespace iapps 98