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 bool ImportedMediaGalleryRegistry::RegisterPicasaFilesystemOnUIThread( |
33 const base::FilePath& database_path) { | 35 const std::string& fs_name, const base::FilePath& database_path) { |
34 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 36 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
37 DCHECK(!fs_name.empty()); | |
35 DCHECK(!database_path.empty()); | 38 DCHECK(!database_path.empty()); |
36 | 39 |
37 std::string fsid; | 40 bool result = false; |
38 | 41 |
39 #if defined(OS_WIN) || defined(OS_MACOSX) | 42 #if defined(OS_WIN) || defined(OS_MACOSX) |
40 fsid = IsolatedContext::GetInstance()->RegisterFileSystemForVirtualPath( | 43 base::FilePath root = ImportedRoot(); |
41 fileapi::kFileSystemTypePicasa, | 44 if (root.empty()) |
42 extension_misc::kMediaFileSystemPathPart, | 45 return false; |
43 base::FilePath()); | 46 result = ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( |
47 fs_name, fileapi::kFileSystemTypePicasa, fileapi::FileSystemMountOption(), | |
48 root.AppendASCII("picasa")); | |
49 if (!result) | |
50 return result; | |
44 | 51 |
45 if (fsid.empty()) | 52 picasa_fs_names_.insert(fs_name); |
46 return fsid; | |
47 | 53 |
48 picasa_fsids_.insert(fsid); | 54 if (picasa_fs_names_.size() == 1) { |
49 | |
50 if (picasa_fsids_.size() == 1) { | |
51 MediaFileSystemBackend::MediaTaskRunner()->PostTask( | 55 MediaFileSystemBackend::MediaTaskRunner()->PostTask( |
52 FROM_HERE, | 56 FROM_HERE, |
53 Bind(&ImportedMediaGalleryRegistry::RegisterPicasaFileSystem, | 57 Bind(&ImportedMediaGalleryRegistry::RegisterPicasaFileSystem, |
54 base::Unretained(this), database_path)); | 58 base::Unretained(this), database_path)); |
55 #ifndef NDEBUG | 59 #ifndef NDEBUG |
56 picasa_database_path_ = database_path; | 60 picasa_database_path_ = database_path; |
57 } else { | 61 } else { |
58 DCHECK_EQ(picasa_database_path_.value(), database_path.value()); | 62 DCHECK_EQ(picasa_database_path_.value(), database_path.value()); |
59 #endif | 63 #endif |
60 } | 64 } |
61 #endif // defined(OS_WIN) || defined(OS_MACOSX) | 65 #endif // defined(OS_WIN) || defined(OS_MACOSX) |
62 | 66 |
63 return fsid; | 67 return result; |
64 } | 68 } |
65 | 69 |
66 std::string ImportedMediaGalleryRegistry::RegisterITunesFilesystemOnUIThread( | 70 bool ImportedMediaGalleryRegistry::RegisterITunesFilesystemOnUIThread( |
67 const base::FilePath& library_xml_path) { | 71 const std::string& fs_name, const base::FilePath& library_xml_path) { |
68 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 72 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
69 DCHECK(!library_xml_path.empty()); | 73 DCHECK(!library_xml_path.empty()); |
70 | 74 |
71 std::string fsid; | 75 bool result = false; |
72 | 76 |
73 #if defined(OS_WIN) || defined(OS_MACOSX) | 77 #if defined(OS_WIN) || defined(OS_MACOSX) |
74 fsid = IsolatedContext::GetInstance()->RegisterFileSystemForVirtualPath( | 78 base::FilePath root = ImportedRoot(); |
75 fileapi::kFileSystemTypeItunes, | 79 if (root.empty()) |
76 extension_misc::kMediaFileSystemPathPart, | 80 return false; |
77 base::FilePath()); | 81 result = ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( |
82 fs_name, fileapi::kFileSystemTypeItunes, fileapi::FileSystemMountOption(), | |
83 root.AppendASCII("itunes")); | |
84 if (!result) | |
85 return result; | |
78 | 86 |
79 if (fsid.empty()) | 87 itunes_fs_names_.insert(fs_name); |
80 return fsid; | |
81 | 88 |
82 itunes_fsids_.insert(fsid); | 89 if (itunes_fs_names_.size() == 1) { |
83 | |
84 if (itunes_fsids_.size() == 1) { | |
85 MediaFileSystemBackend::MediaTaskRunner()->PostTask( | 90 MediaFileSystemBackend::MediaTaskRunner()->PostTask( |
86 FROM_HERE, | 91 FROM_HERE, |
87 Bind(&ImportedMediaGalleryRegistry::RegisterITunesFileSystem, | 92 Bind(&ImportedMediaGalleryRegistry::RegisterITunesFileSystem, |
88 base::Unretained(this), library_xml_path)); | 93 base::Unretained(this), library_xml_path)); |
89 #ifndef NDEBUG | 94 #ifndef NDEBUG |
90 itunes_xml_library_path_ = library_xml_path; | 95 itunes_xml_library_path_ = library_xml_path; |
91 } else { | 96 } else { |
92 DCHECK_EQ(itunes_xml_library_path_.value(), library_xml_path.value()); | 97 DCHECK_EQ(itunes_xml_library_path_.value(), library_xml_path.value()); |
93 #endif | 98 #endif |
94 } | 99 } |
95 #endif // defined(OS_WIN) || defined(OS_MACOSX) | 100 #endif // defined(OS_WIN) || defined(OS_MACOSX) |
96 | 101 |
97 return fsid; | 102 return result; |
98 } | 103 } |
99 | 104 |
100 std::string ImportedMediaGalleryRegistry::RegisterIPhotoFilesystemOnUIThread( | 105 bool ImportedMediaGalleryRegistry::RegisterIPhotoFilesystemOnUIThread( |
101 const base::FilePath& library_xml_path) { | 106 const std::string& fs_name, const base::FilePath& library_xml_path) { |
102 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 107 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
103 DCHECK(!library_xml_path.empty()); | 108 DCHECK(!library_xml_path.empty()); |
104 | 109 |
105 std::string fsid; | 110 bool result = false; |
106 | 111 |
107 // TODO(gbillock): Investigate how to refactor this to reduce duplicated | 112 // TODO(gbillock): Investigate how to refactor this to reduce duplicated |
108 // code. | 113 // code. |
109 #if defined(OS_MACOSX) | 114 #if defined(OS_MACOSX) |
110 fsid = IsolatedContext::GetInstance()->RegisterFileSystemForVirtualPath( | 115 base::FilePath root = ImportedRoot(); |
111 fileapi::kFileSystemTypeIphoto, | 116 if (root.empty()) |
112 extension_misc::kMediaFileSystemPathPart, | 117 return false; |
113 base::FilePath()); | 118 result = ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( |
119 fs_name, fileapi::kFileSystemTypeIphoto, fileapi::FileSystemMountOption(), | |
120 root.AppendASCII("iphoto")); | |
121 if (!result) | |
122 return result; | |
114 | 123 |
115 if (fsid.empty()) | 124 iphoto_fs_names_.insert(fs_name); |
116 return fsid; | |
117 | 125 |
118 iphoto_fsids_.insert(fsid); | 126 if (iphoto_fs_names_.size() == 1) { |
119 | |
120 if (iphoto_fsids_.size() == 1) { | |
121 MediaFileSystemBackend::MediaTaskRunner()->PostTask( | 127 MediaFileSystemBackend::MediaTaskRunner()->PostTask( |
122 FROM_HERE, | 128 FROM_HERE, |
123 Bind(&ImportedMediaGalleryRegistry::RegisterIPhotoFileSystem, | 129 Bind(&ImportedMediaGalleryRegistry::RegisterIPhotoFileSystem, |
124 base::Unretained(this), library_xml_path)); | 130 base::Unretained(this), library_xml_path)); |
125 #ifndef NDEBUG | 131 #ifndef NDEBUG |
126 iphoto_xml_library_path_ = library_xml_path; | 132 iphoto_xml_library_path_ = library_xml_path; |
127 } else { | 133 } else { |
128 DCHECK_EQ(iphoto_xml_library_path_.value(), library_xml_path.value()); | 134 DCHECK_EQ(iphoto_xml_library_path_.value(), library_xml_path.value()); |
129 #endif | 135 #endif |
130 } | 136 } |
131 #endif // defined(OS_MACOSX) | 137 #endif // defined(OS_MACOSX) |
132 | 138 |
133 return fsid; | 139 return result; |
134 } | 140 } |
135 | 141 |
136 bool ImportedMediaGalleryRegistry::RevokeImportedFilesystemOnUIThread( | 142 bool ImportedMediaGalleryRegistry::RevokeImportedFilesystemOnUIThread( |
137 const std::string& fsid) { | 143 const std::string& fs_name) { |
138 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 144 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
139 | 145 |
140 #if defined(OS_WIN) || defined(OS_MACOSX) | 146 #if defined(OS_WIN) || defined(OS_MACOSX) |
141 if (picasa_fsids_.erase(fsid)) { | 147 if (picasa_fs_names_.erase(fs_name)) { |
142 if (picasa_fsids_.empty()) { | 148 if (picasa_fs_names_.empty()) { |
143 MediaFileSystemBackend::MediaTaskRunner()->PostTask( | 149 MediaFileSystemBackend::MediaTaskRunner()->PostTask( |
144 FROM_HERE, | 150 FROM_HERE, |
145 Bind(&ImportedMediaGalleryRegistry::RevokePicasaFileSystem, | 151 Bind(&ImportedMediaGalleryRegistry::RevokePicasaFileSystem, |
146 base::Unretained(this))); | 152 base::Unretained(this))); |
147 } | 153 } |
148 return IsolatedContext::GetInstance()->RevokeFileSystem(fsid); | 154 return ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(fs_name); |
149 } | 155 } |
150 | 156 |
151 if (itunes_fsids_.erase(fsid)) { | 157 if (itunes_fs_names_.erase(fs_name)) { |
152 if (itunes_fsids_.empty()) { | 158 if (itunes_fs_names_.empty()) { |
153 MediaFileSystemBackend::MediaTaskRunner()->PostTask( | 159 MediaFileSystemBackend::MediaTaskRunner()->PostTask( |
154 FROM_HERE, | 160 FROM_HERE, |
155 Bind(&ImportedMediaGalleryRegistry::RevokeITunesFileSystem, | 161 Bind(&ImportedMediaGalleryRegistry::RevokeITunesFileSystem, |
156 base::Unretained(this))); | 162 base::Unretained(this))); |
157 } | 163 } |
158 return IsolatedContext::GetInstance()->RevokeFileSystem(fsid); | 164 return ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(fs_name); |
159 } | 165 } |
160 #endif // defined(OS_WIN) || defined(OS_MACOSX) | 166 #endif // defined(OS_WIN) || defined(OS_MACOSX) |
161 | 167 |
162 #if defined(OS_MACOSX) | 168 #if defined(OS_MACOSX) |
163 if (iphoto_fsids_.erase(fsid)) { | 169 if (iphoto_fs_names_.erase(fs_name)) { |
164 if (iphoto_fsids_.empty()) { | 170 if (iphoto_fs_names_.empty()) { |
165 MediaFileSystemBackend::MediaTaskRunner()->PostTask( | 171 MediaFileSystemBackend::MediaTaskRunner()->PostTask( |
166 FROM_HERE, | 172 FROM_HERE, |
167 Bind(&ImportedMediaGalleryRegistry::RevokeIPhotoFileSystem, | 173 Bind(&ImportedMediaGalleryRegistry::RevokeIPhotoFileSystem, |
168 base::Unretained(this))); | 174 base::Unretained(this))); |
169 } | 175 } |
170 return IsolatedContext::GetInstance()->RevokeFileSystem(fsid); | 176 return ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(fs_name); |
171 } | 177 } |
172 #endif // defined(OS_MACOSX) | 178 #endif // defined(OS_MACOSX) |
173 | 179 |
174 return false; | 180 return false; |
175 } | 181 } |
176 | 182 |
177 #if defined(OS_WIN) || defined(OS_MACOSX) | 183 #if defined(OS_WIN) || defined(OS_MACOSX) |
178 // static | 184 // static |
179 picasa::PicasaDataProvider* | 185 picasa::PicasaDataProvider* |
180 ImportedMediaGalleryRegistry::PicasaDataProvider() { | 186 ImportedMediaGalleryRegistry::PicasaDataProvider() { |
(...skipping 17 matching lines...) Expand all Loading... | |
198 ImportedMediaGalleryRegistry::IPhotoDataProvider() { | 204 ImportedMediaGalleryRegistry::IPhotoDataProvider() { |
199 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 205 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
200 DCHECK(GetInstance()->iphoto_data_provider_); | 206 DCHECK(GetInstance()->iphoto_data_provider_); |
201 return GetInstance()->iphoto_data_provider_.get(); | 207 return GetInstance()->iphoto_data_provider_.get(); |
202 } | 208 } |
203 #endif // defined(OS_MACOSX) | 209 #endif // defined(OS_MACOSX) |
204 | 210 |
205 ImportedMediaGalleryRegistry::ImportedMediaGalleryRegistry() {} | 211 ImportedMediaGalleryRegistry::ImportedMediaGalleryRegistry() {} |
206 | 212 |
207 ImportedMediaGalleryRegistry::~ImportedMediaGalleryRegistry() { | 213 ImportedMediaGalleryRegistry::~ImportedMediaGalleryRegistry() { |
214 if (!imported_root_.empty()) | |
215 base::DeleteFile(imported_root_, false); | |
208 #if defined(OS_WIN) || defined(OS_MACOSX) | 216 #if defined(OS_WIN) || defined(OS_MACOSX) |
209 DCHECK_EQ(0U, picasa_fsids_.size()); | 217 DCHECK_EQ(0U, picasa_fs_names_.size()); |
210 DCHECK_EQ(0U, itunes_fsids_.size()); | 218 DCHECK_EQ(0U, itunes_fs_names_.size()); |
211 #endif // defined(OS_WIN) || defined(OS_MACOSX) | 219 #endif // defined(OS_WIN) || defined(OS_MACOSX) |
212 #if defined(OS_MACOSX) | 220 #if defined(OS_MACOSX) |
213 DCHECK_EQ(0U, iphoto_fsids_.size()); | 221 DCHECK_EQ(0U, iphoto_fs_names_.size()); |
214 #endif // defined(OS_MACOSX) | 222 #endif // defined(OS_MACOSX) |
215 } | 223 } |
216 | 224 |
225 base::FilePath ImportedMediaGalleryRegistry::ImportedRoot() { | |
226 if (imported_root_.empty()) { | |
227 // |ImportedRoot()| is called from both the UI thread and the media task | |
228 // runner thread. However, it is always called from the UI thread first | |
229 // (during file system registry) and won't be called on the task runner | |
230 // thread until after that. | |
231 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
232 if (!base::CreateTemporaryFile(&imported_root_)) | |
Lei Zhang
2014/03/26 17:41:30
You shouldn't be accessing the file system from th
vandebo (ex-Chrome)
2014/03/26 23:25:24
Done.
| |
233 imported_root_ = base::FilePath(); | |
234 // TODO(vandebo) Setting the permissions of |imported_root_| in CPSP to | |
235 // zero would be an extra step to ensure permissions are correctly | |
236 // enforced. | |
237 } | |
238 return imported_root_; | |
239 } | |
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 } |
224 | 248 |
225 void ImportedMediaGalleryRegistry::RevokePicasaFileSystem() { | 249 void ImportedMediaGalleryRegistry::RevokePicasaFileSystem() { |
226 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 250 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
(...skipping 22 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 |