forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add converter for DeviceOffHoursProto
Add converter from DeviceOffHoursProto to DictionaryValue which is used in device settings provider and device policy decoder Bug: 739713 Change-Id: I3ddd2a605a19a0d357ccc145636e2c90edf08a68 Reviewed-on: https://chromium-review.googlesource.com/623308 Commit-Queue: Daria Iakovleva <yakovleva@google.com> Reviewed-by: Xiyuan Xia <xiyuan@chromium.org> Reviewed-by: Maksim Ivanov <emaxx@chromium.org> Reviewed-by: Sergey Poromov <poromov@chromium.org> Cr-Commit-Position: refs/heads/master@{#497359}
- Loading branch information
Daria Yakovleva
authored and
Commit Bot
committed
Aug 25, 2017
1 parent
37d4f63
commit 9faf87c
Showing
7 changed files
with
196 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
chrome/browser/chromeos/policy/device_off_hours_controller.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Copyright 2017 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. | ||
|
||
#include "chrome/browser/chromeos/policy/device_off_hours_controller.h" | ||
|
||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "base/logging.h" | ||
#include "base/memory/ptr_util.h" | ||
#include "base/time/time.h" | ||
|
||
namespace em = enterprise_management; | ||
|
||
namespace policy { | ||
namespace { | ||
|
||
// WeeklyTime class contains weekday and time in milliseconds. | ||
struct WeeklyTime { | ||
WeeklyTime(int weekday, int milliseconds) | ||
: weekday(weekday), milliseconds(milliseconds) {} | ||
|
||
std::unique_ptr<base::DictionaryValue> ToValue() const { | ||
auto weekly_time = base::MakeUnique<base::DictionaryValue>(); | ||
weekly_time->SetInteger("weekday", weekday); | ||
weekly_time->SetInteger("time", milliseconds); | ||
return weekly_time; | ||
} | ||
|
||
// Number of weekday (1 = Monday, 2 = Tuesday, etc.) | ||
int weekday; | ||
// Time of day in milliseconds from the beginning of the day. | ||
int milliseconds; | ||
}; | ||
|
||
// Time interval struct, interval = [start, end] | ||
struct Interval { | ||
Interval(const WeeklyTime& start, const WeeklyTime& end) | ||
: start(start), end(end) {} | ||
|
||
std::unique_ptr<base::DictionaryValue> ToValue() const { | ||
auto interval = base::MakeUnique<base::DictionaryValue>(); | ||
interval->SetDictionary("start", start.ToValue()); | ||
interval->SetDictionary("end", end.ToValue()); | ||
return interval; | ||
} | ||
|
||
WeeklyTime start; | ||
WeeklyTime end; | ||
}; | ||
|
||
// Get and return WeeklyTime structure from WeeklyTimeProto | ||
// Return nullptr if WeeklyTime structure isn't correct | ||
std::unique_ptr<WeeklyTime> GetWeeklyTime( | ||
const em::WeeklyTimeProto& container) { | ||
if (!container.has_weekday() || | ||
container.weekday() == em::WeeklyTimeProto::DAY_OF_WEEK_UNSPECIFIED) { | ||
LOG(ERROR) << "Day of week in interval is absent or unspecified."; | ||
return nullptr; | ||
} | ||
if (!container.has_time()) { | ||
LOG(ERROR) << "Time in interval is absent."; | ||
return nullptr; | ||
} | ||
const int kMillisecondsInDay = base::TimeDelta::FromDays(1).InMilliseconds(); | ||
int time_of_day = container.time(); | ||
if (!(time_of_day >= 0 && time_of_day < kMillisecondsInDay)) { | ||
LOG(ERROR) << "Invalid time value: " << time_of_day | ||
<< ", the value should be in [0; " << kMillisecondsInDay << ")."; | ||
return nullptr; | ||
} | ||
return base::MakeUnique<WeeklyTime>(container.weekday(), time_of_day); | ||
} | ||
|
||
// Get and return list of time intervals from DeviceOffHoursProto structure | ||
std::vector<Interval> GetIntervals(const em::DeviceOffHoursProto& container) { | ||
std::vector<Interval> intervals; | ||
for (const auto& entry : container.interval()) { | ||
if (!entry.has_start() || !entry.has_end()) { | ||
LOG(WARNING) << "Skipping interval without start or/and end."; | ||
continue; | ||
} | ||
auto start = GetWeeklyTime(entry.start()); | ||
auto end = GetWeeklyTime(entry.end()); | ||
if (start && end) { | ||
intervals.push_back(Interval(*start, *end)); | ||
} | ||
} | ||
return intervals; | ||
} | ||
|
||
std::vector<std::string> GetIgnoredPolicies( | ||
const em::DeviceOffHoursProto& container) { | ||
std::vector<std::string> ignored_policies; | ||
return std::vector<std::string>(container.ignored_policy().begin(), | ||
container.ignored_policy().end()); | ||
} | ||
|
||
} // namespace | ||
|
||
namespace off_hours { | ||
|
||
std::unique_ptr<base::DictionaryValue> ConvertPolicyProtoToValue( | ||
const em::DeviceOffHoursProto& container) { | ||
if (!container.has_timezone()) | ||
return nullptr; | ||
auto off_hours = base::MakeUnique<base::DictionaryValue>(); | ||
off_hours->SetString("timezone", container.timezone()); | ||
std::vector<Interval> intervals = GetIntervals(container); | ||
auto intervals_value = base::MakeUnique<base::ListValue>(); | ||
for (const auto& interval : intervals) { | ||
intervals_value->Append(interval.ToValue()); | ||
} | ||
off_hours->SetList("intervals", std::move(intervals_value)); | ||
std::vector<std::string> ignored_policies = GetIgnoredPolicies(container); | ||
auto ignored_policies_value = base::MakeUnique<base::ListValue>(); | ||
for (const auto& policy : ignored_policies) { | ||
ignored_policies_value->AppendString(policy); | ||
} | ||
off_hours->SetList("ignored_policies", std::move(ignored_policies_value)); | ||
return off_hours; | ||
} | ||
|
||
} // namespace off_hours | ||
} // namespace policy |
33 changes: 33 additions & 0 deletions
33
chrome/browser/chromeos/policy/device_off_hours_controller.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2017 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. | ||
|
||
#ifndef CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_OFF_HOURS_CONTROLLER_H_ | ||
#define CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_OFF_HOURS_CONTROLLER_H_ | ||
|
||
#include <memory> | ||
|
||
#include "base/values.h" | ||
#include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h" | ||
|
||
namespace policy { | ||
namespace off_hours { | ||
|
||
// Return DictionaryValue in format: | ||
// { "timezone" : string, | ||
// "intervals" : list of Intervals, | ||
// "ignored_policies" : string list } | ||
// Interval dictionary format: | ||
// { "start" : WeeklyTime, | ||
// "end" : WeeklyTime } | ||
// WeeklyTime dictionary format: | ||
// { "weekday" : int # value is from 1 to 7 (1 = Monday, 2 = Tuesday, etc.) | ||
// "time" : int # in milliseconds from the beginning of the day. | ||
// } | ||
std::unique_ptr<base::DictionaryValue> ConvertPolicyProtoToValue( | ||
const enterprise_management::DeviceOffHoursProto& container); | ||
|
||
} // namespace off_hours | ||
} // namespace policy | ||
|
||
#endif // CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_OFF_HOURS_CONTROLLER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters