| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
|
sergei
2017/03/15 12:17:20
I think the file should be called EventManager.
| |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH | |
|
sergei
2017/03/15 12:17:20
It's already 2017.
| |
| 4 * | |
| 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 | |
| 7 * published by the Free Software Foundation. | |
| 8 * | |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 * GNU General Public License for more details. | |
| 13 * | |
| 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/>. | |
| 16 */ | |
| 17 | |
| 18 #if !defined(EVENT_H) | |
| 19 #define EVENT_H | |
| 20 | |
| 21 #include <map> | |
| 22 #include <mutex> | |
| 23 #include <v8.h> | |
| 24 #include <AdblockPlus/JsEngine.h> | |
| 25 | |
| 26 /** | |
| 27 * Event manager class encapsulates an atomic map from names to event functions. | |
| 28 * | |
| 29 * The present version of this class has a simple event registry. | |
| 30 * Only one callback function may be registered per event. | |
| 31 * | |
| 32 * Event methods are synchronized on the event registry, not event execution. | |
| 33 * Once the callback function of an event executes it will continue to run | |
| 34 * even if the event is deleted before the callback finishes. | |
| 35 */ | |
| 36 class EventManager | |
| 37 { | |
| 38 /** | |
| 39 * Map from event names to callback functions. | |
| 40 * | |
| 41 */ | |
| 42 std::map<std::string, AdblockPlus::JsEngine::EventCallback> eventMap; | |
| 43 | |
| 44 std::mutex m; | |
|
sergei
2017/03/15 12:17:20
Don't mind to call it mutex instead of m?
| |
| 45 | |
| 46 typedef std::unique_lock<std::mutex> UniqueLockType; | |
|
sergei
2017/03/15 12:17:20
Why not to use std::lock_guard?
| |
| 47 | |
| 48 public: | |
| 49 EventManager() {}; // = default; | |
|
sergei
2017/03/15 12:17:20
I still don't see any reason to have such code and
| |
| 50 | |
| 51 /** | |
| 52 * Set the callback function for an event. | |
| 53 * Create event if not present; ovewrite callback if it is. | |
| 54 * @param eventName | |
| 55 * Event name. Any string is permitted here. | |
| 56 * It's not limited to valid JavaScript identifiers. | |
| 57 * @param callback | |
| 58 * Function called when event triggers | |
| 59 */ | |
| 60 void Set(const std::string& eventName, AdblockPlus::JsEngine::EventCallback ca llback); | |
|
sergei
2017/03/15 12:17:20
It's better to use const reference for callback ar
| |
| 61 | |
| 62 /** | |
| 63 * Remove an event and its callback function. | |
| 64 * The event need not be present. | |
|
sergei
2017/03/15 12:17:20
Actually it won't trigger any error if there is no
| |
| 65 * @param eventName | |
| 66 * Event name | |
| 67 */ | |
| 68 void Remove(const std::string& eventName); | |
| 69 | |
| 70 /** | |
| 71 * Trigger an event and execute its callback synchronously. | |
|
sergei
2017/03/15 12:17:20
What does "execute its callback synchronously" mea
| |
| 72 * @param eventName | |
| 73 * Event name | |
| 74 * @param args | |
| 75 * The callback function runs with these arguments passed. | |
| 76 */ | |
| 77 void Trigger(const std::string& eventName, AdblockPlus::JsValueList& args); | |
|
sergei
2017/03/15 12:17:20
What about adding const to args? It's clear that u
sergei
2017/03/15 12:17:20
What about adding const modifier to this method?
| |
| 78 }; | |
| 79 | |
| 80 #endif | |
| OLD | NEW |