Commit 6e3503be authored by Leonid Baraz's avatar Leonid Baraz Committed by Commit Bot

Add proto-serialization to Status in reporting.

Bug: b:153361703
Change-Id: I5c19a27fc74055b1659acc8ec8f07c460bd69fa0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2158870Reviewed-by: default avatarAnqing Zhao <anqing@google.com>
Reviewed-by: default avatarSergey Poromov <poromov@chromium.org>
Commit-Queue: Leonid Baraz <lbaraz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#761975}
parent 6829205c
...@@ -60,6 +60,7 @@ source_set("chromeos") { ...@@ -60,6 +60,7 @@ source_set("chromeos") {
":reporting_constants_proto", ":reporting_constants_proto",
":reporting_record_proto", ":reporting_record_proto",
":screen_brightness_event_proto", ":screen_brightness_event_proto",
":status_proto",
":user_activity_event_proto", ":user_activity_event_proto",
":user_charging_event_proto", ":user_charging_event_proto",
"crostini:crostini_installer_types_mojom", "crostini:crostini_installer_types_mojom",
...@@ -3271,6 +3272,7 @@ source_set("unit_tests") { ...@@ -3271,6 +3272,7 @@ source_set("unit_tests") {
deps = [ deps = [
":arc_test_support", ":arc_test_support",
":attestation_proto", ":attestation_proto",
":status_proto",
":test_support", ":test_support",
":user_activity_event_proto", ":user_activity_event_proto",
"//ash", "//ash",
...@@ -3424,6 +3426,11 @@ proto_library("reporting_constants_proto") { ...@@ -3424,6 +3426,11 @@ proto_library("reporting_constants_proto") {
sources = [ "policy/messaging_layer/public/record_constants.proto" ] sources = [ "policy/messaging_layer/public/record_constants.proto" ]
} }
proto_library("status_proto") {
sources = [ "policy/messaging_layer/util/status.proto" ]
generate_python = false
}
device_policy_remover_path = "$target_gen_dir/device_policy_remover.cc" device_policy_remover_path = "$target_gen_dir/device_policy_remover.cc"
action("device_policy_remover_generate") { action("device_policy_remover_generate") {
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "base/no_destructor.h" #include "base/no_destructor.h"
#include "base/strings/strcat.h" #include "base/strings/strcat.h"
#include "chrome/browser/chromeos/policy/messaging_layer/util/status.pb.h"
namespace reporting { namespace reporting {
namespace error { namespace error {
...@@ -96,6 +97,24 @@ std::string Status::ToString() const { ...@@ -96,6 +97,24 @@ std::string Status::ToString() const {
return output; return output;
} }
void Status::SaveTo(StatusProto* status_proto) const {
status_proto->set_code(error_code_);
if (error_code_ != error::OK) {
status_proto->set_error_message(error_message_);
} else {
status_proto->clear_error_message();
}
}
void Status::RestoreFrom(const StatusProto& status_proto) {
error_code_ = static_cast<error::Code>(status_proto.code());
if (error_code_ != error::OK) {
error_message_ = status_proto.error_message();
} else {
error_message_.clear();
}
}
std::ostream& operator<<(std::ostream& os, const Status& x) { std::ostream& operator<<(std::ostream& os, const Status& x) {
os << x.ToString(); os << x.ToString();
return os; return os;
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/strings/string_piece.h" #include "base/strings/string_piece.h"
#include "chrome/browser/chromeos/policy/messaging_layer/util/status.pb.h"
namespace reporting { namespace reporting {
namespace error { namespace error {
...@@ -67,6 +68,13 @@ class WARN_UNUSED_RESULT Status { ...@@ -67,6 +68,13 @@ class WARN_UNUSED_RESULT Status {
// Return a combination of the error code name and message. // Return a combination of the error code name and message.
std::string ToString() const; std::string ToString() const;
// Exports the contents of this object into |status_proto|. This method sets
// all fields in |status_proto| (for OK status clears |error_message|).
void SaveTo(StatusProto* status_proto) const;
// Populates this object using the contents of the given |status_proto|.
void RestoreFrom(const StatusProto& status_proto);
private: private:
error::Code error_code_; error::Code error_code_;
std::string error_message_; std::string error_message_;
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
syntax = "proto2";
package reporting;
option optimize_for = LITE_RUNTIME;
// Wire-format representation for a Status object.
message StatusProto {
// Numeric error code.
optional int32 code = 1;
// Detailed error message explaining the error.
optional string error_message = 2;
}
\ No newline at end of file
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <stdio.h> #include <stdio.h>
#include "chrome/browser/chromeos/policy/messaging_layer/util/status.pb.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace reporting { namespace reporting {
...@@ -104,5 +105,49 @@ TEST(Status, EqualsDifferentMessage) { ...@@ -104,5 +105,49 @@ TEST(Status, EqualsDifferentMessage) {
const Status b = Status(error::CANCELLED, "another"); const Status b = Status(error::CANCELLED, "another");
EXPECT_NE(a, b); EXPECT_NE(a, b);
} }
TEST(Status, SaveOkTo) {
StatusProto status_proto;
Status::StatusOK().SaveTo(&status_proto);
EXPECT_EQ(status_proto.code(), error::OK);
EXPECT_TRUE(status_proto.error_message().empty())
<< status_proto.error_message();
}
TEST(Status, SaveTo) {
Status status(error::INVALID_ARGUMENT, "argument error");
StatusProto status_proto;
status.SaveTo(&status_proto);
EXPECT_EQ(status_proto.code(), status.error_code());
EXPECT_EQ(status_proto.error_message(), status.error_message());
}
TEST(Status, RestoreFromOk) {
StatusProto status_proto;
status_proto.set_code(error::OK);
status_proto.set_error_message("invalid error");
Status status;
status.RestoreFrom(status_proto);
EXPECT_EQ(status.error_code(), status_proto.code());
// Error messages are ignored for OK status objects.
EXPECT_TRUE(status.error_message().empty()) << status.error_message();
EXPECT_TRUE(status.ok());
}
TEST(Status, RestoreFromNonOk) {
StatusProto status_proto;
status_proto.set_code(error::INVALID_ARGUMENT);
status_proto.set_error_message("argument error");
Status status;
status.RestoreFrom(status_proto);
EXPECT_EQ(status.error_code(), status_proto.code());
EXPECT_EQ(status.error_message(), status_proto.error_message());
}
} // namespace } // namespace
} // namespace reporting } // namespace reporting
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment