buffet: XmppChannel listens for connectivity changes

ShillClent provides notifications for network changes.
XmppChannel issue XMPP ping on every network change and restart if
failed. Only exception is soon scheduled upcoming reconnect.

BUG=brillo:1139
TEST=register device, disconnect network, wait device is offline in
     GCD dashboard, connect network.
     Dashboard should show device as online in less then 60 seconds.

Change-Id: I73dffc54400777b2325c26fb8aaf259b515174ce
Reviewed-on: https://chromium-review.googlesource.com/281413
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Vitaly Buka <vitalybuka@chromium.org>
Tested-by: Vitaly Buka <vitalybuka@chromium.org>
diff --git a/buffet/notification/xmpp_channel.h b/buffet/notification/xmpp_channel.h
index bdddd91..3d279e4 100644
--- a/buffet/notification/xmpp_channel.h
+++ b/buffet/notification/xmpp_channel.h
@@ -14,7 +14,6 @@
 #include <base/macros.h>
 #include <base/memory/weak_ptr.h>
 #include <base/single_thread_task_runner.h>
-#include <base/timer/timer.h>
 #include <chromeos/backoff_entry.h>
 #include <chromeos/streams/stream.h>
 
@@ -22,6 +21,10 @@
 #include "buffet/notification/xmpp_iq_stanza_handler.h"
 #include "buffet/notification/xmpp_stream_parser.h"
 
+namespace privetd {
+class ShillClient;
+}
+
 namespace buffet {
 
 // Simple interface to abstract XmppChannel's SendMessage() method.
@@ -42,7 +45,8 @@
   // so you will need to reset the XmppClient every time this happens.
   XmppChannel(const std::string& account,
               const std::string& access_token,
-              const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
+              const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
+              privetd::ShillClient* shill);
   ~XmppChannel() override = default;
 
   // Overrides from NotificationChannel.
@@ -57,7 +61,8 @@
   // Internal states for the XMPP stream.
   enum class XmppState {
     kNotStarted,
-    kStarted,
+    kConnecting,
+    kConnected,
     kTlsStarted,
     kTlsCompleted,
     kAuthenticationStarted,
@@ -74,8 +79,9 @@
   // to help provide unit-test-specific functionality.
   virtual void Connect(const std::string& host, uint16_t port,
                        const base::Closure& callback);
-  virtual void StartPingTimer();
-  virtual void StopPingTimer();
+  virtual void SchedulePing(base::TimeDelta interval, base::TimeDelta timeout);
+  void ScheduleRegularPing();
+  void ScheduleFastPing();
 
   XmppState state_{XmppState::kNotStarted};
 
@@ -119,9 +125,11 @@
 
   // Sends a ping request to the server to check if the connection is still
   // valid.
-  void PingServer();
-  void OnPingResponse(std::unique_ptr<XmlNode> reply);
-  void OnPingTimeout();
+  void PingServer(base::TimeDelta timeout);
+  void OnPingResponse(base::Time sent_time, std::unique_ptr<XmlNode> reply);
+  void OnPingTimeout(base::Time sent_time);
+
+  void OnConnectivityChanged(bool online);
 
   // Robot account name for the device.
   std::string account_;
@@ -153,8 +161,8 @@
   bool write_pending_{false};
   std::unique_ptr<IqStanzaHandler> iq_stanza_handler_;
 
-  base::Timer ping_timer_{true, true};
-
+  base::WeakPtrFactory<XmppChannel> ping_ptr_factory_{this};
+  base::WeakPtrFactory<XmppChannel> task_ptr_factory_{this};
   base::WeakPtrFactory<XmppChannel> weak_ptr_factory_{this};
   DISALLOW_COPY_AND_ASSIGN(XmppChannel);
 };