Roll src/third_party/WebKit 8b42d1d:744641d (svn 186770:186771)
[chromium-blink-merge.git] / chrome / browser / chromeos / fileapi / file_access_permissions.cc
blobbfa1c539c861f7622a84cf9f2d03679b9cd3da46
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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/fileapi/file_access_permissions.h"
7 #include "base/logging.h"
9 namespace chromeos {
11 namespace {
13 // Empty path is prefix of any other paths, hence it represents full permission.
14 base::FilePath FullPermission() { return base::FilePath(); }
16 } // namespace
18 FileAccessPermissions::FileAccessPermissions() {}
20 FileAccessPermissions::~FileAccessPermissions() {}
22 void FileAccessPermissions::GrantFullAccessPermission(
23 const std::string& extension_id) {
24 base::AutoLock locker(lock_);
25 path_map_[extension_id].insert(FullPermission());
28 void FileAccessPermissions::GrantAccessPermission(
29 const std::string& extension_id, const base::FilePath& path) {
30 DCHECK(!path.empty());
31 base::AutoLock locker(lock_);
32 path_map_[extension_id].insert(path);
35 bool FileAccessPermissions::HasAccessPermission(
36 const std::string& extension_id, const base::FilePath& path) const {
37 base::AutoLock locker(lock_);
38 PathAccessMap::const_iterator path_map_iter = path_map_.find(extension_id);
39 if (path_map_iter == path_map_.end())
40 return false;
41 const PathSet& path_set = path_map_iter->second;
43 if (path_set.find(FullPermission()) != path_set.end())
44 return true;
46 // Check this file and walk up its directory tree to find if this extension
47 // has access to it.
48 base::FilePath current_path = path.StripTrailingSeparators();
49 base::FilePath last_path;
50 while (current_path != last_path) {
51 if (path_set.find(current_path) != path_set.end())
52 return true;
53 last_path = current_path;
54 current_path = current_path.DirName();
56 return false;
59 void FileAccessPermissions::RevokePermissions(
60 const std::string& extension_id) {
61 base::AutoLock locker(lock_);
62 path_map_.erase(extension_id);
65 } // namespace chromeos