Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(521)

Side by Side Diff: webkit/fileapi/isolated_context.cc

Issue 10536200: Manage IsolatedContext with reference counts (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: win test fix Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « webkit/fileapi/isolated_context.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 // If the given path contains any '..' or is not an absolute path, 32 // The given path should not contain any '..' and must be an absolute path.
33 // return an empty (invalid) id. 33 DCHECK(!iter->ReferencesParent() && iter->IsAbsolute());
tzik 2012/06/25 10:37:54 As we chatted, platform_app_launcher seems to pass
34 if (iter->ReferencesParent() || !iter->IsAbsolute())
35 return std::string();
36 34
37 // Register the basename -> fullpath map. (We only expose the basename 35 // Register the basename -> fullpath map. (We only expose the basename
38 // part to the user scripts) 36 // part to the user scripts)
39 FilePath fullpath = iter->NormalizePathSeparators(); 37 FilePath fullpath = iter->NormalizePathSeparators();
40 FilePath basename = iter->BaseName(); 38 FilePath basename = iter->BaseName();
41 // TODO(kinuko): Append a suffix or something if we have multiple pathnames 39 // TODO(kinuko): Append a suffix or something if we have multiple pathnames
42 // with the same basename. For now we only register the first one. 40 // with the same basename. For now we only register the first one.
43 toplevels.insert(std::make_pair(basename, fullpath)); 41 toplevels.insert(std::make_pair(basename, fullpath));
44 } 42 }
45 toplevel_map_[filesystem_id] = toplevels; 43 toplevel_map_[filesystem_id] = toplevels;
44
45 // Each file system is created with refcount == 0.
46 ref_counts_[filesystem_id] = 0;
47
46 return filesystem_id; 48 return filesystem_id;
47 } 49 }
48 50
49 // Revoke any registered drag context for the child_id.
50 void IsolatedContext::RevokeIsolatedFileSystem( 51 void IsolatedContext::RevokeIsolatedFileSystem(
51 const std::string& filesystem_id) { 52 const std::string& filesystem_id) {
52 base::AutoLock locker(lock_); 53 base::AutoLock locker(lock_);
53 toplevel_map_.erase(filesystem_id); 54 RevokeWithoutLocking(filesystem_id);
54 writable_ids_.erase(filesystem_id); 55 }
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);
55 } 72 }
56 73
57 bool IsolatedContext::CrackIsolatedPath(const FilePath& virtual_path, 74 bool IsolatedContext::CrackIsolatedPath(const FilePath& virtual_path,
58 std::string* filesystem_id, 75 std::string* filesystem_id,
59 FilePath* root_path, 76 FilePath* root_path,
60 FilePath* platform_path) const { 77 FilePath* platform_path) const {
61 DCHECK(filesystem_id); 78 DCHECK(filesystem_id);
62 DCHECK(platform_path); 79 DCHECK(platform_path);
63 80
64 // This should not contain any '..' references. 81 // This should not contain any '..' references.
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 full_path = full_path.Append(relative_path); 157 full_path = full_path.Append(relative_path);
141 return full_path; 158 return full_path;
142 } 159 }
143 160
144 IsolatedContext::IsolatedContext() { 161 IsolatedContext::IsolatedContext() {
145 } 162 }
146 163
147 IsolatedContext::~IsolatedContext() { 164 IsolatedContext::~IsolatedContext() {
148 } 165 }
149 166
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
150 std::string IsolatedContext::GetNewFileSystemId() const { 174 std::string IsolatedContext::GetNewFileSystemId() const {
151 // Returns an arbitrary random string which must be unique in the map. 175 // Returns an arbitrary random string which must be unique in the map.
152 uint32 random_data[4]; 176 uint32 random_data[4];
153 std::string id; 177 std::string id;
154 do { 178 do {
155 base::RandBytes(random_data, sizeof(random_data)); 179 base::RandBytes(random_data, sizeof(random_data));
156 id = base::HexEncode(random_data, sizeof(random_data)); 180 id = base::HexEncode(random_data, sizeof(random_data));
157 } while (toplevel_map_.find(id) != toplevel_map_.end()); 181 } while (toplevel_map_.find(id) != toplevel_map_.end());
158 return id; 182 return id;
159 } 183 }
160 184
161 } // namespace fileapi 185 } // namespace fileapi
OLDNEW
« no previous file with comments | « webkit/fileapi/isolated_context.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698