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/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 std::string IsolatedContext::RegisterIsolatedFileSystem( | 23 std::string IsolatedContext::RegisterIsolatedFileSystem( |
24 const std::set<FilePath>& files) { | 24 const std::set<FilePath>& files) { |
25 base::AutoLock locker(lock_); | 25 base::AutoLock locker(lock_); |
26 std::string filesystem_id = GetNewFileSystemId(); | 26 std::string filesystem_id = GetNewFileSystemId(); |
27 // Stores basename to fullpath map, as we store the basenames as | 27 // Stores basename to fullpath map, as we store the basenames as |
28 // the filesystem's toplevel entries. | 28 // the filesystem's toplevel entries. |
29 PathMap toplevels; | 29 PathMap toplevels; |
30 for (std::set<FilePath>::const_iterator iter = files.begin(); | 30 for (std::set<FilePath>::const_iterator iter = files.begin(); |
31 iter != files.end(); ++iter) { | 31 iter != files.end(); ++iter) { |
32 // The given path should not contain any '..' and must be an absolute path. | 32 // If the given path contains any '..' or is not an absolute path, |
33 DCHECK(!iter->ReferencesParent() && iter->IsAbsolute()); | 33 // return an empty (invalid) id. |
| 34 if (iter->ReferencesParent() || !iter->IsAbsolute()) |
| 35 return std::string(); |
34 | 36 |
35 // Register the basename -> fullpath map. (We only expose the basename | 37 // Register the basename -> fullpath map. (We only expose the basename |
36 // part to the user scripts) | 38 // part to the user scripts) |
37 FilePath fullpath = iter->NormalizePathSeparators(); | 39 FilePath fullpath = iter->NormalizePathSeparators(); |
38 FilePath basename = iter->BaseName(); | 40 FilePath basename = iter->BaseName(); |
39 // TODO(kinuko): Append a suffix or something if we have multiple pathnames | 41 // TODO(kinuko): Append a suffix or something if we have multiple pathnames |
40 // with the same basename. For now we only register the first one. | 42 // with the same basename. For now we only register the first one. |
41 toplevels.insert(std::make_pair(basename, fullpath)); | 43 toplevels.insert(std::make_pair(basename, fullpath)); |
42 } | 44 } |
43 toplevel_map_[filesystem_id] = toplevels; | 45 toplevel_map_[filesystem_id] = toplevels; |
44 | |
45 // Each file system is created with refcount == 0. | |
46 ref_counts_[filesystem_id] = 0; | |
47 | |
48 return filesystem_id; | 46 return filesystem_id; |
49 } | 47 } |
50 | 48 |
| 49 // Revoke any registered drag context for the child_id. |
51 void IsolatedContext::RevokeIsolatedFileSystem( | 50 void IsolatedContext::RevokeIsolatedFileSystem( |
52 const std::string& filesystem_id) { | 51 const std::string& filesystem_id) { |
53 base::AutoLock locker(lock_); | 52 base::AutoLock locker(lock_); |
54 RevokeWithoutLocking(filesystem_id); | 53 toplevel_map_.erase(filesystem_id); |
55 } | 54 writable_ids_.erase(filesystem_id); |
56 | |
57 void IsolatedContext::AddReference(const std::string& filesystem_id) { | |
58 base::AutoLock locker(lock_); | |
59 DCHECK(ref_counts_.find(filesystem_id) != ref_counts_.end()); | |
60 ref_counts_[filesystem_id]++; | |
61 } | |
62 | |
63 void IsolatedContext::RemoveReference(const std::string& filesystem_id) { | |
64 base::AutoLock locker(lock_); | |
65 // This could get called for non-existent filesystem if it has been | |
66 // already deleted by RevokeIsolatedFileSystem. | |
67 if (ref_counts_.find(filesystem_id) == ref_counts_.end()) | |
68 return; | |
69 DCHECK(ref_counts_[filesystem_id] > 0); | |
70 if (--ref_counts_[filesystem_id] == 0) | |
71 RevokeWithoutLocking(filesystem_id); | |
72 } | 55 } |
73 | 56 |
74 bool IsolatedContext::CrackIsolatedPath(const FilePath& virtual_path, | 57 bool IsolatedContext::CrackIsolatedPath(const FilePath& virtual_path, |
75 std::string* filesystem_id, | 58 std::string* filesystem_id, |
76 FilePath* root_path, | 59 FilePath* root_path, |
77 FilePath* platform_path) const { | 60 FilePath* platform_path) const { |
78 DCHECK(filesystem_id); | 61 DCHECK(filesystem_id); |
79 DCHECK(platform_path); | 62 DCHECK(platform_path); |
80 | 63 |
81 // This should not contain any '..' references. | 64 // This should not contain any '..' references. |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 full_path = full_path.Append(relative_path); | 140 full_path = full_path.Append(relative_path); |
158 return full_path; | 141 return full_path; |
159 } | 142 } |
160 | 143 |
161 IsolatedContext::IsolatedContext() { | 144 IsolatedContext::IsolatedContext() { |
162 } | 145 } |
163 | 146 |
164 IsolatedContext::~IsolatedContext() { | 147 IsolatedContext::~IsolatedContext() { |
165 } | 148 } |
166 | 149 |
167 void IsolatedContext::RevokeWithoutLocking( | |
168 const std::string& filesystem_id) { | |
169 toplevel_map_.erase(filesystem_id); | |
170 writable_ids_.erase(filesystem_id); | |
171 ref_counts_.erase(filesystem_id); | |
172 } | |
173 | |
174 std::string IsolatedContext::GetNewFileSystemId() const { | 150 std::string IsolatedContext::GetNewFileSystemId() const { |
175 // Returns an arbitrary random string which must be unique in the map. | 151 // Returns an arbitrary random string which must be unique in the map. |
176 uint32 random_data[4]; | 152 uint32 random_data[4]; |
177 std::string id; | 153 std::string id; |
178 do { | 154 do { |
179 base::RandBytes(random_data, sizeof(random_data)); | 155 base::RandBytes(random_data, sizeof(random_data)); |
180 id = base::HexEncode(random_data, sizeof(random_data)); | 156 id = base::HexEncode(random_data, sizeof(random_data)); |
181 } while (toplevel_map_.find(id) != toplevel_map_.end()); | 157 } while (toplevel_map_.find(id) != toplevel_map_.end()); |
182 return id; | 158 return id; |
183 } | 159 } |
184 | 160 |
185 } // namespace fileapi | 161 } // namespace fileapi |
OLD | NEW |