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: fix forgotten sizeof and add check for string length Created Feb. 18, 2015, 2:37 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;
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);
86 std::auto_ptr<T::value_type> data(new T::value_type[length]); 99 if (length > 0)
87 buffer.read(reinterpret_cast<char*>(data.get()), sizeof(T::value_type) * l ength); 100 {
101 buffer.read(&value[0], length);
102 }
88 if (buffer.fail()) 103 if (buffer.fail())
89 throw new std::runtime_error("Unexpected end of input buffer"); 104 throw new std::runtime_error("Unexpected end of input buffer");
Eric 2015/02/18 18:12:47 This test for failure should be guarded by the con
sergei 2015/02/19 17:26:57 moved
90
91 value.assign(data.get(), length);
92 return *this; 105 return *this;
93 } 106 }
94 107
95 InputBuffer& ReadStrings(std::vector<std::string>& value) 108 InputBuffer& ReadStrings(std::vector<std::string>& value)
96 { 109 {
97 value.clear(); 110 value.clear();
98 CheckType(ValueType::TYPE_STRINGS); 111 CheckType(ValueType::TYPE_STRINGS);
99 112
100 SizeType length; 113 SizeType length;
101 ReadBinary(length); 114 ReadBinary(length);
(...skipping 29 matching lines...) Expand all
131 OutputBuffer() {} 144 OutputBuffer() {}
132 145
133 // Explicit copy constructor to allow returning OutputBuffer by value 146 // Explicit copy constructor to allow returning OutputBuffer by value
134 OutputBuffer(const OutputBuffer& copy) : buffer(copy.buffer.str()) {} 147 OutputBuffer(const OutputBuffer& copy) : buffer(copy.buffer.str()) {}
135 148
136 std::string Get() 149 std::string Get()
137 { 150 {
138 return buffer.str(); 151 return buffer.str();
139 } 152 }
140 OutputBuffer& operator<<(ProcType value) { return Write(value, TYPE_PROC); } 153 OutputBuffer& operator<<(ProcType value) { return Write(value, TYPE_PROC); }
141 OutputBuffer& operator<<(const std::string& value) { return WriteString(valu e, TYPE_STRING); } 154 OutputBuffer& operator<<(const std::string& value) { return WriteString(valu e); }
142 OutputBuffer& operator<<(const std::wstring& value) { return WriteString(val ue, TYPE_WSTRING); } 155 OutputBuffer& operator<<(const std::wstring& value)
156 {
157 return operator<<(ToUtf8String(value));
158 }
143 OutputBuffer& operator<<(int64_t value) { return Write(value, TYPE_INT64); } 159 OutputBuffer& operator<<(int64_t value) { return Write(value, TYPE_INT64); }
144 OutputBuffer& operator<<(int32_t value) { return Write(value, TYPE_INT32); } 160 OutputBuffer& operator<<(int32_t value) { return Write(value, TYPE_INT32); }
145 OutputBuffer& operator<<(bool value) { return Write(value, TYPE_BOOL); } 161 OutputBuffer& operator<<(bool value) { return Write(value, TYPE_BOOL); }
146 OutputBuffer& operator<<(const std::vector<std::string>& value) { return Wri teStrings(value); } 162 OutputBuffer& operator<<(const std::vector<std::string>& value) { return Wri teStrings(value); }
147 private: 163 private:
148 std::ostringstream buffer; 164 std::ostringstream buffer;
149 165
150 // Disallow copying 166 // Disallow copying
151 const OutputBuffer& operator=(const OutputBuffer&); 167 const OutputBuffer& operator=(const OutputBuffer&);
152 168
153 template<class T> 169 OutputBuffer& WriteString(const std::string& value)
154 OutputBuffer& WriteString(const T& value, ValueType type)
155 { 170 {
156 WriteBinary(type); 171 WriteBinary(TYPE_STRING);
157 172
158 SizeType length = static_cast<SizeType>(value.size()); 173 SizeType length = static_cast<SizeType>(value.size());
159 WriteBinary(length); 174 WriteBinary(length);
160 175 buffer.write(value.c_str(), length);
161 buffer.write(reinterpret_cast<const char*>(value.c_str()), sizeof(T::value _type) * length);
162 if (buffer.fail()) 176 if (buffer.fail())
163 throw new std::runtime_error("Unexpected error writing to output buffer" ); 177 throw new std::runtime_error("Unexpected error writing to output buffer" );
164 178
165 return *this; 179 return *this;
166 } 180 }
167 181
168 OutputBuffer& WriteStrings(const std::vector<std::string>& value) 182 OutputBuffer& WriteStrings(const std::vector<std::string>& value)
169 { 183 {
170 WriteBinary(ValueType::TYPE_STRINGS); 184 WriteBinary(ValueType::TYPE_STRINGS);
171 WriteBinary(static_cast<SizeType>(value.size())); 185 WriteBinary(static_cast<SizeType>(value.size()));
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 235
222 InputBuffer ReadMessage(); 236 InputBuffer ReadMessage();
223 void WriteMessage(OutputBuffer& message); 237 void WriteMessage(OutputBuffer& message);
224 238
225 protected: 239 protected:
226 HANDLE pipe; 240 HANDLE pipe;
227 }; 241 };
228 } 242 }
229 243
230 #endif 244 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld