update_engine: Watch file descriptors using chromeos::MessageLoop.

This patch removes all the calls to g_io_*() functions used to create
io_channels from a file descriptor and watch for them in the main loop.
Instead, we use the chromeos::MessageLoop backed with the glib
implementation.

This patch also removes the duplicated process handling work done in
P2PManager and uses the common Subprocess class instead.

BUG=chromium:499886
TEST=Added and updated unittests.

Change-Id: Ia093b060d2396325fce69b2bbdb62957ba7bfbc6
Reviewed-on: https://chromium-review.googlesource.com/284593
Tested-by: Alex Deymo <deymo@chromium.org>
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
Trybot-Ready: Alex Deymo <deymo@chromium.org>
diff --git a/postinstall_runner_action_unittest.cc b/postinstall_runner_action_unittest.cc
index 0460c6c..4549272 100644
--- a/postinstall_runner_action_unittest.cc
+++ b/postinstall_runner_action_unittest.cc
@@ -15,12 +15,16 @@
 #include <base/files/file_util.h>
 #include <base/strings/string_util.h>
 #include <base/strings/stringprintf.h>
+#include <chromeos/bind_lambda.h>
+#include <chromeos/message_loops/glib_message_loop.h>
+#include <chromeos/message_loops/message_loop_utils.h>
 #include <gtest/gtest.h>
 
 #include "update_engine/constants.h"
 #include "update_engine/test_utils.h"
 #include "update_engine/utils.h"
 
+using chromeos::MessageLoop;
 using chromeos_update_engine::test_utils::System;
 using chromeos_update_engine::test_utils::WriteFileString;
 using std::string;
@@ -29,34 +33,34 @@
 
 namespace chromeos_update_engine {
 
-namespace {
-gboolean StartProcessorInRunLoop(gpointer data) {
-  ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
-  processor->StartProcessing();
-  return FALSE;
-}
-}  // namespace
-
 class PostinstallRunnerActionTest : public ::testing::Test {
- public:
+ protected:
+  void SetUp() override {
+    loop_.SetAsCurrent();
+  }
+
+  void TearDown() override {
+    EXPECT_EQ(0, chromeos::MessageLoopRunMaxIterations(&loop_, 1));
+  }
+
   // DoTest with various combinations of do_losetup, err_code and
   // powerwash_required.
   void DoTest(bool do_losetup, int err_code, bool powerwash_required);
 
  private:
   static const char* kImageMountPointTemplate;
+
+  chromeos::GlibMessageLoop loop_;
 };
 
 class PostinstActionProcessorDelegate : public ActionProcessorDelegate {
  public:
   PostinstActionProcessorDelegate()
-      : loop_(nullptr),
-        code_(ErrorCode::kError),
+      : code_(ErrorCode::kError),
         code_set_(false) {}
   void ProcessingDone(const ActionProcessor* processor,
                       ErrorCode code) {
-    ASSERT_TRUE(loop_);
-    g_main_loop_quit(loop_);
+    MessageLoop::current()->BreakLoop();
   }
   void ActionCompleted(ActionProcessor* processor,
                        AbstractAction* action,
@@ -66,38 +70,31 @@
       code_set_ = true;
     }
   }
-  GMainLoop* loop_;
   ErrorCode code_;
   bool code_set_;
 };
 
 TEST_F(PostinstallRunnerActionTest, RunAsRootSimpleTest) {
-  ASSERT_EQ(0, getuid());
   DoTest(true, 0, false);
 }
 
 TEST_F(PostinstallRunnerActionTest, RunAsRootPowerwashRequiredTest) {
-  ASSERT_EQ(0, getuid());
   DoTest(true, 0, true);
 }
 
 TEST_F(PostinstallRunnerActionTest, RunAsRootCantMountTest) {
-  ASSERT_EQ(0, getuid());
   DoTest(false, 0, true);
 }
 
 TEST_F(PostinstallRunnerActionTest, RunAsRootErrScriptTest) {
-  ASSERT_EQ(0, getuid());
   DoTest(true, 1, false);
 }
 
 TEST_F(PostinstallRunnerActionTest, RunAsRootFirmwareBErrScriptTest) {
-  ASSERT_EQ(0, getuid());
   DoTest(true, 3, false);
 }
 
 TEST_F(PostinstallRunnerActionTest, RunAsRootFirmwareROErrScriptTest) {
-  ASSERT_EQ(0, getuid());
   DoTest(true, 4, false);
 }
 
@@ -189,11 +186,9 @@
   processor.EnqueueAction(&collector_action);
   processor.set_delegate(&delegate);
 
-  GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
-  delegate.loop_ = loop;
-  g_timeout_add(0, &StartProcessorInRunLoop, &processor);
-  g_main_loop_run(loop);
-  g_main_loop_unref(loop);
+  loop_.PostTask(FROM_HERE,
+                 base::Bind([&processor] { processor.StartProcessing(); }));
+  loop_.Run();
   ASSERT_FALSE(processor.IsRunning());
 
   EXPECT_TRUE(delegate.code_set_);