1 // Copyright 2015 The Weave Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/notification/notification_parser.h"
6
7 #include <base/logging.h>
8
9 namespace weave {
10
11 namespace {
12
13 // Processes COMMAND_CREATED notifications.
ParseCommandCreated(const base::DictionaryValue & notification,NotificationDelegate * delegate,const std::string & channel_name)14 bool ParseCommandCreated(const base::DictionaryValue& notification,
15 NotificationDelegate* delegate,
16 const std::string& channel_name) {
17 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
23 delegate->OnCommandCreated(*command, channel_name);
24 return true;
25 }
26
27 // Processes DEVICE_DELETED notifications.
ParseDeviceDeleted(const base::DictionaryValue & notification,NotificationDelegate * delegate)28 bool ParseDeviceDeleted(const base::DictionaryValue& notification,
29 NotificationDelegate* delegate) {
30 std::string cloud_id;
31 if (!notification.GetString("deviceId", &cloud_id)) {
32 LOG(ERROR) << "DEVICE_DELETED notification is missing 'deviceId' property";
33 return false;
34 }
35
36 delegate->OnDeviceDeleted(cloud_id);
37 return true;
38 }
39
40 } // anonymous namespace
41
ParseNotificationJson(const base::DictionaryValue & notification,NotificationDelegate * delegate,const std::string & channel_name)42 bool ParseNotificationJson(const base::DictionaryValue& notification,
43 NotificationDelegate* delegate,
44 const std::string& channel_name) {
45 CHECK(delegate);
46
47 std::string kind;
48 if (!notification.GetString("kind", &kind) || kind != "weave#notification") {
49 LOG(WARNING) << "Push notification should have 'kind' property set to "
50 "weave#notification";
51 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")
61 return ParseCommandCreated(notification, delegate, channel_name);
62
63 if (type == "DEVICE_DELETED")
64 return ParseDeviceDeleted(notification, delegate);
65
66 // Here we ignore other types of notifications for now.
67 LOG(INFO) << "Ignoring push notification of type " << type;
68 return true;
69 }
70
71 } // namespace weave
72