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" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
15 #include "webkit/fileapi/file_system_url.h" | 15 #include "webkit/fileapi/file_system_url.h" |
16 | 16 |
17 namespace fileapi { | 17 namespace fileapi { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 FilePath::StringType GetRegisterNameForPath(const FilePath& path) { | 21 base::FilePath::StringType GetRegisterNameForPath(const base::FilePath& path) { |
22 // If it's not a root path simply return a base name. | 22 // If it's not a root path simply return a base name. |
23 if (path.DirName() != path) | 23 if (path.DirName() != path) |
24 return path.BaseName().value(); | 24 return path.BaseName().value(); |
25 | 25 |
26 #if defined(FILE_PATH_USES_DRIVE_LETTERS) | 26 #if defined(FILE_PATH_USES_DRIVE_LETTERS) |
27 FilePath::StringType name; | 27 base::FilePath::StringType name; |
28 for (size_t i = 0; | 28 for (size_t i = 0; |
29 i < path.value().size() && !FilePath::IsSeparator(path.value()[i]); | 29 i < path.value().size() && !base::FilePath::IsSeparator(path.value()[i])
; |
30 ++i) { | 30 ++i) { |
31 if (path.value()[i] == L':') { | 31 if (path.value()[i] == L':') { |
32 name.append(L"_drive"); | 32 name.append(L"_drive"); |
33 break; | 33 break; |
34 } | 34 } |
35 name.append(1, path.value()[i]); | 35 name.append(1, path.value()[i]); |
36 } | 36 } |
37 return name; | 37 return name; |
38 #else | 38 #else |
39 return FILE_PATH_LITERAL("<root>"); | 39 return FILE_PATH_LITERAL("<root>"); |
(...skipping 20 matching lines...) Expand all Loading... |
60 | 60 |
61 static base::LazyInstance<IsolatedContext>::Leaky g_isolated_context = | 61 static base::LazyInstance<IsolatedContext>::Leaky g_isolated_context = |
62 LAZY_INSTANCE_INITIALIZER; | 62 LAZY_INSTANCE_INITIALIZER; |
63 | 63 |
64 } // namespace | 64 } // namespace |
65 | 65 |
66 IsolatedContext::FileInfoSet::FileInfoSet() {} | 66 IsolatedContext::FileInfoSet::FileInfoSet() {} |
67 IsolatedContext::FileInfoSet::~FileInfoSet() {} | 67 IsolatedContext::FileInfoSet::~FileInfoSet() {} |
68 | 68 |
69 bool IsolatedContext::FileInfoSet::AddPath( | 69 bool IsolatedContext::FileInfoSet::AddPath( |
70 const FilePath& path, std::string* registered_name) { | 70 const base::FilePath& path, std::string* registered_name) { |
71 // The given path should not contain any '..' and should be absolute. | 71 // The given path should not contain any '..' and should be absolute. |
72 if (path.ReferencesParent() || !path.IsAbsolute()) | 72 if (path.ReferencesParent() || !path.IsAbsolute()) |
73 return false; | 73 return false; |
74 FilePath::StringType name = GetRegisterNameForPath(path); | 74 base::FilePath::StringType name = GetRegisterNameForPath(path); |
75 std::string utf8name = FilePath(name).AsUTF8Unsafe(); | 75 std::string utf8name = base::FilePath(name).AsUTF8Unsafe(); |
76 FilePath normalized_path = path.NormalizePathSeparators(); | 76 base::FilePath normalized_path = path.NormalizePathSeparators(); |
77 bool inserted = | 77 bool inserted = |
78 fileset_.insert(MountPointInfo(utf8name, normalized_path)).second; | 78 fileset_.insert(MountPointInfo(utf8name, normalized_path)).second; |
79 if (!inserted) { | 79 if (!inserted) { |
80 int suffix = 1; | 80 int suffix = 1; |
81 std::string basepart = FilePath(name).RemoveExtension().AsUTF8Unsafe(); | 81 std::string basepart = base::FilePath(name).RemoveExtension().AsUTF8Unsafe()
; |
82 std::string ext = FilePath(FilePath(name).Extension()).AsUTF8Unsafe(); | 82 std::string ext = base::FilePath(base::FilePath(name).Extension()).AsUTF8Uns
afe(); |
83 while (!inserted) { | 83 while (!inserted) { |
84 utf8name = base::StringPrintf("%s (%d)", basepart.c_str(), suffix++); | 84 utf8name = base::StringPrintf("%s (%d)", basepart.c_str(), suffix++); |
85 if (!ext.empty()) | 85 if (!ext.empty()) |
86 utf8name.append(ext); | 86 utf8name.append(ext); |
87 inserted = | 87 inserted = |
88 fileset_.insert(MountPointInfo(utf8name, normalized_path)).second; | 88 fileset_.insert(MountPointInfo(utf8name, normalized_path)).second; |
89 } | 89 } |
90 } | 90 } |
91 if (registered_name) | 91 if (registered_name) |
92 *registered_name = utf8name; | 92 *registered_name = utf8name; |
93 return true; | 93 return true; |
94 } | 94 } |
95 | 95 |
96 bool IsolatedContext::FileInfoSet::AddPathWithName( | 96 bool IsolatedContext::FileInfoSet::AddPathWithName( |
97 const FilePath& path, const std::string& name) { | 97 const base::FilePath& path, const std::string& name) { |
98 // The given path should not contain any '..' and should be absolute. | 98 // The given path should not contain any '..' and should be absolute. |
99 if (path.ReferencesParent() || !path.IsAbsolute()) | 99 if (path.ReferencesParent() || !path.IsAbsolute()) |
100 return false; | 100 return false; |
101 return fileset_.insert( | 101 return fileset_.insert( |
102 MountPointInfo(name, path.NormalizePathSeparators())).second; | 102 MountPointInfo(name, path.NormalizePathSeparators())).second; |
103 } | 103 } |
104 | 104 |
105 //-------------------------------------------------------------------------- | 105 //-------------------------------------------------------------------------- |
106 | 106 |
107 class IsolatedContext::Instance { | 107 class IsolatedContext::Instance { |
(...skipping 11 matching lines...) Expand all Loading... |
119 ~Instance(); | 119 ~Instance(); |
120 | 120 |
121 FileSystemType type() const { return type_; } | 121 FileSystemType type() const { return type_; } |
122 const MountPointInfo& file_info() const { return file_info_; } | 122 const MountPointInfo& file_info() const { return file_info_; } |
123 const std::set<MountPointInfo>& files() const { return files_; } | 123 const std::set<MountPointInfo>& files() const { return files_; } |
124 int ref_counts() const { return ref_counts_; } | 124 int ref_counts() const { return ref_counts_; } |
125 | 125 |
126 void AddRef() { ++ref_counts_; } | 126 void AddRef() { ++ref_counts_; } |
127 void RemoveRef() { --ref_counts_; } | 127 void RemoveRef() { --ref_counts_; } |
128 | 128 |
129 bool ResolvePathForName(const std::string& name, FilePath* path) const; | 129 bool ResolvePathForName(const std::string& name, base::FilePath* path) const; |
130 | 130 |
131 // Returns true if the instance is a single-path instance. | 131 // Returns true if the instance is a single-path instance. |
132 bool IsSinglePathInstance() const; | 132 bool IsSinglePathInstance() const; |
133 | 133 |
134 private: | 134 private: |
135 const FileSystemType type_; | 135 const FileSystemType type_; |
136 | 136 |
137 // For single-path instance. | 137 // For single-path instance. |
138 const MountPointInfo file_info_; | 138 const MountPointInfo file_info_; |
139 | 139 |
(...skipping 19 matching lines...) Expand all Loading... |
159 const std::set<MountPointInfo>& files) | 159 const std::set<MountPointInfo>& files) |
160 : type_(type), | 160 : type_(type), |
161 files_(files), | 161 files_(files), |
162 ref_counts_(0) { | 162 ref_counts_(0) { |
163 DCHECK(!IsSinglePathIsolatedFileSystem(type_)); | 163 DCHECK(!IsSinglePathIsolatedFileSystem(type_)); |
164 } | 164 } |
165 | 165 |
166 IsolatedContext::Instance::~Instance() {} | 166 IsolatedContext::Instance::~Instance() {} |
167 | 167 |
168 bool IsolatedContext::Instance::ResolvePathForName(const std::string& name, | 168 bool IsolatedContext::Instance::ResolvePathForName(const std::string& name, |
169 FilePath* path) const { | 169 base::FilePath* path) const { |
170 if (IsSinglePathIsolatedFileSystem(type_)) { | 170 if (IsSinglePathIsolatedFileSystem(type_)) { |
171 *path = file_info_.path; | 171 *path = file_info_.path; |
172 return file_info_.name == name; | 172 return file_info_.name == name; |
173 } | 173 } |
174 std::set<MountPointInfo>::const_iterator found = files_.find( | 174 std::set<MountPointInfo>::const_iterator found = files_.find( |
175 MountPointInfo(name, FilePath())); | 175 MountPointInfo(name, base::FilePath())); |
176 if (found == files_.end()) | 176 if (found == files_.end()) |
177 return false; | 177 return false; |
178 *path = found->path; | 178 *path = found->path; |
179 return true; | 179 return true; |
180 } | 180 } |
181 | 181 |
182 bool IsolatedContext::Instance::IsSinglePathInstance() const { | 182 bool IsolatedContext::Instance::IsSinglePathInstance() const { |
183 return IsSinglePathIsolatedFileSystem(type_); | 183 return IsSinglePathIsolatedFileSystem(type_); |
184 } | 184 } |
185 | 185 |
(...skipping 13 matching lines...) Expand all Loading... |
199 const FileInfoSet& files) { | 199 const FileInfoSet& files) { |
200 base::AutoLock locker(lock_); | 200 base::AutoLock locker(lock_); |
201 std::string filesystem_id = GetNewFileSystemId(); | 201 std::string filesystem_id = GetNewFileSystemId(); |
202 instance_map_[filesystem_id] = new Instance( | 202 instance_map_[filesystem_id] = new Instance( |
203 kFileSystemTypeDragged, files.fileset()); | 203 kFileSystemTypeDragged, files.fileset()); |
204 return filesystem_id; | 204 return filesystem_id; |
205 } | 205 } |
206 | 206 |
207 std::string IsolatedContext::RegisterFileSystemForPath( | 207 std::string IsolatedContext::RegisterFileSystemForPath( |
208 FileSystemType type, | 208 FileSystemType type, |
209 const FilePath& path_in, | 209 const base::FilePath& path_in, |
210 std::string* register_name) { | 210 std::string* register_name) { |
211 FilePath path(path_in.NormalizePathSeparators()); | 211 base::FilePath path(path_in.NormalizePathSeparators()); |
212 DCHECK(!path.ReferencesParent() && path.IsAbsolute()); | 212 DCHECK(!path.ReferencesParent() && path.IsAbsolute()); |
213 std::string name; | 213 std::string name; |
214 if (register_name && !register_name->empty()) { | 214 if (register_name && !register_name->empty()) { |
215 name = *register_name; | 215 name = *register_name; |
216 } else { | 216 } else { |
217 name = FilePath(GetRegisterNameForPath(path)).AsUTF8Unsafe(); | 217 name = base::FilePath(GetRegisterNameForPath(path)).AsUTF8Unsafe(); |
218 if (register_name) | 218 if (register_name) |
219 register_name->assign(name); | 219 register_name->assign(name); |
220 } | 220 } |
221 | 221 |
222 base::AutoLock locker(lock_); | 222 base::AutoLock locker(lock_); |
223 std::string filesystem_id = GetNewFileSystemId(); | 223 std::string filesystem_id = GetNewFileSystemId(); |
224 instance_map_[filesystem_id] = new Instance(type, MountPointInfo(name, path)); | 224 instance_map_[filesystem_id] = new Instance(type, MountPointInfo(name, path)); |
225 path_to_id_map_[path].insert(filesystem_id); | 225 path_to_id_map_[path].insert(filesystem_id); |
226 return filesystem_id; | 226 return filesystem_id; |
227 } | 227 } |
228 | 228 |
229 bool IsolatedContext::HandlesFileSystemMountType(FileSystemType type) const { | 229 bool IsolatedContext::HandlesFileSystemMountType(FileSystemType type) const { |
230 return type == kFileSystemTypeIsolated; | 230 return type == kFileSystemTypeIsolated; |
231 } | 231 } |
232 | 232 |
233 bool IsolatedContext::RevokeFileSystem(const std::string& filesystem_id) { | 233 bool IsolatedContext::RevokeFileSystem(const std::string& filesystem_id) { |
234 base::AutoLock locker(lock_); | 234 base::AutoLock locker(lock_); |
235 return UnregisterFileSystem(filesystem_id); | 235 return UnregisterFileSystem(filesystem_id); |
236 } | 236 } |
237 | 237 |
238 bool IsolatedContext::GetRegisteredPath( | 238 bool IsolatedContext::GetRegisteredPath( |
239 const std::string& filesystem_id, FilePath* path) const { | 239 const std::string& filesystem_id, base::FilePath* path) const { |
240 DCHECK(path); | 240 DCHECK(path); |
241 base::AutoLock locker(lock_); | 241 base::AutoLock locker(lock_); |
242 IDToInstance::const_iterator found = instance_map_.find(filesystem_id); | 242 IDToInstance::const_iterator found = instance_map_.find(filesystem_id); |
243 if (found == instance_map_.end() || !found->second->IsSinglePathInstance()) | 243 if (found == instance_map_.end() || !found->second->IsSinglePathInstance()) |
244 return false; | 244 return false; |
245 *path = found->second->file_info().path; | 245 *path = found->second->file_info().path; |
246 return true; | 246 return true; |
247 } | 247 } |
248 | 248 |
249 bool IsolatedContext::CrackVirtualPath(const FilePath& virtual_path, | 249 bool IsolatedContext::CrackVirtualPath(const base::FilePath& virtual_path, |
250 std::string* id_or_name, | 250 std::string* id_or_name, |
251 FileSystemType* type, | 251 FileSystemType* type, |
252 FilePath* path) const { | 252 base::FilePath* path) const { |
253 DCHECK(id_or_name); | 253 DCHECK(id_or_name); |
254 DCHECK(path); | 254 DCHECK(path); |
255 | 255 |
256 // This should not contain any '..' references. | 256 // This should not contain any '..' references. |
257 if (virtual_path.ReferencesParent()) | 257 if (virtual_path.ReferencesParent()) |
258 return false; | 258 return false; |
259 | 259 |
260 // The virtual_path should comprise <id_or_name> and <relative_path> parts. | 260 // The virtual_path should comprise <id_or_name> and <relative_path> parts. |
261 std::vector<FilePath::StringType> components; | 261 std::vector<base::FilePath::StringType> components; |
262 virtual_path.GetComponents(&components); | 262 virtual_path.GetComponents(&components); |
263 if (components.size() < 1) | 263 if (components.size() < 1) |
264 return false; | 264 return false; |
265 std::vector<FilePath::StringType>::iterator component_iter = | 265 std::vector<base::FilePath::StringType>::iterator component_iter = |
266 components.begin(); | 266 components.begin(); |
267 std::string fsid = FilePath(*component_iter++).MaybeAsASCII(); | 267 std::string fsid = base::FilePath(*component_iter++).MaybeAsASCII(); |
268 if (fsid.empty()) | 268 if (fsid.empty()) |
269 return false; | 269 return false; |
270 | 270 |
271 FilePath cracked_path; | 271 base::FilePath cracked_path; |
272 { | 272 { |
273 base::AutoLock locker(lock_); | 273 base::AutoLock locker(lock_); |
274 IDToInstance::const_iterator found_instance = instance_map_.find(fsid); | 274 IDToInstance::const_iterator found_instance = instance_map_.find(fsid); |
275 if (found_instance == instance_map_.end()) | 275 if (found_instance == instance_map_.end()) |
276 return false; | 276 return false; |
277 *id_or_name = fsid; | 277 *id_or_name = fsid; |
278 const Instance* instance = found_instance->second; | 278 const Instance* instance = found_instance->second; |
279 if (type) | 279 if (type) |
280 *type = instance->type(); | 280 *type = instance->type(); |
281 | 281 |
282 if (component_iter == components.end()) { | 282 if (component_iter == components.end()) { |
283 // The virtual root case. | 283 // The virtual root case. |
284 path->clear(); | 284 path->clear(); |
285 return true; | 285 return true; |
286 } | 286 } |
287 | 287 |
288 // *component_iter should be a name of the registered path. | 288 // *component_iter should be a name of the registered path. |
289 std::string name = FilePath(*component_iter++).AsUTF8Unsafe(); | 289 std::string name = base::FilePath(*component_iter++).AsUTF8Unsafe(); |
290 if (!instance->ResolvePathForName(name, &cracked_path)) | 290 if (!instance->ResolvePathForName(name, &cracked_path)) |
291 return false; | 291 return false; |
292 } | 292 } |
293 | 293 |
294 for (; component_iter != components.end(); ++component_iter) | 294 for (; component_iter != components.end(); ++component_iter) |
295 cracked_path = cracked_path.Append(*component_iter); | 295 cracked_path = cracked_path.Append(*component_iter); |
296 *path = cracked_path; | 296 *path = cracked_path; |
297 return true; | 297 return true; |
298 } | 298 } |
299 | 299 |
300 FileSystemURL IsolatedContext::CrackURL(const GURL& url) const { | 300 FileSystemURL IsolatedContext::CrackURL(const GURL& url) const { |
301 FileSystemURL filesystem_url = FileSystemURL(url); | 301 FileSystemURL filesystem_url = FileSystemURL(url); |
302 if (!filesystem_url.is_valid()) | 302 if (!filesystem_url.is_valid()) |
303 return FileSystemURL(); | 303 return FileSystemURL(); |
304 return CreateCrackedFileSystemURL(filesystem_url.origin(), | 304 return CreateCrackedFileSystemURL(filesystem_url.origin(), |
305 filesystem_url.mount_type(), | 305 filesystem_url.mount_type(), |
306 filesystem_url.path()); | 306 filesystem_url.path()); |
307 } | 307 } |
308 | 308 |
309 FileSystemURL IsolatedContext::CreateCrackedFileSystemURL( | 309 FileSystemURL IsolatedContext::CreateCrackedFileSystemURL( |
310 const GURL& origin, | 310 const GURL& origin, |
311 FileSystemType type, | 311 FileSystemType type, |
312 const FilePath& path) const { | 312 const base::FilePath& path) const { |
313 if (!HandlesFileSystemMountType(type)) | 313 if (!HandlesFileSystemMountType(type)) |
314 return FileSystemURL(); | 314 return FileSystemURL(); |
315 | 315 |
316 std::string mount_name; | 316 std::string mount_name; |
317 FileSystemType cracked_type; | 317 FileSystemType cracked_type; |
318 FilePath cracked_path; | 318 base::FilePath cracked_path; |
319 if (!CrackVirtualPath(path, &mount_name, &cracked_type, &cracked_path)) | 319 if (!CrackVirtualPath(path, &mount_name, &cracked_type, &cracked_path)) |
320 return FileSystemURL(); | 320 return FileSystemURL(); |
321 | 321 |
322 return FileSystemURL(origin, type, path, | 322 return FileSystemURL(origin, type, path, |
323 mount_name, cracked_type, cracked_path); | 323 mount_name, cracked_type, cracked_path); |
324 } | 324 } |
325 | 325 |
326 void IsolatedContext::RevokeFileSystemByPath(const FilePath& path_in) { | 326 void IsolatedContext::RevokeFileSystemByPath(const base::FilePath& path_in) { |
327 base::AutoLock locker(lock_); | 327 base::AutoLock locker(lock_); |
328 FilePath path(path_in.NormalizePathSeparators()); | 328 base::FilePath path(path_in.NormalizePathSeparators()); |
329 PathToID::iterator ids_iter = path_to_id_map_.find(path); | 329 PathToID::iterator ids_iter = path_to_id_map_.find(path); |
330 if (ids_iter == path_to_id_map_.end()) | 330 if (ids_iter == path_to_id_map_.end()) |
331 return; | 331 return; |
332 std::set<std::string>& ids = ids_iter->second; | 332 std::set<std::string>& ids = ids_iter->second; |
333 for (std::set<std::string>::iterator iter = ids.begin(); | 333 for (std::set<std::string>::iterator iter = ids.begin(); |
334 iter != ids.end(); ++iter) { | 334 iter != ids.end(); ++iter) { |
335 IDToInstance::iterator found = instance_map_.find(*iter); | 335 IDToInstance::iterator found = instance_map_.find(*iter); |
336 if (found != instance_map_.end()) { | 336 if (found != instance_map_.end()) { |
337 delete found->second; | 337 delete found->second; |
338 instance_map_.erase(found); | 338 instance_map_.erase(found); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 base::AutoLock locker(lock_); | 370 base::AutoLock locker(lock_); |
371 IDToInstance::const_iterator found = instance_map_.find(filesystem_id); | 371 IDToInstance::const_iterator found = instance_map_.find(filesystem_id); |
372 if (found == instance_map_.end() || | 372 if (found == instance_map_.end() || |
373 found->second->type() != kFileSystemTypeDragged) | 373 found->second->type() != kFileSystemTypeDragged) |
374 return false; | 374 return false; |
375 files->assign(found->second->files().begin(), | 375 files->assign(found->second->files().begin(), |
376 found->second->files().end()); | 376 found->second->files().end()); |
377 return true; | 377 return true; |
378 } | 378 } |
379 | 379 |
380 FilePath IsolatedContext::CreateVirtualRootPath( | 380 base::FilePath IsolatedContext::CreateVirtualRootPath( |
381 const std::string& filesystem_id) const { | 381 const std::string& filesystem_id) const { |
382 return FilePath().AppendASCII(filesystem_id); | 382 return base::FilePath().AppendASCII(filesystem_id); |
383 } | 383 } |
384 | 384 |
385 IsolatedContext::IsolatedContext() { | 385 IsolatedContext::IsolatedContext() { |
386 } | 386 } |
387 | 387 |
388 IsolatedContext::~IsolatedContext() { | 388 IsolatedContext::~IsolatedContext() { |
389 STLDeleteContainerPairSecondPointers(instance_map_.begin(), | 389 STLDeleteContainerPairSecondPointers(instance_map_.begin(), |
390 instance_map_.end()); | 390 instance_map_.end()); |
391 } | 391 } |
392 | 392 |
(...skipping 22 matching lines...) Expand all Loading... |
415 uint32 random_data[4]; | 415 uint32 random_data[4]; |
416 std::string id; | 416 std::string id; |
417 do { | 417 do { |
418 base::RandBytes(random_data, sizeof(random_data)); | 418 base::RandBytes(random_data, sizeof(random_data)); |
419 id = base::HexEncode(random_data, sizeof(random_data)); | 419 id = base::HexEncode(random_data, sizeof(random_data)); |
420 } while (instance_map_.find(id) != instance_map_.end()); | 420 } while (instance_map_.find(id) != instance_map_.end()); |
421 return id; | 421 return id; |
422 } | 422 } |
423 | 423 |
424 } // namespace fileapi | 424 } // namespace fileapi |
OLD | NEW |