OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/extensions/extension_special_storage_policy.h" | 5 #include "chrome/browser/extensions/extension_special_storage_policy.h" |
6 | 6 |
| 7 #include "base/command_line.h" |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "chrome/common/chrome_switches.h" |
8 #include "chrome/common/extensions/extension.h" | 10 #include "chrome/common/extensions/extension.h" |
9 #include "chrome/common/url_constants.h" | 11 #include "chrome/common/url_constants.h" |
10 | 12 |
11 ExtensionSpecialStoragePolicy::ExtensionSpecialStoragePolicy() {} | 13 ExtensionSpecialStoragePolicy::ExtensionSpecialStoragePolicy() {} |
12 | 14 |
13 ExtensionSpecialStoragePolicy::~ExtensionSpecialStoragePolicy() {} | 15 ExtensionSpecialStoragePolicy::~ExtensionSpecialStoragePolicy() {} |
14 | 16 |
15 bool ExtensionSpecialStoragePolicy::IsStorageProtected(const GURL& origin) { | 17 bool ExtensionSpecialStoragePolicy::IsStorageProtected(const GURL& origin) { |
16 if (origin.SchemeIs(chrome::kExtensionScheme)) | 18 if (origin.SchemeIs(chrome::kExtensionScheme)) |
17 return true; | 19 return true; |
18 base::AutoLock locker(lock_); | 20 base::AutoLock locker(lock_); |
19 return protected_apps_.Contains(origin); | 21 return protected_apps_.Contains(origin); |
20 } | 22 } |
21 | 23 |
22 bool ExtensionSpecialStoragePolicy::IsStorageUnlimited(const GURL& origin) { | 24 bool ExtensionSpecialStoragePolicy::IsStorageUnlimited(const GURL& origin) { |
23 base::AutoLock locker(lock_); | 25 base::AutoLock locker(lock_); |
24 return unlimited_extensions_.Contains(origin); | 26 return unlimited_extensions_.Contains(origin); |
25 } | 27 } |
26 | 28 |
27 bool ExtensionSpecialStoragePolicy::IsLocalFileSystemAccessAllowed( | 29 bool ExtensionSpecialStoragePolicy::IsLocalFileSystemAccessAllowed( |
28 const GURL& origin) { | 30 const GURL& origin, const FilePath& virtual_path) { |
29 base::AutoLock locker(lock_); | 31 base::AutoLock locker(lock_); |
30 return local_filesystem_extensions_.Contains(origin); | 32 return local_filesystem_extensions_.ContainsPath(origin, virtual_path); |
| 33 } |
| 34 |
| 35 void ExtensionSpecialStoragePolicy::GrantLocalFileSystemAccess( |
| 36 const GURL& origin, const FilePath& virtual_path) { |
| 37 base::AutoLock locker(lock_); |
| 38 local_filesystem_extensions_.AddPath(origin, virtual_path); |
31 } | 39 } |
32 | 40 |
33 void ExtensionSpecialStoragePolicy::GrantRightsForExtension( | 41 void ExtensionSpecialStoragePolicy::GrantRightsForExtension( |
34 const Extension* extension) { | 42 const Extension* extension) { |
35 DCHECK(extension); | 43 DCHECK(extension); |
36 if (!extension->is_hosted_app() && | 44 if (!extension->is_hosted_app() && |
37 !extension->HasApiPermission(Extension::kUnlimitedStoragePermission) && | 45 !extension->HasApiPermission(Extension::kUnlimitedStoragePermission) && |
38 !extension->HasApiPermission(Extension::kFileSystemPermission)) { | 46 !extension->HasApiPermission(Extension::kFileSystemPermission)) { |
39 return; | 47 return; |
40 } | 48 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 void ExtensionSpecialStoragePolicy::SpecialCollection::Remove( | 113 void ExtensionSpecialStoragePolicy::SpecialCollection::Remove( |
106 const Extension* extension) { | 114 const Extension* extension) { |
107 cached_resuts_.clear(); | 115 cached_resuts_.clear(); |
108 extensions_.erase(extension); | 116 extensions_.erase(extension); |
109 } | 117 } |
110 | 118 |
111 void ExtensionSpecialStoragePolicy::SpecialCollection::Clear() { | 119 void ExtensionSpecialStoragePolicy::SpecialCollection::Clear() { |
112 cached_resuts_.clear(); | 120 cached_resuts_.clear(); |
113 extensions_.clear(); | 121 extensions_.clear(); |
114 } | 122 } |
| 123 |
| 124 //----------------------------------------------------------------------------- |
| 125 // SpecialPathCollection helper class |
| 126 //----------------------------------------------------------------------------- |
| 127 |
| 128 ExtensionSpecialStoragePolicy::SpecialPathCollection::SpecialPathCollection() {} |
| 129 |
| 130 ExtensionSpecialStoragePolicy::SpecialPathCollection::~SpecialPathCollection() { |
| 131 } |
| 132 |
| 133 void ExtensionSpecialStoragePolicy::SpecialPathCollection::Clear() { |
| 134 ExtensionSpecialStoragePolicy::SpecialCollection::Clear(); |
| 135 path_map_.clear(); |
| 136 } |
| 137 |
| 138 void ExtensionSpecialStoragePolicy::SpecialPathCollection::Remove( |
| 139 const Extension* extension) { |
| 140 ExtensionSpecialStoragePolicy::SpecialCollection::Remove(extension); |
| 141 path_map_.erase(extension); |
| 142 } |
| 143 |
| 144 void ExtensionSpecialStoragePolicy::SpecialPathCollection::AddPath( |
| 145 const GURL& origin, const FilePath& path) { |
| 146 const Extension* extension = GetExtension(origin); |
| 147 if (!extension) |
| 148 return; |
| 149 PathAccessMap::iterator path_map_iter = path_map_.find(extension); |
| 150 if (path_map_iter == path_map_.end()) { |
| 151 PathSet path_set; |
| 152 path_set.insert(path); |
| 153 path_map_.insert(PathAccessMap::value_type(extension, path_set)); |
| 154 } else { |
| 155 if (path_map_iter->second.find(path) != path_map_iter->second.end()) |
| 156 return; |
| 157 path_map_iter->second.insert(path); |
| 158 } |
| 159 } |
| 160 |
| 161 bool ExtensionSpecialStoragePolicy::SpecialPathCollection::ContainsPath( |
| 162 const GURL& origin, const FilePath& path) { |
| 163 const Extension* extension = GetExtension(origin); |
| 164 if (!extension) |
| 165 return false; |
| 166 |
| 167 // Allow access to any local path from component extensions. |
| 168 if (extension->location() == Extension::COMPONENT |
| 169 #ifndef NDEBUG |
| 170 || CommandLine::ForCurrentProcess()->HasSwitch( |
| 171 switches::kExposePrivateExtensionApi) |
| 172 #endif |
| 173 ) { |
| 174 return true; |
| 175 } |
| 176 |
| 177 PathAccessMap::const_iterator path_map_iter = path_map_.find(extension); |
| 178 if (path_map_iter == path_map_.end()) |
| 179 return false; |
| 180 |
| 181 // Check this file and walk up its directory tree to find if this extension |
| 182 // has access to it. |
| 183 FilePath current_path = path.StripTrailingSeparators(); |
| 184 FilePath last_path; |
| 185 while (current_path != last_path) { |
| 186 if (path_map_iter->second.find(current_path) != path_map_iter->second.end()) |
| 187 return true; |
| 188 last_path = current_path; |
| 189 current_path = current_path.DirName(); |
| 190 } |
| 191 return false; |
| 192 } |
| 193 |
| 194 const Extension* |
| 195 ExtensionSpecialStoragePolicy::SpecialPathCollection::GetExtension( |
| 196 const GURL& origin) { |
| 197 for (Extensions::const_iterator iter = extensions_.begin(); |
| 198 iter != extensions_.end(); ++iter) { |
| 199 if ((*iter)->OverlapsWithOrigin(origin)) |
| 200 return (*iter); |
| 201 } |
| 202 return NULL; |
| 203 } |
| 204 |
OLD | NEW |