libweave: Remove base::Timer from PullChannel

base::Timer needs base::TaskRunner, but we remove this dependency.

BUG=brillo:1256
TEST=`FEATURES=test emerge-gizmo libweave`

Change-Id: I2a0ef093a4ac90bf66df64cdb29d1037761a52b8
Reviewed-on: https://chromium-review.googlesource.com/292977
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Trybot-Ready: Vitaly Buka <vitalybuka@chromium.org>
Tested-by: Vitaly Buka <vitalybuka@chromium.org>
Commit-Queue: Vitaly Buka <vitalybuka@chromium.org>
diff --git a/libweave/src/notification/pull_channel.cc b/libweave/src/notification/pull_channel.cc
index 2f954e5..38ac0b1 100644
--- a/libweave/src/notification/pull_channel.cc
+++ b/libweave/src/notification/pull_channel.cc
@@ -13,9 +13,7 @@
 PullChannel::PullChannel(
     base::TimeDelta pull_interval,
     const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
-    : pull_interval_{pull_interval}, timer_{true, true} {
-  timer_.SetTaskRunner(task_runner);
-}
+    : pull_interval_{pull_interval}, task_runner_{task_runner} {}
 
 std::string PullChannel::GetName() const {
   return "pull";
@@ -32,23 +30,32 @@
 void PullChannel::Start(NotificationDelegate* delegate) {
   CHECK(delegate);
   delegate_ = delegate;
-  timer_.Start(
-      FROM_HERE, pull_interval_,
-      base::Bind(&PullChannel::OnTimer, weak_ptr_factory_.GetWeakPtr()));
+  RePost();
+}
+
+void PullChannel::RePost() {
+  CHECK(delegate_);
+  weak_ptr_factory_.InvalidateWeakPtrs();
+  task_runner_->PostDelayedTask(
+      FROM_HERE,
+      base::Bind(&PullChannel::OnTimer, weak_ptr_factory_.GetWeakPtr()),
+      pull_interval_);
 }
 
 void PullChannel::Stop() {
   weak_ptr_factory_.InvalidateWeakPtrs();
-  timer_.Stop();
+  delegate_ = nullptr;
 }
 
 void PullChannel::UpdatePullInterval(base::TimeDelta pull_interval) {
-  timer_.Stop();
   pull_interval_ = pull_interval;
-  Start(delegate_);
+  if (delegate_)
+    RePost();
 }
 
 void PullChannel::OnTimer() {
+  // Repost before delegate notification to give it a chance to stop channel.
+  RePost();
   base::DictionaryValue empty_dict;
   delegate_->OnCommandCreated(empty_dict);
 }
diff --git a/libweave/src/notification/pull_channel.h b/libweave/src/notification/pull_channel.h
index 79955d0..fef6d56 100644
--- a/libweave/src/notification/pull_channel.h
+++ b/libweave/src/notification/pull_channel.h
@@ -34,10 +34,11 @@
 
  private:
   void OnTimer();
+  void RePost();
 
-  NotificationDelegate* delegate_{nullptr};
   base::TimeDelta pull_interval_;
-  base::Timer timer_;
+  scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
+  NotificationDelegate* delegate_{nullptr};
 
   base::WeakPtrFactory<PullChannel> weak_ptr_factory_{this};
   DISALLOW_COPY_AND_ASSIGN(PullChannel);