File: off_hours_proto_parser.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (78 lines) | stat: -rw-r--r-- 2,732 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/policy/off_hours/off_hours_proto_parser.h"

#include "base/time/default_clock.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chromeos/ash/components/policy/weekly_time/time_utils.h"

namespace em = enterprise_management;

namespace policy {
namespace off_hours {

std::vector<WeeklyTimeInterval> ExtractWeeklyTimeIntervalsFromProto(
    const em::DeviceOffHoursProto& container,
    const std::string& timezone,
    base::Clock* clock) {
  int offset;
  if (!weekly_time_utils::GetOffsetFromTimezoneToGmt(timezone, clock,
                                                     &offset)) {
    return {};
  }
  std::vector<WeeklyTimeInterval> intervals;
  for (const auto& entry : container.intervals()) {
    // The offset is to convert from |timezone| to GMT. Negate it to get the
    // offset from GMT to |timezone|.
    auto interval = WeeklyTimeInterval::ExtractFromProto(entry, -offset);
    if (interval) {
      intervals.push_back(*interval);
    }
  }
  return intervals;
}

std::vector<int> ExtractIgnoredPolicyProtoTagsFromProto(
    const em::DeviceOffHoursProto& container) {
  return std::vector<int>(container.ignored_policy_proto_tags().begin(),
                          container.ignored_policy_proto_tags().end());
}

std::optional<std::string> ExtractTimezoneFromProto(
    const em::DeviceOffHoursProto& container) {
  if (!container.has_timezone()) {
    return std::nullopt;
  }
  return std::make_optional(container.timezone());
}

std::optional<base::Value::Dict> ConvertOffHoursProtoToValue(
    const em::DeviceOffHoursProto& container) {
  std::optional<std::string> timezone = ExtractTimezoneFromProto(container);
  if (!timezone) {
    return std::nullopt;
  }
  auto off_hours = base::Value::Dict().Set("timezone", *timezone);
  std::vector<WeeklyTimeInterval> intervals =
      ExtractWeeklyTimeIntervalsFromProto(container, *timezone,
                                          base::DefaultClock::GetInstance());
  base::Value::List intervals_value;
  for (const auto& interval : intervals) {
    intervals_value.Append(interval.ToValue());
  }
  off_hours.Set("intervals", std::move(intervals_value));
  std::vector<int> ignored_policy_proto_tags =
      ExtractIgnoredPolicyProtoTagsFromProto(container);
  base::Value::List ignored_policies_value;
  for (const auto& policy : ignored_policy_proto_tags) {
    ignored_policies_value.Append(policy);
  }
  off_hours.Set("ignored_policy_proto_tags", std::move(ignored_policies_value));
  return off_hours;
}

}  // namespace off_hours
}  // namespace policy