p2p: Make P2PManager available from the SystemState singleton

BUG=chromium:260426
TEST=Unit tests pass
Change-Id: Iadaa531d631c8d7268f35d512ff33f8d53e1cc5e
Reviewed-on: https://chromium-review.googlesource.com/64828
Reviewed-by: David Zeuthen <zeuthen@chromium.org>
Commit-Queue: David Zeuthen <zeuthen@chromium.org>
Tested-by: David Zeuthen <zeuthen@chromium.org>
diff --git a/constants.h b/constants.h
index ce80389..e114956 100644
--- a/constants.h
+++ b/constants.h
@@ -99,6 +99,9 @@
 // two days.
 const int kMaxP2PAttemptTimeSeconds = 2*24*60*60;
 
+// The maximum number of payload files to keep in /var/cache/p2p.
+const int kMaxP2PFilesToKeep = 3;
+
 // The default number of UMA buckets for metrics.
 const int kNumDefaultUmaBuckets = 50;
 
diff --git a/mock_system_state.cc b/mock_system_state.cc
index 0ee9e5e..13581f7 100644
--- a/mock_system_state.cc
+++ b/mock_system_state.cc
@@ -14,7 +14,9 @@
     clock_(&default_clock_),
     hardware_(&default_hardware_),
     prefs_(&mock_prefs_),
-    powerwash_safe_prefs_(&mock_powerwash_safe_prefs_) {
+    powerwash_safe_prefs_(&mock_powerwash_safe_prefs_),
+    p2p_manager_(&mock_p2p_manager_),
+    payload_state_(&mock_payload_state_) {
   request_params_ = &default_request_params_;
   mock_payload_state_.Initialize(this);
   mock_gpio_handler_ = new testing::NiceMock<MockGpioHandler>();
diff --git a/mock_system_state.h b/mock_system_state.h
index a666a66..de0caef 100644
--- a/mock_system_state.h
+++ b/mock_system_state.h
@@ -12,6 +12,7 @@
 
 #include "update_engine/mock_dbus_interface.h"
 #include "update_engine/mock_gpio_handler.h"
+#include "update_engine/mock_p2p_manager.h"
 #include "update_engine/mock_payload_state.h"
 #include "update_engine/clock.h"
 #include "update_engine/hardware.h"
@@ -62,7 +63,7 @@
   }
 
   inline virtual PayloadStateInterface* payload_state() {
-    return &mock_payload_state_;
+    return payload_state_;
   }
 
   inline virtual GpioHandler* gpio_handler() const {
@@ -75,6 +76,10 @@
     return request_params_;
   }
 
+  inline virtual P2PManager* p2p_manager() {
+    return p2p_manager_;
+  }
+
   // MockSystemState-specific public method.
   inline void set_connection_manager(ConnectionManager* connection_manager) {
     connection_manager_ = connection_manager;
@@ -116,11 +121,20 @@
     request_params_ = params;
   }
 
+  inline void set_p2p_manager(P2PManager *p2p_manager) {
+    p2p_manager_ = p2p_manager;
+  }
+
+  inline void set_payload_state(PayloadStateInterface *payload_state) {
+    payload_state_ = payload_state;
+  }
+
  private:
   // These are Mock objects or objects we own.
   testing::NiceMock<MetricsLibraryMock> mock_metrics_lib_;
   testing::NiceMock<PrefsMock> mock_prefs_;
   testing::NiceMock<PrefsMock> mock_powerwash_safe_prefs_;
+  testing::NiceMock<MockP2PManager> mock_p2p_manager_;
   testing::NiceMock<MockPayloadState> mock_payload_state_;
   testing::NiceMock<MockGpioHandler>* mock_gpio_handler_;
   testing::NiceMock<UpdateAttempterMock>* mock_update_attempter_;
@@ -138,6 +152,8 @@
   PrefsInterface* powerwash_safe_prefs_;
   ConnectionManager* connection_manager_;
   OmahaRequestParams* request_params_;
+  P2PManager *p2p_manager_;
+  PayloadStateInterface *payload_state_;
 };
 
 } // namespeace chromeos_update_engine
diff --git a/real_system_state.h b/real_system_state.h
index c51094e..fa7b544 100644
--- a/real_system_state.h
+++ b/real_system_state.h
@@ -14,6 +14,7 @@
 #include <update_engine/payload_state.h>
 #include <update_engine/prefs.h>
 #include <update_engine/update_attempter.h>
+#include <update_engine/p2p_manager.h>
 
 namespace chromeos_update_engine {
 
@@ -79,6 +80,10 @@
     return &request_params_;
   }
 
+  virtual inline P2PManager* p2p_manager() {
+    return p2p_manager_.get();
+  }
+
   virtual inline bool system_rebooted(){
     return system_rebooted_;
   }
@@ -130,6 +135,8 @@
   // Common parameters for all Omaha requests.
   OmahaRequestParams request_params_;
 
+  scoped_ptr<P2PManager> p2p_manager_;
+
   // If true, this is the first instance of the update engine since the system
   // rebooted. Important for tracking whether you are running instance of the
   // update engine on first boot or due to a crash/restart.
diff --git a/system_state.cc b/system_state.cc
index d9d0818..5465196 100644
--- a/system_state.cc
+++ b/system_state.cc
@@ -15,6 +15,7 @@
     : device_policy_(NULL),
       connection_manager_(this),
       request_params_(this),
+      p2p_manager_(NULL),
       system_rebooted_(false) {}
 
 bool RealSystemState::Initialize(bool enable_gpio) {
@@ -38,6 +39,9 @@
     system_rebooted_ = true;
   }
 
+  p2p_manager_.reset(P2PManager::Construct(NULL, &prefs_, "cros_au",
+                                           kMaxP2PFilesToKeep));
+
   if (!payload_state_.Initialize(this))
     return false;
 
diff --git a/system_state.h b/system_state.h
index 08092f1..f4bd19c 100644
--- a/system_state.h
+++ b/system_state.h
@@ -24,6 +24,7 @@
 class PayloadStateInterface;
 class GpioHandler;
 class UpdateAttempter;
+class P2PManager;
 
 // An interface to global system context, including platform resources,
 // the current state of the system, high-level objects whose lifetime is same
@@ -83,6 +84,9 @@
   // common to all Omaha requests.
   virtual OmahaRequestParams* request_params() = 0;
 
+  // Returns a pointer to the P2PManager singleton.
+  virtual P2PManager* p2p_manager() = 0;
+
   // If true, this is the first instance of the update engine since the system
   // restarted. Important for tracking whether you are running instance of the
   // update engine on first boot or due to a crash/restart.