Steve Fung | 921ec21 | 2015-01-14 02:04:48 -0800 | [diff] [blame] | 1 | // 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 Grant | 53f8e48 | 2015-07-08 17:56:38 -0700 | [diff] [blame] | 8 | #include <memory> |
Steve Fung | 921ec21 | 2015-01-14 02:04:48 -0800 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <utility> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include <base/macros.h> |
Eric Caruso | 3d9034b | 2018-11-28 10:58:26 -0800 | [diff] [blame] | 14 | #include <base/time/time.h> |
Steve Fung | 921ec21 | 2015-01-14 02:04:48 -0800 | [diff] [blame] | 15 | |
Reilly Grant | 3636d1e | 2015-06-04 12:14:02 -0700 | [diff] [blame] | 16 | #include "permission_broker/rule.h" |
Reilly Grant | 53f8e48 | 2015-07-08 17:56:38 -0700 | [diff] [blame] | 17 | #include "permission_broker/udev_scopers.h" |
Steve Fung | 921ec21 | 2015-01-14 02:04:48 -0800 | [diff] [blame] | 18 | |
Reilly Grant | 3636d1e | 2015-06-04 12:14:02 -0700 | [diff] [blame] | 19 | struct udev; |
| 20 | |
| 21 | namespace permission_broker { |
Steve Fung | 921ec21 | 2015-01-14 02:04:48 -0800 | [diff] [blame] | 22 | |
Reilly Grant | 53f8e48 | 2015-07-08 17:56:38 -0700 | [diff] [blame] | 23 | class UdevRule; |
| 24 | |
Steve Fung | 921ec21 | 2015-01-14 02:04:48 -0800 | [diff] [blame] | 25 | class RuleEngine { |
| 26 | public: |
Eric Caruso | 3d9034b | 2018-11-28 10:58:26 -0800 | [diff] [blame] | 27 | RuleEngine(const std::string& udev_run_path, |
| 28 | const base::TimeDelta& poll_interval); |
Steve Fung | 921ec21 | 2015-01-14 02:04:48 -0800 | [diff] [blame] | 29 | 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 Grant | 3636d1e | 2015-06-04 12:14:02 -0700 | [diff] [blame] | 40 | Rule::Result ProcessPath(const std::string& path); |
Steve Fung | 921ec21 | 2015-01-14 02:04:48 -0800 | [diff] [blame] | 41 | |
| 42 | protected: |
| 43 | // This constructor is for use by test code only. |
Reilly Grant | c1ac798 | 2015-03-25 18:50:03 -0700 | [diff] [blame] | 44 | RuleEngine(); |
Steve Fung | 921ec21 | 2015-01-14 02:04:48 -0800 | [diff] [blame] | 45 | |
| 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 Grant | 53f8e48 | 2015-07-08 17:56:38 -0700 | [diff] [blame] | 54 | // 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 Fung | 921ec21 | 2015-01-14 02:04:48 -0800 | [diff] [blame] | 59 | |
Eric Caruso | 3d9034b | 2018-11-28 10:58:26 -0800 | [diff] [blame] | 60 | base::TimeDelta poll_interval_; |
Steve Fung | 921ec21 | 2015-01-14 02:04:48 -0800 | [diff] [blame] | 61 | 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_ |