| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/chromeos/fileapi/file_access_permissions.h" | 5 #include "chrome/browser/chromeos/fileapi/file_access_permissions.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | |
| 8 #include "base/logging.h" | 7 #include "base/logging.h" |
| 9 | 8 |
| 10 namespace chromeos { | 9 namespace chromeos { |
| 11 | 10 |
| 11 namespace { |
| 12 |
| 13 // Empty path is prefix of any other paths, hence it represents full permission. |
| 14 base::FilePath FullPermission() { return base::FilePath(); } |
| 15 |
| 16 } // namespace |
| 17 |
| 12 FileAccessPermissions::FileAccessPermissions() {} | 18 FileAccessPermissions::FileAccessPermissions() {} |
| 13 | 19 |
| 14 FileAccessPermissions::~FileAccessPermissions() {} | 20 FileAccessPermissions::~FileAccessPermissions() {} |
| 15 | 21 |
| 22 void FileAccessPermissions::GrantFullAccessPermission( |
| 23 const std::string& extension_id) { |
| 24 base::AutoLock locker(lock_); |
| 25 path_map_[extension_id].insert(FullPermission()); |
| 26 } |
| 16 | 27 |
| 17 void FileAccessPermissions::GrantAccessPermission( | 28 void FileAccessPermissions::GrantAccessPermission( |
| 18 const std::string& extension_id, const base::FilePath& path) { | 29 const std::string& extension_id, const base::FilePath& path) { |
| 30 DCHECK(!path.empty()); |
| 19 base::AutoLock locker(lock_); | 31 base::AutoLock locker(lock_); |
| 20 PathAccessMap::iterator path_map_iter = path_map_.find(extension_id); | 32 path_map_[extension_id].insert(path); |
| 21 if (path_map_iter == path_map_.end()) { | |
| 22 PathSet path_set; | |
| 23 path_set.insert(path); | |
| 24 path_map_.insert(PathAccessMap::value_type(extension_id, path_set)); | |
| 25 } else { | |
| 26 if (path_map_iter->second.find(path) != path_map_iter->second.end()) | |
| 27 return; | |
| 28 path_map_iter->second.insert(path); | |
| 29 } | |
| 30 } | 33 } |
| 31 | 34 |
| 32 bool FileAccessPermissions::HasAccessPermission( | 35 bool FileAccessPermissions::HasAccessPermission( |
| 33 const std::string& extension_id, const base::FilePath& path) const { | 36 const std::string& extension_id, const base::FilePath& path) const { |
| 34 base::AutoLock locker(lock_); | 37 base::AutoLock locker(lock_); |
| 35 PathAccessMap::const_iterator path_map_iter = path_map_.find(extension_id); | 38 PathAccessMap::const_iterator path_map_iter = path_map_.find(extension_id); |
| 36 if (path_map_iter == path_map_.end()) | 39 if (path_map_iter == path_map_.end()) |
| 37 return false; | 40 return false; |
| 41 const PathSet& path_set = path_map_iter->second; |
| 42 |
| 43 if (path_set.find(FullPermission()) != path_set.end()) |
| 44 return true; |
| 38 | 45 |
| 39 // Check this file and walk up its directory tree to find if this extension | 46 // Check this file and walk up its directory tree to find if this extension |
| 40 // has access to it. | 47 // has access to it. |
| 41 base::FilePath current_path = path.StripTrailingSeparators(); | 48 base::FilePath current_path = path.StripTrailingSeparators(); |
| 42 base::FilePath last_path; | 49 base::FilePath last_path; |
| 43 while (current_path != last_path) { | 50 while (current_path != last_path) { |
| 44 if (path_map_iter->second.find(current_path) != path_map_iter->second.end()) | 51 if (path_set.find(current_path) != path_set.end()) |
| 45 return true; | 52 return true; |
| 46 last_path = current_path; | 53 last_path = current_path; |
| 47 current_path = current_path.DirName(); | 54 current_path = current_path.DirName(); |
| 48 } | 55 } |
| 49 return false; | 56 return false; |
| 50 } | 57 } |
| 51 | 58 |
| 52 void FileAccessPermissions::RevokePermissions( | 59 void FileAccessPermissions::RevokePermissions( |
| 53 const std::string& extension_id) { | 60 const std::string& extension_id) { |
| 54 base::AutoLock locker(lock_); | 61 base::AutoLock locker(lock_); |
| 55 path_map_.erase(extension_id); | 62 path_map_.erase(extension_id); |
| 56 } | 63 } |
| 57 | 64 |
| 58 } // namespace chromeos | 65 } // namespace chromeos |
| OLD | NEW |