blob: 69e7e7e3336ef30c5b653445ff9495c72710cb60 [file] [log] [blame]
Vitaly Buka4615e0d2015-10-14 15:35:12 -07001// Copyright 2015 The Weave Authors. All rights reserved.
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Stefan Sauer2d16dfa2015-09-25 17:08:35 +02005#include "src/notification/notification_parser.h"
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -07006
7#include <base/logging.h>
8
Vitaly Bukab6f015a2015-07-09 14:59:23 -07009namespace weave {
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070010
11namespace {
12
13// Processes COMMAND_CREATED notifications.
14bool ParseCommandCreated(const base::DictionaryValue& notification,
Alex Vakulenkoe07c29d2015-10-22 10:31:12 -070015 NotificationDelegate* delegate,
16 const std::string& channel_name) {
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070017 const base::DictionaryValue* command = nullptr;
18 if (!notification.GetDictionary("command", &command)) {
19 LOG(ERROR) << "COMMAND_CREATED notification is missing 'command' property";
20 return false;
21 }
22
Alex Vakulenkoe07c29d2015-10-22 10:31:12 -070023 delegate->OnCommandCreated(*command, channel_name);
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070024 return true;
25}
26
Alex Vakulenko6b40d8f2015-06-24 11:44:22 -070027// Processes DEVICE_DELETED notifications.
28bool ParseDeviceDeleted(const base::DictionaryValue& notification,
29 NotificationDelegate* delegate) {
Johan Euphrosine312c2f52015-09-29 00:04:29 -070030 std::string cloud_id;
31 if (!notification.GetString("deviceId", &cloud_id)) {
Alex Vakulenko6b40d8f2015-06-24 11:44:22 -070032 LOG(ERROR) << "DEVICE_DELETED notification is missing 'deviceId' property";
33 return false;
34 }
35
Johan Euphrosine312c2f52015-09-29 00:04:29 -070036 delegate->OnDeviceDeleted(cloud_id);
Alex Vakulenko6b40d8f2015-06-24 11:44:22 -070037 return true;
38}
39
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070040} // anonymous namespace
41
42bool ParseNotificationJson(const base::DictionaryValue& notification,
Alex Vakulenkoe07c29d2015-10-22 10:31:12 -070043 NotificationDelegate* delegate,
44 const std::string& channel_name) {
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070045 CHECK(delegate);
46
47 std::string kind;
Vitaly Buka34668e72015-12-15 14:46:47 -080048 if (!notification.GetString("kind", &kind) || kind != "weave#notification") {
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070049 LOG(WARNING) << "Push notification should have 'kind' property set to "
Alex Vakulenkof21c83a2015-11-25 11:34:33 -080050 "weave#notification";
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070051 return false;
52 }
53
54 std::string type;
55 if (!notification.GetString("type", &type)) {
56 LOG(WARNING) << "Push notification should have 'type' property";
57 return false;
58 }
59
60 if (type == "COMMAND_CREATED")
Alex Vakulenkoe07c29d2015-10-22 10:31:12 -070061 return ParseCommandCreated(notification, delegate, channel_name);
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070062
Alex Vakulenko6b40d8f2015-06-24 11:44:22 -070063 if (type == "DEVICE_DELETED")
64 return ParseDeviceDeleted(notification, delegate);
65
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070066 // Here we ignore other types of notifications for now.
67 LOG(INFO) << "Ignoring push notification of type " << type;
68 return true;
69}
70
Vitaly Bukab6f015a2015-07-09 14:59:23 -070071} // namespace weave