blob: e55c32157350e727779308ffed196c33d5b5ef26 [file] [log] [blame]
Steve Fung921ec212015-01-14 02:04:48 -08001// Copyright 2015 The Chromium OS 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#ifndef PERMISSION_BROKER_RULE_ENGINE_H_
6#define PERMISSION_BROKER_RULE_ENGINE_H_
7
Reilly Grant53f8e482015-07-08 17:56:38 -07008#include <memory>
Steve Fung921ec212015-01-14 02:04:48 -08009#include <string>
10#include <utility>
11#include <vector>
12
13#include <base/macros.h>
Eric Caruso3d9034b2018-11-28 10:58:26 -080014#include <base/time/time.h>
Steve Fung921ec212015-01-14 02:04:48 -080015
Reilly Grant3636d1e2015-06-04 12:14:02 -070016#include "permission_broker/rule.h"
Reilly Grant53f8e482015-07-08 17:56:38 -070017#include "permission_broker/udev_scopers.h"
Steve Fung921ec212015-01-14 02:04:48 -080018
Reilly Grant3636d1e2015-06-04 12:14:02 -070019struct udev;
20
21namespace permission_broker {
Steve Fung921ec212015-01-14 02:04:48 -080022
Reilly Grant53f8e482015-07-08 17:56:38 -070023class UdevRule;
24
Steve Fung921ec212015-01-14 02:04:48 -080025class RuleEngine {
26 public:
Eric Caruso3d9034b2018-11-28 10:58:26 -080027 RuleEngine(const std::string& udev_run_path,
28 const base::TimeDelta& poll_interval);
Steve Fung921ec212015-01-14 02:04:48 -080029 virtual ~RuleEngine();
30
31 // Adds |rule| to the end of the existing rule chain. Takes ownership of
32 // |rule|.
33 void AddRule(Rule* rule);
34
35 // Invokes each of the rules in order on |path| until either a rule explicitly
36 // denies access to the path or until there are no more rules left. If, after
37 // executing all of the stored rules, no rule has explicitly allowed access to
38 // the path then access is denied. If _any_ rule denies access to |path| then
39 // processing the rules is aborted early and access is denied.
Reilly Grant3636d1e2015-06-04 12:14:02 -070040 Rule::Result ProcessPath(const std::string& path);
Steve Fung921ec212015-01-14 02:04:48 -080041
42 protected:
43 // This constructor is for use by test code only.
Reilly Grantc1ac7982015-03-25 18:50:03 -070044 RuleEngine();
Qijiang Fan6bc59e12020-11-11 02:51:06 +090045 RuleEngine(const RuleEngine&) = delete;
46 RuleEngine& operator=(const RuleEngine&) = delete;
Steve Fung921ec212015-01-14 02:04:48 -080047
48 private:
49 friend class RuleEngineTest;
50
51 // Waits for all queued udev events to complete before returning. Is
52 // equivalent to invoking 'udevadm settle', but without the external
53 // dependency and overhead.
54 virtual void WaitForEmptyUdevQueue();
55
Reilly Grant53f8e482015-07-08 17:56:38 -070056 // Finds the udev_device where udev_device_get_devnode returns |path|.
57 ScopedUdevDevicePtr FindUdevDevice(const std::string& path);
58
59 ScopedUdevPtr udev_;
60 std::vector<std::unique_ptr<Rule>> rules_;
Steve Fung921ec212015-01-14 02:04:48 -080061
Eric Caruso3d9034b2018-11-28 10:58:26 -080062 base::TimeDelta poll_interval_;
Steve Fung921ec212015-01-14 02:04:48 -080063 std::string udev_run_path_;
Steve Fung921ec212015-01-14 02:04:48 -080064};
65
66} // namespace permission_broker
67
68#endif // PERMISSION_BROKER_RULE_ENGINE_H_