blob: a8e8a7a4c6c2c4f77ef3d6b1b96e1449449010ae [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();
Steve Fung921ec212015-01-14 02:04:48 -080045
46 private:
47 friend class RuleEngineTest;
48
49 // Waits for all queued udev events to complete before returning. Is
50 // equivalent to invoking 'udevadm settle', but without the external
51 // dependency and overhead.
52 virtual void WaitForEmptyUdevQueue();
53
Reilly Grant53f8e482015-07-08 17:56:38 -070054 // Finds the udev_device where udev_device_get_devnode returns |path|.
55 ScopedUdevDevicePtr FindUdevDevice(const std::string& path);
56
57 ScopedUdevPtr udev_;
58 std::vector<std::unique_ptr<Rule>> rules_;
Steve Fung921ec212015-01-14 02:04:48 -080059
Eric Caruso3d9034b2018-11-28 10:58:26 -080060 base::TimeDelta poll_interval_;
Steve Fung921ec212015-01-14 02:04:48 -080061 std::string udev_run_path_;
62
63 DISALLOW_COPY_AND_ASSIGN(RuleEngine);
64};
65
66} // namespace permission_broker
67
68#endif // PERMISSION_BROKER_RULE_ENGINE_H_