| OLD | NEW |
| 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 #ifndef COMMUNICATION_H | 18 #ifndef COMMUNICATION_H |
| 19 #define COMMUNICATION_H | 19 #define COMMUNICATION_H |
| 20 | 20 |
| 21 #include <memory> | 21 #include <memory> |
| 22 #include <sstream> | 22 #include <sstream> |
| 23 #include <stdexcept> | 23 #include <stdexcept> |
| 24 #include <stdint.h> | 24 #include <stdint.h> |
| 25 #include <string> | 25 #include <string> |
| 26 #include <vector> | 26 #include <vector> |
| 27 #include <Windows.h> | 27 #include <Windows.h> |
| 28 #include "Utils.h" |
| 28 | 29 |
| 29 namespace Communication | 30 namespace Communication |
| 30 { | 31 { |
| 31 extern const std::wstring pipeName; | 32 extern const std::wstring pipeName; |
| 32 extern std::wstring browserSID; | 33 extern std::wstring browserSID; |
| 33 | 34 |
| 34 enum ProcType : uint32_t { | 35 enum ProcType : uint32_t { |
| 35 PROC_MATCHES, | 36 PROC_MATCHES, |
| 36 PROC_GET_ELEMHIDE_SELECTORS, | 37 PROC_GET_ELEMHIDE_SELECTORS, |
| 37 PROC_AVAILABLE_SUBSCRIPTIONS, | 38 PROC_AVAILABLE_SUBSCRIPTIONS, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 48 PROC_SET_PREF, | 49 PROC_SET_PREF, |
| 49 PROC_GET_PREF, | 50 PROC_GET_PREF, |
| 50 PROC_IS_FIRST_RUN_ACTION_NEEDED, | 51 PROC_IS_FIRST_RUN_ACTION_NEEDED, |
| 51 PROC_CHECK_FOR_UPDATES, | 52 PROC_CHECK_FOR_UPDATES, |
| 52 PROC_GET_DOCUMENTATION_LINK, | 53 PROC_GET_DOCUMENTATION_LINK, |
| 53 PROC_TOGGLE_PLUGIN_ENABLED, | 54 PROC_TOGGLE_PLUGIN_ENABLED, |
| 54 PROC_GET_HOST, | 55 PROC_GET_HOST, |
| 55 PROC_COMPARE_VERSIONS | 56 PROC_COMPARE_VERSIONS |
| 56 }; | 57 }; |
| 57 enum ValueType : uint32_t { | 58 enum ValueType : uint32_t { |
| 58 TYPE_PROC, TYPE_STRING, TYPE_WSTRING, TYPE_INT64, TYPE_INT32, TYPE_BOOL, TYP
E_STRINGS | 59 TYPE_PROC, TYPE_STRING, TYPE_INT64, TYPE_INT32, TYPE_BOOL, TYPE_STRINGS |
| 59 }; | 60 }; |
| 60 typedef uint32_t SizeType; | 61 typedef uint32_t SizeType; |
| 61 | 62 |
| 62 class InputBuffer | 63 class InputBuffer |
| 63 { | 64 { |
| 64 public: | 65 public: |
| 65 InputBuffer() : buffer(), hasType(false) {} | 66 InputBuffer() : buffer(), hasType(false) {} |
| 66 InputBuffer(const std::string& data) : buffer(data), hasType(false) {} | 67 InputBuffer(const std::string& data) : buffer(data), hasType(false) {} |
| 67 InputBuffer(const InputBuffer& copy) | 68 InputBuffer(const InputBuffer& copy) |
| 68 { | 69 { |
| 69 hasType = copy.hasType; | 70 hasType = copy.hasType; |
| 70 buffer = std::istringstream(copy.buffer.str()); | 71 buffer = std::istringstream(copy.buffer.str()); |
| 71 currentType = copy.currentType; | 72 currentType = copy.currentType; |
| 72 } | 73 } |
| 73 InputBuffer& operator>>(ProcType& value) { return Read(value, TYPE_PROC); } | 74 InputBuffer& operator>>(ProcType& value) { return Read(value, TYPE_PROC); } |
| 74 InputBuffer& operator>>(std::string& value) { return ReadString(value, TYPE_
STRING); } | 75 InputBuffer& operator>>(std::string& value) { return ReadString(value); } |
| 75 InputBuffer& operator>>(std::wstring& value) { return ReadString(value, TYPE
_WSTRING); } | 76 InputBuffer& operator>>(std::wstring& value) |
| 77 { |
| 78 std::string tmpString; |
| 79 operator>>(tmpString); |
| 80 value = ToUtf16String(tmpString); |
| 81 return *this; |
| 82 } |
| 76 InputBuffer& operator>>(int64_t& value) { return Read(value, TYPE_INT64); } | 83 InputBuffer& operator>>(int64_t& value) { return Read(value, TYPE_INT64); } |
| 77 InputBuffer& operator>>(int32_t& value) { return Read(value, TYPE_INT32); } | 84 InputBuffer& operator>>(int32_t& value) { return Read(value, TYPE_INT32); } |
| 78 InputBuffer& operator>>(bool& value) { return Read(value, TYPE_BOOL); } | 85 InputBuffer& operator>>(bool& value) { return Read(value, TYPE_BOOL); } |
| 79 InputBuffer& operator>>(std::vector<std::string>& value) { return ReadString
s(value); } | 86 InputBuffer& operator>>(std::vector<std::string>& value) { return ReadString
s(value); } |
| 87 InputBuffer& operator>>(std::vector<std::wstring>& value) |
| 88 { |
| 89 std::vector<std::string> tmpStrings; |
| 90 operator>>(tmpStrings); |
| 91 value = ToUtf16Strings(tmpStrings); |
| 92 return *this; |
| 93 } |
| 80 InputBuffer& operator=(const InputBuffer& copy) | 94 InputBuffer& operator=(const InputBuffer& copy) |
| 81 { | 95 { |
| 82 hasType = copy.hasType; | 96 hasType = copy.hasType; |
| 83 buffer = std::istringstream(copy.buffer.str()); | 97 buffer = std::istringstream(copy.buffer.str()); |
| 84 currentType = copy.currentType; | 98 currentType = copy.currentType; |
| 85 return *this; | 99 return *this; |
| 86 } | 100 } |
| 87 ValueType GetType(); | 101 ValueType GetType(); |
| 88 private: | 102 private: |
| 89 std::istringstream buffer; | 103 std::istringstream buffer; |
| 90 ValueType currentType; | 104 ValueType currentType; |
| 91 bool hasType; | 105 bool hasType; |
| 92 | 106 |
| 93 void CheckType(ValueType expectedType); | 107 void CheckType(ValueType expectedType); |
| 94 | 108 |
| 95 template<class T> | 109 InputBuffer& ReadString(std::string& value) |
| 96 InputBuffer& ReadString(T& value, ValueType expectedType) | |
| 97 { | 110 { |
| 98 CheckType(expectedType); | 111 CheckType(TYPE_STRING); |
| 99 | 112 |
| 100 SizeType length; | 113 SizeType length; |
| 101 ReadBinary(length); | 114 ReadBinary(length); |
| 102 | 115 value.resize(length); |
| 103 std::auto_ptr<T::value_type> data(new T::value_type[length]); | 116 if (length > 0) |
| 104 buffer.read(reinterpret_cast<char*>(data.get()), sizeof(T::value_type) * l
ength); | 117 { |
| 105 if (buffer.fail()) | 118 buffer.read(&value[0], length); |
| 106 throw new std::runtime_error("Unexpected end of input buffer"); | 119 if (buffer.fail()) |
| 107 | 120 throw new std::runtime_error("Unexpected end of input buffer"); |
| 108 value.assign(data.get(), length); | 121 } |
| 109 return *this; | 122 return *this; |
| 110 } | 123 } |
| 111 | 124 |
| 112 InputBuffer& ReadStrings(std::vector<std::string>& value) | 125 InputBuffer& ReadStrings(std::vector<std::string>& value) |
| 113 { | 126 { |
| 114 value.clear(); | 127 value.clear(); |
| 115 CheckType(ValueType::TYPE_STRINGS); | 128 CheckType(ValueType::TYPE_STRINGS); |
| 116 | 129 |
| 117 SizeType length; | 130 SizeType length; |
| 118 ReadBinary(length); | 131 ReadBinary(length); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 148 OutputBuffer() {} | 161 OutputBuffer() {} |
| 149 | 162 |
| 150 // Explicit copy constructor to allow returning OutputBuffer by value | 163 // Explicit copy constructor to allow returning OutputBuffer by value |
| 151 OutputBuffer(const OutputBuffer& copy) : buffer(copy.buffer.str()) {} | 164 OutputBuffer(const OutputBuffer& copy) : buffer(copy.buffer.str()) {} |
| 152 | 165 |
| 153 std::string Get() | 166 std::string Get() |
| 154 { | 167 { |
| 155 return buffer.str(); | 168 return buffer.str(); |
| 156 } | 169 } |
| 157 OutputBuffer& operator<<(ProcType value) { return Write(value, TYPE_PROC); } | 170 OutputBuffer& operator<<(ProcType value) { return Write(value, TYPE_PROC); } |
| 158 OutputBuffer& operator<<(const std::string& value) { return WriteString(valu
e, TYPE_STRING); } | 171 OutputBuffer& operator<<(const std::string& value) { return WriteString(valu
e); } |
| 159 OutputBuffer& operator<<(const std::wstring& value) { return WriteString(val
ue, TYPE_WSTRING); } | 172 OutputBuffer& operator<<(const std::wstring& value) |
| 173 { |
| 174 return operator<<(ToUtf8String(value)); |
| 175 } |
| 160 OutputBuffer& operator<<(int64_t value) { return Write(value, TYPE_INT64); } | 176 OutputBuffer& operator<<(int64_t value) { return Write(value, TYPE_INT64); } |
| 161 OutputBuffer& operator<<(int32_t value) { return Write(value, TYPE_INT32); } | 177 OutputBuffer& operator<<(int32_t value) { return Write(value, TYPE_INT32); } |
| 162 OutputBuffer& operator<<(bool value) { return Write(value, TYPE_BOOL); } | 178 OutputBuffer& operator<<(bool value) { return Write(value, TYPE_BOOL); } |
| 163 OutputBuffer& operator<<(const std::vector<std::string>& value) { return Wri
teStrings(value); } | 179 OutputBuffer& operator<<(const std::vector<std::string>& value) { return Wri
teStrings(value); } |
| 164 private: | 180 private: |
| 165 std::ostringstream buffer; | 181 std::ostringstream buffer; |
| 166 | 182 |
| 167 // Disallow copying | 183 // Disallow copying |
| 168 const OutputBuffer& operator=(const OutputBuffer&); | 184 const OutputBuffer& operator=(const OutputBuffer&); |
| 169 | 185 |
| 170 template<class T> | 186 OutputBuffer& WriteString(const std::string& value) |
| 171 OutputBuffer& WriteString(const T& value, ValueType type) | |
| 172 { | 187 { |
| 173 WriteBinary(type); | 188 WriteBinary(TYPE_STRING); |
| 174 | 189 |
| 175 SizeType length = static_cast<SizeType>(value.size()); | 190 SizeType length = static_cast<SizeType>(value.size()); |
| 176 WriteBinary(length); | 191 WriteBinary(length); |
| 177 | 192 buffer.write(value.c_str(), length); |
| 178 buffer.write(reinterpret_cast<const char*>(value.c_str()), sizeof(T::value
_type) * length); | |
| 179 if (buffer.fail()) | 193 if (buffer.fail()) |
| 180 throw new std::runtime_error("Unexpected error writing to output buffer"
); | 194 throw new std::runtime_error("Unexpected error writing to output buffer"
); |
| 181 | 195 |
| 182 return *this; | 196 return *this; |
| 183 } | 197 } |
| 184 | 198 |
| 185 OutputBuffer& WriteStrings(const std::vector<std::string>& value) | 199 OutputBuffer& WriteStrings(const std::vector<std::string>& value) |
| 186 { | 200 { |
| 187 WriteBinary(ValueType::TYPE_STRINGS); | 201 WriteBinary(ValueType::TYPE_STRINGS); |
| 188 WriteBinary(static_cast<SizeType>(value.size())); | 202 WriteBinary(static_cast<SizeType>(value.size())); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 | 252 |
| 239 InputBuffer ReadMessage(); | 253 InputBuffer ReadMessage(); |
| 240 void WriteMessage(OutputBuffer& message); | 254 void WriteMessage(OutputBuffer& message); |
| 241 | 255 |
| 242 protected: | 256 protected: |
| 243 HANDLE pipe; | 257 HANDLE pipe; |
| 244 }; | 258 }; |
| 245 } | 259 } |
| 246 | 260 |
| 247 #endif | 261 #endif |
| OLD | NEW |