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