1 // Copyright 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/imported_media_gallery_registry.h" 6 7 #include "base/bind.h" 8 #include "base/logging.h" 9 #include "chrome/browser/media_galleries/fileapi/itunes_data_provider.h" 10 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" 11 #include "chrome/browser/media_galleries/fileapi/picasa/picasa_data_provider.h" 12 #include "chrome/common/extensions/extension_constants.h" 13 #include "content/public/browser/browser_thread.h" 14 #include "webkit/browser/fileapi/isolated_context.h" 15 16 using base::Bind; 17 using fileapi::IsolatedContext; 18 19 namespace chrome { 20 21 namespace { 22 23 static base::LazyInstance<ImportedMediaGalleryRegistry>::Leaky 24 g_imported_media_gallery_registry = LAZY_INSTANCE_INITIALIZER; 25 26 } // namespace 27 28 // static 29 ImportedMediaGalleryRegistry* ImportedMediaGalleryRegistry::GetInstance() { 30 return g_imported_media_gallery_registry.Pointer(); 31 } 32 33 std::string ImportedMediaGalleryRegistry::RegisterPicasaFilesystemOnUIThread( 34 const base::FilePath& database_path) { 35 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 36 DCHECK(!database_path.empty()); 37 38 std::string fsid; 39 40 #if defined(OS_WIN) || defined(OS_MACOSX) 41 fsid = IsolatedContext::GetInstance()->RegisterFileSystemForVirtualPath( 42 fileapi::kFileSystemTypePicasa, 43 extension_misc::kMediaFileSystemPathPart, 44 base::FilePath()); 45 46 if (fsid.empty()) 47 return fsid; 48 49 picasa_fsids_.insert(fsid); 50 51 if (picasa_fsids_.size() == 1) { 52 MediaFileSystemBackend::MediaTaskRunner()->PostTask( 53 FROM_HERE, 54 Bind(&ImportedMediaGalleryRegistry::RegisterPicasaFileSystem, 55 base::Unretained(this), database_path)); 56 #ifndef NDEBUG 57 picasa_database_path_ = database_path; 58 } else { 59 DCHECK_EQ(picasa_database_path_.value(), database_path.value()); 60 #endif 61 } 62 #endif // defined(OS_WIN) || defined(OS_MACOSX) 63 64 return fsid; 65 } 66 67 std::string ImportedMediaGalleryRegistry::RegisterITunesFilesystemOnUIThread( 68 const base::FilePath& library_xml_path) { 69 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 70 DCHECK(!library_xml_path.empty()); 71 72 std::string fsid; 73 74 #if defined(OS_WIN) || defined(OS_MACOSX) 75 fsid = IsolatedContext::GetInstance()->RegisterFileSystemForVirtualPath( 76 fileapi::kFileSystemTypeItunes, 77 extension_misc::kMediaFileSystemPathPart, 78 base::FilePath()); 79 80 if (fsid.empty()) 81 return fsid; 82 83 itunes_fsids_.insert(fsid); 84 85 if (itunes_fsids_.size() == 1) { 86 MediaFileSystemBackend::MediaTaskRunner()->PostTask( 87 FROM_HERE, 88 Bind(&ImportedMediaGalleryRegistry::RegisterITunesFileSystem, 89 base::Unretained(this), library_xml_path)); 90 #ifndef NDEBUG 91 itunes_xml_library_path_ = library_xml_path; 92 } else { 93 DCHECK_EQ(itunes_xml_library_path_.value(), library_xml_path.value()); 94 #endif 95 } 96 #endif // defined(OS_WIN) || defined(OS_MACOSX) 97 98 return fsid; 99 } 100 101 bool ImportedMediaGalleryRegistry::RevokeImportedFilesystemOnUIThread( 102 const std::string& fsid) { 103 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 104 105 #if defined(OS_WIN) || defined(OS_MACOSX) 106 if (picasa_fsids_.erase(fsid)) { 107 if (picasa_fsids_.empty()) { 108 MediaFileSystemBackend::MediaTaskRunner()->PostTask( 109 FROM_HERE, 110 Bind(&ImportedMediaGalleryRegistry::RevokePicasaFileSystem, 111 base::Unretained(this))); 112 } 113 return IsolatedContext::GetInstance()->RevokeFileSystem(fsid); 114 } 115 116 if (itunes_fsids_.erase(fsid)) { 117 if (itunes_fsids_.empty()) { 118 MediaFileSystemBackend::MediaTaskRunner()->PostTask( 119 FROM_HERE, 120 Bind(&ImportedMediaGalleryRegistry::RevokeITunesFileSystem, 121 base::Unretained(this))); 122 } 123 return IsolatedContext::GetInstance()->RevokeFileSystem(fsid); 124 } 125 #endif // defined(OS_WIN) || defined(OS_MACOSX) 126 127 return false; 128 } 129 130 #if defined(OS_WIN) || defined(OS_MACOSX) 131 // static 132 picasa::PicasaDataProvider* 133 ImportedMediaGalleryRegistry::PicasaDataProvider() { 134 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); 135 DCHECK(GetInstance()->picasa_data_provider_); 136 return GetInstance()->picasa_data_provider_.get(); 137 } 138 139 // static 140 itunes::ITunesDataProvider* 141 ImportedMediaGalleryRegistry::ITunesDataProvider() { 142 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); 143 DCHECK(GetInstance()->itunes_data_provider_); 144 return GetInstance()->itunes_data_provider_.get(); 145 } 146 #endif // defined(OS_WIN) || defined(OS_MACOSX) 147 148 ImportedMediaGalleryRegistry::ImportedMediaGalleryRegistry() {} 149 150 ImportedMediaGalleryRegistry::~ImportedMediaGalleryRegistry() { 151 #if defined(OS_WIN) || defined(OS_MACOSX) 152 DCHECK_EQ(0U, picasa_fsids_.size()); 153 DCHECK_EQ(0U, itunes_fsids_.size()); 154 #endif // defined(OS_WIN) || defined(OS_MACOSX) 155 } 156 157 #if defined(OS_WIN) || defined(OS_MACOSX) 158 void ImportedMediaGalleryRegistry::RegisterPicasaFileSystem( 159 const base::FilePath& database_path) { 160 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); 161 DCHECK(!picasa_data_provider_); 162 picasa_data_provider_.reset(new picasa::PicasaDataProvider(database_path)); 163 } 164 165 void ImportedMediaGalleryRegistry::RevokePicasaFileSystem() { 166 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); 167 DCHECK(picasa_data_provider_); 168 picasa_data_provider_.reset(); 169 } 170 171 void ImportedMediaGalleryRegistry::RegisterITunesFileSystem( 172 const base::FilePath& xml_library_path) { 173 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); 174 DCHECK(!itunes_data_provider_); 175 itunes_data_provider_.reset(new itunes::ITunesDataProvider(xml_library_path)); 176 } 177 178 void ImportedMediaGalleryRegistry::RevokeITunesFileSystem() { 179 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); 180 DCHECK(itunes_data_provider_); 181 itunes_data_provider_.reset(); 182 } 183 #endif // defined(OS_WIN) || defined(OS_MACOSX) 184 185 } // namespace chrome 186