blob: 4416b2662d9f6cfff4be209f2f5f472d96afc743 [file] [log] [blame]
David Pursellbdf81e72014-11-03 17:28:09 -08001// Copyright 2014 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// This file provides the RestrictedToolWrapper template class, which helps
6// control access to tools that should not always be available for use. Typical
7// usage will look something like this:
8//
9// // Instantiate the tool wrapper.
10// RestrictedToolWrapper<FooTool>* foo_tool_wrapper =
11// new RestrictedToolWrapper<FooTool>(...);
12//
13// // Unwrap and use the tool.
Eric Carusocc7106c2017-04-27 14:22:42 -070014// brillo::ErrorPtr error;
David Pursellbdf81e72014-11-03 17:28:09 -080015// int result = 0;
Eric Caruso7e432462017-04-26 17:31:03 -070016// FooTool* tool = foo_tool_wrapper->GetTool(&error);
17// if (tool)
18// tool->ToolFunction(&error);
David Pursellbdf81e72014-11-03 17:28:09 -080019//
20// Some advantages of using a wrapper rather than putting the condition check
21// inside the tool functions themselves are:
22// 1. Conditions are declared in a single location during tool instantiation,
23// rather than being spread around into each tool implementation.
24// 2. The compiler prevents forgotten condition checks, since trying to use a
25// wrapper directly will cause compilation errors. This becomes important
26// with multiple access-restricted functions to avoid having to manually
27// put the right condition in each one.
28// 3. Reusability - currently only the DevFeaturesTool class is wrapped,
29// but the template wrapper could be applied to future classes without
30// any condition logic in the classes themselves.
31
32#ifndef DEBUGD_SRC_RESTRICTED_TOOL_WRAPPER_H_
33#define DEBUGD_SRC_RESTRICTED_TOOL_WRAPPER_H_
34
David Pursellbdf81e72014-11-03 17:28:09 -080035#include <base/macros.h>
Eric Carusocc7106c2017-04-27 14:22:42 -070036#include <brillo/errors/error.h>
David Pursellbdf81e72014-11-03 17:28:09 -080037
38#include "debugd/src/dev_mode_no_owner_restriction.h"
39
40namespace debugd {
41
42// Templated wrapper to enforce tool access restrictions. See comments at the
43// top of the file for usage notes.
44template <class T>
45class RestrictedToolWrapper {
46 public:
47 // Tools without a default constructor may need specialized
48 // RestrictedToolWrapper classes for additional constructor parameters. If
49 // possible, use a tool Initialize() function instead of passing additional
50 // parameters to the constructor.
Eric Carusocc7106c2017-04-27 14:22:42 -070051 explicit RestrictedToolWrapper(scoped_refptr<dbus::Bus> bus)
52 : restriction_(bus) {}
Qijiang Fan6bc59e12020-11-11 02:51:06 +090053 RestrictedToolWrapper(const RestrictedToolWrapper&) = delete;
54 RestrictedToolWrapper& operator=(const RestrictedToolWrapper&) = delete;
David Pursellbdf81e72014-11-03 17:28:09 -080055
56 ~RestrictedToolWrapper() = default;
57
58 // Returns a raw pointer to the underlying tool instance if both conditions
59 // from the DevModeNoOwnerRestriction class are met:
60 // 1. Device is in dev mode.
61 // 2. Device has no owner.
62 // Otherwise, returns nullptr and |error| is set (if it's non-null).
63 //
64 // Do not store the direct tool pointer longer than needed for immediate use,
Eric Caruso7e432462017-04-26 17:31:03 -070065 // to avoid bypassing the wrapper's condition checks.
Eric Carusocc7106c2017-04-27 14:22:42 -070066 T* GetTool(brillo::ErrorPtr* error) {
David Pursellbdf81e72014-11-03 17:28:09 -080067 if (restriction_.AllowToolUse(error)) {
68 return &tool_;
69 }
70 return nullptr;
71 }
72
Tom Hughesd6c2d392020-08-24 18:12:11 -070073 const DevModeNoOwnerRestriction& restriction() const { return restriction_; }
Xiaohui Chena8bced82015-02-27 10:35:26 -080074
David Pursellbdf81e72014-11-03 17:28:09 -080075 private:
76 T tool_;
77 DevModeNoOwnerRestriction restriction_;
David Pursellbdf81e72014-11-03 17:28:09 -080078};
79
80} // namespace debugd
81
82#endif // DEBUGD_SRC_RESTRICTED_TOOL_WRAPPER_H_