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
|
// Copyright 2018 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/metrics/testing/metrics_reporting_pref_helper.h"
#include "base/files/file_path.h"
#include "base/json/json_file_value_serializer.h"
#include "base/path_service.h"
#include "build/build_config.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/pref_names.h"
#include "components/metrics/metrics_pref_names.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chromeos/ash/components/settings/device_settings_cache.h"
#include "components/policy/proto/chrome_device_policy.pb.h"
#include "components/policy/proto/device_management_backend.pb.h"
#endif
#if BUILDFLAG(IS_CHROMEOS)
namespace {
void SetMetricsReportingEnabledChromeOS(bool is_enabled,
base::Value::Dict& local_state_dict) {
namespace em = enterprise_management;
em::ChromeDeviceSettingsProto device_settings_proto;
device_settings_proto.mutable_metrics_enabled()->set_metrics_enabled(
is_enabled);
em::PolicyData policy_data;
policy_data.set_policy_type("google/chromeos/device");
policy_data.set_policy_value(device_settings_proto.SerializeAsString());
local_state_dict.Set(
ash::device_settings_cache::prefs::kDeviceSettingsCache,
ash::device_settings_cache::PolicyDataToString(policy_data));
}
} // namespace
#endif
namespace metrics {
base::FilePath SetUpUserDataDirectoryForTesting(bool is_enabled) {
base::Value::Dict local_state_dict;
local_state_dict.SetByDottedPath(metrics::prefs::kMetricsReportingEnabled,
is_enabled);
base::FilePath user_data_dir;
if (!base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir))
return base::FilePath();
#if BUILDFLAG(IS_CHROMEOS)
// ChromeOS checks a separate place for reporting enabled.
SetMetricsReportingEnabledChromeOS(is_enabled, local_state_dict);
#endif
base::FilePath local_state_path =
user_data_dir.Append(chrome::kLocalStateFilename);
if (!JSONFileValueSerializer(local_state_path).Serialize(local_state_dict))
return base::FilePath();
return local_state_path;
}
} // namespace metrics
|