Commit e104cf24 authored by Anqing Zhao's avatar Anqing Zhao Committed by Commit Bot

Report user data from Chrome OS

Currently, CBCM reporting are supported on many operation systems (like
Linux, Mac, Windows), but not on Chrome OS. In this change list, a
report scheduler will be triggered to report related information
periodically.

Bug:1010213

Change-Id: I18ecce77852b16e21456d666c5c301c05e5f0d61
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1862447
Commit-Queue: Anqing Zhao <anqing@google.com>
Reviewed-by: default avatarSergey Poromov <poromov@chromium.org>
Reviewed-by: default avatarOwen Min <zmin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#721371}
parent 4d87480d
...@@ -28,6 +28,9 @@ ...@@ -28,6 +28,9 @@
#include "chrome/browser/chromeos/policy/policy_oauth2_token_fetcher.h" #include "chrome/browser/chromeos/policy/policy_oauth2_token_fetcher.h"
#include "chrome/browser/chromeos/policy/remote_commands/user_commands_factory_chromeos.h" #include "chrome/browser/chromeos/policy/remote_commands/user_commands_factory_chromeos.h"
#include "chrome/browser/chromeos/policy/wildcard_login_checker.h" #include "chrome/browser/chromeos/policy/wildcard_login_checker.h"
#include "chrome/browser/enterprise_reporting/report_generator.h"
#include "chrome/browser/enterprise_reporting/report_scheduler.h"
#include "chrome/browser/enterprise_reporting/request_timer.h"
#include "chrome/browser/invalidation/deprecated_profile_invalidation_provider_factory.h" #include "chrome/browser/invalidation/deprecated_profile_invalidation_provider_factory.h"
#include "chrome/browser/invalidation/profile_invalidation_provider_factory.h" #include "chrome/browser/invalidation/profile_invalidation_provider_factory.h"
#include "chrome/browser/lifetime/application_lifetime.h" #include "chrome/browser/lifetime/application_lifetime.h"
...@@ -350,6 +353,7 @@ UserCloudPolicyManagerChromeOS::GetAppInstallEventLogUploader() { ...@@ -350,6 +353,7 @@ UserCloudPolicyManagerChromeOS::GetAppInstallEventLogUploader() {
void UserCloudPolicyManagerChromeOS::Shutdown() { void UserCloudPolicyManagerChromeOS::Shutdown() {
observed_profile_manager_.RemoveAll(); observed_profile_manager_.RemoveAll();
app_install_event_log_uploader_.reset(); app_install_event_log_uploader_.reset();
report_scheduler_.reset();
if (client()) if (client())
client()->RemoveObserver(this); client()->RemoveObserver(this);
if (service()) if (service())
...@@ -408,6 +412,9 @@ void UserCloudPolicyManagerChromeOS:: ...@@ -408,6 +412,9 @@ void UserCloudPolicyManagerChromeOS::
// available. If refresh scheduler is already started this call will do // available. If refresh scheduler is already started this call will do
// nothing. // nothing.
StartRefreshSchedulerIfReady(); StartRefreshSchedulerIfReady();
// Start the report scheduler to periodically upload usage data to DM server.
StartReportSchedulerIfReady();
} }
void UserCloudPolicyManagerChromeOS::OnPolicyFetched( void UserCloudPolicyManagerChromeOS::OnPolicyFetched(
...@@ -668,6 +675,9 @@ void UserCloudPolicyManagerChromeOS::OnInitialPolicyFetchComplete( ...@@ -668,6 +675,9 @@ void UserCloudPolicyManagerChromeOS::OnInitialPolicyFetchComplete(
UMA_HISTOGRAM_MEDIUM_TIMES(kUMAInitialFetchDelayTotal, UMA_HISTOGRAM_MEDIUM_TIMES(kUMAInitialFetchDelayTotal,
now - time_init_started_); now - time_init_started_);
CancelWaitForPolicyFetch(success); CancelWaitForPolicyFetch(success);
if (success)
StartReportSchedulerIfReady();
} }
void UserCloudPolicyManagerChromeOS::OnPolicyRefreshTimeout() { void UserCloudPolicyManagerChromeOS::OnPolicyRefreshTimeout() {
...@@ -731,6 +741,20 @@ void UserCloudPolicyManagerChromeOS::StartRefreshSchedulerIfReady() { ...@@ -731,6 +741,20 @@ void UserCloudPolicyManagerChromeOS::StartRefreshSchedulerIfReady() {
policy_prefs::kUserPolicyRefreshRate); policy_prefs::kUserPolicyRefreshRate);
} }
void UserCloudPolicyManagerChromeOS::StartReportSchedulerIfReady() {
if (!base::FeatureList::IsEnabled(features::kEnterpriseReportingInChromeOS))
return;
if (!client() || !client()->is_registered())
return;
report_scheduler_ = std::make_unique<enterprise_reporting::ReportScheduler>(
client(), std::make_unique<enterprise_reporting::RequestTimer>(),
std::make_unique<enterprise_reporting::ReportGenerator>());
report_scheduler_->OnDMTokenUpdated();
}
void UserCloudPolicyManagerChromeOS::OnProfileAdded(Profile* profile) { void UserCloudPolicyManagerChromeOS::OnProfileAdded(Profile* profile) {
if (profile != profile_) if (profile != profile_)
return; return;
...@@ -784,4 +808,9 @@ void UserCloudPolicyManagerChromeOS::SetUserContextRefreshTokenForTests( ...@@ -784,4 +808,9 @@ void UserCloudPolicyManagerChromeOS::SetUserContextRefreshTokenForTests(
user_context_refresh_token_for_tests_ = base::make_optional(refresh_token); user_context_refresh_token_for_tests_ = base::make_optional(refresh_token);
} }
enterprise_reporting::ReportScheduler*
UserCloudPolicyManagerChromeOS::GetReportSchedulerForTesting() {
return report_scheduler_.get();
}
} // namespace policy } // namespace policy
...@@ -40,6 +40,10 @@ namespace network { ...@@ -40,6 +40,10 @@ namespace network {
class SharedURLLoaderFactory; class SharedURLLoaderFactory;
} }
namespace enterprise_reporting {
class ReportScheduler;
}
namespace policy { namespace policy {
class AppInstallEventLogUploader; class AppInstallEventLogUploader;
...@@ -176,6 +180,9 @@ class UserCloudPolicyManagerChromeOS : public CloudPolicyManager, ...@@ -176,6 +180,9 @@ class UserCloudPolicyManagerChromeOS : public CloudPolicyManager,
// token when fetching the policy OAuth token. // token when fetching the policy OAuth token.
void SetUserContextRefreshTokenForTests(const std::string& refresh_token); void SetUserContextRefreshTokenForTests(const std::string& refresh_token);
// Return the ReportScheduler used to report usage data to the server.
enterprise_reporting::ReportScheduler* GetReportSchedulerForTesting();
protected: protected:
// CloudPolicyManager: // CloudPolicyManager:
void GetChromePolicy(PolicyMap* policy_map) override; void GetChromePolicy(PolicyMap* policy_map) override;
...@@ -223,6 +230,10 @@ class UserCloudPolicyManagerChromeOS : public CloudPolicyManager, ...@@ -223,6 +230,10 @@ class UserCloudPolicyManagerChromeOS : public CloudPolicyManager,
// call it multiple times. // call it multiple times.
void StartRefreshSchedulerIfReady(); void StartRefreshSchedulerIfReady();
// Starts report scheduler if all the required conditions are fulfilled.
// Exits immediately if corresponding feature flag is closed.
void StartReportSchedulerIfReady();
// ProfileManagerObserver: // ProfileManagerObserver:
void OnProfileAdded(Profile* profile) override; void OnProfileAdded(Profile* profile) override;
...@@ -241,6 +252,9 @@ class UserCloudPolicyManagerChromeOS : public CloudPolicyManager, ...@@ -241,6 +252,9 @@ class UserCloudPolicyManagerChromeOS : public CloudPolicyManager,
// Helper used to send app push-install event logs to the policy server. // Helper used to send app push-install event logs to the policy server.
std::unique_ptr<AppInstallEventLogUploader> app_install_event_log_uploader_; std::unique_ptr<AppInstallEventLogUploader> app_install_event_log_uploader_;
// Scheduler used to report usage data to DM server periodically.
std::unique_ptr<enterprise_reporting::ReportScheduler> report_scheduler_;
// Username for the wildcard login check if applicable, empty otherwise. // Username for the wildcard login check if applicable, empty otherwise.
std::string wildcard_username_; std::string wildcard_username_;
......
...@@ -27,12 +27,15 @@ ...@@ -27,12 +27,15 @@
#include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
#include "chrome/browser/chromeos/policy/user_cloud_policy_token_forwarder.h" #include "chrome/browser/chromeos/policy/user_cloud_policy_token_forwarder.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h" #include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/enterprise_reporting/report_scheduler.h"
#include "chrome/browser/enterprise_reporting/request_timer.h"
#include "chrome/browser/policy/cloud/cloud_policy_test_utils.h" #include "chrome/browser/policy/cloud/cloud_policy_test_utils.h"
#include "chrome/browser/prefs/browser_prefs.h" #include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/browser/signin/identity_manager_factory.h" #include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/signin/identity_test_environment_profile_adaptor.h" #include "chrome/browser/signin/identity_test_environment_profile_adaptor.h"
#include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_features.h" #include "chrome/common/chrome_features.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_browser_process.h" #include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h" #include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h" #include "chrome/test/base/testing_profile_manager.h"
...@@ -100,6 +103,7 @@ constexpr char kOAuth2AccessTokenData[] = R"( ...@@ -100,6 +103,7 @@ constexpr char kOAuth2AccessTokenData[] = R"(
})"; })";
constexpr char kOAuthToken[] = "5678"; constexpr char kOAuthToken[] = "5678";
constexpr char kDMToken[] = "dmtoken123"; constexpr char kDMToken[] = "dmtoken123";
constexpr char kDeviceId[] = "id987";
// UserCloudPolicyManagerChromeOS test class that can be used with different // UserCloudPolicyManagerChromeOS test class that can be used with different
// feature flags. // feature flags.
...@@ -118,8 +122,6 @@ class UserCloudPolicyManagerChromeOSTest ...@@ -118,8 +122,6 @@ class UserCloudPolicyManagerChromeOSTest
PolicyEnforcement::kPolicyRequired); PolicyEnforcement::kPolicyRequired);
// The manager should already be initialized by this point. // The manager should already be initialized by this point.
EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
InitAndConnectManager();
EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete());
} }
protected: protected:
...@@ -190,6 +192,7 @@ class UserCloudPolicyManagerChromeOSTest ...@@ -190,6 +192,7 @@ class UserCloudPolicyManagerChromeOSTest
ASSERT_TRUE( ASSERT_TRUE(
policy_proto.SerializeToString(policy_data_.mutable_policy_value())); policy_proto.SerializeToString(policy_data_.mutable_policy_value()));
policy_data_.set_policy_type(dm_protocol::kChromeUserPolicyType); policy_data_.set_policy_type(dm_protocol::kChromeUserPolicyType);
policy_data_.set_device_id(kDeviceId);
policy_data_.set_request_token(kDMToken); policy_data_.set_request_token(kDMToken);
policy_data_.set_device_id("id987"); policy_data_.set_device_id("id987");
policy_data_.set_username("user@example.com"); policy_data_.set_username("user@example.com");
...@@ -431,6 +434,10 @@ class UserCloudPolicyManagerChromeOSTest ...@@ -431,6 +434,10 @@ class UserCloudPolicyManagerChromeOSTest
return identity_test_env_profile_adaptor_->identity_test_env(); return identity_test_env_profile_adaptor_->identity_test_env();
} }
base::test::ScopedFeatureList* scoped_feature_list() {
return &scoped_feature_list_;
}
private: private:
// Invoked when a fatal error is encountered. // Invoked when a fatal error is encountered.
void OnFatalErrorEncountered() { fatal_error_encountered_ = true; } void OnFatalErrorEncountered() { fatal_error_encountered_ = true; }
...@@ -804,6 +811,8 @@ TEST_P(UserCloudPolicyManagerChromeOSTest, SynchronousLoadWithPreloadedStore) { ...@@ -804,6 +811,8 @@ TEST_P(UserCloudPolicyManagerChromeOSTest, SynchronousLoadWithPreloadedStore) {
// initializing a profile during a crash restart). The manager gets // initializing a profile during a crash restart). The manager gets
// initialized straight away after the construction. // initialized straight away after the construction.
MakeManagerWithPreloadedStore(base::TimeDelta()); MakeManagerWithPreloadedStore(base::TimeDelta());
InitAndConnectManager();
EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete());
EXPECT_TRUE(manager_->policies().Equals(expected_bundle_)); EXPECT_TRUE(manager_->policies().Equals(expected_bundle_));
} }
...@@ -850,6 +859,53 @@ TEST_P(UserCloudPolicyManagerChromeOSTest, TestHasAppInstallEventLogUploader) { ...@@ -850,6 +859,53 @@ TEST_P(UserCloudPolicyManagerChromeOSTest, TestHasAppInstallEventLogUploader) {
EXPECT_TRUE(manager_->GetAppInstallEventLogUploader()); EXPECT_TRUE(manager_->GetAppInstallEventLogUploader());
} }
TEST_P(UserCloudPolicyManagerChromeOSTest, TestHasReportScheduler) {
// Open policy and feature flag to enable report scheduler.
g_browser_process->local_state()->SetBoolean(prefs::kCloudReportingEnabled,
true);
scoped_feature_list()->Reset();
scoped_feature_list()->InitAndEnableFeature(
features::kEnterpriseReportingInChromeOS);
// Before UserCloudPolicyManagerChromeOS is initialized, report scheduler is
// not existing.
MakeManagerWithPreloadedStore(base::TimeDelta());
EXPECT_FALSE(manager_->core()->service());
EXPECT_FALSE(manager_->core()->client());
EXPECT_FALSE(manager_->GetReportSchedulerForTesting());
// After UserCloudPolicyManagerChromeOS is initialized, report scheduler is
// created with valid |client_id| and |dm token|.
InitAndConnectManager();
EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete());
EXPECT_TRUE(manager_->core()->client()->is_registered());
EXPECT_EQ(kDeviceId, manager_->core()->client()->client_id());
EXPECT_EQ(kDMToken, manager_->core()->client()->dm_token());
EXPECT_TRUE(manager_->GetReportSchedulerForTesting());
// Make sure the |report_scheduler| submit the request to DM Server.
enterprise_reporting::RequestTimer* request_timer =
manager_->GetReportSchedulerForTesting()->GetRequestTimerForTesting();
EXPECT_TRUE(request_timer->IsFirstTimerRunning());
EXPECT_FALSE(request_timer->IsRepeatTimerRunning());
}
TEST_P(UserCloudPolicyManagerChromeOSTest,
EnterpriseReportingInChromeOSDisabled) {
// Open policy but close the feature flag for Chrome OS to disable report
// scheduler.
g_browser_process->local_state()->SetBoolean(prefs::kCloudReportingEnabled,
true);
scoped_feature_list()->Reset();
scoped_feature_list()->InitAndDisableFeature(
features::kEnterpriseReportingInChromeOS);
// Report scheduler won't be created.
MakeManagerWithPreloadedStore(base::TimeDelta());
InitAndConnectManager();
EXPECT_FALSE(manager_->GetReportSchedulerForTesting());
}
TEST_P(UserCloudPolicyManagerChromeOSTest, Reregistration) { TEST_P(UserCloudPolicyManagerChromeOSTest, Reregistration) {
// Tests the initialization of a manager whose Profile is waiting for the // Tests the initialization of a manager whose Profile is waiting for the
// initial fetch, when the policy cache is empty. // initial fetch, when the policy cache is empty.
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "base/syslog_logging.h" #include "base/syslog_logging.h"
#include "base/task/post_task.h" #include "base/task/post_task.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/enterprise_reporting/prefs.h" #include "chrome/browser/enterprise_reporting/prefs.h"
#include "chrome/browser/enterprise_reporting/request_timer.h" #include "chrome/browser/enterprise_reporting/request_timer.h"
...@@ -64,6 +65,10 @@ void ReportScheduler::SetReportUploaderForTesting( ...@@ -64,6 +65,10 @@ void ReportScheduler::SetReportUploaderForTesting(
report_uploader_ = std::move(uploader); report_uploader_ = std::move(uploader);
} }
RequestTimer* ReportScheduler::GetRequestTimerForTesting() const {
return request_timer_.get();
}
void ReportScheduler::OnDMTokenUpdated() { void ReportScheduler::OnDMTokenUpdated() {
OnReportEnabledPrefChanged(); OnReportEnabledPrefChanged();
} }
......
...@@ -37,6 +37,8 @@ class ReportScheduler : public ProfileManagerObserver { ...@@ -37,6 +37,8 @@ class ReportScheduler : public ProfileManagerObserver {
void SetReportUploaderForTesting(std::unique_ptr<ReportUploader> uploader); void SetReportUploaderForTesting(std::unique_ptr<ReportUploader> uploader);
RequestTimer* GetRequestTimerForTesting() const;
void OnDMTokenUpdated(); void OnDMTokenUpdated();
private: private:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment