blob: 9c150f61814677bc5c052e0425821fb894109e74 [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.
14// DBus::Error error;
15// 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>
36#include <dbus-c++/dbus.h>
37
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.
51 explicit RestrictedToolWrapper(DBus::Connection* system_dbus)
52 : restriction_(system_dbus) {}
53
54 ~RestrictedToolWrapper() = default;
55
56 // Returns a raw pointer to the underlying tool instance if both conditions
57 // from the DevModeNoOwnerRestriction class are met:
58 // 1. Device is in dev mode.
59 // 2. Device has no owner.
60 // Otherwise, returns nullptr and |error| is set (if it's non-null).
61 //
62 // Do not store the direct tool pointer longer than needed for immediate use,
Eric Caruso7e432462017-04-26 17:31:03 -070063 // to avoid bypassing the wrapper's condition checks.
David Pursellbdf81e72014-11-03 17:28:09 -080064 T* GetTool(DBus::Error* error) {
65 if (restriction_.AllowToolUse(error)) {
66 return &tool_;
67 }
68 return nullptr;
69 }
70
Xiaohui Chena8bced82015-02-27 10:35:26 -080071 const DevModeNoOwnerRestriction& restriction() const {
72 return restriction_;
73 }
74
David Pursellbdf81e72014-11-03 17:28:09 -080075 private:
76 T tool_;
77 DevModeNoOwnerRestriction restriction_;
78
79 DISALLOW_COPY_AND_ASSIGN(RestrictedToolWrapper);
80};
81
82} // namespace debugd
83
84#endif // DEBUGD_SRC_RESTRICTED_TOOL_WRAPPER_H_