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_cftyperef.h" |
10 #import "base/mac/scoped_nsobject.h" | 11 #import "base/mac/scoped_nsobject.h" |
11 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
12 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "chrome/browser/policy/preferences_mac.h" |
13 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
14 | 16 |
15 using base::mac::CFCast; | 17 using base::mac::CFCast; |
16 using base::mac::CFToNSCast; | 18 using base::mac::CFToNSCast; |
17 | 19 |
| 20 namespace { |
| 21 |
| 22 static MacPreferences* g_mac_preferences = NULL; |
| 23 |
| 24 NSArray* GetItunesRecentDatabasePaths() { |
| 25 scoped_ptr<MacPreferences> real_preferences; |
| 26 MacPreferences* prefs = g_mac_preferences; |
| 27 if (!prefs) { |
| 28 real_preferences.reset(new MacPreferences()); |
| 29 prefs = real_preferences.get(); |
| 30 } |
| 31 CFStringRef iapp_id = CFSTR("com.apple.iApps"); |
| 32 base::ScopedCFTypeRef<CFString> recent_database_path_key( |
| 33 CFStringCreateWithCStringNoCopy(NULL, kITunesRecentDatabasePathsKey, |
| 34 kCFStringEncodingASCII, NULL)); |
| 35 return CFToNSCast(CFCast<CFArrayRef>(prefs->CopyAppValue( |
| 36 recent_database_path_key.get(), iapp_id))); |
| 37 } |
| 38 |
| 39 } // namespace |
| 40 |
18 namespace itunes { | 41 namespace itunes { |
19 | 42 |
| 43 const char kITunesRecentDatabasePathsKey[] = "iTunesRecentDatabasePaths"; |
| 44 |
20 ITunesFinderMac::ITunesFinderMac(const ITunesFinderCallback& callback) | 45 ITunesFinderMac::ITunesFinderMac(const ITunesFinderCallback& callback) |
21 : ITunesFinder(callback) { | 46 : ITunesFinder(callback) { |
22 } | 47 } |
23 | 48 |
24 ITunesFinderMac::~ITunesFinderMac() {} | 49 ITunesFinderMac::~ITunesFinderMac() {} |
25 | 50 |
| 51 // static |
| 52 void ITunesFinderMac::SetMacPreferencesForTesting(MacPreferences* preferences) { |
| 53 g_mac_preferences = preferences; |
| 54 } |
| 55 |
26 void ITunesFinderMac::FindITunesLibraryOnFileThread() { | 56 void ITunesFinderMac::FindITunesLibraryOnFileThread() { |
27 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 57 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
28 | 58 |
29 CFStringRef iapp_id = CFSTR("com.apple.iApps"); | 59 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) { | 60 if (!plist) { |
34 PostResultToUIThread(std::string()); | 61 PostResultToUIThread(std::string()); |
35 return; | 62 return; |
36 } | 63 } |
37 | 64 |
38 // Find the most recently used iTunes XML database from the list of database | 65 // 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. | 66 // paths. Most of the time |plist| has a size of 1. |
40 base::Time most_recent_db_time; | 67 base::Time most_recent_db_time; |
41 base::FilePath most_recent_db_path; | 68 base::FilePath most_recent_db_path; |
42 for (NSString* path_ns in plist.get()) { | 69 for (NSString* path_ns in plist.get()) { |
43 NSString* expanded_path_ns = [path_ns stringByExpandingTildeInPath]; | 70 NSString* expanded_path_ns = [path_ns stringByExpandingTildeInPath]; |
44 base::FilePath db_path(base::mac::NSStringToFilePath(expanded_path_ns)); | 71 base::FilePath db_path(base::mac::NSStringToFilePath(expanded_path_ns)); |
45 | 72 |
46 base::PlatformFileInfo file_info; | 73 base::PlatformFileInfo file_info; |
47 if (!file_util::GetFileInfo(db_path, &file_info)) | 74 if (!file_util::GetFileInfo(db_path, &file_info)) |
48 continue; | 75 continue; |
49 | 76 |
50 // In case of two databases with the same modified time, tie breaker goes | 77 // In case of two databases with the same modified time, tie breaker goes |
51 // to the first one on the list. | 78 // to the first one on the list. |
52 if (file_info.last_modified <= most_recent_db_time) | 79 if (file_info.last_modified <= most_recent_db_time) |
53 continue; | 80 continue; |
54 | 81 |
55 most_recent_db_time = file_info.last_modified; | 82 most_recent_db_time = file_info.last_modified; |
56 most_recent_db_path = db_path; | 83 most_recent_db_path = db_path; |
57 } | 84 } |
58 PostResultToUIThread(most_recent_db_path.value()); | 85 PostResultToUIThread(most_recent_db_path.value()); |
59 } | 86 } |
60 | 87 |
61 } // namespace itunes | 88 } // namespace itunes |
OLD | NEW |