Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/media_galleries/fileapi/itunes_finder_mac.h" | 5 #include "chrome/browser/media_galleries/fileapi/itunes_finder_mac.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #import "base/mac/foundation_util.h" | 9 #import "base/mac/foundation_util.h" |
| 10 #import "base/mac/scoped_nsobject.h" | 10 #import "base/mac/scoped_nsobject.h" |
| 11 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "chrome/browser/policy/preferences_mac.h" | |
| 13 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 14 | 15 |
| 15 using base::mac::CFCast; | 16 using base::mac::CFCast; |
| 16 using base::mac::CFToNSCast; | 17 using base::mac::CFToNSCast; |
| 17 | 18 |
| 19 namespace { | |
| 20 | |
| 21 static MacPreferences* g_mac_preferences = NULL; | |
| 22 | |
| 23 NSArray* GetItunesRecentDatabasePaths() { | |
| 24 scoped_ptr<MacPreferences> real_preferences; | |
| 25 MacPreferences* prefs = g_mac_preferences; | |
| 26 if (!prefs) { | |
| 27 real_preferences.reset(new MacPreferences()); | |
| 28 prefs = real_preferences.get(); | |
| 29 } | |
| 30 CFStringRef iapp_id = CFSTR("com.apple.iApps"); | |
| 31 CFStringRef itunes_db_key = CFSTR("iTunesRecentDatabasePaths"); | |
| 32 return CFToNSCast(CFCast<CFArrayRef>(prefs->CopyAppValue(itunes_db_key, | |
| 33 iapp_id))); | |
| 34 } | |
| 35 | |
| 36 } // namespace | |
| 37 | |
| 18 namespace itunes { | 38 namespace itunes { |
| 19 | 39 |
| 20 ITunesFinderMac::ITunesFinderMac(const ITunesFinderCallback& callback) | 40 ITunesFinderMac::ITunesFinderMac(const ITunesFinderCallback& callback) |
| 21 : ITunesFinder(callback) { | 41 : ITunesFinder(callback) { |
| 22 } | 42 } |
| 23 | 43 |
| 24 ITunesFinderMac::~ITunesFinderMac() {} | 44 ITunesFinderMac::~ITunesFinderMac() {} |
| 25 | 45 |
| 46 // static | |
| 47 void ITunesFinderMac::SetMacPreferencesForTesting(MacPreferences* preferences) { | |
| 48 if (g_mac_preferences) | |
|
Lei Zhang
2013/08/13 20:02:34
delete does this check already.
| |
| 49 delete g_mac_preferences; | |
| 50 g_mac_preferences = preferences; | |
| 51 } | |
| 52 | |
| 26 void ITunesFinderMac::FindITunesLibraryOnFileThread() { | 53 void ITunesFinderMac::FindITunesLibraryOnFileThread() { |
| 27 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 54 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 28 | 55 |
| 29 CFStringRef iapp_id = CFSTR("com.apple.iApps"); | 56 base::scoped_nsobject<NSArray> plist(GetItunesRecentDatabasePaths()); |
| 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) { | 57 if (!plist) { |
| 34 PostResultToUIThread(std::string()); | 58 PostResultToUIThread(std::string()); |
| 35 return; | 59 return; |
| 36 } | 60 } |
| 37 | 61 |
| 38 // Find the most recently used iTunes XML database from the list of database | 62 // 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. | 63 // paths. Most of the time |plist| has a size of 1. |
| 40 base::Time most_recent_db_time; | 64 base::Time most_recent_db_time; |
| 41 base::FilePath most_recent_db_path; | 65 base::FilePath most_recent_db_path; |
| 42 for (NSString* path_ns in plist.get()) { | 66 for (NSString* path_ns in plist.get()) { |
| 43 NSString* expanded_path_ns = [path_ns stringByExpandingTildeInPath]; | 67 NSString* expanded_path_ns = [path_ns stringByExpandingTildeInPath]; |
| 44 base::FilePath db_path(base::mac::NSStringToFilePath(expanded_path_ns)); | 68 base::FilePath db_path(base::mac::NSStringToFilePath(expanded_path_ns)); |
| 45 | 69 |
| 46 base::PlatformFileInfo file_info; | 70 base::PlatformFileInfo file_info; |
| 47 if (!file_util::GetFileInfo(db_path, &file_info)) | 71 if (!file_util::GetFileInfo(db_path, &file_info)) |
| 48 continue; | 72 continue; |
| 49 | 73 |
| 50 // In case of two databases with the same modified time, tie breaker goes | 74 // In case of two databases with the same modified time, tie breaker goes |
| 51 // to the first one on the list. | 75 // to the first one on the list. |
| 52 if (file_info.last_modified <= most_recent_db_time) | 76 if (file_info.last_modified <= most_recent_db_time) |
| 53 continue; | 77 continue; |
| 54 | 78 |
| 55 most_recent_db_time = file_info.last_modified; | 79 most_recent_db_time = file_info.last_modified; |
| 56 most_recent_db_path = db_path; | 80 most_recent_db_path = db_path; |
| 57 } | 81 } |
| 58 PostResultToUIThread(most_recent_db_path.value()); | 82 PostResultToUIThread(most_recent_db_path.value()); |
| 59 } | 83 } |
| 60 | 84 |
| 61 } // namespace itunes | 85 } // namespace itunes |
| OLD | NEW |