Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -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 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 5 | #include "update_engine/policy_manager/policy_manager.h" |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 6 | |
| 7 | #include "update_engine/policy_manager/chromeos_policy.h" |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 8 | #include "update_engine/policy_manager/real_state.h" |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 9 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 10 | using base::Closure; |
| 11 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 12 | namespace chromeos_policy_manager { |
| 13 | |
| 14 | template <typename T> |
| 15 | bool InitProvider(scoped_ptr<T>* handle_ptr, T* provider) { |
| 16 | handle_ptr->reset(provider); |
| 17 | return handle_ptr->get() && (*handle_ptr)->Init(); |
| 18 | } |
| 19 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 20 | bool PolicyManager::Init(chromeos_update_engine::DBusWrapperInterface* dbus, |
| 21 | chromeos_update_engine::ClockInterface* clock) { |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 22 | // TODO(deymo): Make it possible to replace this policy with a different |
| 23 | // implementation with a build-time flag. |
| 24 | policy_.reset(new ChromeOSPolicy()); |
| 25 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 26 | state_.reset(new RealState(dbus, clock)); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 27 | |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 28 | return state_->Init(); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 29 | } |
| 30 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 31 | void PolicyManager::RunFromMainLoop(const Closure& callback) { |
| 32 | Closure* callback_p = new base::Closure(callback); |
| 33 | g_idle_add_full(G_PRIORITY_DEFAULT, |
| 34 | OnRanFromMainLoop, |
| 35 | reinterpret_cast<gpointer>(callback_p), |
| 36 | NULL); |
| 37 | } |
| 38 | |
| 39 | void PolicyManager::RunFromMainLoopAfterTimeout( |
| 40 | const Closure& callback, |
| 41 | base::TimeDelta timeout) { |
| 42 | Closure* callback_p = new Closure(callback); |
| 43 | g_timeout_add_seconds(timeout.InSeconds(), |
| 44 | OnRanFromMainLoop, |
| 45 | reinterpret_cast<gpointer>(callback_p)); |
| 46 | } |
| 47 | |
| 48 | gboolean PolicyManager::OnRanFromMainLoop(gpointer user_data) { |
| 49 | Closure* callback_p = reinterpret_cast<Closure*>(user_data); |
| 50 | callback_p->Run(); |
| 51 | delete callback_p; |
| 52 | return FALSE; // Removes the source since a callback can only be called once. |
| 53 | } |
| 54 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 55 | } // namespace chromeos_policy_manager |