| LEFT | RIGHT |
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 Eyeo GmbH |
| 4 * | 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
| 8 * | 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 #include <AdblockPlus/JsValue.h> | 18 #include <AdblockPlus/JsValue.h> |
| 19 #include <AdblockPlus/JsEngine.h> | 19 #include <AdblockPlus/JsEngine.h> |
| 20 #include <AdblockPlus/Notification.h> | 20 #include <AdblockPlus/Notification.h> |
| 21 #include <algorithm> | 21 #include <algorithm> |
| 22 | 22 |
| 23 using namespace AdblockPlus; | 23 using namespace AdblockPlus; |
| 24 | 24 |
| 25 namespace | 25 namespace |
| 26 { | 26 { |
| 27 typedef std::pair<NotificationType, std::string> NotificationTypeString; | 27 typedef std::pair<NotificationType, std::string> NotificationTypeString; |
| 28 typedef std::vector<NotificationTypeString> NotificationTypes; | 28 typedef std::vector<NotificationTypeString> NotificationTypes; |
| 29 const NotificationTypes g_notificationTypes = []()->NotificationTypes | 29 NotificationTypes InitNotificationTypes() |
| 30 { | 30 { |
| 31 NotificationTypes retValue; | 31 NotificationTypes retValue; |
| 32 retValue.emplace_back(std::make_pair(NotificationType::NOTIFICATION_TYPE_QUE
STION, "question")); | 32 retValue.push_back(std::make_pair(NotificationType::NOTIFICATION_TYPE_QUESTI
ON, "question")); |
| 33 retValue.emplace_back(std::make_pair(NotificationType::NOTIFICATION_TYPE_CRI
TICAL, "critical")); | 33 retValue.push_back(std::make_pair(NotificationType::NOTIFICATION_TYPE_CRITIC
AL, "critical")); |
| 34 retValue.emplace_back(std::make_pair(NotificationType::NOTIFICATION_TYPE_INF
ORMATION, "information")); | 34 retValue.push_back(std::make_pair(NotificationType::NOTIFICATION_TYPE_INFORM
ATION, "information")); |
| 35 return retValue; | 35 return retValue; |
| 36 }(); | 36 } |
| 37 |
| 38 const NotificationTypes notificationTypes = InitNotificationTypes(); |
| 37 | 39 |
| 38 NotificationType StringToNotificationType(const std::string& value) | 40 NotificationType StringToNotificationType(const std::string& value) |
| 39 { | 41 { |
| 40 auto ii_notificationType = std::find_if(g_notificationTypes.begin(), g_notif
icationTypes.end(), | 42 struct IsSecondEqualToPredicate |
| 41 [&value](const NotificationTypeString& pair)->bool | |
| 42 { | 43 { |
| 43 return value == pair.second; | 44 std::string value; |
| 44 }); | 45 bool operator()(const NotificationTypeString& pair) const |
| 45 if (ii_notificationType == g_notificationTypes.end()) | 46 { |
| 47 return value == pair.second; |
| 48 } |
| 49 } findBySecond = {value}; |
| 50 NotificationTypes::const_iterator notificationTypeIterator = std::find_if( |
| 51 notificationTypes.begin(), notificationTypes.end(), findBySecond); |
| 52 if (notificationTypeIterator == notificationTypes.end()) |
| 46 { | 53 { |
| 47 return NotificationType::NOTIFICATION_TYPE_INFORMATION; | 54 return NotificationType::NOTIFICATION_TYPE_INFORMATION; |
| 48 } | 55 } |
| 49 return ii_notificationType->first; | 56 return notificationTypeIterator->first; |
| 50 } | |
| 51 | |
| 52 std::string NotificationTypeToString(NotificationType value) | |
| 53 { | |
| 54 auto ii_notificationType = std::find_if(g_notificationTypes.begin(), g_notif
icationTypes.end(), | |
| 55 [&value](const NotificationTypeString& pair)->bool | |
| 56 { | |
| 57 return value == pair.first; | |
| 58 }); | |
| 59 if (ii_notificationType == g_notificationTypes.end()) | |
| 60 { | |
| 61 return "information"; | |
| 62 } | |
| 63 return ii_notificationType->second; | |
| 64 } | 57 } |
| 65 } | 58 } |
| 66 | 59 |
| 67 Notification::Notification(NotificationType type, const std::string& id, const J
sValuePtr& jsValue, PrivateCtrArg) | 60 Notification::Notification(const JsValuePtr& jsValue) |
| 68 : JsValue(jsValue) | 61 : JsValue(jsValue) |
| 69 { | 62 { |
| 70 SetProperty("type", NotificationTypeToString(type)); | |
| 71 SetProperty("id", id); | |
| 72 } | |
| 73 | |
| 74 std::string Notification::GetId() const | |
| 75 { | |
| 76 auto jsValue = GetProperty("id"); | |
| 77 return jsValue ? jsValue->AsString() : ""; | |
| 78 } | 63 } |
| 79 | 64 |
| 80 NotificationType Notification::GetType() const | 65 NotificationType Notification::GetType() const |
| 81 { | 66 { |
| 82 auto jsValue = GetProperty("type"); | 67 return type; |
| 83 return StringToNotificationType(jsValue ? GetProperty("type")->AsString() : ""
); | |
| 84 } | 68 } |
| 85 | 69 |
| 86 NotificationTexts Notification::GetTexts() | 70 const std::string& Notification::GetTitle() const |
| 87 { | 71 { |
| 88 JsValuePtr func = jsEngine->Evaluate("API.getNotificationTexts"); | 72 return title; |
| 89 if (!func) | |
| 90 { | |
| 91 return NotificationTexts(); | |
| 92 } | |
| 93 auto jsTexts = func->Call(); | |
| 94 if (!jsTexts) | |
| 95 { | |
| 96 return NotificationTexts(); | |
| 97 } | |
| 98 return Notification::JsTextsToNotificationTexts(*jsTexts); | |
| 99 } | 73 } |
| 100 | 74 |
| 101 void Notification::SetTitle(const std::string& value) | 75 const std::string& Notification::GetMessageString() const |
| 102 { | 76 { |
| 103 SetProperty("title", value); | 77 return message; |
| 104 } | |
| 105 | |
| 106 void Notification::SetMessage(const std::string& value) | |
| 107 { | |
| 108 SetProperty("message", value); | |
| 109 } | |
| 110 | |
| 111 std::vector<std::string> Notification::GetUrlFilters() const | |
| 112 { | |
| 113 std::vector<std::string> retValue; | |
| 114 auto jsUrlFilters = GetProperty("urlFilters"); | |
| 115 if (!jsUrlFilters || !jsUrlFilters->IsArray()) | |
| 116 { | |
| 117 return retValue; | |
| 118 } | |
| 119 auto urlFiltersList = jsUrlFilters->AsList(); | |
| 120 for (auto ii_urlFilter = urlFiltersList.begin(); ii_urlFilter != urlFiltersLis
t.end(); ++ii_urlFilter) | |
| 121 { | |
| 122 if (!*ii_urlFilter) | |
| 123 { | |
| 124 continue; | |
| 125 } | |
| 126 retValue.emplace_back((*ii_urlFilter)->AsString()); | |
| 127 } | |
| 128 return retValue; | |
| 129 } | |
| 130 | |
| 131 void Notification::AddUrlFilter(const std::string& value) | |
| 132 { | |
| 133 if (value.empty()) | |
| 134 { | |
| 135 return; | |
| 136 } | |
| 137 auto jsFilter = jsEngine->NewValue(value); | |
| 138 auto jsUrlFilters = GetProperty("urlFilters"); | |
| 139 if (jsUrlFilters && jsUrlFilters->IsArray()) | |
| 140 { | |
| 141 jsUrlFilters->Push(jsFilter); | |
| 142 return; | |
| 143 } | |
| 144 jsUrlFilters = jsEngine->NewArray(); | |
| 145 jsUrlFilters->Push(jsFilter); | |
| 146 SetProperty("urlFilters", jsUrlFilters); | |
| 147 } | 78 } |
| 148 | 79 |
| 149 void Notification::MarkAsShown() | 80 void Notification::MarkAsShown() |
| 150 { | 81 { |
| 151 JsValuePtr func = jsEngine->Evaluate("API.markNotificationAsShown"); | |
| 152 if (!func) | |
| 153 { | |
| 154 return; | |
| 155 } | |
| 156 JsValueList params; | 82 JsValueList params; |
| 157 params.push_back(jsEngine->NewValue(GetId())); | 83 params.push_back(GetProperty("id")); |
| 158 func->Call(params); | 84 jsEngine->Evaluate("API.markNotificationAsShown")->Call(params); |
| 159 } | 85 } |
| 160 | 86 |
| 161 Notification::Notification(const JsValuePtr& jsValue, PrivateCtrArg) | 87 NotificationPtr Notification::JsValueToNotification(const JsValuePtr& jsValue) |
| 162 : JsValue(jsValue) | |
| 163 { | 88 { |
| 164 } | 89 if (!jsValue || !jsValue->IsObject()) |
| 90 { |
| 91 return NotificationPtr(); |
| 92 } |
| 165 | 93 |
| 166 std::tr1::shared_ptr<Notification> Notification::JsValueToNotification(const JsV
aluePtr& jsValue) | 94 NotificationPtr notification(new Notification(jsValue)); |
| 167 { | 95 JsValuePtr jsType = notification->GetProperty("type"); |
| 168 if(!jsValue || !jsValue->IsObject()) | 96 notification->type = StringToNotificationType(jsType ? jsType->AsString() : ""
); |
| 97 |
| 98 JsValueList params; |
| 99 params.push_back(notification); |
| 100 JsValuePtr func = notification->jsEngine->Evaluate("API.getNotificationTexts")
; |
| 101 JsValuePtr jsTexts = func->Call(params); |
| 102 JsValuePtr jsTitle = jsTexts->GetProperty("title"); |
| 103 if (jsTitle->IsString()) |
| 169 { | 104 { |
| 170 return std::tr1::shared_ptr<Notification>(); | 105 notification->title = jsTitle->AsString(); |
| 171 } | 106 } |
| 172 return std::tr1::make_shared<Notification>(jsValue, PrivateCtrArg()); | 107 JsValuePtr jsMessage = jsTexts->GetProperty("message"); |
| 173 } | 108 if (jsMessage->IsString()) |
| 174 | |
| 175 NotificationTexts Notification::JsTextsToNotificationTexts(const JsValue& jsText
) | |
| 176 { | |
| 177 NotificationTexts retValue; | |
| 178 auto jsTitle = jsText.GetProperty("title"); | |
| 179 if (jsTitle && jsTitle->IsString()) | |
| 180 { | 109 { |
| 181 retValue.title = jsTitle->AsString(); | 110 notification->message = jsMessage->AsString(); |
| 182 } | 111 } |
| 183 auto jsMessage = jsText.GetProperty("message"); | 112 return notification; |
| 184 if (jsMessage && jsMessage->IsString()) | |
| 185 { | |
| 186 retValue.message = jsMessage->AsString(); | |
| 187 } | |
| 188 return retValue; | |
| 189 } | 113 } |
| LEFT | RIGHT |