Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(980)

Side by Side Diff: dbus/message.cc

Issue 9315006: Adding support for sending/receiving proto bufs to dbus library. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Added in the DEPS file as suggested Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #include "dbus/message.h" 5 #include "dbus/message.h"
6 6
7 #include <string>
8
7 #include "base/basictypes.h" 9 #include "base/basictypes.h"
8 #include "base/format_macros.h" 10 #include "base/format_macros.h"
9 #include "base/logging.h" 11 #include "base/logging.h"
10 #include "base/stringprintf.h" 12 #include "base/stringprintf.h"
13 #include "third_party/protobuf/src/google/protobuf/message_lite.h"
11 14
12 namespace { 15 namespace {
13 16
14 // Appends the header name and the value to |output|, if the value is 17 // Appends the header name and the value to |output|, if the value is
15 // not empty. 18 // not empty.
16 static void AppendStringHeader(const std::string& header_name, 19 static void AppendStringHeader(const std::string& header_name,
17 const std::string& header_value, 20 const std::string& header_value,
18 std::string* output) { 21 std::string* output) {
19 if (!header_value.empty()) { 22 if (!header_value.empty()) {
20 *output += header_name + ": " + header_value + "\n"; 23 *output += header_name + ": " + header_value + "\n";
(...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after
585 const std::vector<std::string>& object_paths) { 588 const std::vector<std::string>& object_paths) {
586 DCHECK(!container_is_open_); 589 DCHECK(!container_is_open_);
587 MessageWriter array_writer(message_); 590 MessageWriter array_writer(message_);
588 OpenArray("o", &array_writer); 591 OpenArray("o", &array_writer);
589 for (size_t i = 0; i < object_paths.size(); ++i) { 592 for (size_t i = 0; i < object_paths.size(); ++i) {
590 array_writer.AppendObjectPath(object_paths[i]); 593 array_writer.AppendObjectPath(object_paths[i]);
591 } 594 }
592 CloseContainer(&array_writer); 595 CloseContainer(&array_writer);
593 } 596 }
594 597
598 bool MessageWriter::AppendProtoAsArrayOfBytes(
599 const google::protobuf::MessageLite& protobuf) {
600 std::string serialized_proto;
601 if (!protobuf.SerializeToString(&serialized_proto)) {
602 LOG(ERROR) << "Unable to serialize supplied protocol buffer";
603 return false;
604 }
605 AppendArrayOfBytes(reinterpret_cast<const uint8*>(serialized_proto.data()),
606 serialized_proto.size());
607 return true;
608 }
609
595 void MessageWriter::AppendVariantOfByte(uint8 value) { 610 void MessageWriter::AppendVariantOfByte(uint8 value) {
596 AppendVariantOfBasic(DBUS_TYPE_BYTE, &value); 611 AppendVariantOfBasic(DBUS_TYPE_BYTE, &value);
597 } 612 }
598 613
599 void MessageWriter::AppendVariantOfBool(bool value) { 614 void MessageWriter::AppendVariantOfBool(bool value) {
600 // See the comment at MessageWriter::AppendBool(). 615 // See the comment at MessageWriter::AppendBool().
601 dbus_bool_t dbus_value = value; 616 dbus_bool_t dbus_value = value;
602 AppendVariantOfBasic(DBUS_TYPE_BOOLEAN, &dbus_value); 617 AppendVariantOfBasic(DBUS_TYPE_BOOLEAN, &dbus_value);
603 } 618 }
604 619
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
792 return false; 807 return false;
793 while (array_reader.HasMoreData()) { 808 while (array_reader.HasMoreData()) {
794 std::string object_path; 809 std::string object_path;
795 if (!array_reader.PopObjectPath(&object_path)) 810 if (!array_reader.PopObjectPath(&object_path))
796 return false; 811 return false;
797 object_paths->push_back(object_path); 812 object_paths->push_back(object_path);
798 } 813 }
799 return true; 814 return true;
800 } 815 }
801 816
817 bool MessageReader::PopArrayOfBytesAsProto(
818 google::protobuf::MessageLite* protobuf) {
819 DCHECK(protobuf != NULL);
820 char* serialized_buf = NULL;
821 size_t buf_size = 0;
822 if (!PopArrayOfBytes(reinterpret_cast<uint8**>(&serialized_buf), &buf_size)) {
823 LOG(ERROR) << "Error reading array of bytes";
824 return false;
825 }
826 if (!protobuf->ParseFromArray(serialized_buf, buf_size)) {
827 LOG(ERROR) << "Failed to parse protocol buffer from array";
828 return false;
829 }
830 return true;
831 }
832
802 bool MessageReader::PopVariantOfByte(uint8* value) { 833 bool MessageReader::PopVariantOfByte(uint8* value) {
803 return PopVariantOfBasic(DBUS_TYPE_BYTE, value); 834 return PopVariantOfBasic(DBUS_TYPE_BYTE, value);
804 } 835 }
805 836
806 bool MessageReader::PopVariantOfBool(bool* value) { 837 bool MessageReader::PopVariantOfBool(bool* value) {
807 // See the comment at MessageReader::PopBool(). 838 // See the comment at MessageReader::PopBool().
808 dbus_bool_t dbus_value = FALSE; 839 dbus_bool_t dbus_value = FALSE;
809 const bool success = PopVariantOfBasic(DBUS_TYPE_BOOLEAN, &dbus_value); 840 const bool success = PopVariantOfBasic(DBUS_TYPE_BOOLEAN, &dbus_value);
810 *value = static_cast<bool>(dbus_value); 841 *value = static_cast<bool>(dbus_value);
811 return success; 842 return success;
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
894 } 925 }
895 926
896 bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) { 927 bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) {
897 dbus::MessageReader variant_reader(message_); 928 dbus::MessageReader variant_reader(message_);
898 if (!PopVariant(&variant_reader)) 929 if (!PopVariant(&variant_reader))
899 return false; 930 return false;
900 return variant_reader.PopBasic(dbus_type, value); 931 return variant_reader.PopBasic(dbus_type, value);
901 } 932 }
902 933
903 } // namespace dbus 934 } // namespace dbus
OLDNEW
« no previous file with comments | « dbus/message.h ('k') | dbus/message_unittest.cc » ('j') | dbus/test_proto.proto » ('J')

Powered by Google App Engine
This is Rietveld 408576698