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.h" 6 7 #include "base/bind.h" 8 #include "base/logging.h" 9 #include "chrome/browser/storage_monitor/storage_info.h" 10 #include "content/public/browser/browser_thread.h" 11 12 #if defined(OS_WIN) 13 #include "chrome/browser/media_galleries/fileapi/itunes_finder_win.h" 14 #elif defined(OS_MACOSX) 15 #include "chrome/browser/media_galleries/fileapi/itunes_finder_mac.h" 16 #endif 17 18 namespace itunes { 19 20 ITunesFinder::~ITunesFinder() {} 21 22 // static 23 void ITunesFinder::FindITunesLibrary(const ITunesFinderCallback& callback) { 24 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 25 DCHECK(!callback.is_null()); 26 27 ITunesFinder* finder = NULL; 28 #if defined(OS_WIN) 29 finder = new ITunesFinderWin(callback); 30 #elif defined(OS_MACOSX) 31 finder = new ITunesFinderMac(callback); 32 #endif 33 if (finder) { 34 content::BrowserThread::PostTask( 35 content::BrowserThread::FILE, 36 FROM_HERE, 37 base::Bind(&ITunesFinder::FindITunesLibraryOnFileThread, 38 base::Unretained(finder))); 39 } else { 40 callback.Run(std::string()); 41 } 42 } 43 44 ITunesFinder::ITunesFinder(const ITunesFinderCallback& callback) 45 : callback_(callback) { 46 } 47 48 void ITunesFinder::PostResultToUIThread(const std::string& unique_id) { 49 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); 50 // The use of base::Owned() below will cause this class to get deleted either 51 // when FinishOnUIThread() finishes or if the PostTask() fails. 52 content::BrowserThread::PostTask( 53 content::BrowserThread::UI, 54 FROM_HERE, 55 base::Bind(&ITunesFinder::FinishOnUIThread, 56 base::Owned(this), 57 unique_id)); 58 } 59 60 void ITunesFinder::FinishOnUIThread(const std::string& unique_id) const { 61 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 62 63 std::string device_id; 64 if (!unique_id.empty()) { 65 device_id = chrome::StorageInfo::MakeDeviceId(chrome::StorageInfo::ITUNES, 66 unique_id); 67 } 68 callback_.Run(device_id); 69 } 70 71 } // namespace itunes 72