David Pursell | bdf81e7 | 2014-11-03 17:28:09 -0800 | [diff] [blame] | 1 | // 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 Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 14 | // brillo::ErrorPtr error; |
David Pursell | bdf81e7 | 2014-11-03 17:28:09 -0800 | [diff] [blame] | 15 | // int result = 0; |
Eric Caruso | 7e43246 | 2017-04-26 17:31:03 -0700 | [diff] [blame] | 16 | // FooTool* tool = foo_tool_wrapper->GetTool(&error); |
| 17 | // if (tool) |
| 18 | // tool->ToolFunction(&error); |
David Pursell | bdf81e7 | 2014-11-03 17:28:09 -0800 | [diff] [blame] | 19 | // |
| 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 Pursell | bdf81e7 | 2014-11-03 17:28:09 -0800 | [diff] [blame] | 35 | #include <base/macros.h> |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 36 | #include <brillo/errors/error.h> |
David Pursell | bdf81e7 | 2014-11-03 17:28:09 -0800 | [diff] [blame] | 37 | |
| 38 | #include "debugd/src/dev_mode_no_owner_restriction.h" |
| 39 | |
| 40 | namespace debugd { |
| 41 | |
| 42 | // Templated wrapper to enforce tool access restrictions. See comments at the |
| 43 | // top of the file for usage notes. |
| 44 | template <class T> |
| 45 | class 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 Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 51 | explicit RestrictedToolWrapper(scoped_refptr<dbus::Bus> bus) |
| 52 | : restriction_(bus) {} |
David Pursell | bdf81e7 | 2014-11-03 17:28:09 -0800 | [diff] [blame] | 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 Caruso | 7e43246 | 2017-04-26 17:31:03 -0700 | [diff] [blame] | 63 | // to avoid bypassing the wrapper's condition checks. |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 64 | T* GetTool(brillo::ErrorPtr* error) { |
David Pursell | bdf81e7 | 2014-11-03 17:28:09 -0800 | [diff] [blame] | 65 | if (restriction_.AllowToolUse(error)) { |
| 66 | return &tool_; |
| 67 | } |
| 68 | return nullptr; |
| 69 | } |
| 70 | |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 71 | const DevModeNoOwnerRestriction& restriction() const { return restriction_; } |
Xiaohui Chen | a8bced8 | 2015-02-27 10:35:26 -0800 | [diff] [blame] | 72 | |
David Pursell | bdf81e7 | 2014-11-03 17:28:09 -0800 | [diff] [blame] | 73 | private: |
| 74 | T tool_; |
| 75 | DevModeNoOwnerRestriction restriction_; |
| 76 | |
| 77 | DISALLOW_COPY_AND_ASSIGN(RestrictedToolWrapper); |
| 78 | }; |
| 79 | |
| 80 | } // namespace debugd |
| 81 | |
| 82 | #endif // DEBUGD_SRC_RESTRICTED_TOOL_WRAPPER_H_ |