blob: 425000d882b7430d16929d0abe38537ffd2821a1 [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>
14
Reilly Grant3636d1e2015-06-04 12:14:02 -070015#include "permission_broker/rule.h"
Reilly Grant53f8e482015-07-08 17:56:38 -070016#include "permission_broker/udev_scopers.h"
Steve Fung921ec212015-01-14 02:04:48 -080017
Reilly Grant3636d1e2015-06-04 12:14:02 -070018struct udev;
19
20namespace permission_broker {
Steve Fung921ec212015-01-14 02:04:48 -080021
Reilly Grant53f8e482015-07-08 17:56:38 -070022class UdevRule;
23
Steve Fung921ec212015-01-14 02:04:48 -080024class RuleEngine {
25 public:
Reilly Grantc1ac7982015-03-25 18:50:03 -070026 RuleEngine(const std::string& udev_run_path, int poll_interval_msecs);
Steve Fung921ec212015-01-14 02:04:48 -080027 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 Grant3636d1e2015-06-04 12:14:02 -070038 Rule::Result ProcessPath(const std::string& path);
Steve Fung921ec212015-01-14 02:04:48 -080039
40 protected:
41 // This constructor is for use by test code only.
Reilly Grantc1ac7982015-03-25 18:50:03 -070042 RuleEngine();
Steve Fung921ec212015-01-14 02:04:48 -080043
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 Grant53f8e482015-07-08 17:56:38 -070052 // 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 Fung921ec212015-01-14 02:04:48 -080057
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_