AU/PM: Easier destruction of provider variables
This affords as simple, automated and safe destruction of global
Variable pointer.
* We define a ScopedPtrVarCloser<T> class, which frees and nullifies the
pointer it is pointing to. This is placed in provider_utils.h (for
now), for use by provider implementations.
* Accordingly, it is straightforward to define a closer member in
a concrete provider implementation via the typeof directive (see
random_provider.h).
* It's now safe to remove trivial desctructors in Providers, e.g. the
one of RandomProvider.
BUG=None
TEST=Builds and passes unit tests.
Change-Id: I956a0b28a6ae6d1e22e669fdc4f28e9ac5c67415
Reviewed-on: https://chromium-review.googlesource.com/184353
Tested-by: Gilad Arnold <garnold@chromium.org>
Reviewed-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Gilad Arnold <garnold@chromium.org>
diff --git a/policy_manager/generic_variables.h b/policy_manager/generic_variables.h
index e78bbbd..32ea1b9 100644
--- a/policy_manager/generic_variables.h
+++ b/policy_manager/generic_variables.h
@@ -39,8 +39,6 @@
// GetValue() method is called.
CopyVariable(const T& ref) : ref_(ref) {};
- virtual ~CopyVariable() {}
-
protected:
friend class PmCopyVariableTest;
FRIEND_TEST(PmCopyVariableTest, SimpleTest);
diff --git a/policy_manager/provider_utils.h b/policy_manager/provider_utils.h
new file mode 100644
index 0000000..8786c90
--- /dev/null
+++ b/policy_manager/provider_utils.h
@@ -0,0 +1,35 @@
+// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PROVIDER_UTILS_H
+#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PROVIDER_UTILS_H
+
+namespace chromeos_policy_manager {
+
+// Scoped closer for a pointer variable. It is initialized with a reference to
+// a pointer variable. Upon destruction, it will destruct the object pointed to
+// by the variable and nullify the variable. This template can be easily
+// instantiated via 'typeof' of the variable that is being scoped:
+//
+// ScopedPtrVarCloser<typeof(foo)> foo_closer(&foo);
+//
+// where 'foo' is pointer variable of some type.
+template<typename T>
+class ScopedPtrVarCloser {
+ public:
+ ScopedPtrVarCloser(T* ptr_var_p) : ptr_var_p_(ptr_var_p) {}
+ ~ScopedPtrVarCloser() {
+ if (ptr_var_p_) {
+ delete *ptr_var_p_;
+ *ptr_var_p_ = NULL;
+ }
+ }
+
+ private:
+ T* ptr_var_p_;
+};
+
+} // namespace chromeos_policy_manager
+
+#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_PROVIDER_UTILS_H
diff --git a/policy_manager/random_provider.cc b/policy_manager/random_provider.cc
index 70dc31b..34e82c1 100644
--- a/policy_manager/random_provider.cc
+++ b/policy_manager/random_provider.cc
@@ -9,7 +9,6 @@
#include <base/file_util.h>
#include "policy_manager/random_provider.h"
-#include "policy_manager/random_vars.h"
using std::string;
@@ -62,13 +61,6 @@
// RandomProvider implementation.
-RandomProvider::~RandomProvider(void) {
- if (var_random_seed) {
- delete var_random_seed;
- var_random_seed = NULL;
- }
-}
-
bool RandomProvider::DoInit(void) {
FILE* fp = fopen(kRandomDevice, "r");
if (!fp)
diff --git a/policy_manager/random_provider.h b/policy_manager/random_provider.h
index a610786..ae27301 100644
--- a/policy_manager/random_provider.h
+++ b/policy_manager/random_provider.h
@@ -6,19 +6,22 @@
#define CHROMEOS_PLATFORM_UPDATE_ENGINE_PM_RANDOM_PROVIDER_H
#include "policy_manager/provider.h"
+#include "policy_manager/provider_utils.h"
+#include "policy_manager/random_vars.h"
namespace chromeos_policy_manager {
// Provider of random values.
class RandomProvider : public Provider {
public:
- RandomProvider() {}
- virtual ~RandomProvider();
+ RandomProvider() : seed_closer_(&var_random_seed) {}
protected:
virtual bool DoInit();
private:
+ ScopedPtrVarCloser<typeof(var_random_seed)> seed_closer_;
+
DISALLOW_COPY_AND_ASSIGN(RandomProvider);
};
diff --git a/policy_manager/variable.h b/policy_manager/variable.h
index 79953ed..22039c0 100644
--- a/policy_manager/variable.h
+++ b/policy_manager/variable.h
@@ -12,7 +12,8 @@
namespace chromeos_policy_manager {
-// Interface to a Policy Manager variable to be implemented by the providers.
+// Interface to a Policy Manager variable. Implementation internals are hidden
+// as protected members, since policies should not be using them directly.
template<typename T>
class Variable {
public: