OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "webkit/fileapi/isolated_context.h" | 5 #include "webkit/fileapi/isolated_context.h" |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
159 const FileInfoSet& files) { | 159 const FileInfoSet& files) { |
160 base::AutoLock locker(lock_); | 160 base::AutoLock locker(lock_); |
161 std::string filesystem_id = GetNewFileSystemId(); | 161 std::string filesystem_id = GetNewFileSystemId(); |
162 instance_map_[filesystem_id] = new Instance( | 162 instance_map_[filesystem_id] = new Instance( |
163 kFileSystemTypeDragged, files.fileset()); | 163 kFileSystemTypeDragged, files.fileset()); |
164 return filesystem_id; | 164 return filesystem_id; |
165 } | 165 } |
166 | 166 |
167 std::string IsolatedContext::RegisterFileSystemForPath( | 167 std::string IsolatedContext::RegisterFileSystemForPath( |
168 FileSystemType type, | 168 FileSystemType type, |
169 const FilePath& path, | 169 const FilePath& path_in, |
170 std::string* register_name) { | 170 std::string* register_name) { |
171 FilePath path(path_in.NormalizePathSeparators()); | |
171 DCHECK(!path.ReferencesParent() && path.IsAbsolute()); | 172 DCHECK(!path.ReferencesParent() && path.IsAbsolute()); |
172 std::string name; | 173 std::string name; |
173 if (register_name && !register_name->empty()) { | 174 if (register_name && !register_name->empty()) { |
174 name = *register_name; | 175 name = *register_name; |
175 } else { | 176 } else { |
176 name = FilePath(GetRegisterNameForPath(path)).AsUTF8Unsafe(); | 177 name = FilePath(GetRegisterNameForPath(path)).AsUTF8Unsafe(); |
177 if (register_name) | 178 if (register_name) |
178 register_name->assign(name); | 179 register_name->assign(name); |
179 } | 180 } |
180 | 181 |
181 base::AutoLock locker(lock_); | 182 base::AutoLock locker(lock_); |
182 std::string filesystem_id = GetNewFileSystemId(); | 183 std::string filesystem_id = GetNewFileSystemId(); |
183 instance_map_[filesystem_id] = new Instance(type, FileInfo(name, path)); | 184 instance_map_[filesystem_id] = new Instance(type, FileInfo(name, path)); |
184 path_to_id_map_[path].insert(filesystem_id); | 185 path_to_id_map_[path].insert(filesystem_id); |
185 return filesystem_id; | 186 return filesystem_id; |
186 } | 187 } |
187 | 188 |
188 void IsolatedContext::RevokeFileSystemByPath(const FilePath& path) { | 189 bool IsolatedContext::RevokeFileSystem(const std::string& id_or_name) { |
189 base::AutoLock locker(lock_); | 190 base::AutoLock locker(lock_); |
191 return UnregisterFileSystem(id_or_name); | |
192 } | |
193 | |
194 void IsolatedContext::RevokeFileSystemByPath(const FilePath& path_in) { | |
195 base::AutoLock locker(lock_); | |
196 FilePath path(path_in.NormalizePathSeparators()); | |
190 PathToID::iterator ids_iter = path_to_id_map_.find(path); | 197 PathToID::iterator ids_iter = path_to_id_map_.find(path); |
191 if (ids_iter == path_to_id_map_.end()) | 198 if (ids_iter == path_to_id_map_.end()) |
192 return; | 199 return; |
193 std::set<std::string>& ids = ids_iter->second; | 200 std::set<std::string>& ids = ids_iter->second; |
194 for (std::set<std::string>::iterator iter = ids.begin(); | 201 for (std::set<std::string>::iterator iter = ids.begin(); |
195 iter != ids.end(); ++iter) { | 202 iter != ids.end(); ++iter) { |
196 IDToInstance::iterator found = instance_map_.find(*iter); | 203 IDToInstance::iterator found = instance_map_.find(*iter); |
197 if (found != instance_map_.end()) { | 204 if (found != instance_map_.end()) { |
198 delete found->second; | 205 delete found->second; |
199 instance_map_.erase(found); | 206 instance_map_.erase(found); |
(...skipping 12 matching lines...) Expand all Loading... | |
212 base::AutoLock locker(lock_); | 219 base::AutoLock locker(lock_); |
213 // This could get called for non-existent filesystem if it has been | 220 // This could get called for non-existent filesystem if it has been |
214 // already deleted by RevokeFileSystemByPath. | 221 // already deleted by RevokeFileSystemByPath. |
215 IDToInstance::iterator found = instance_map_.find(filesystem_id); | 222 IDToInstance::iterator found = instance_map_.find(filesystem_id); |
216 if (found == instance_map_.end()) | 223 if (found == instance_map_.end()) |
217 return; | 224 return; |
218 Instance* instance = found->second; | 225 Instance* instance = found->second; |
219 DCHECK(instance->ref_counts() > 0); | 226 DCHECK(instance->ref_counts() > 0); |
220 instance->RemoveRef(); | 227 instance->RemoveRef(); |
221 if (instance->ref_counts() == 0) { | 228 if (instance->ref_counts() == 0) { |
222 if (instance->IsSinglePathInstance()) { | 229 bool deleted = UnregisterFileSystem(filesystem_id); |
223 PathToID::iterator ids_iter = path_to_id_map_.find( | 230 DCHECK(deleted); |
224 instance->file_info().path); | |
225 DCHECK(ids_iter != path_to_id_map_.end()); | |
226 ids_iter->second.erase(filesystem_id); | |
227 if (ids_iter->second.empty()) | |
228 path_to_id_map_.erase(ids_iter); | |
229 } | |
230 delete instance; | |
231 instance_map_.erase(found); | |
232 } | 231 } |
233 } | 232 } |
234 | 233 |
235 bool IsolatedContext::CrackIsolatedPath(const FilePath& virtual_path, | 234 bool IsolatedContext::CrackIsolatedPath(const FilePath& virtual_path, |
236 std::string* filesystem_id, | 235 std::string* filesystem_id, |
237 FileSystemType* type, | 236 FileSystemType* type, |
238 FilePath* path) const { | 237 FilePath* path) const { |
239 DCHECK(filesystem_id); | 238 DCHECK(filesystem_id); |
240 DCHECK(path); | 239 DCHECK(path); |
241 | 240 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
304 } | 303 } |
305 | 304 |
306 IsolatedContext::IsolatedContext() { | 305 IsolatedContext::IsolatedContext() { |
307 } | 306 } |
308 | 307 |
309 IsolatedContext::~IsolatedContext() { | 308 IsolatedContext::~IsolatedContext() { |
310 STLDeleteContainerPairSecondPointers(instance_map_.begin(), | 309 STLDeleteContainerPairSecondPointers(instance_map_.begin(), |
311 instance_map_.end()); | 310 instance_map_.end()); |
312 } | 311 } |
313 | 312 |
313 bool IsolatedContext::UnregisterFileSystem(const std::string& filesystem_id) { | |
tzik
2012/08/13 09:03:32
Could you add some assertion like this (maybe in s
kinuko
2012/08/13 09:20:26
Good idea! Done.
| |
314 IDToInstance::iterator found = instance_map_.find(filesystem_id); | |
315 if (found == instance_map_.end()) | |
316 return false; | |
317 Instance* instance = found->second; | |
318 if (instance->IsSinglePathInstance()) { | |
319 PathToID::iterator ids_iter = path_to_id_map_.find( | |
320 instance->file_info().path); | |
321 DCHECK(ids_iter != path_to_id_map_.end()); | |
322 ids_iter->second.erase(filesystem_id); | |
323 if (ids_iter->second.empty()) | |
324 path_to_id_map_.erase(ids_iter); | |
325 } | |
326 delete found->second; | |
327 instance_map_.erase(found); | |
328 return true; | |
329 } | |
330 | |
314 std::string IsolatedContext::GetNewFileSystemId() const { | 331 std::string IsolatedContext::GetNewFileSystemId() const { |
315 // Returns an arbitrary random string which must be unique in the map. | 332 // Returns an arbitrary random string which must be unique in the map. |
316 uint32 random_data[4]; | 333 uint32 random_data[4]; |
317 std::string id; | 334 std::string id; |
318 do { | 335 do { |
319 base::RandBytes(random_data, sizeof(random_data)); | 336 base::RandBytes(random_data, sizeof(random_data)); |
320 id = base::HexEncode(random_data, sizeof(random_data)); | 337 id = base::HexEncode(random_data, sizeof(random_data)); |
321 } while (instance_map_.find(id) != instance_map_.end()); | 338 } while (instance_map_.find(id) != instance_map_.end()); |
322 return id; | 339 return id; |
323 } | 340 } |
324 | 341 |
325 } // namespace fileapi | 342 } // namespace fileapi |
OLD | NEW |