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.
4
5#include "chrome/browser/chromeos/fileapi/file_access_permissions.h"
6
7#include "base/logging.h"
8
9namespace chromeos {
10
11namespace {
12
13// Empty path is prefix of any other paths, hence it represents full permission.
14base::FilePath FullPermission() { return base::FilePath(); }
15
16}  // namespace
17
18FileAccessPermissions::FileAccessPermissions() {}
19
20FileAccessPermissions::~FileAccessPermissions() {}
21
22void FileAccessPermissions::GrantFullAccessPermission(
23    const std::string& extension_id) {
24  base::AutoLock locker(lock_);
25  path_map_[extension_id].insert(FullPermission());
26}
27
28void 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);
33}
34
35bool 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;
42
43  if (path_set.find(FullPermission()) != path_set.end())
44    return true;
45
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();
55  }
56  return false;
57}
58
59void FileAccessPermissions::RevokePermissions(
60    const std::string& extension_id) {
61  base::AutoLock locker(lock_);
62  path_map_.erase(extension_id);
63}
64
65}  // namespace chromeos
66