Home | History | Annotate | Download | only in fileapi
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/media_galleries/fileapi/itunes_finder_win.h"
      6 
      7 #include <string>
      8 
      9 #include "base/base_paths_win.h"
     10 #include "base/file_util.h"
     11 #include "base/files/file_path.h"
     12 #include "base/logging.h"
     13 #include "base/path_service.h"
     14 #include "chrome/browser/media_galleries/fileapi/safe_itunes_pref_parser_win.h"
     15 #include "chrome/common/chrome_paths.h"
     16 #include "content/public/browser/browser_thread.h"
     17 
     18 namespace itunes {
     19 
     20 namespace {
     21 
     22 // Try to read the iTunes preferences file from the default location and return
     23 // its contents if found.
     24 std::string GetPrefFileData() {
     25   std::string xml_pref_data;
     26 
     27   base::FilePath appdata_dir;
     28   if (PathService::Get(base::DIR_APP_DATA, &appdata_dir)) {
     29     base::FilePath pref_file = appdata_dir.AppendASCII("Apple Computer")
     30                                           .AppendASCII("iTunes")
     31                                           .AppendASCII("iTunesPrefs.xml");
     32     file_util::ReadFileToString(pref_file, &xml_pref_data);
     33   }
     34   return xml_pref_data;
     35 }
     36 
     37 }  // namespace
     38 
     39 ITunesFinderWin::ITunesFinderWin(const ITunesFinderCallback& callback)
     40     : ITunesFinder(callback) {
     41 }
     42 
     43 ITunesFinderWin::~ITunesFinderWin() {}
     44 
     45 void ITunesFinderWin::FindITunesLibraryOnFileThread() {
     46   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
     47 
     48   std::string xml_pref_data = GetPrefFileData();
     49   if (xml_pref_data.empty()) {
     50     TryDefaultLocation();
     51     return;
     52   }
     53 
     54   scoped_refptr<SafeITunesPrefParserWin> parser =
     55       new SafeITunesPrefParserWin(
     56           xml_pref_data,
     57           base::Bind(&ITunesFinderWin::FinishedParsingPrefXML,
     58                      base::Unretained(this)));
     59   parser->Start();
     60 }
     61 
     62 void ITunesFinderWin::TryDefaultLocation() {
     63   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
     64 
     65   base::FilePath music_dir;
     66   if (!PathService::Get(chrome::DIR_USER_MUSIC, &music_dir)) {
     67     PostResultToUIThread(std::string());
     68     return;
     69   }
     70   base::FilePath library_file =
     71       music_dir.AppendASCII("iTunes").AppendASCII("iTunes Music Library.xml");
     72 
     73   if (!base::PathExists(library_file)) {
     74     PostResultToUIThread(std::string());
     75     return;
     76   }
     77   PostResultToUIThread(library_file.AsUTF8Unsafe());
     78 }
     79 
     80 void ITunesFinderWin::FinishedParsingPrefXML(
     81     const base::FilePath& library_file) {
     82   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
     83 
     84   if (library_file.empty() || !base::PathExists(library_file)) {
     85     TryDefaultLocation();
     86     return;
     87   }
     88   PostResultToUIThread(library_file.AsUTF8Unsafe());
     89 }
     90 
     91 }  // namespace itunes
     92