1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/media_galleries/imported_media_gallery_registry.h"
#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "chrome/browser/media_galleries/fileapi/iphoto_data_provider.h"
#include "chrome/browser/media_galleries/fileapi/itunes_data_provider.h"
#include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h"
#include "chrome/browser/media_galleries/fileapi/picasa_data_provider.h"
#include "chrome/common/extensions/extension_constants.h"
#include "content/public/browser/browser_thread.h"
#include "storage/browser/fileapi/external_mount_points.h"
#include "storage/common/fileapi/file_system_mount_option.h"
using base::Bind;
using storage::ExternalMountPoints;
namespace {
static base::LazyInstance<ImportedMediaGalleryRegistry>::Leaky
g_imported_media_gallery_registry = LAZY_INSTANCE_INITIALIZER;
} // namespace
// static
ImportedMediaGalleryRegistry* ImportedMediaGalleryRegistry::GetInstance() {
return g_imported_media_gallery_registry.Pointer();
}
void ImportedMediaGalleryRegistry::Initialize() {
base::ThreadRestrictions::AssertIOAllowed();
if (imported_root_.empty()) {
if (!base::CreateTemporaryFile(&imported_root_))
imported_root_ = base::FilePath();
// TODO(vandebo) Setting the permissions of |imported_root_| in CPSP to
// zero would be an extra step to ensure permissions are correctly
// enforced.
}
}
bool ImportedMediaGalleryRegistry::RegisterPicasaFilesystemOnUIThread(
const std::string& fs_name, const base::FilePath& database_path) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(!fs_name.empty());
DCHECK(!database_path.empty());
bool result = false;
#if defined(OS_WIN) || defined(OS_MACOSX)
base::FilePath root = ImportedRoot();
if (root.empty())
return false;
result = ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
fs_name,
storage::kFileSystemTypePicasa,
storage::FileSystemMountOption(),
root.AppendASCII("picasa"));
if (!result)
return result;
picasa_fs_names_.insert(fs_name);
if (picasa_fs_names_.size() == 1) {
MediaFileSystemBackend::MediaTaskRunner()->PostTask(
FROM_HERE,
Bind(&ImportedMediaGalleryRegistry::RegisterPicasaFileSystem,
base::Unretained(this), database_path));
#ifndef NDEBUG
picasa_database_path_ = database_path;
} else {
DCHECK_EQ(picasa_database_path_.value(), database_path.value());
#endif
}
#endif // defined(OS_WIN) || defined(OS_MACOSX)
return result;
}
bool ImportedMediaGalleryRegistry::RegisterITunesFilesystemOnUIThread(
const std::string& fs_name, const base::FilePath& library_xml_path) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(!library_xml_path.empty());
bool result = false;
#if defined(OS_WIN) || defined(OS_MACOSX)
base::FilePath root = ImportedRoot();
if (root.empty())
return false;
result = ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
fs_name,
storage::kFileSystemTypeItunes,
storage::FileSystemMountOption(),
root.AppendASCII("itunes"));
if (!result)
return result;
itunes_fs_names_.insert(fs_name);
if (itunes_fs_names_.size() == 1) {
MediaFileSystemBackend::MediaTaskRunner()->PostTask(
FROM_HERE,
Bind(&ImportedMediaGalleryRegistry::RegisterITunesFileSystem,
base::Unretained(this), library_xml_path));
#ifndef NDEBUG
itunes_xml_library_path_ = library_xml_path;
} else {
DCHECK_EQ(itunes_xml_library_path_.value(), library_xml_path.value());
#endif
}
#endif // defined(OS_WIN) || defined(OS_MACOSX)
return result;
}
bool ImportedMediaGalleryRegistry::RegisterIPhotoFilesystemOnUIThread(
const std::string& fs_name, const base::FilePath& library_xml_path) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(!library_xml_path.empty());
bool result = false;
// TODO(gbillock): Investigate how to refactor this to reduce duplicated
// code.
#if defined(OS_MACOSX)
base::FilePath root = ImportedRoot();
if (root.empty())
return false;
result = ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
fs_name,
storage::kFileSystemTypeIphoto,
storage::FileSystemMountOption(),
root.AppendASCII("iphoto"));
if (!result)
return result;
iphoto_fs_names_.insert(fs_name);
if (iphoto_fs_names_.size() == 1) {
MediaFileSystemBackend::MediaTaskRunner()->PostTask(
FROM_HERE,
Bind(&ImportedMediaGalleryRegistry::RegisterIPhotoFileSystem,
base::Unretained(this), library_xml_path));
#ifndef NDEBUG
iphoto_xml_library_path_ = library_xml_path;
} else {
DCHECK_EQ(iphoto_xml_library_path_.value(), library_xml_path.value());
#endif
}
#endif // defined(OS_MACOSX)
return result;
}
bool ImportedMediaGalleryRegistry::RevokeImportedFilesystemOnUIThread(
const std::string& fs_name) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
#if defined(OS_WIN) || defined(OS_MACOSX)
if (picasa_fs_names_.erase(fs_name)) {
if (picasa_fs_names_.empty()) {
MediaFileSystemBackend::MediaTaskRunner()->PostTask(
FROM_HERE,
Bind(&ImportedMediaGalleryRegistry::RevokePicasaFileSystem,
base::Unretained(this)));
}
return ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(fs_name);
}
if (itunes_fs_names_.erase(fs_name)) {
if (itunes_fs_names_.empty()) {
MediaFileSystemBackend::MediaTaskRunner()->PostTask(
FROM_HERE,
Bind(&ImportedMediaGalleryRegistry::RevokeITunesFileSystem,
base::Unretained(this)));
}
return ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(fs_name);
}
#endif // defined(OS_WIN) || defined(OS_MACOSX)
#if defined(OS_MACOSX)
if (iphoto_fs_names_.erase(fs_name)) {
if (iphoto_fs_names_.empty()) {
MediaFileSystemBackend::MediaTaskRunner()->PostTask(
FROM_HERE,
Bind(&ImportedMediaGalleryRegistry::RevokeIPhotoFileSystem,
base::Unretained(this)));
}
return ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(fs_name);
}
#endif // defined(OS_MACOSX)
return false;
}
base::FilePath ImportedMediaGalleryRegistry::ImportedRoot() {
DCHECK(!imported_root_.empty());
return imported_root_;
}
#if defined(OS_WIN) || defined(OS_MACOSX)
// static
picasa::PicasaDataProvider*
ImportedMediaGalleryRegistry::PicasaDataProvider() {
DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
DCHECK(GetInstance()->picasa_data_provider_);
return GetInstance()->picasa_data_provider_.get();
}
// static
itunes::ITunesDataProvider*
ImportedMediaGalleryRegistry::ITunesDataProvider() {
DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
DCHECK(GetInstance()->itunes_data_provider_);
return GetInstance()->itunes_data_provider_.get();
}
#endif // defined(OS_WIN) || defined(OS_MACOSX)
#if defined(OS_MACOSX)
// static
iphoto::IPhotoDataProvider*
ImportedMediaGalleryRegistry::IPhotoDataProvider() {
DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
DCHECK(GetInstance()->iphoto_data_provider_);
return GetInstance()->iphoto_data_provider_.get();
}
#endif // defined(OS_MACOSX)
ImportedMediaGalleryRegistry::ImportedMediaGalleryRegistry() {}
ImportedMediaGalleryRegistry::~ImportedMediaGalleryRegistry() {
if (!imported_root_.empty())
base::DeleteFile(imported_root_, false);
#if defined(OS_WIN) || defined(OS_MACOSX)
DCHECK_EQ(0U, picasa_fs_names_.size());
DCHECK_EQ(0U, itunes_fs_names_.size());
#endif // defined(OS_WIN) || defined(OS_MACOSX)
#if defined(OS_MACOSX)
DCHECK_EQ(0U, iphoto_fs_names_.size());
#endif // defined(OS_MACOSX)
}
#if defined(OS_WIN) || defined(OS_MACOSX)
void ImportedMediaGalleryRegistry::RegisterPicasaFileSystem(
const base::FilePath& database_path) {
DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
DCHECK(!picasa_data_provider_);
picasa_data_provider_.reset(new picasa::PicasaDataProvider(database_path));
}
void ImportedMediaGalleryRegistry::RevokePicasaFileSystem() {
DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
DCHECK(picasa_data_provider_);
picasa_data_provider_.reset();
}
void ImportedMediaGalleryRegistry::RegisterITunesFileSystem(
const base::FilePath& xml_library_path) {
DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
DCHECK(!itunes_data_provider_);
itunes_data_provider_.reset(new itunes::ITunesDataProvider(xml_library_path));
}
void ImportedMediaGalleryRegistry::RevokeITunesFileSystem() {
DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
DCHECK(itunes_data_provider_);
itunes_data_provider_.reset();
}
#endif // defined(OS_WIN) || defined(OS_MACOSX)
#if defined(OS_MACOSX)
void ImportedMediaGalleryRegistry::RegisterIPhotoFileSystem(
const base::FilePath& xml_library_path) {
DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
DCHECK(!iphoto_data_provider_);
iphoto_data_provider_.reset(new iphoto::IPhotoDataProvider(xml_library_path));
}
void ImportedMediaGalleryRegistry::RevokeIPhotoFileSystem() {
DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
DCHECK(iphoto_data_provider_);
iphoto_data_provider_.reset();
}
#endif // defined(OS_MACOSX)
|