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

Side by Side Diff: src/shared/Communication.h

Issue 4882650246414336: Issue 2005 - Refactor working with strings in InputBuffer and OutputBuffer
Patch Set: Created Feb. 17, 2015, 12:36 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 #ifndef COMMUNICATION_H 1 #ifndef COMMUNICATION_H
2 #define COMMUNICATION_H 2 #define COMMUNICATION_H
3 3
4 #include <memory> 4 #include <memory>
5 #include <sstream> 5 #include <sstream>
6 #include <stdexcept> 6 #include <stdexcept>
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 #include <Windows.h> 10 #include <Windows.h>
11 #include "Utils.h"
11 12
12 namespace Communication 13 namespace Communication
13 { 14 {
14 extern const std::wstring pipeName; 15 extern const std::wstring pipeName;
15 extern std::wstring browserSID; 16 extern std::wstring browserSID;
16 17
17 enum ProcType : uint32_t { 18 enum ProcType : uint32_t {
18 PROC_MATCHES, 19 PROC_MATCHES,
19 PROC_GET_ELEMHIDE_SELECTORS, 20 PROC_GET_ELEMHIDE_SELECTORS,
20 PROC_AVAILABLE_SUBSCRIPTIONS, 21 PROC_AVAILABLE_SUBSCRIPTIONS,
(...skipping 10 matching lines...) Expand all
31 PROC_SET_PREF, 32 PROC_SET_PREF,
32 PROC_GET_PREF, 33 PROC_GET_PREF,
33 PROC_IS_FIRST_RUN_ACTION_NEEDED, 34 PROC_IS_FIRST_RUN_ACTION_NEEDED,
34 PROC_CHECK_FOR_UPDATES, 35 PROC_CHECK_FOR_UPDATES,
35 PROC_GET_DOCUMENTATION_LINK, 36 PROC_GET_DOCUMENTATION_LINK,
36 PROC_TOGGLE_PLUGIN_ENABLED, 37 PROC_TOGGLE_PLUGIN_ENABLED,
37 PROC_GET_HOST, 38 PROC_GET_HOST,
38 PROC_COMPARE_VERSIONS 39 PROC_COMPARE_VERSIONS
39 }; 40 };
40 enum ValueType : uint32_t { 41 enum ValueType : uint32_t {
41 TYPE_PROC, TYPE_STRING, TYPE_WSTRING, TYPE_INT64, TYPE_INT32, TYPE_BOOL, TYP E_STRINGS 42 TYPE_PROC, TYPE_STRING, TYPE_INT64, TYPE_INT32, TYPE_BOOL, TYPE_STRINGS
42 }; 43 };
43 typedef uint32_t SizeType; 44 typedef uint32_t SizeType;
44 45
45 class InputBuffer 46 class InputBuffer
46 { 47 {
47 public: 48 public:
48 InputBuffer() : buffer(), hasType(false) {} 49 InputBuffer() : buffer(), hasType(false) {}
49 InputBuffer(const std::string& data) : buffer(data), hasType(false) {} 50 InputBuffer(const std::string& data) : buffer(data), hasType(false) {}
50 InputBuffer(const InputBuffer& copy) 51 InputBuffer(const InputBuffer& copy)
51 { 52 {
52 hasType = copy.hasType; 53 hasType = copy.hasType;
53 buffer = std::istringstream(copy.buffer.str()); 54 buffer = std::istringstream(copy.buffer.str());
54 currentType = copy.currentType; 55 currentType = copy.currentType;
55 } 56 }
56 InputBuffer& operator>>(ProcType& value) { return Read(value, TYPE_PROC); } 57 InputBuffer& operator>>(ProcType& value) { return Read(value, TYPE_PROC); }
57 InputBuffer& operator>>(std::string& value) { return ReadString(value, TYPE_ STRING); } 58 InputBuffer& operator>>(std::string& value) { return ReadString(value); }
58 InputBuffer& operator>>(std::wstring& value) { return ReadString(value, TYPE _WSTRING); } 59 InputBuffer& operator>>(std::wstring& value)
60 {
61 std::string tmpString;
62 operator>>(tmpString);
63 value = ToUtf16String(tmpString);
64 return *this;
65 }
59 InputBuffer& operator>>(int64_t& value) { return Read(value, TYPE_INT64); } 66 InputBuffer& operator>>(int64_t& value) { return Read(value, TYPE_INT64); }
60 InputBuffer& operator>>(int32_t& value) { return Read(value, TYPE_INT32); } 67 InputBuffer& operator>>(int32_t& value) { return Read(value, TYPE_INT32); }
61 InputBuffer& operator>>(bool& value) { return Read(value, TYPE_BOOL); } 68 InputBuffer& operator>>(bool& value) { return Read(value, TYPE_BOOL); }
62 InputBuffer& operator>>(std::vector<std::string>& value) { return ReadString s(value); } 69 InputBuffer& operator>>(std::vector<std::string>& value) { return ReadString s(value); }
70 InputBuffer& operator>>(std::vector<std::wstring>& value)
71 {
72 std::vector<std::string> tmpStrings;
Eric 2015/02/17 18:15:13 Ugh. A temporary vector is just ugly and inefficie
73 operator>>(tmpStrings);
74 value = ToUtf16Strings(tmpStrings);
75 return *this;
76 }
63 InputBuffer& operator=(const InputBuffer& copy) 77 InputBuffer& operator=(const InputBuffer& copy)
64 { 78 {
65 hasType = copy.hasType; 79 hasType = copy.hasType;
66 buffer = std::istringstream(copy.buffer.str()); 80 buffer = std::istringstream(copy.buffer.str());
67 currentType = copy.currentType; 81 currentType = copy.currentType;
68 return *this; 82 return *this;
69 } 83 }
70 ValueType GetType(); 84 ValueType GetType();
71 private: 85 private:
72 std::istringstream buffer; 86 std::istringstream buffer;
73 ValueType currentType; 87 ValueType currentType;
74 bool hasType; 88 bool hasType;
75 89
76 void CheckType(ValueType expectedType); 90 void CheckType(ValueType expectedType);
77 91
78 template<class T> 92 InputBuffer& ReadString(std::string& value)
79 InputBuffer& ReadString(T& value, ValueType expectedType)
80 { 93 {
81 CheckType(expectedType); 94 CheckType(TYPE_STRING);
82 95
83 SizeType length; 96 SizeType length;
84 ReadBinary(length); 97 ReadBinary(length);
85 98 value.resize(length, '\0');
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
86 std::auto_ptr<T::value_type> data(new T::value_type[length]); 99 buffer.read(&value[0], sizeof(std::string::value_type) * length);
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
87 buffer.read(reinterpret_cast<char*>(data.get()), sizeof(T::value_type) * l ength);
88 if (buffer.fail()) 100 if (buffer.fail())
89 throw new std::runtime_error("Unexpected end of input buffer"); 101 throw new std::runtime_error("Unexpected end of input buffer");
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
90
91 value.assign(data.get(), length);
92 return *this; 102 return *this;
93 } 103 }
94 104
95 InputBuffer& ReadStrings(std::vector<std::string>& value) 105 InputBuffer& ReadStrings(std::vector<std::string>& value)
96 { 106 {
97 value.clear(); 107 value.clear();
98 CheckType(ValueType::TYPE_STRINGS); 108 CheckType(ValueType::TYPE_STRINGS);
99 109
100 SizeType length; 110 SizeType length;
101 ReadBinary(length); 111 ReadBinary(length);
(...skipping 29 matching lines...) Expand all
131 OutputBuffer() {} 141 OutputBuffer() {}
132 142
133 // Explicit copy constructor to allow returning OutputBuffer by value 143 // Explicit copy constructor to allow returning OutputBuffer by value
134 OutputBuffer(const OutputBuffer& copy) : buffer(copy.buffer.str()) {} 144 OutputBuffer(const OutputBuffer& copy) : buffer(copy.buffer.str()) {}
135 145
136 std::string Get() 146 std::string Get()
137 { 147 {
138 return buffer.str(); 148 return buffer.str();
139 } 149 }
140 OutputBuffer& operator<<(ProcType value) { return Write(value, TYPE_PROC); } 150 OutputBuffer& operator<<(ProcType value) { return Write(value, TYPE_PROC); }
141 OutputBuffer& operator<<(const std::string& value) { return WriteString(valu e, TYPE_STRING); } 151 OutputBuffer& operator<<(const std::string& value) { return WriteString(valu e); }
142 OutputBuffer& operator<<(const std::wstring& value) { return WriteString(val ue, TYPE_WSTRING); } 152 OutputBuffer& operator<<(const std::wstring& value)
153 {
154 return operator<<(ToUtf8String(value));
155 }
143 OutputBuffer& operator<<(int64_t value) { return Write(value, TYPE_INT64); } 156 OutputBuffer& operator<<(int64_t value) { return Write(value, TYPE_INT64); }
144 OutputBuffer& operator<<(int32_t value) { return Write(value, TYPE_INT32); } 157 OutputBuffer& operator<<(int32_t value) { return Write(value, TYPE_INT32); }
145 OutputBuffer& operator<<(bool value) { return Write(value, TYPE_BOOL); } 158 OutputBuffer& operator<<(bool value) { return Write(value, TYPE_BOOL); }
146 OutputBuffer& operator<<(const std::vector<std::string>& value) { return Wri teStrings(value); } 159 OutputBuffer& operator<<(const std::vector<std::string>& value) { return Wri teStrings(value); }
147 private: 160 private:
148 std::ostringstream buffer; 161 std::ostringstream buffer;
149 162
150 // Disallow copying 163 // Disallow copying
151 const OutputBuffer& operator=(const OutputBuffer&); 164 const OutputBuffer& operator=(const OutputBuffer&);
152 165
153 template<class T> 166 OutputBuffer& WriteString(const std::string& value)
154 OutputBuffer& WriteString(const T& value, ValueType type)
155 { 167 {
156 WriteBinary(type); 168 WriteBinary(TYPE_STRING);
157 169
158 SizeType length = static_cast<SizeType>(value.size()); 170 SizeType length = static_cast<SizeType>(value.size());
159 WriteBinary(length); 171 WriteBinary(length);
160 172 buffer.write(value.c_str(), sizeof(std::string::value_type) * length);
Eric 2015/02/17 18:15:13 See sizeof comment above.
sergei 2015/02/18 14:13:06 done.
161 buffer.write(reinterpret_cast<const char*>(value.c_str()), sizeof(T::value _type) * length);
162 if (buffer.fail()) 173 if (buffer.fail())
163 throw new std::runtime_error("Unexpected error writing to output buffer" ); 174 throw new std::runtime_error("Unexpected error writing to output buffer" );
164 175
165 return *this; 176 return *this;
166 } 177 }
167 178
168 OutputBuffer& WriteStrings(const std::vector<std::string>& value) 179 OutputBuffer& WriteStrings(const std::vector<std::string>& value)
169 { 180 {
170 WriteBinary(ValueType::TYPE_STRINGS); 181 WriteBinary(ValueType::TYPE_STRINGS);
171 WriteBinary(static_cast<SizeType>(value.size())); 182 WriteBinary(static_cast<SizeType>(value.size()));
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 232
222 InputBuffer ReadMessage(); 233 InputBuffer ReadMessage();
223 void WriteMessage(OutputBuffer& message); 234 void WriteMessage(OutputBuffer& message);
224 235
225 protected: 236 protected:
226 HANDLE pipe; 237 HANDLE pipe;
227 }; 238 };
228 } 239 }
229 240
230 #endif 241 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld