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