1// Copyright 2017 The Chromium OS 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
5#include "policy/policy_util.h"
6
7#include <base/files/file_util.h>
8#include <base/logging.h>
9
10namespace policy {
11
12LoadPolicyResult LoadPolicyFromPath(
13    const base::FilePath& policy_path,
14    std::string* policy_data_str_out,
15    enterprise_management::PolicyFetchResponse* policy_out) {
16  DCHECK(policy_data_str_out);
17  DCHECK(policy_out);
18  if (!base::PathExists(policy_path)) {
19    return LoadPolicyResult::kFileNotFound;
20  }
21
22  if (!base::ReadFileToString(policy_path, policy_data_str_out)) {
23    PLOG(ERROR) << "Could not read policy off disk at " << policy_path.value();
24    return LoadPolicyResult::kFailedToReadFile;
25  }
26
27  if (policy_data_str_out->empty()) {
28    LOG(ERROR) << "Empty policy file at " << policy_path.value();
29    return LoadPolicyResult::kEmptyFile;
30  }
31
32  if (!policy_out->ParseFromString(*policy_data_str_out)) {
33    LOG(ERROR) << "Policy on disk could not be parsed, file: "
34               << policy_path.value();
35    return LoadPolicyResult::kInvalidPolicyData;
36  }
37
38  return LoadPolicyResult::kSuccess;
39}
40
41}  // namespace policy
42