buffet: Hook up XMPP to deliver push notifications to buffet

Now using XMPP not only for the device presence but for delivering
push notifications, specifically COMMAND_CREATED notification and
extracting the command instance from the notification message.
Add the commands received over XMPP to the command execution queue.

The remaining tasks for XMPP (making polling optional, provide
notification channel configuration options for buffet, update the
channel changes on GCD server, etc) are coming in follow-up CLs.

BUG=brillo:458
TEST=`FEATURES=test emerge-link buffet`
     Tested this on the device.

Change-Id: I6ba42e3687563133734aaf36d3802d6f4888f348
Reviewed-on: https://chromium-review.googlesource.com/272782
Trybot-Ready: Alex Vakulenko <avakulenko@chromium.org>
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Vitaly Buka <vitalybuka@chromium.org>
Commit-Queue: Vitaly Buka <vitalybuka@chromium.org>
diff --git a/buffet/notification/xmpp_channel.cc b/buffet/notification/xmpp_channel.cc
index 3674184..75c8004 100644
--- a/buffet/notification/xmpp_channel.cc
+++ b/buffet/notification/xmpp_channel.cc
@@ -13,6 +13,7 @@
 #include <chromeos/streams/tls_stream.h>
 
 #include "buffet/notification/notification_delegate.h"
+#include "buffet/notification/notification_parser.h"
 #include "buffet/notification/xml_node.h"
 #include "buffet/utils.h"
 
@@ -213,6 +214,10 @@
       }
       break;
     default:
+      if (stanza->name() == "message") {
+        HandleMessageStanza(std::move(stanza));
+        return;
+      }
       LOG(INFO) << "Unexpected XMPP stanza ignored: " << stanza->ToString();
       return;
   }
@@ -222,6 +227,25 @@
   SendMessage("</stream:stream>");
 }
 
+void XmppChannel::HandleMessageStanza(std::unique_ptr<XmlNode> stanza) {
+  const XmlNode* node = stanza->FindFirstChild("push:push/push:data", true);
+  if (!node) {
+    LOG(WARNING) << "XMPP message stanza is missing <push:data> element";
+    return;
+  }
+  std::string data = node->text();
+  std::string json_data;
+  if (!chromeos::data_encoding::Base64Decode(data, &json_data)) {
+    LOG(WARNING) << "Failed to decode base64-encoded message payload: " << data;
+    return;
+  }
+
+  VLOG(2) << "XMPP push notification data: " << json_data;
+  auto json_dict = LoadJsonDict(json_data, nullptr);
+  if (json_dict && delegate_)
+    ParseNotificationJson(*json_dict, delegate_);
+}
+
 void XmppChannel::StartTlsHandshake() {
   stream_->CancelPendingAsyncOperations();
   chromeos::TlsStream::Connect(