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/notification_delegate.h b/buffet/notification/notification_delegate.h
index 06b30d4..b2d7184 100644
--- a/buffet/notification/notification_delegate.h
+++ b/buffet/notification/notification_delegate.h
@@ -5,8 +5,11 @@
 #ifndef BUFFET_NOTIFICATION_NOTIFICATION_DELEGATE_H_
 #define BUFFET_NOTIFICATION_NOTIFICATION_DELEGATE_H_
 
+#include <memory>
 #include <string>
 
+#include <base/values.h>
+
 namespace buffet {
 
 class NotificationDelegate {
@@ -14,6 +17,8 @@
   virtual void OnConnected(const std::string& channel_name) = 0;
   virtual void OnDisconnected() = 0;
   virtual void OnPermanentFailure() = 0;
+  // Called when a new command is sent via the notification channel.
+  virtual void OnCommandCreated(const base::DictionaryValue& command) = 0;
 
  protected:
   virtual ~NotificationDelegate() = default;
diff --git a/buffet/notification/notification_parser.cc b/buffet/notification/notification_parser.cc
new file mode 100644
index 0000000..5885afa
--- /dev/null
+++ b/buffet/notification/notification_parser.cc
@@ -0,0 +1,55 @@
+// 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.
+
+#include "buffet/notification/notification_parser.h"
+
+#include <base/logging.h>
+
+namespace buffet {
+
+namespace {
+
+// Processes COMMAND_CREATED notifications.
+bool ParseCommandCreated(const base::DictionaryValue& notification,
+                         NotificationDelegate* delegate) {
+  const base::DictionaryValue* command = nullptr;
+  if (!notification.GetDictionary("command", &command)) {
+    LOG(ERROR) << "COMMAND_CREATED notification is missing 'command' property";
+    return false;
+  }
+
+  delegate->OnCommandCreated(*command);
+  return true;
+}
+
+}  // anonymous namespace
+
+bool ParseNotificationJson(const base::DictionaryValue& notification,
+                           NotificationDelegate* delegate) {
+  CHECK(delegate);
+
+  std::string kind;
+  if (!notification.GetString("kind", &kind) ||
+      kind != "clouddevices#notification") {
+    LOG(WARNING) << "Push notification should have 'kind' property set to "
+                    "clouddevices#notification";
+    return false;
+  }
+
+  std::string type;
+  if (!notification.GetString("type", &type)) {
+    LOG(WARNING) << "Push notification should have 'type' property";
+    return false;
+  }
+
+  if (type == "COMMAND_CREATED")
+    return ParseCommandCreated(notification, delegate);
+
+  // Here we ignore other types of notifications for now.
+  LOG(INFO) << "Ignoring push notification of type " << type;
+  return true;
+}
+
+
+}  // namespace buffet
diff --git a/buffet/notification/notification_parser.h b/buffet/notification/notification_parser.h
new file mode 100644
index 0000000..eb50dc1
--- /dev/null
+++ b/buffet/notification/notification_parser.h
@@ -0,0 +1,24 @@
+// 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_NOTIFICATION_PARSER_H_
+#define BUFFET_NOTIFICATION_NOTIFICATION_PARSER_H_
+
+#include <string>
+
+#include <base/values.h>
+
+#include "buffet/notification/notification_delegate.h"
+
+namespace buffet {
+
+// Parses the notification JSON object received from GCD server and invokes
+// the appropriate method from the |delegate|.
+// Returns false if unexpected or malformed notification is received.
+bool ParseNotificationJson(const base::DictionaryValue& notification,
+                           NotificationDelegate* delegate);
+
+}  // namespace buffet
+
+#endif  // BUFFET_NOTIFICATION_NOTIFICATION_PARSER_H_
diff --git a/buffet/notification/notification_parser_unittest.cc b/buffet/notification/notification_parser_unittest.cc
new file mode 100644
index 0000000..c6be507
--- /dev/null
+++ b/buffet/notification/notification_parser_unittest.cc
@@ -0,0 +1,142 @@
+// 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.
+
+#include "buffet/notification/notification_parser.h"
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include "buffet/commands/unittest_utils.h"
+
+using testing::Invoke;
+using testing::_;
+
+namespace buffet {
+
+using unittests::CreateDictionaryValue;
+
+class MockNotificationDelegate : public NotificationDelegate {
+ public:
+  MOCK_METHOD1(OnConnected, void(const std::string&));
+  MOCK_METHOD0(OnDisconnected, void());
+  MOCK_METHOD0(OnPermanentFailure, void());
+  MOCK_METHOD1(OnCommandCreated, void(const base::DictionaryValue& command));
+};
+
+class NotificationParserTest : public ::testing::Test {
+ protected:
+  testing::StrictMock<MockNotificationDelegate> delegate_;
+};
+
+TEST_F(NotificationParserTest, CommandCreated) {
+  auto json = CreateDictionaryValue(R"({
+    "kind": "clouddevices#notification",
+    "type": "COMMAND_CREATED",
+    "deviceId": "device_id",
+    "command": {
+      "kind": "clouddevices#command",
+      "deviceId": "device_id",
+      "state": "queued",
+      "name": "storage.list",
+      "parameters": {
+        "path": "/somepath1"
+      },
+      "expirationTimeMs": "1406036174811",
+      "id": "command_id",
+      "creationTimeMs": "1403444174811"
+    },
+    "commandId": "command_id"
+  })");
+
+  base::DictionaryValue command_instance;
+  auto on_command = [&command_instance](const base::DictionaryValue& command) {
+    command_instance.MergeDictionary(&command);
+  };
+
+  EXPECT_CALL(delegate_, OnCommandCreated(_)).WillOnce(Invoke(on_command));
+  EXPECT_TRUE(ParseNotificationJson(*json, &delegate_));
+
+  const char expected_json[] = R"({
+      "kind": "clouddevices#command",
+      "deviceId": "device_id",
+      "state": "queued",
+      "name": "storage.list",
+      "parameters": {
+        "path": "/somepath1"
+      },
+      "expirationTimeMs": "1406036174811",
+      "id": "command_id",
+      "creationTimeMs": "1403444174811"
+    })";
+  EXPECT_JSON_EQ(expected_json, command_instance);
+}
+
+TEST_F(NotificationParserTest, Failure_NoKind) {
+  auto json = CreateDictionaryValue(R"({
+    "type": "COMMAND_CREATED",
+    "deviceId": "device_id",
+    "command": {
+      "kind": "clouddevices#command",
+      "deviceId": "device_id",
+      "state": "queued",
+      "name": "storage.list",
+      "parameters": {
+        "path": "/somepath1"
+      },
+      "expirationTimeMs": "1406036174811",
+      "id": "command_id",
+      "creationTimeMs": "1403444174811"
+    },
+    "commandId": "command_id"
+  })");
+
+  EXPECT_FALSE(ParseNotificationJson(*json, &delegate_));
+}
+
+TEST_F(NotificationParserTest, Failure_NoType) {
+  auto json = CreateDictionaryValue(R"({
+    "kind": "clouddevices#notification",
+    "deviceId": "device_id",
+    "command": {
+      "kind": "clouddevices#command",
+      "deviceId": "device_id",
+      "state": "queued",
+      "name": "storage.list",
+      "parameters": {
+        "path": "/somepath1"
+      },
+      "expirationTimeMs": "1406036174811",
+      "id": "command_id",
+      "creationTimeMs": "1403444174811"
+    },
+    "commandId": "command_id"
+  })");
+
+  EXPECT_FALSE(ParseNotificationJson(*json, &delegate_));
+}
+
+TEST_F(NotificationParserTest, IgnoredNotificationType) {
+  auto json = CreateDictionaryValue(R"({
+    "kind": "clouddevices#notification",
+    "type": "COMMAND_EXPIRED",
+    "deviceId": "device_id",
+    "command": {
+      "kind": "clouddevices#command",
+      "deviceId": "device_id",
+      "state": "queued",
+      "name": "storage.list",
+      "parameters": {
+        "path": "/somepath1"
+      },
+      "expirationTimeMs": "1406036174811",
+      "id": "command_id",
+      "creationTimeMs": "1403444174811"
+    },
+    "commandId": "command_id"
+  })");
+
+  EXPECT_TRUE(ParseNotificationJson(*json, &delegate_));
+}
+
+}  // namespace buffet
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(
diff --git a/buffet/notification/xmpp_channel.h b/buffet/notification/xmpp_channel.h
index 4cf540e..8db127e 100644
--- a/buffet/notification/xmpp_channel.h
+++ b/buffet/notification/xmpp_channel.h
@@ -71,6 +71,7 @@
   void OnStanza(std::unique_ptr<XmlNode> stanza) override;
 
   void HandleStanza(std::unique_ptr<XmlNode> stanza);
+  void HandleMessageStanza(std::unique_ptr<XmlNode> stanza);
   void RestartXmppStream();
 
   void StartTlsHandshake();