Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: src/shared/Communication.h

Issue 4882650246414336: Issue 2005 - Refactor working with strings in InputBuffer and OutputBuffer
Left Patch Set: Created Feb. 17, 2015, 12:36 p.m.
Right Patch Set: rebase Created April 2, 2015, 11:09 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH
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
1 #ifndef COMMUNICATION_H 18 #ifndef COMMUNICATION_H
2 #define COMMUNICATION_H 19 #define COMMUNICATION_H
3 20
4 #include <memory> 21 #include <memory>
5 #include <sstream> 22 #include <sstream>
6 #include <stdexcept> 23 #include <stdexcept>
7 #include <stdint.h> 24 #include <stdint.h>
8 #include <string> 25 #include <string>
9 #include <vector> 26 #include <vector>
10 #include <Windows.h> 27 #include <Windows.h>
11 #include "Utils.h" 28 #include "Utils.h"
12 29
13 namespace Communication 30 namespace Communication
14 { 31 {
15 extern const std::wstring pipeName; 32 extern const std::wstring pipeName;
16 extern std::wstring browserSID; 33 extern std::wstring browserSID;
17 34
18 enum ProcType : uint32_t { 35 enum ProcType : uint32_t {
19 PROC_MATCHES, 36 PROC_MATCHES,
20 PROC_GET_ELEMHIDE_SELECTORS, 37 PROC_GET_ELEMHIDE_SELECTORS,
21 PROC_AVAILABLE_SUBSCRIPTIONS, 38 PROC_AVAILABLE_SUBSCRIPTIONS,
22 PROC_LISTED_SUBSCRIPTIONS, 39 PROC_LISTED_SUBSCRIPTIONS,
23 PROC_SET_SUBSCRIPTION, 40 PROC_SET_SUBSCRIPTION,
24 PROC_ADD_SUBSCRIPTION, 41 PROC_ADD_SUBSCRIPTION,
25 PROC_REMOVE_SUBSCRIPTION, 42 PROC_REMOVE_SUBSCRIPTION,
26 PROC_UPDATE_ALL_SUBSCRIPTIONS, 43 PROC_UPDATE_ALL_SUBSCRIPTIONS,
27 PROC_GET_EXCEPTION_DOMAINS, 44 PROC_GET_EXCEPTION_DOMAINS,
28 PROC_IS_WHITELISTED_URL, 45 PROC_GET_WHITELISTING_FITER,
29 PROC_IS_ELEMHIDE_WHITELISTED_ON_URL, 46 PROC_IS_ELEMHIDE_WHITELISTED_ON_URL,
30 PROC_ADD_FILTER, 47 PROC_ADD_FILTER,
31 PROC_REMOVE_FILTER, 48 PROC_REMOVE_FILTER,
32 PROC_SET_PREF, 49 PROC_SET_PREF,
33 PROC_GET_PREF, 50 PROC_GET_PREF,
34 PROC_IS_FIRST_RUN_ACTION_NEEDED, 51 PROC_IS_FIRST_RUN_ACTION_NEEDED,
35 PROC_CHECK_FOR_UPDATES, 52 PROC_CHECK_FOR_UPDATES,
36 PROC_GET_DOCUMENTATION_LINK, 53 PROC_GET_DOCUMENTATION_LINK,
37 PROC_TOGGLE_PLUGIN_ENABLED, 54 PROC_TOGGLE_PLUGIN_ENABLED,
38 PROC_GET_HOST, 55 PROC_GET_HOST,
(...skipping 23 matching lines...) Expand all
62 operator>>(tmpString); 79 operator>>(tmpString);
63 value = ToUtf16String(tmpString); 80 value = ToUtf16String(tmpString);
64 return *this; 81 return *this;
65 } 82 }
66 InputBuffer& operator>>(int64_t& value) { return Read(value, TYPE_INT64); } 83 InputBuffer& operator>>(int64_t& value) { return Read(value, TYPE_INT64); }
67 InputBuffer& operator>>(int32_t& value) { return Read(value, TYPE_INT32); } 84 InputBuffer& operator>>(int32_t& value) { return Read(value, TYPE_INT32); }
68 InputBuffer& operator>>(bool& value) { return Read(value, TYPE_BOOL); } 85 InputBuffer& operator>>(bool& value) { return Read(value, TYPE_BOOL); }
69 InputBuffer& operator>>(std::vector<std::string>& value) { return ReadString s(value); } 86 InputBuffer& operator>>(std::vector<std::string>& value) { return ReadString s(value); }
70 InputBuffer& operator>>(std::vector<std::wstring>& value) 87 InputBuffer& operator>>(std::vector<std::wstring>& value)
71 { 88 {
72 std::vector<std::string> tmpStrings; 89 std::vector<std::string> tmpStrings;
Eric 2015/02/17 18:15:13 Ugh. A temporary vector is just ugly and inefficie
73 operator>>(tmpStrings); 90 operator>>(tmpStrings);
74 value = ToUtf16Strings(tmpStrings); 91 value = ToUtf16Strings(tmpStrings);
75 return *this; 92 return *this;
76 } 93 }
77 InputBuffer& operator=(const InputBuffer& copy) 94 InputBuffer& operator=(const InputBuffer& copy)
78 { 95 {
79 hasType = copy.hasType; 96 hasType = copy.hasType;
80 buffer = std::istringstream(copy.buffer.str()); 97 buffer = std::istringstream(copy.buffer.str());
81 currentType = copy.currentType; 98 currentType = copy.currentType;
82 return *this; 99 return *this;
83 } 100 }
84 ValueType GetType(); 101 ValueType GetType();
85 private: 102 private:
86 std::istringstream buffer; 103 std::istringstream buffer;
87 ValueType currentType; 104 ValueType currentType;
88 bool hasType; 105 bool hasType;
89 106
90 void CheckType(ValueType expectedType); 107 void CheckType(ValueType expectedType);
91 108
92 InputBuffer& ReadString(std::string& value) 109 InputBuffer& ReadString(std::string& value)
93 { 110 {
94 CheckType(TYPE_STRING); 111 CheckType(TYPE_STRING);
95 112
96 SizeType length; 113 SizeType length;
97 ReadBinary(length); 114 ReadBinary(length);
98 value.resize(length, '\0'); 115 value.resize(length);
Eric 2015/02/17 18:15:13 We don't need a fill character, since we're immedi
sergei 2015/02/18 14:13:06 I've removed it but it's still used because void r
Eric 2015/02/18 18:12:47 That's fine. Nothing about external string initial
99 buffer.read(&value[0], sizeof(std::string::value_type) * length); 116 if (length > 0)
Eric 2015/02/17 18:15:13 We need two things here. -- An explicit const_cast
Eric 2015/02/17 18:15:13 Separately, we don't need the sizeof expression he
sergei 2015/02/18 14:13:06 done
sergei 2015/02/18 14:13:06 I have just thought about it again and I'm not sur
sergei 2015/02/18 14:41:39 In addition, Because of http://en.cppreference.com
Eric 2015/02/18 18:12:47 From my point of view, it is _already_ suspicious
Eric 2015/02/18 18:12:47 From that same page: "data()[i] == operator[](
sergei 2015/02/19 17:26:57 I've found it, http://codereview.adblockplus.org/5
sergei 2015/02/19 17:26:57 Don't mix const methods and non-const methods. On
100 if (buffer.fail()) 117 {
101 throw new std::runtime_error("Unexpected end of input buffer"); 118 buffer.read(&value[0], length);
Eric 2015/02/17 18:15:13 It wasn't your error, but we should be calling bad
sergei 2015/02/18 14:13:06 Right now I'm not sure about it and it's not relev
Eric 2015/02/18 18:12:47 A separate issue for this is overkill, but very br
sergei 2015/02/19 17:26:57 The reason to change it clear, but it's not a sing
119 if (buffer.fail())
120 throw new std::runtime_error("Unexpected end of input buffer");
121 }
102 return *this; 122 return *this;
103 } 123 }
104 124
105 InputBuffer& ReadStrings(std::vector<std::string>& value) 125 InputBuffer& ReadStrings(std::vector<std::string>& value)
106 { 126 {
107 value.clear(); 127 value.clear();
108 CheckType(ValueType::TYPE_STRINGS); 128 CheckType(ValueType::TYPE_STRINGS);
109 129
110 SizeType length; 130 SizeType length;
111 ReadBinary(length); 131 ReadBinary(length);
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 182
163 // Disallow copying 183 // Disallow copying
164 const OutputBuffer& operator=(const OutputBuffer&); 184 const OutputBuffer& operator=(const OutputBuffer&);
165 185
166 OutputBuffer& WriteString(const std::string& value) 186 OutputBuffer& WriteString(const std::string& value)
167 { 187 {
168 WriteBinary(TYPE_STRING); 188 WriteBinary(TYPE_STRING);
169 189
170 SizeType length = static_cast<SizeType>(value.size()); 190 SizeType length = static_cast<SizeType>(value.size());
171 WriteBinary(length); 191 WriteBinary(length);
172 buffer.write(value.c_str(), sizeof(std::string::value_type) * length); 192 buffer.write(value.c_str(), length);
Eric 2015/02/17 18:15:13 See sizeof comment above.
sergei 2015/02/18 14:13:06 done.
173 if (buffer.fail()) 193 if (buffer.fail())
174 throw new std::runtime_error("Unexpected error writing to output buffer" ); 194 throw new std::runtime_error("Unexpected error writing to output buffer" );
175 195
176 return *this; 196 return *this;
177 } 197 }
178 198
179 OutputBuffer& WriteStrings(const std::vector<std::string>& value) 199 OutputBuffer& WriteStrings(const std::vector<std::string>& value)
180 { 200 {
181 WriteBinary(ValueType::TYPE_STRINGS); 201 WriteBinary(ValueType::TYPE_STRINGS);
182 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
232 252
233 InputBuffer ReadMessage(); 253 InputBuffer ReadMessage();
234 void WriteMessage(OutputBuffer& message); 254 void WriteMessage(OutputBuffer& message);
235 255
236 protected: 256 protected:
237 HANDLE pipe; 257 HANDLE pipe;
238 }; 258 };
239 } 259 }
240 260
241 #endif 261 #endif
LEFTRIGHT

Powered by Google App Engine
This is Rietveld