PM: Add a shill provider skeleton.
This introduces an initial implementation of a shill provider. None of
the backend works, of course; for now, this includes the following:
* Three variables used for obtaining the connectivity status, current
connection type and the last time the connection has changed. This
should suffice for computing current policy-related decisions, such as
whether/when to update depending on the connection type. However, as
I'm not entirely sure how to track the last-changed time, it might
change as the implementation ramps up. The variables are currently
initialized to a deterministic default.
* Unit tests for the existing (very minimal) functionality.
BUG=None
TEST=Builds and passes unit tests.
Change-Id: Ib4fcefb6bcbed43cd3ba7615de5eaad996fb7fb3
Reviewed-on: https://chromium-review.googlesource.com/184491
Reviewed-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
diff --git a/policy_manager/real_shill_provider.cc b/policy_manager/real_shill_provider.cc
new file mode 100644
index 0000000..29a913f
--- /dev/null
+++ b/policy_manager/real_shill_provider.cc
@@ -0,0 +1,24 @@
+// 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.
+
+#include "update_engine/policy_manager/generic_variables.h"
+#include "update_engine/policy_manager/real_shill_provider.h"
+
+namespace chromeos_policy_manager {
+
+// ShillProvider implementation.
+
+bool RealShillProvider::DoInit(void) {
+ // TODO(garnold) Initialize with actual (or fake) DBus connection.
+
+ var_is_connected_.reset(
+ new CopyVariable<bool>("is_connected", is_connected_));
+ var_conn_type_.reset(
+ new CopyVariable<ShillConnType>("conn_type", conn_type_));
+ var_conn_last_changed_.reset(
+ new CopyVariable<Time>("conn_last_changed", conn_last_changed_));
+ return true;
+}
+
+} // namespace chromeos_policy_manager
diff --git a/policy_manager/real_shill_provider.h b/policy_manager/real_shill_provider.h
new file mode 100644
index 0000000..1a120b7
--- /dev/null
+++ b/policy_manager/real_shill_provider.h
@@ -0,0 +1,44 @@
+// 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_REAL_SHILL_PROVIDER_H
+#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_REAL_SHILL_PROVIDER_H
+
+#include "update_engine/policy_manager/shill_provider.h"
+
+using base::Time;
+
+namespace chromeos_policy_manager {
+
+// ShillProvider concrete implementation.
+//
+// TODO(garnold) Much of the functionality in this module was adapted from
+// connection_manager, with slight changes (annotated inline). We need to make
+// sure to deprecate use of connection manager when the time comes.
+class RealShillProvider : public ShillProvider {
+ public:
+ // TODO(garnold) This should take a DBus object for communicating with shill.
+ RealShillProvider()
+ : is_connected_(false), conn_type_(kShillConnTypeUnknown),
+ conn_last_changed_(Time::Now()) {}
+
+ protected:
+ virtual bool DoInit();
+
+ private:
+ // Whether we have network connectivity.
+ bool is_connected_;
+
+ // The current network connection type as reported by shill.
+ ShillConnType conn_type_;
+
+ // The time when the connection type last changed.
+ Time conn_last_changed_;
+
+ DISALLOW_COPY_AND_ASSIGN(RealShillProvider);
+};
+
+} // namespace chromeos_policy_manager
+
+#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_REAL_SHILL_PROVIDER_H
diff --git a/policy_manager/real_shill_provider_unittest.cc b/policy_manager/real_shill_provider_unittest.cc
new file mode 100644
index 0000000..6bc544c
--- /dev/null
+++ b/policy_manager/real_shill_provider_unittest.cc
@@ -0,0 +1,56 @@
+// 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.
+
+#include <base/memory/scoped_ptr.h>
+#include <base/time.h>
+#include <gtest/gtest.h>
+
+#include "update_engine/policy_manager/real_shill_provider.h"
+#include "update_engine/policy_manager/pmtest_utils.h"
+
+using base::Time;
+using base::TimeDelta;
+
+namespace chromeos_policy_manager {
+
+class PmRealShillProviderTest : public ::testing::Test {
+ protected:
+ virtual void SetUp() {
+ default_timeout_ = TimeDelta::FromSeconds(1);
+
+ // The provider initializes correctly.
+ time_before_init_ = Time::Now();
+ provider_.reset(new RealShillProvider());
+ time_after_init_ = Time::Now();
+ PMTEST_ASSERT_NOT_NULL(provider_.get());
+ ASSERT_TRUE(provider_->Init());
+ }
+
+ TimeDelta default_timeout_;
+ Time time_before_init_;
+ Time time_after_init_;
+ scoped_ptr<RealShillProvider> provider_;
+};
+
+
+TEST_F(PmRealShillProviderTest, DefaultValues) {
+ // Tests that initial values were set correctly.
+ scoped_ptr<const bool> is_connected(
+ provider_->var_is_connected()->GetValue(default_timeout_, NULL));
+ PMTEST_ASSERT_NOT_NULL(is_connected.get());
+ EXPECT_FALSE(*is_connected);
+
+ scoped_ptr<const ShillConnType> conn_type(
+ provider_->var_conn_type()->GetValue(default_timeout_, NULL));
+ PMTEST_ASSERT_NOT_NULL(conn_type.get());
+ EXPECT_EQ(kShillConnTypeUnknown, *conn_type);
+
+ scoped_ptr<const Time> conn_last_changed(
+ provider_->var_conn_last_changed()->GetValue(default_timeout_, NULL));
+ PMTEST_ASSERT_NOT_NULL(conn_last_changed.get());
+ EXPECT_LE(time_before_init_, *conn_last_changed);
+ EXPECT_GE(time_after_init_, *conn_last_changed);
+}
+
+} // namespace chromeos_policy_manager
diff --git a/policy_manager/shill_provider.h b/policy_manager/shill_provider.h
new file mode 100644
index 0000000..4b56583
--- /dev/null
+++ b/policy_manager/shill_provider.h
@@ -0,0 +1,60 @@
+// 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_SHILL_PROVIDER_H
+#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_SHILL_PROVIDER_H
+
+#include <base/memory/scoped_ptr.h>
+#include <base/time.h>
+
+#include "update_engine/policy_manager/provider.h"
+#include "update_engine/policy_manager/variable.h"
+
+using base::Time;
+
+namespace chromeos_policy_manager {
+
+// TODO(garnold) Adapted from connection_manager.h.
+enum ShillConnType {
+ kShillConnTypeEthernet = 0,
+ kShillConnTypeWifi,
+ kShillConnTypeWimax,
+ kShillConnTypeBluetooth,
+ kShillConnTypeCellular,
+ kShillConnTypeUnknown
+};
+
+// Provider for networking related information.
+class ShillProvider : public Provider {
+ public:
+ // Returns whether we currently have network connectivity.
+ Variable<bool>* var_is_connected() const {
+ return var_is_connected_.get();
+ }
+
+ // Returns the current network connection type. Unknown if not connected.
+ Variable<ShillConnType>* var_conn_type() const {
+ return var_conn_type_.get();
+ }
+
+ // Returns the time when network connection last changed; initialized to
+ // current time.
+ Variable<base::Time>* var_conn_last_changed() const {
+ return var_conn_last_changed_.get();
+ }
+
+ protected:
+ ShillProvider() {}
+
+ scoped_ptr<Variable<bool> > var_is_connected_;
+ scoped_ptr<Variable<ShillConnType> > var_conn_type_;
+ scoped_ptr<Variable<base::Time> > var_conn_last_changed_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(ShillProvider);
+};
+
+} // namespace chromeos_policy_manager
+
+#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_SHILL_PROVIDER_H
diff --git a/policy_manager/variable.h b/policy_manager/variable.h
index 29fa546..ce239ce 100644
--- a/policy_manager/variable.h
+++ b/policy_manager/variable.h
@@ -45,6 +45,8 @@
friend class PmRealRandomProviderTest;
FRIEND_TEST(PmRealRandomProviderTest, GetRandomValues);
+ friend class PmRealShillProviderTest;
+ FRIEND_TEST(PmRealShillProviderTest, DefaultValues);
// Gets the current value of the variable. The current value is copied to a
// new object and returned. The caller of this method owns the object and