OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <google/protobuf/message_lite.h> |
| 6 #include <memory> |
| 7 |
| 8 #include "base/macros.h" |
| 9 #include "chromeos/components/tether/proto/tether.pb.h" |
| 10 |
| 11 namespace chromeos { |
| 12 |
| 13 namespace tether { |
| 14 |
| 15 // Wraps a message sent between devices. To send a message over the wire, |
| 16 // construct a MessageWrapper by passing the message's proto as a constructor |
| 17 // parameter, then call |ToRawMessage()| and send the resulting string. When a |
| 18 // message has been received, pass it to |FromRawMessage()| to create a |
| 19 // MessageWrapper object, then use |GetMessageType()| to determine the type of |
| 20 // message that has been received. To access the wrapped proto for a received |
| 21 // message, call |GetProto()| and cast the result to the associated message |
| 22 // class. |
| 23 class MessageWrapper { |
| 24 public: |
| 25 // Creates a MessageWrapper from a raw string received from a remote device. |
| 26 // Returns |nullptr| if the received message is not a valid tethering message. |
| 27 static std::unique_ptr<MessageWrapper> FromRawMessage( |
| 28 const std::string& message); |
| 29 |
| 30 MessageWrapper(const ConnectTetheringRequest& request); |
| 31 MessageWrapper(const ConnectTetheringResponse& response); |
| 32 MessageWrapper(const DisconnectTetheringRequest& request); |
| 33 MessageWrapper(const KeepAliveTickle& tickle); |
| 34 MessageWrapper(const TetherAvailabilityRequest& request); |
| 35 MessageWrapper(const TetherAvailabilityResponse& response); |
| 36 |
| 37 ~MessageWrapper(); |
| 38 |
| 39 std::shared_ptr<google::protobuf::MessageLite> GetProto() const; |
| 40 MessageType GetMessageType() const; |
| 41 |
| 42 // To send a message to a remote device, use ToRawMessage() and send the |
| 43 // result over the wire. |
| 44 std::string ToRawMessage() const; |
| 45 |
| 46 protected: |
| 47 MessageWrapper(const MessageType& type, |
| 48 std::shared_ptr<google::protobuf::MessageLite> proto); |
| 49 |
| 50 private: |
| 51 MessageType type_; |
| 52 std::shared_ptr<google::protobuf::MessageLite> proto_; |
| 53 |
| 54 DISALLOW_COPY_AND_ASSIGN(MessageWrapper); |
| 55 }; |
| 56 |
| 57 } // namespace tether |
| 58 |
| 59 } // namespace chromeos |
OLD | NEW |