PolicyManager: Move the global variables to Provider members.
Per the Google C++ Style Guide, the global variables can't be
instances of a class and that includes smart pointers like
scoped_ptr. The alternative is to use plain pointers such as a
Variable<T>*. The disadvantage of this approach is that the memory
pointed by this global variable is managed by a singleton class
(a FooProvider) on production code and a different class during
testing (a FakeFooProvider). Also, these classes, fake and real
implementation, aren't forced to implement the same set of variables
since there's no common interface. Finally, the
construction/destruction of these variables also needs to handle the
global pointer, nulifying it after deletion.
This patch moves the global variable to a member of a base class that
hides the initialization of these variables to a subclass. This patch
includes a subclass with the real implementation.
BUG=chromium:338591
TEST=unittest passes.
Change-Id: I0aa43c51d72c65d97be280fd58307d085bb8577b
Reviewed-on: https://chromium-review.googlesource.com/184774
Tested-by: Alex Deymo <deymo@chromium.org>
Reviewed-by: Gilad Arnold <garnold@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
diff --git a/policy_manager/random_provider.h b/policy_manager/random_provider.h
index 7eaa8f6..60d7b2c 100644
--- a/policy_manager/random_provider.h
+++ b/policy_manager/random_provider.h
@@ -5,23 +5,29 @@
#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_RANDOM_PROVIDER_H
#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_RANDOM_PROVIDER_H
+#include <base/memory/scoped_ptr.h>
+
#include "policy_manager/provider.h"
-#include "policy_manager/provider_utils.h"
-#include "policy_manager/random_vars.h"
+#include "policy_manager/variable.h"
namespace chromeos_policy_manager {
// Provider of random values.
class RandomProvider : public Provider {
public:
- RandomProvider() : seed_closer_(&var_random_seed) {}
+ // Return a random number every time it is requested. Note that values
+ // returned by the variables are cached by the EvaluationContext, so the
+ // returned value will be the same during the same policy request. If more
+ // random values are needed use a PRNG seeded with this value.
+ Variable<uint64_t>* seed() const { return seed_.get(); }
protected:
- virtual bool DoInit();
+ RandomProvider() {}
+
+ // The seed() scoped variable.
+ scoped_ptr<Variable<uint64_t> > seed_;
private:
- DECLARE_VAR_CLOSER(seed_closer_, var_random_seed);
-
DISALLOW_COPY_AND_ASSIGN(RandomProvider);
};