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

Side by Side Diff: dbus/message.cc

Issue 9378039: dbus: add ObjectPath type (Closed) Base URL: http://git.chromium.org/git/chromium/src@master
Patch Set: add patch for cryptohome_client 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
« no previous file with comments | « dbus/message.h ('k') | dbus/message_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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> 7 #include <string>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/format_macros.h" 10 #include "base/format_macros.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/stringprintf.h" 12 #include "base/stringprintf.h"
13 #include "dbus/object_path.h"
13 #include "third_party/protobuf/src/google/protobuf/message_lite.h" 14 #include "third_party/protobuf/src/google/protobuf/message_lite.h"
14 15
15 namespace { 16 namespace {
16 17
17 // Appends the header name and the value to |output|, if the value is 18 // Appends the header name and the value to |output|, if the value is
18 // not empty. 19 // not empty.
19 static void AppendStringHeader(const std::string& header_name, 20 static void AppendStringHeader(const std::string& header_name,
20 const std::string& header_value, 21 const std::string& header_value,
21 std::string* output) { 22 std::string* output) {
22 if (!header_value.empty()) { 23 if (!header_value.empty()) {
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 break; 151 break;
151 } 152 }
152 case STRING: { 153 case STRING: {
153 std::string value; 154 std::string value;
154 if (!reader->PopString(&value)) 155 if (!reader->PopString(&value))
155 return kBrokenMessage; 156 return kBrokenMessage;
156 output += indent + "string \"" + value + "\"\n"; 157 output += indent + "string \"" + value + "\"\n";
157 break; 158 break;
158 } 159 }
159 case OBJECT_PATH: { 160 case OBJECT_PATH: {
160 std::string value; 161 ObjectPath value;
161 if (!reader->PopObjectPath(&value)) 162 if (!reader->PopObjectPath(&value))
162 return kBrokenMessage; 163 return kBrokenMessage;
163 output += indent + "object_path \"" + value + "\"\n"; 164 output += indent + "object_path \"" + value.value() + "\"\n";
164 break; 165 break;
165 } 166 }
166 case ARRAY: { 167 case ARRAY: {
167 MessageReader sub_reader(this); 168 MessageReader sub_reader(this);
168 if (!reader->PopArray(&sub_reader)) 169 if (!reader->PopArray(&sub_reader))
169 return kBrokenMessage; 170 return kBrokenMessage;
170 output += indent + "array [\n"; 171 output += indent + "array [\n";
171 output += ToStringInternal(indent + " ", &sub_reader); 172 output += ToStringInternal(indent + " ", &sub_reader);
172 output += indent + "]\n"; 173 output += indent + "]\n";
173 break; 174 break;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 // string \"payload\" 218 // string \"payload\"
218 // ... 219 // ...
219 std::string Message::ToString() { 220 std::string Message::ToString() {
220 if (!raw_message_) 221 if (!raw_message_)
221 return ""; 222 return "";
222 223
223 // Generate headers first. 224 // Generate headers first.
224 std::string headers; 225 std::string headers;
225 AppendStringHeader("message_type", GetMessageTypeAsString(), &headers); 226 AppendStringHeader("message_type", GetMessageTypeAsString(), &headers);
226 AppendStringHeader("destination", GetDestination(), &headers); 227 AppendStringHeader("destination", GetDestination(), &headers);
227 AppendStringHeader("path", GetPath(), &headers); 228 AppendStringHeader("path", GetPath().value(), &headers);
228 AppendStringHeader("interface", GetInterface(), &headers); 229 AppendStringHeader("interface", GetInterface(), &headers);
229 AppendStringHeader("member", GetMember(), &headers); 230 AppendStringHeader("member", GetMember(), &headers);
230 AppendStringHeader("error_name", GetErrorName(), &headers); 231 AppendStringHeader("error_name", GetErrorName(), &headers);
231 AppendStringHeader("sender", GetSender(), &headers); 232 AppendStringHeader("sender", GetSender(), &headers);
232 AppendStringHeader("signature", GetSignature(), &headers); 233 AppendStringHeader("signature", GetSignature(), &headers);
233 AppendUint32Header("serial", GetSerial(), &headers); 234 AppendUint32Header("serial", GetSerial(), &headers);
234 AppendUint32Header("reply_serial", GetReplySerial(), &headers); 235 AppendUint32Header("reply_serial", GetReplySerial(), &headers);
235 236
236 // Generate the payload. 237 // Generate the payload.
237 MessageReader reader(this); 238 MessageReader reader(this);
238 return headers + "\n" + ToStringInternal("", &reader); 239 return headers + "\n" + ToStringInternal("", &reader);
239 } 240 }
240 241
241 void Message::SetDestination(const std::string& destination) { 242 void Message::SetDestination(const std::string& destination) {
242 const bool success = dbus_message_set_destination(raw_message_, 243 const bool success = dbus_message_set_destination(raw_message_,
243 destination.c_str()); 244 destination.c_str());
244 CHECK(success) << "Unable to allocate memory"; 245 CHECK(success) << "Unable to allocate memory";
245 } 246 }
246 247
247 void Message::SetPath(const std::string& path) { 248 void Message::SetPath(const ObjectPath& path) {
248 const bool success = dbus_message_set_path(raw_message_, 249 const bool success = dbus_message_set_path(raw_message_,
249 path.c_str()); 250 path.value().c_str());
250 CHECK(success) << "Unable to allocate memory"; 251 CHECK(success) << "Unable to allocate memory";
251 } 252 }
252 253
253 void Message::SetInterface(const std::string& interface) { 254 void Message::SetInterface(const std::string& interface) {
254 const bool success = dbus_message_set_interface(raw_message_, 255 const bool success = dbus_message_set_interface(raw_message_,
255 interface.c_str()); 256 interface.c_str());
256 CHECK(success) << "Unable to allocate memory"; 257 CHECK(success) << "Unable to allocate memory";
257 } 258 }
258 259
259 void Message::SetMember(const std::string& member) { 260 void Message::SetMember(const std::string& member) {
(...skipping 20 matching lines...) Expand all
280 281
281 void Message::SetReplySerial(uint32 reply_serial) { 282 void Message::SetReplySerial(uint32 reply_serial) {
282 dbus_message_set_reply_serial(raw_message_, reply_serial); 283 dbus_message_set_reply_serial(raw_message_, reply_serial);
283 } 284 }
284 285
285 std::string Message::GetDestination() { 286 std::string Message::GetDestination() {
286 const char* destination = dbus_message_get_destination(raw_message_); 287 const char* destination = dbus_message_get_destination(raw_message_);
287 return destination ? destination : ""; 288 return destination ? destination : "";
288 } 289 }
289 290
290 std::string Message::GetPath() { 291 ObjectPath Message::GetPath() {
291 const char* path = dbus_message_get_path(raw_message_); 292 const char* path = dbus_message_get_path(raw_message_);
292 return path ? path : ""; 293 return ObjectPath(path ? path : "");
293 } 294 }
294 295
295 std::string Message::GetInterface() { 296 std::string Message::GetInterface() {
296 const char* interface = dbus_message_get_interface(raw_message_); 297 const char* interface = dbus_message_get_interface(raw_message_);
297 return interface ? interface : ""; 298 return interface ? interface : "";
298 } 299 }
299 300
300 std::string Message::GetMember() { 301 std::string Message::GetMember() {
301 const char* member = dbus_message_get_member(raw_message_); 302 const char* member = dbus_message_get_member(raw_message_);
302 return member ? member : ""; 303 return member ? member : "";
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 } 484 }
484 485
485 void MessageWriter::AppendString(const std::string& value) { 486 void MessageWriter::AppendString(const std::string& value) {
486 const char* pointer = value.c_str(); 487 const char* pointer = value.c_str();
487 AppendBasic(DBUS_TYPE_STRING, &pointer); 488 AppendBasic(DBUS_TYPE_STRING, &pointer);
488 // TODO(satorux): It may make sense to return an error here, as the 489 // TODO(satorux): It may make sense to return an error here, as the
489 // input string can be large. If needed, we could add something like 490 // input string can be large. If needed, we could add something like
490 // bool AppendStringWithErrorChecking(). 491 // bool AppendStringWithErrorChecking().
491 } 492 }
492 493
493 void MessageWriter::AppendObjectPath(const std::string& value) { 494 void MessageWriter::AppendObjectPath(const ObjectPath& value) {
494 const char* pointer = value.c_str(); 495 const char* pointer = value.value().c_str();
495 AppendBasic(DBUS_TYPE_OBJECT_PATH, &pointer); 496 AppendBasic(DBUS_TYPE_OBJECT_PATH, &pointer);
496 } 497 }
497 498
498 // Ideally, client shouldn't need to supply the signature string, but 499 // Ideally, client shouldn't need to supply the signature string, but
499 // the underlying D-Bus library requires us to supply this before 500 // the underlying D-Bus library requires us to supply this before
500 // appending contents to array and variant. It's technically possible 501 // appending contents to array and variant. It's technically possible
501 // for us to design API that doesn't require the signature but it will 502 // for us to design API that doesn't require the signature but it will
502 // complicate the implementation so we decided to have the signature 503 // complicate the implementation so we decided to have the signature
503 // parameter. Hopefully, variants are less used in request messages from 504 // parameter. Hopefully, variants are less used in request messages from
504 // client side than response message from server side, so this should 505 // client side than response message from server side, so this should
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
580 DCHECK(!container_is_open_); 581 DCHECK(!container_is_open_);
581 MessageWriter array_writer(message_); 582 MessageWriter array_writer(message_);
582 OpenArray("s", &array_writer); 583 OpenArray("s", &array_writer);
583 for (size_t i = 0; i < strings.size(); ++i) { 584 for (size_t i = 0; i < strings.size(); ++i) {
584 array_writer.AppendString(strings[i]); 585 array_writer.AppendString(strings[i]);
585 } 586 }
586 CloseContainer(&array_writer); 587 CloseContainer(&array_writer);
587 } 588 }
588 589
589 void MessageWriter::AppendArrayOfObjectPaths( 590 void MessageWriter::AppendArrayOfObjectPaths(
590 const std::vector<std::string>& object_paths) { 591 const std::vector<ObjectPath>& object_paths) {
591 DCHECK(!container_is_open_); 592 DCHECK(!container_is_open_);
592 MessageWriter array_writer(message_); 593 MessageWriter array_writer(message_);
593 OpenArray("o", &array_writer); 594 OpenArray("o", &array_writer);
594 for (size_t i = 0; i < object_paths.size(); ++i) { 595 for (size_t i = 0; i < object_paths.size(); ++i) {
595 array_writer.AppendObjectPath(object_paths[i]); 596 array_writer.AppendObjectPath(object_paths[i]);
596 } 597 }
597 CloseContainer(&array_writer); 598 CloseContainer(&array_writer);
598 } 599 }
599 600
600 bool MessageWriter::AppendProtoAsArrayOfBytes( 601 bool MessageWriter::AppendProtoAsArrayOfBytes(
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
645 646
646 void MessageWriter::AppendVariantOfDouble(double value) { 647 void MessageWriter::AppendVariantOfDouble(double value) {
647 AppendVariantOfBasic(DBUS_TYPE_DOUBLE, &value); 648 AppendVariantOfBasic(DBUS_TYPE_DOUBLE, &value);
648 } 649 }
649 650
650 void MessageWriter::AppendVariantOfString(const std::string& value) { 651 void MessageWriter::AppendVariantOfString(const std::string& value) {
651 const char* pointer = value.c_str(); 652 const char* pointer = value.c_str();
652 AppendVariantOfBasic(DBUS_TYPE_STRING, &pointer); 653 AppendVariantOfBasic(DBUS_TYPE_STRING, &pointer);
653 } 654 }
654 655
655 void MessageWriter::AppendVariantOfObjectPath(const std::string& value) { 656 void MessageWriter::AppendVariantOfObjectPath(const ObjectPath& value) {
656 const char* pointer = value.c_str(); 657 const char* pointer = value.value().c_str();
657 AppendVariantOfBasic(DBUS_TYPE_OBJECT_PATH, &pointer); 658 AppendVariantOfBasic(DBUS_TYPE_OBJECT_PATH, &pointer);
658 } 659 }
659 660
660 void MessageWriter::AppendBasic(int dbus_type, const void* value) { 661 void MessageWriter::AppendBasic(int dbus_type, const void* value) {
661 DCHECK(!container_is_open_); 662 DCHECK(!container_is_open_);
662 663
663 const bool success = dbus_message_iter_append_basic( 664 const bool success = dbus_message_iter_append_basic(
664 &raw_message_iter_, dbus_type, value); 665 &raw_message_iter_, dbus_type, value);
665 // dbus_message_iter_append_basic() fails only when there is not enough 666 // dbus_message_iter_append_basic() fails only when there is not enough
666 // memory. We don't return this error as there is nothing we can do when 667 // memory. We don't return this error as there is nothing we can do when
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
739 } 740 }
740 741
741 bool MessageReader::PopString(std::string* value) { 742 bool MessageReader::PopString(std::string* value) {
742 char* tmp_value = NULL; 743 char* tmp_value = NULL;
743 const bool success = PopBasic(DBUS_TYPE_STRING, &tmp_value); 744 const bool success = PopBasic(DBUS_TYPE_STRING, &tmp_value);
744 if (success) 745 if (success)
745 value->assign(tmp_value); 746 value->assign(tmp_value);
746 return success; 747 return success;
747 } 748 }
748 749
749 bool MessageReader::PopObjectPath(std::string* value) { 750 bool MessageReader::PopObjectPath(ObjectPath* value) {
750 char* tmp_value = NULL; 751 char* tmp_value = NULL;
751 const bool success = PopBasic(DBUS_TYPE_OBJECT_PATH, &tmp_value); 752 const bool success = PopBasic(DBUS_TYPE_OBJECT_PATH, &tmp_value);
752 if (success) 753 if (success)
753 value->assign(tmp_value); 754 *value = ObjectPath(tmp_value);
754 return success; 755 return success;
755 } 756 }
756 757
757 bool MessageReader::PopArray(MessageReader* sub_reader) { 758 bool MessageReader::PopArray(MessageReader* sub_reader) {
758 return PopContainer(DBUS_TYPE_ARRAY, sub_reader); 759 return PopContainer(DBUS_TYPE_ARRAY, sub_reader);
759 } 760 }
760 761
761 bool MessageReader::PopStruct(MessageReader* sub_reader) { 762 bool MessageReader::PopStruct(MessageReader* sub_reader) {
762 return PopContainer(DBUS_TYPE_STRUCT, sub_reader); 763 return PopContainer(DBUS_TYPE_STRUCT, sub_reader);
763 } 764 }
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
798 while (array_reader.HasMoreData()) { 799 while (array_reader.HasMoreData()) {
799 std::string string; 800 std::string string;
800 if (!array_reader.PopString(&string)) 801 if (!array_reader.PopString(&string))
801 return false; 802 return false;
802 strings->push_back(string); 803 strings->push_back(string);
803 } 804 }
804 return true; 805 return true;
805 } 806 }
806 807
807 bool MessageReader::PopArrayOfObjectPaths( 808 bool MessageReader::PopArrayOfObjectPaths(
808 std::vector<std::string> *object_paths) { 809 std::vector<ObjectPath> *object_paths) {
809 MessageReader array_reader(message_); 810 MessageReader array_reader(message_);
810 if (!PopArray(&array_reader)) 811 if (!PopArray(&array_reader))
811 return false; 812 return false;
812 while (array_reader.HasMoreData()) { 813 while (array_reader.HasMoreData()) {
813 std::string object_path; 814 ObjectPath object_path;
814 if (!array_reader.PopObjectPath(&object_path)) 815 if (!array_reader.PopObjectPath(&object_path))
815 return false; 816 return false;
816 object_paths->push_back(object_path); 817 object_paths->push_back(object_path);
817 } 818 }
818 return true; 819 return true;
819 } 820 }
820 821
821 bool MessageReader::PopArrayOfBytesAsProto( 822 bool MessageReader::PopArrayOfBytesAsProto(
822 google::protobuf::MessageLite* protobuf) { 823 google::protobuf::MessageLite* protobuf) {
823 DCHECK(protobuf != NULL); 824 DCHECK(protobuf != NULL);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
875 } 876 }
876 877
877 bool MessageReader::PopVariantOfString(std::string* value) { 878 bool MessageReader::PopVariantOfString(std::string* value) {
878 char* tmp_value = NULL; 879 char* tmp_value = NULL;
879 const bool success = PopVariantOfBasic(DBUS_TYPE_STRING, &tmp_value); 880 const bool success = PopVariantOfBasic(DBUS_TYPE_STRING, &tmp_value);
880 if (success) 881 if (success)
881 value->assign(tmp_value); 882 value->assign(tmp_value);
882 return success; 883 return success;
883 } 884 }
884 885
885 bool MessageReader::PopVariantOfObjectPath(std::string* value) { 886 bool MessageReader::PopVariantOfObjectPath(ObjectPath* value) {
886 char* tmp_value = NULL; 887 char* tmp_value = NULL;
887 const bool success = PopVariantOfBasic(DBUS_TYPE_OBJECT_PATH, &tmp_value); 888 const bool success = PopVariantOfBasic(DBUS_TYPE_OBJECT_PATH, &tmp_value);
888 if (success) 889 if (success)
889 value->assign(tmp_value); 890 *value = ObjectPath(tmp_value);
890 return success; 891 return success;
891 } 892 }
892 893
893 Message::DataType MessageReader::GetDataType() { 894 Message::DataType MessageReader::GetDataType() {
894 const int dbus_type = dbus_message_iter_get_arg_type(&raw_message_iter_); 895 const int dbus_type = dbus_message_iter_get_arg_type(&raw_message_iter_);
895 return static_cast<Message::DataType>(dbus_type); 896 return static_cast<Message::DataType>(dbus_type);
896 } 897 }
897 898
898 bool MessageReader::CheckDataType(int dbus_type) { 899 bool MessageReader::CheckDataType(int dbus_type) {
899 const int actual_type = dbus_message_iter_get_arg_type(&raw_message_iter_); 900 const int actual_type = dbus_message_iter_get_arg_type(&raw_message_iter_);
(...skipping 29 matching lines...) Expand all
929 } 930 }
930 931
931 bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) { 932 bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) {
932 dbus::MessageReader variant_reader(message_); 933 dbus::MessageReader variant_reader(message_);
933 if (!PopVariant(&variant_reader)) 934 if (!PopVariant(&variant_reader))
934 return false; 935 return false;
935 return variant_reader.PopBasic(dbus_type, value); 936 return variant_reader.PopBasic(dbus_type, value);
936 } 937 }
937 938
938 } // namespace dbus 939 } // namespace dbus
OLDNEW
« no previous file with comments | « dbus/message.h ('k') | dbus/message_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698