blob: 0a27f1c871ddc643feec5886e1bddfdbb7e8f9f9 [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;
48 if (!notification.GetString("kind", &kind) ||
49 kind != "clouddevices#notification") {
50 LOG(WARNING) << "Push notification should have 'kind' property set to "
51 "clouddevices#notification";
52 return false;
53 }
54
55 std::string type;
56 if (!notification.GetString("type", &type)) {
57 LOG(WARNING) << "Push notification should have 'type' property";
58 return false;
59 }
60
61 if (type == "COMMAND_CREATED")
Alex Vakulenkoe07c29d2015-10-22 10:31:12 -070062 return ParseCommandCreated(notification, delegate, channel_name);
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070063
Alex Vakulenko6b40d8f2015-06-24 11:44:22 -070064 if (type == "DEVICE_DELETED")
65 return ParseDeviceDeleted(notification, delegate);
66
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070067 // Here we ignore other types of notifications for now.
68 LOG(INFO) << "Ignoring push notification of type " << type;
69 return true;
70}
71
Vitaly Bukab6f015a2015-07-09 14:59:23 -070072} // namespace weave