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> |
| 14 | |
Reilly Grant | 3636d1e | 2015-06-04 12:14:02 -0700 | [diff] [blame] | 15 | #include "permission_broker/rule.h" |
Reilly Grant | 53f8e48 | 2015-07-08 17:56:38 -0700 | [diff] [blame^] | 16 | #include "permission_broker/udev_scopers.h" |
Steve Fung | 921ec21 | 2015-01-14 02:04:48 -0800 | [diff] [blame] | 17 | |
Reilly Grant | 3636d1e | 2015-06-04 12:14:02 -0700 | [diff] [blame] | 18 | struct udev; |
| 19 | |
| 20 | namespace permission_broker { |
Steve Fung | 921ec21 | 2015-01-14 02:04:48 -0800 | [diff] [blame] | 21 | |
Reilly Grant | 53f8e48 | 2015-07-08 17:56:38 -0700 | [diff] [blame^] | 22 | class UdevRule; |
| 23 | |
Steve Fung | 921ec21 | 2015-01-14 02:04:48 -0800 | [diff] [blame] | 24 | class RuleEngine { |
| 25 | public: |
Reilly Grant | c1ac798 | 2015-03-25 18:50:03 -0700 | [diff] [blame] | 26 | RuleEngine(const std::string& udev_run_path, int poll_interval_msecs); |
Steve Fung | 921ec21 | 2015-01-14 02:04:48 -0800 | [diff] [blame] | 27 | virtual ~RuleEngine(); |
| 28 | |
| 29 | // Adds |rule| to the end of the existing rule chain. Takes ownership of |
| 30 | // |rule|. |
| 31 | void AddRule(Rule* rule); |
| 32 | |
| 33 | // Invokes each of the rules in order on |path| until either a rule explicitly |
| 34 | // denies access to the path or until there are no more rules left. If, after |
| 35 | // executing all of the stored rules, no rule has explicitly allowed access to |
| 36 | // the path then access is denied. If _any_ rule denies access to |path| then |
| 37 | // processing the rules is aborted early and access is denied. |
Reilly Grant | 3636d1e | 2015-06-04 12:14:02 -0700 | [diff] [blame] | 38 | Rule::Result ProcessPath(const std::string& path); |
Steve Fung | 921ec21 | 2015-01-14 02:04:48 -0800 | [diff] [blame] | 39 | |
| 40 | protected: |
| 41 | // This constructor is for use by test code only. |
Reilly Grant | c1ac798 | 2015-03-25 18:50:03 -0700 | [diff] [blame] | 42 | RuleEngine(); |
Steve Fung | 921ec21 | 2015-01-14 02:04:48 -0800 | [diff] [blame] | 43 | |
| 44 | private: |
| 45 | friend class RuleEngineTest; |
| 46 | |
| 47 | // Waits for all queued udev events to complete before returning. Is |
| 48 | // equivalent to invoking 'udevadm settle', but without the external |
| 49 | // dependency and overhead. |
| 50 | virtual void WaitForEmptyUdevQueue(); |
| 51 | |
Reilly Grant | 53f8e48 | 2015-07-08 17:56:38 -0700 | [diff] [blame^] | 52 | // Finds the udev_device where udev_device_get_devnode returns |path|. |
| 53 | ScopedUdevDevicePtr FindUdevDevice(const std::string& path); |
| 54 | |
| 55 | ScopedUdevPtr udev_; |
| 56 | std::vector<std::unique_ptr<Rule>> rules_; |
Steve Fung | 921ec21 | 2015-01-14 02:04:48 -0800 | [diff] [blame] | 57 | |
| 58 | int poll_interval_msecs_; |
| 59 | std::string udev_run_path_; |
| 60 | |
| 61 | DISALLOW_COPY_AND_ASSIGN(RuleEngine); |
| 62 | }; |
| 63 | |
| 64 | } // namespace permission_broker |
| 65 | |
| 66 | #endif // PERMISSION_BROKER_RULE_ENGINE_H_ |