summaryrefslogtreecommitdiff
path: root/chromium/components/enterprise/browser/reporting
diff options
context:
space:
mode:
Diffstat (limited to 'chromium/components/enterprise/browser/reporting')
-rw-r--r--chromium/components/enterprise/browser/reporting/policy_info.cc142
-rw-r--r--chromium/components/enterprise/browser/reporting/policy_info.h36
-rw-r--r--chromium/components/enterprise/browser/reporting/report_request_definition.h29
3 files changed, 207 insertions, 0 deletions
diff --git a/chromium/components/enterprise/browser/reporting/policy_info.cc b/chromium/components/enterprise/browser/reporting/policy_info.cc
new file mode 100644
index 00000000000..dd690664510
--- /dev/null
+++ b/chromium/components/enterprise/browser/reporting/policy_info.cc
@@ -0,0 +1,142 @@
+// Copyright 2019 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 "components/enterprise/browser/reporting/policy_info.h"
+
+#include <string>
+
+#include "base/json/json_writer.h"
+#include "build/build_config.h"
+#include "components/policy/core/browser/policy_conversions.h"
+#include "components/policy/core/common/cloud/cloud_policy_client.h"
+#include "components/policy/core/common/cloud/cloud_policy_constants.h"
+#include "components/policy/core/common/cloud/machine_level_user_cloud_policy_manager.h"
+#include "components/policy/core/common/policy_types.h"
+#include "components/strings/grit/components_strings.h"
+
+namespace em = enterprise_management;
+
+namespace enterprise_reporting {
+
+namespace {
+
+em::Policy_PolicyLevel GetLevel(const base::Value& policy) {
+ switch (static_cast<policy::PolicyLevel>(*policy.FindIntKey("level"))) {
+ case policy::POLICY_LEVEL_RECOMMENDED:
+ return em::Policy_PolicyLevel_LEVEL_RECOMMENDED;
+ case policy::POLICY_LEVEL_MANDATORY:
+ return em::Policy_PolicyLevel_LEVEL_MANDATORY;
+ }
+ NOTREACHED() << "Invalid policy level: " << *policy.FindIntKey("level");
+ return em::Policy_PolicyLevel_LEVEL_UNKNOWN;
+}
+
+em::Policy_PolicyScope GetScope(const base::Value& policy) {
+ switch (static_cast<policy::PolicyScope>(*policy.FindIntKey("scope"))) {
+ case policy::POLICY_SCOPE_USER:
+ return em::Policy_PolicyScope_SCOPE_USER;
+ case policy::POLICY_SCOPE_MACHINE:
+ return em::Policy_PolicyScope_SCOPE_MACHINE;
+ }
+ NOTREACHED() << "Invalid policy scope: " << *policy.FindIntKey("scope");
+ return em::Policy_PolicyScope_SCOPE_UNKNOWN;
+}
+
+em::Policy_PolicySource GetSource(const base::Value& policy) {
+ switch (static_cast<policy::PolicySource>(*policy.FindIntKey("source"))) {
+ case policy::POLICY_SOURCE_ENTERPRISE_DEFAULT:
+ return em::Policy_PolicySource_SOURCE_ENTERPRISE_DEFAULT;
+ case policy::POLICY_SOURCE_CLOUD:
+ return em::Policy_PolicySource_SOURCE_CLOUD;
+ case policy::POLICY_SOURCE_ACTIVE_DIRECTORY:
+ return em::Policy_PolicySource_SOURCE_ACTIVE_DIRECTORY;
+ case policy::POLICY_SOURCE_DEVICE_LOCAL_ACCOUNT_OVERRIDE:
+ return em::Policy_PolicySource_SOURCE_DEVICE_LOCAL_ACCOUNT_OVERRIDE;
+ case policy::POLICY_SOURCE_PLATFORM:
+ return em::Policy_PolicySource_SOURCE_PLATFORM;
+ case policy::POLICY_SOURCE_PRIORITY_CLOUD:
+ return em::Policy_PolicySource_SOURCE_PRIORITY_CLOUD;
+ case policy::POLICY_SOURCE_MERGED:
+ return em::Policy_PolicySource_SOURCE_MERGED;
+ case policy::POLICY_SOURCE_COUNT:
+ NOTREACHED();
+ return em::Policy_PolicySource_SOURCE_UNKNOWN;
+ }
+ NOTREACHED() << "Invalid policy source: " << *policy.FindIntKey("source");
+ return em::Policy_PolicySource_SOURCE_UNKNOWN;
+}
+
+void UpdatePolicyInfo(em::Policy* policy_info,
+ const std::string& policy_name,
+ const base::Value& policy) {
+ policy_info->set_name(policy_name);
+ policy_info->set_level(GetLevel(policy));
+ policy_info->set_scope(GetScope(policy));
+ policy_info->set_source(GetSource(policy));
+ base::JSONWriter::Write(*policy.FindKey("value"),
+ policy_info->mutable_value());
+ const std::string* error = policy.FindStringKey("error");
+ std::string deprecated_error;
+ std::string future_error;
+ // Because server side use keyword "deprecated" to determine policy
+ // deprecation error. Using l10n string actually causing issue.
+ if (policy.FindBoolKey("deprecated"))
+ deprecated_error = "This policy has been deprecated";
+
+ if (policy.FindBoolKey("future"))
+ future_error = "This policy hasn't been released";
+
+ if (error && !deprecated_error.empty())
+ policy_info->set_error(
+ base::JoinString({*error, deprecated_error, future_error}, "\n"));
+ else if (error)
+ policy_info->set_error(*error);
+ else if (!deprecated_error.empty())
+ policy_info->set_error(deprecated_error);
+}
+
+} // namespace
+
+void AppendChromePolicyInfoIntoProfileReport(
+ const base::Value& policies,
+ em::ChromeUserProfileInfo* profile_info) {
+ for (const auto& policy_iter :
+ policies.FindKey("chromePolicies")->DictItems()) {
+ UpdatePolicyInfo(profile_info->add_chrome_policies(), policy_iter.first,
+ policy_iter.second);
+ }
+}
+
+void AppendExtensionPolicyInfoIntoProfileReport(
+ const base::Value& policies,
+ em::ChromeUserProfileInfo* profile_info) {
+ for (const auto& extension_iter :
+ policies.FindKey("extensionPolicies")->DictItems()) {
+ const base::Value& policies = extension_iter.second;
+ if (policies.DictSize() == 0)
+ continue;
+ auto* extension = profile_info->add_extension_policies();
+ extension->set_extension_id(extension_iter.first);
+ for (const auto& policy_iter : policies.DictItems()) {
+ UpdatePolicyInfo(extension->add_policies(), policy_iter.first,
+ policy_iter.second);
+ }
+ }
+}
+
+void AppendMachineLevelUserCloudPolicyFetchTimestamp(
+ em::ChromeUserProfileInfo* profile_info,
+ policy::MachineLevelUserCloudPolicyManager* manager) {
+#if !defined(OS_CHROMEOS)
+ if (!manager || !manager->IsClientRegistered())
+ return;
+ auto* timestamp = profile_info->add_policy_fetched_timestamps();
+ timestamp->set_type(
+ policy::dm_protocol::kChromeMachineLevelExtensionCloudPolicyType);
+ timestamp->set_timestamp(
+ manager->core()->client()->last_policy_timestamp().ToJavaTime());
+#endif // !defined(OS_CHROMEOS)
+}
+
+} // namespace enterprise_reporting
diff --git a/chromium/components/enterprise/browser/reporting/policy_info.h b/chromium/components/enterprise/browser/reporting/policy_info.h
new file mode 100644
index 00000000000..82a5fa9ea7a
--- /dev/null
+++ b/chromium/components/enterprise/browser/reporting/policy_info.h
@@ -0,0 +1,36 @@
+// Copyright 2019 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 COMPONENTS_ENTERPRISE_BROWSER_REPORTING_POLICY_INFO_H_
+#define COMPONENTS_ENTERPRISE_BROWSER_REPORTING_POLICY_INFO_H_
+
+#include "components/policy/proto/device_management_backend.pb.h"
+
+namespace base {
+class Value;
+}
+
+namespace policy {
+class MachineLevelUserCloudPolicyManager;
+}
+
+// Unit tests are in chrome\browser\enterprise\reporting\policy_info_unittest.cc
+// TODO(crbug.com/1096499): Move the tests to this directory.
+namespace enterprise_reporting {
+
+void AppendChromePolicyInfoIntoProfileReport(
+ const base::Value& policies,
+ enterprise_management::ChromeUserProfileInfo* profile_info);
+
+void AppendExtensionPolicyInfoIntoProfileReport(
+ const base::Value& policies,
+ enterprise_management::ChromeUserProfileInfo* profile_info);
+
+void AppendMachineLevelUserCloudPolicyFetchTimestamp(
+ enterprise_management::ChromeUserProfileInfo* profile_info,
+ policy::MachineLevelUserCloudPolicyManager* manager);
+
+} // namespace enterprise_reporting
+
+#endif // COMPONENTS_ENTERPRISE_BROWSER_REPORTING_POLICY_INFO_H_
diff --git a/chromium/components/enterprise/browser/reporting/report_request_definition.h b/chromium/components/enterprise/browser/reporting/report_request_definition.h
new file mode 100644
index 00000000000..5fdcc22ca81
--- /dev/null
+++ b/chromium/components/enterprise/browser/reporting/report_request_definition.h
@@ -0,0 +1,29 @@
+// Copyright 2019 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 COMPONENTS_ENTERPRISE_BROWSER_REPORTING_REPORT_REQUEST_DEFINITION_H_
+#define COMPONENTS_ENTERPRISE_BROWSER_REPORTING_REPORT_REQUEST_DEFINITION_H_
+
+#include "build/build_config.h"
+#include "components/policy/proto/device_management_backend.pb.h"
+
+namespace enterprise_reporting {
+
+namespace definition {
+
+// Both ChromeOsUserReportRequest and ChromeDesktopReportRequest are used to
+// upload usage data to DM Server. By the reference to this macro, most classes
+// in enterprise_reporting namespace can share the same logic for various
+// operation systems.
+#if defined(OS_CHROMEOS)
+using ReportRequest = enterprise_management::ChromeOsUserReportRequest;
+#else
+using ReportRequest = enterprise_management::ChromeDesktopReportRequest;
+#endif
+
+} // namespace definition
+
+} // namespace enterprise_reporting
+
+#endif // COMPONENTS_ENTERPRISE_BROWSER_REPORTING_REPORT_REQUEST_DEFINITION_H_