blob: 8bf3fabc30c0555780c3f538b0091ce1543d9ecf [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2013 The Chromium Authors
[email protected]b777b332011-04-16 04:01:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Yeunjoo Choief8a4a42022-11-08 04:21:055#include "chrome/browser/ash/fileapi/file_access_permissions.h"
[email protected]b777b332011-04-16 04:01:086
Hans Wennborg1790e6b2020-04-24 19:10:337#include "base/check.h"
[email protected]b777b332011-04-16 04:01:088
Yeunjoo Choi3d9ed38a2022-11-10 02:51:249namespace ash {
[email protected]b777b332011-04-16 04:01:0810
Sorin Jianu091a41a2024-12-03 00:51:4411FileAccessPermissions::FileAccessPermissions() = default;
[email protected]b777b332011-04-16 04:01:0812
Sorin Jianu091a41a2024-12-03 00:51:4413FileAccessPermissions::~FileAccessPermissions() = default;
[email protected]b777b332011-04-16 04:01:0814
Bo Majewski762d913a2021-06-30 03:54:2515void FileAccessPermissions::GrantAccessPermission(const url::Origin& origin,
Bo Majewskic0e8a982021-02-24 03:57:2116 const base::FilePath& path) {
[email protected]c392aa52014-01-30 06:25:1217 DCHECK(!path.empty());
[email protected]b777b332011-04-16 04:01:0818 base::AutoLock locker(lock_);
Bo Majewskic0e8a982021-02-24 03:57:2119 path_map_[origin].insert(path);
[email protected]b777b332011-04-16 04:01:0820}
21
22bool FileAccessPermissions::HasAccessPermission(
Bo Majewski762d913a2021-06-30 03:54:2523 const url::Origin& origin,
Bo Majewskic0e8a982021-02-24 03:57:2124 const base::FilePath& path) const {
[email protected]b777b332011-04-16 04:01:0825 base::AutoLock locker(lock_);
Bo Majewskic0e8a982021-02-24 03:57:2126 PathAccessMap::const_iterator path_map_iter = path_map_.find(origin);
[email protected]b777b332011-04-16 04:01:0827 if (path_map_iter == path_map_.end())
28 return false;
[email protected]c392aa52014-01-30 06:25:1229 const PathSet& path_set = path_map_iter->second;
30
[email protected]b777b332011-04-16 04:01:0831 // Check this file and walk up its directory tree to find if this extension
32 // has access to it.
[email protected]a3ef4832013-02-02 05:12:3333 base::FilePath current_path = path.StripTrailingSeparators();
34 base::FilePath last_path;
[email protected]b777b332011-04-16 04:01:0835 while (current_path != last_path) {
[email protected]c392aa52014-01-30 06:25:1236 if (path_set.find(current_path) != path_set.end())
[email protected]b777b332011-04-16 04:01:0837 return true;
38 last_path = current_path;
39 current_path = current_path.DirName();
40 }
41 return false;
42}
43
Bo Majewski762d913a2021-06-30 03:54:2544void FileAccessPermissions::RevokePermissions(const url::Origin& origin) {
[email protected]b777b332011-04-16 04:01:0845 base::AutoLock locker(lock_);
Bo Majewski762d913a2021-06-30 03:54:2546 path_map_.erase(origin);
[email protected]b777b332011-04-16 04:01:0847}
48
Yeunjoo Choi3d9ed38a2022-11-10 02:51:2449} // namespace ash