buffet: Make periodic polling secondary if XMPP channel is up

Now that GCD server allows us to change the supported notification
channel at run-time, start with the frequent poll (every 7 seconds)
and start up XMPP channel. Once XMPP connection is established, switch
over to using XMPP as the primary command delivery mechanism and
throttle down periodic polling to once every 30 minutes.

If, for some reason, XMPP channel gets disconnected, start polling
the server frequently again, until XMPP connection is re-established.

BUG=brillo:458, brillo:713
TEST=`FEATURES=test emerge-link buffet`

Change-Id: I148a98b8229aa4597a0f6a40e596aba15265ec91
Reviewed-on: https://chromium-review.googlesource.com/273631
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/buffet/notification/pull_channel.h b/buffet/notification/pull_channel.h
new file mode 100644
index 0000000..fe89d40
--- /dev/null
+++ b/buffet/notification/pull_channel.h
@@ -0,0 +1,47 @@
+// Copyright 2015 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 BUFFET_NOTIFICATION_PULL_CHANNEL_H_
+#define BUFFET_NOTIFICATION_PULL_CHANNEL_H_
+
+#include <memory>
+#include <string>
+
+#include <base/macros.h>
+#include <base/memory/weak_ptr.h>
+#include <base/single_thread_task_runner.h>
+#include <base/timer/timer.h>
+
+#include "buffet/notification/notification_channel.h"
+
+namespace buffet {
+
+class PullChannel : public NotificationChannel {
+ public:
+  PullChannel(base::TimeDelta pull_interval,
+              const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
+  ~PullChannel() override = default;
+
+  // Overrides from NotificationChannel.
+  std::string GetName() const override;
+  void AddChannelParameters(base::DictionaryValue* channel_json) override;
+  void Start(NotificationDelegate* delegate) override;
+  void Stop() override;
+
+  void UpdatePullInterval(base::TimeDelta pull_interval);
+
+ private:
+  void OnTimer();
+
+  NotificationDelegate* delegate_{nullptr};
+  base::TimeDelta pull_interval_;
+  base::Timer timer_;
+
+  base::WeakPtrFactory<PullChannel> weak_ptr_factory_{this};
+  DISALLOW_COPY_AND_ASSIGN(PullChannel);
+};
+
+}  // namespace buffet
+
+#endif  // BUFFET_NOTIFICATION_PULL_CHANNEL_H_