Alex Deymo | 272d949 | 2014-02-03 20:28:40 -0800 | [diff] [blame^] | 1 | // Copyright (c) 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 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_FAKE_VARIABLE_H |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_FAKE_VARIABLE_H |
| 7 | |
| 8 | #include <base/memory/scoped_ptr.h> |
| 9 | |
| 10 | #include "policy_manager/variable.h" |
| 11 | |
| 12 | namespace chromeos_policy_manager { |
| 13 | |
| 14 | // A fake typed variable to use while testing policy implementations. The |
| 15 | // variable can be instructed to return any object of its type. |
| 16 | template<typename T> |
| 17 | class FakeVariable : public Variable<T> { |
| 18 | public: |
| 19 | explicit FakeVariable(const std::string& name) : Variable<T>(name) {} |
| 20 | virtual ~FakeVariable() {} |
| 21 | |
| 22 | // Sets the next value of this variable to the passed |p_value| pointer. Once |
| 23 | // returned by GetValue(), the pointer is released and has to be set again. |
| 24 | // A value of NULL means that the GetValue() call will fail and return NULL. |
| 25 | void reset(const T* p_value) { |
| 26 | ptr_.reset(p_value); |
| 27 | } |
| 28 | |
| 29 | protected: |
| 30 | // Variable<T> overrides. |
| 31 | // Returns the pointer set with reset(). The ownership of the object is passed |
| 32 | // to the caller and the pointer is release from the FakeVariable. A second |
| 33 | // call to GetValue() without reset() will return NULL and set the error |
| 34 | // message. |
| 35 | virtual const T* GetValue(base::TimeDelta /* timeout */, |
| 36 | std::string* errmsg) { |
| 37 | if (ptr_ == NULL && errmsg != NULL) |
| 38 | *errmsg = this->GetName() + " is an empty FakeVariable"; |
| 39 | // Passes the pointer ownership to the caller. |
| 40 | return ptr_.release(); |
| 41 | } |
| 42 | |
| 43 | private: |
| 44 | // The pointer returned by GetValue(). |
| 45 | scoped_ptr<const T> ptr_; |
| 46 | }; |
| 47 | |
| 48 | } // namespace chromeos_policy_manager |
| 49 | |
| 50 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_FAKE_VARIABLE_H |