OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_CRYPTAUTH_WIRE_MESSAGE_H_ | 5 #ifndef COMPONENTS_CRYPTAUTH_WIRE_MESSAGE_H_ |
6 #define COMPONENTS_CRYPTAUTH_WIRE_MESSAGE_H_ | 6 #define COMPONENTS_CRYPTAUTH_WIRE_MESSAGE_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 | 12 |
13 namespace cryptauth { | 13 namespace cryptauth { |
14 | 14 |
15 class WireMessage { | 15 class WireMessage { |
16 public: | 16 public: |
17 // Creates a WireMessage containing |payload| for feature |feature|. | 17 // Creates a WireMessage containing |payload| for feature |feature|. |
18 explicit WireMessage(const std::string& payload, const std::string& feature); | 18 explicit WireMessage(const std::string& payload, const std::string& feature); |
| 19 |
| 20 // Creates a WireMessage containing |body| (a serialized JSON) as the message |
| 21 // body. |
| 22 explicit WireMessage(const std::string& body); |
| 23 |
19 virtual ~WireMessage(); | 24 virtual ~WireMessage(); |
20 | 25 |
21 // Returns the deserialized message from |serialized_message|, or nullptr if | 26 // Returns the deserialized message from |serialized_message|, or nullptr if |
22 // the message is malformed. Sets |is_incomplete_message| to true if the | 27 // the message is malformed. Sets |is_incomplete_message| to true if the |
23 // message does not have enough data to parse the header, or if the message | 28 // message does not have enough data to parse the header, or if the message |
24 // length encoded in the message header exceeds the size of the | 29 // length encoded in the message header exceeds the size of the |
25 // |serialized_message|. | 30 // |serialized_message|. |
26 static std::unique_ptr<WireMessage> Deserialize( | 31 static std::unique_ptr<WireMessage> Deserialize( |
27 const std::string& serialized_message, | 32 const std::string& serialized_message, |
28 bool* is_incomplete_message); | 33 bool* is_incomplete_message); |
29 | 34 |
30 // Returns a serialized representation of |this| message. | 35 // Returns a serialized representation of |this| message. |
31 virtual std::string Serialize() const; | 36 virtual std::string Serialize() const; |
32 | 37 |
33 const std::string& payload() const { return payload_; } | 38 const std::string& payload() const { return payload_; } |
34 const std::string& feature() const { return feature_; } | 39 const std::string& feature() const { return feature_; } |
| 40 const std::string& body() const { return body_; } |
35 | 41 |
36 private: | 42 private: |
37 // The message payload. | 43 // The message payload. |
38 const std::string payload_; | 44 const std::string payload_; |
39 | 45 |
40 // The feature which sends or intends to receive this message (e.g., | 46 // The feature which sends or intends to receive this message (e.g., |
41 // EasyUnlock). | 47 // EasyUnlock). |
42 const std::string feature_; | 48 const std::string feature_; |
43 | 49 |
| 50 // The message body. When this is set |payload_| and |feature_| are empty, and |
| 51 // vice-versa. |
| 52 const std::string body_; |
| 53 |
44 DISALLOW_COPY_AND_ASSIGN(WireMessage); | 54 DISALLOW_COPY_AND_ASSIGN(WireMessage); |
45 }; | 55 }; |
46 | 56 |
47 } // namespace cryptauth | 57 } // namespace cryptauth |
48 | 58 |
49 #endif // COMPONENTS_CRYPTAUTH_WIRE_MESSAGE_H_ | 59 #endif // COMPONENTS_CRYPTAUTH_WIRE_MESSAGE_H_ |
OLD | NEW |