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.
5 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h"
8 #include "base/file_util.h"
9 #include "base/logging.h"
10 #include "chrome/browser/media_galleries/fileapi/iphoto_data_provider.h"
11 #include "chrome/browser/media_galleries/fileapi/itunes_data_provider.h"
12 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h"
13 #include "chrome/browser/media_galleries/fileapi/picasa_data_provider.h"
14 #include "chrome/common/extensions/extension_constants.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "webkit/browser/fileapi/external_mount_points.h"
17 #include "webkit/common/fileapi/file_system_mount_option.h"
20 using fileapi::ExternalMountPoints
;
24 static base::LazyInstance
<ImportedMediaGalleryRegistry
>::Leaky
25 g_imported_media_gallery_registry
= LAZY_INSTANCE_INITIALIZER
;
30 ImportedMediaGalleryRegistry
* ImportedMediaGalleryRegistry::GetInstance() {
31 return g_imported_media_gallery_registry
.Pointer();
34 void ImportedMediaGalleryRegistry::Initialize() {
35 base::ThreadRestrictions::AssertIOAllowed();
36 if (imported_root_
.empty()) {
37 if (!base::CreateTemporaryFile(&imported_root_
))
38 imported_root_
= base::FilePath();
39 // TODO(vandebo) Setting the permissions of |imported_root_| in CPSP to
40 // zero would be an extra step to ensure permissions are correctly
45 bool ImportedMediaGalleryRegistry::RegisterPicasaFilesystemOnUIThread(
46 const std::string
& fs_name
, const base::FilePath
& database_path
) {
47 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
48 DCHECK(!fs_name
.empty());
49 DCHECK(!database_path
.empty());
53 #if defined(OS_WIN) || defined(OS_MACOSX)
54 base::FilePath root
= ImportedRoot();
57 result
= ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
58 fs_name
, fileapi::kFileSystemTypePicasa
, fileapi::FileSystemMountOption(),
59 root
.AppendASCII("picasa"));
63 picasa_fs_names_
.insert(fs_name
);
65 if (picasa_fs_names_
.size() == 1) {
66 MediaFileSystemBackend::MediaTaskRunner()->PostTask(
68 Bind(&ImportedMediaGalleryRegistry::RegisterPicasaFileSystem
,
69 base::Unretained(this), database_path
));
71 picasa_database_path_
= database_path
;
73 DCHECK_EQ(picasa_database_path_
.value(), database_path
.value());
76 #endif // defined(OS_WIN) || defined(OS_MACOSX)
81 bool ImportedMediaGalleryRegistry::RegisterITunesFilesystemOnUIThread(
82 const std::string
& fs_name
, const base::FilePath
& library_xml_path
) {
83 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
84 DCHECK(!library_xml_path
.empty());
88 #if defined(OS_WIN) || defined(OS_MACOSX)
89 base::FilePath root
= ImportedRoot();
92 result
= ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
93 fs_name
, fileapi::kFileSystemTypeItunes
, fileapi::FileSystemMountOption(),
94 root
.AppendASCII("itunes"));
98 itunes_fs_names_
.insert(fs_name
);
100 if (itunes_fs_names_
.size() == 1) {
101 MediaFileSystemBackend::MediaTaskRunner()->PostTask(
103 Bind(&ImportedMediaGalleryRegistry::RegisterITunesFileSystem
,
104 base::Unretained(this), library_xml_path
));
106 itunes_xml_library_path_
= library_xml_path
;
108 DCHECK_EQ(itunes_xml_library_path_
.value(), library_xml_path
.value());
111 #endif // defined(OS_WIN) || defined(OS_MACOSX)
116 bool ImportedMediaGalleryRegistry::RegisterIPhotoFilesystemOnUIThread(
117 const std::string
& fs_name
, const base::FilePath
& library_xml_path
) {
118 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
119 DCHECK(!library_xml_path
.empty());
123 // TODO(gbillock): Investigate how to refactor this to reduce duplicated
125 #if defined(OS_MACOSX)
126 base::FilePath root
= ImportedRoot();
129 result
= ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
130 fs_name
, fileapi::kFileSystemTypeIphoto
, fileapi::FileSystemMountOption(),
131 root
.AppendASCII("iphoto"));
135 iphoto_fs_names_
.insert(fs_name
);
137 if (iphoto_fs_names_
.size() == 1) {
138 MediaFileSystemBackend::MediaTaskRunner()->PostTask(
140 Bind(&ImportedMediaGalleryRegistry::RegisterIPhotoFileSystem
,
141 base::Unretained(this), library_xml_path
));
143 iphoto_xml_library_path_
= library_xml_path
;
145 DCHECK_EQ(iphoto_xml_library_path_
.value(), library_xml_path
.value());
148 #endif // defined(OS_MACOSX)
153 bool ImportedMediaGalleryRegistry::RevokeImportedFilesystemOnUIThread(
154 const std::string
& fs_name
) {
155 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
157 #if defined(OS_WIN) || defined(OS_MACOSX)
158 if (picasa_fs_names_
.erase(fs_name
)) {
159 if (picasa_fs_names_
.empty()) {
160 MediaFileSystemBackend::MediaTaskRunner()->PostTask(
162 Bind(&ImportedMediaGalleryRegistry::RevokePicasaFileSystem
,
163 base::Unretained(this)));
165 return ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(fs_name
);
168 if (itunes_fs_names_
.erase(fs_name
)) {
169 if (itunes_fs_names_
.empty()) {
170 MediaFileSystemBackend::MediaTaskRunner()->PostTask(
172 Bind(&ImportedMediaGalleryRegistry::RevokeITunesFileSystem
,
173 base::Unretained(this)));
175 return ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(fs_name
);
177 #endif // defined(OS_WIN) || defined(OS_MACOSX)
179 #if defined(OS_MACOSX)
180 if (iphoto_fs_names_
.erase(fs_name
)) {
181 if (iphoto_fs_names_
.empty()) {
182 MediaFileSystemBackend::MediaTaskRunner()->PostTask(
184 Bind(&ImportedMediaGalleryRegistry::RevokeIPhotoFileSystem
,
185 base::Unretained(this)));
187 return ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(fs_name
);
189 #endif // defined(OS_MACOSX)
194 base::FilePath
ImportedMediaGalleryRegistry::ImportedRoot() {
195 DCHECK(!imported_root_
.empty());
196 return imported_root_
;
199 #if defined(OS_WIN) || defined(OS_MACOSX)
201 picasa::PicasaDataProvider
*
202 ImportedMediaGalleryRegistry::PicasaDataProvider() {
203 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
204 DCHECK(GetInstance()->picasa_data_provider_
);
205 return GetInstance()->picasa_data_provider_
.get();
209 itunes::ITunesDataProvider
*
210 ImportedMediaGalleryRegistry::ITunesDataProvider() {
211 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
212 DCHECK(GetInstance()->itunes_data_provider_
);
213 return GetInstance()->itunes_data_provider_
.get();
215 #endif // defined(OS_WIN) || defined(OS_MACOSX)
217 #if defined(OS_MACOSX)
219 iphoto::IPhotoDataProvider
*
220 ImportedMediaGalleryRegistry::IPhotoDataProvider() {
221 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
222 DCHECK(GetInstance()->iphoto_data_provider_
);
223 return GetInstance()->iphoto_data_provider_
.get();
225 #endif // defined(OS_MACOSX)
227 ImportedMediaGalleryRegistry::ImportedMediaGalleryRegistry() {}
229 ImportedMediaGalleryRegistry::~ImportedMediaGalleryRegistry() {
230 if (!imported_root_
.empty())
231 base::DeleteFile(imported_root_
, false);
232 #if defined(OS_WIN) || defined(OS_MACOSX)
233 DCHECK_EQ(0U, picasa_fs_names_
.size());
234 DCHECK_EQ(0U, itunes_fs_names_
.size());
235 #endif // defined(OS_WIN) || defined(OS_MACOSX)
236 #if defined(OS_MACOSX)
237 DCHECK_EQ(0U, iphoto_fs_names_
.size());
238 #endif // defined(OS_MACOSX)
241 #if defined(OS_WIN) || defined(OS_MACOSX)
242 void ImportedMediaGalleryRegistry::RegisterPicasaFileSystem(
243 const base::FilePath
& database_path
) {
244 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
245 DCHECK(!picasa_data_provider_
);
246 picasa_data_provider_
.reset(new picasa::PicasaDataProvider(database_path
));
249 void ImportedMediaGalleryRegistry::RevokePicasaFileSystem() {
250 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
251 DCHECK(picasa_data_provider_
);
252 picasa_data_provider_
.reset();
255 void ImportedMediaGalleryRegistry::RegisterITunesFileSystem(
256 const base::FilePath
& xml_library_path
) {
257 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
258 DCHECK(!itunes_data_provider_
);
259 itunes_data_provider_
.reset(new itunes::ITunesDataProvider(xml_library_path
));
262 void ImportedMediaGalleryRegistry::RevokeITunesFileSystem() {
263 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
264 DCHECK(itunes_data_provider_
);
265 itunes_data_provider_
.reset();
267 #endif // defined(OS_WIN) || defined(OS_MACOSX)
269 #if defined(OS_MACOSX)
270 void ImportedMediaGalleryRegistry::RegisterIPhotoFileSystem(
271 const base::FilePath
& xml_library_path
) {
272 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
273 DCHECK(!iphoto_data_provider_
);
274 iphoto_data_provider_
.reset(new iphoto::IPhotoDataProvider(xml_library_path
));
277 void ImportedMediaGalleryRegistry::RevokeIPhotoFileSystem() {
278 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
279 DCHECK(iphoto_data_provider_
);
280 iphoto_data_provider_
.reset();
282 #endif // defined(OS_MACOSX)