report_queue_configuration.cc 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2020 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. #include "components/reporting/client/report_queue_configuration.h"
  5. #include <string>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/memory/ptr_util.h"
  9. #include "components/reporting/proto/synced/record_constants.pb.h"
  10. #include "components/reporting/util/status.h"
  11. #include "components/reporting/util/status_macros.h"
  12. #include "components/reporting/util/statusor.h"
  13. namespace reporting {
  14. ReportQueueConfiguration::ReportQueueConfiguration() = default;
  15. ReportQueueConfiguration::~ReportQueueConfiguration() = default;
  16. StatusOr<std::unique_ptr<ReportQueueConfiguration>>
  17. ReportQueueConfiguration::Create(EventType event_type,
  18. Destination destination,
  19. PolicyCheckCallback policy_check_callback) {
  20. auto config = base::WrapUnique<ReportQueueConfiguration>(
  21. new ReportQueueConfiguration());
  22. RETURN_IF_ERROR(config->SetEventType(event_type));
  23. RETURN_IF_ERROR(config->SetDestination(destination));
  24. RETURN_IF_ERROR(config->SetPolicyCheckCallback(policy_check_callback));
  25. return config;
  26. }
  27. StatusOr<std::unique_ptr<ReportQueueConfiguration>>
  28. ReportQueueConfiguration::Create(base::StringPiece dm_token,
  29. Destination destination,
  30. PolicyCheckCallback policy_check_callback) {
  31. auto config_result = Create(/*event_type=*/EventType::kDevice, destination,
  32. policy_check_callback);
  33. if (!config_result.ok()) {
  34. return config_result;
  35. }
  36. std::unique_ptr<ReportQueueConfiguration> config =
  37. std::move(config_result.ValueOrDie());
  38. RETURN_IF_ERROR(config->SetDMToken(dm_token));
  39. return std::move(config);
  40. }
  41. Status ReportQueueConfiguration::SetPolicyCheckCallback(
  42. PolicyCheckCallback policy_check_callback) {
  43. if (policy_check_callback.is_null()) {
  44. return (Status(error::INVALID_ARGUMENT,
  45. "PolicyCheckCallback must not be null"));
  46. }
  47. policy_check_callback_ = std::move(policy_check_callback);
  48. return Status::StatusOK();
  49. }
  50. Status ReportQueueConfiguration::SetEventType(EventType event_type) {
  51. event_type_ = event_type;
  52. return Status::StatusOK();
  53. }
  54. Status ReportQueueConfiguration::CheckPolicy() const {
  55. return policy_check_callback_.Run();
  56. }
  57. Status ReportQueueConfiguration::SetDMToken(base::StringPiece dm_token) {
  58. dm_token_ = std::string(dm_token);
  59. return Status::StatusOK();
  60. }
  61. Status ReportQueueConfiguration::SetDestination(Destination destination) {
  62. if (destination == Destination::UNDEFINED_DESTINATION) {
  63. return Status(error::INVALID_ARGUMENT, "Destination must be defined");
  64. }
  65. destination_ = destination;
  66. return Status::StatusOK();
  67. }
  68. } // namespace reporting