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_mac.h" 6 7 #include "base/file_util.h" 8 #include "base/logging.h" 9 #import "base/mac/foundation_util.h" 10 #import "base/mac/scoped_nsobject.h" 11 #include "base/strings/sys_string_conversions.h" 12 #include "base/time/time.h" 13 #include "content/public/browser/browser_thread.h" 14 15 using base::mac::CFCast; 16 using base::mac::CFToNSCast; 17 18 namespace itunes { 19 20 ITunesFinderMac::ITunesFinderMac(const ITunesFinderCallback& callback) 21 : ITunesFinder(callback) { 22 } 23 24 ITunesFinderMac::~ITunesFinderMac() {} 25 26 void ITunesFinderMac::FindITunesLibraryOnFileThread() { 27 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); 28 29 CFStringRef iapp_id = CFSTR("com.apple.iApps"); 30 CFStringRef itunes_db_key = CFSTR("iTunesRecentDatabasePaths"); 31 base::scoped_nsobject<NSArray> plist(CFToNSCast( 32 CFCast<CFArrayRef>(CFPreferencesCopyAppValue(itunes_db_key, iapp_id)))); 33 if (!plist) { 34 PostResultToUIThread(std::string()); 35 return; 36 } 37 38 // Find the most recently used iTunes XML database from the list of database 39 // paths. Most of the time |plist| has a size of 1. 40 base::Time most_recent_db_time; 41 base::FilePath most_recent_db_path; 42 for (NSString* path_ns in plist.get()) { 43 NSString* expanded_path_ns = [path_ns stringByExpandingTildeInPath]; 44 base::FilePath db_path(base::mac::NSStringToFilePath(expanded_path_ns)); 45 46 base::PlatformFileInfo file_info; 47 if (!file_util::GetFileInfo(db_path, &file_info)) 48 continue; 49 50 // In case of two databases with the same modified time, tie breaker goes 51 // to the first one on the list. 52 if (file_info.last_modified <= most_recent_db_time) 53 continue; 54 55 most_recent_db_time = file_info.last_modified; 56 most_recent_db_path = db_path; 57 } 58 PostResultToUIThread(most_recent_db_path.value()); 59 } 60 61 } // namespace itunes 62