Commit d34680b0 authored by Zach Trudo's avatar Zach Trudo Committed by Commit Bot

Move ExtensionInstallEvents to Encrypted pipeline

Bug: chromium:1078512
Change-Id: I160ad5d61ad05f5e95f63188c40f9084b6e816e6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2369659
Commit-Queue: Zach Trudo <zatrudo@google.com>
Reviewed-by: default avatarSergey Poromov <poromov@chromium.org>
Reviewed-by: default avatarLeonid Baraz <lbaraz@chromium.org>
Reviewed-by: default avatarSwapnil Gupta <swapnilgupta@google.com>
Cr-Commit-Position: refs/heads/master@{#817556}
parent d298af52
...@@ -5,9 +5,16 @@ ...@@ -5,9 +5,16 @@
#ifndef CHROME_BROWSER_CHROMEOS_POLICY_EXTENSION_INSTALL_EVENT_LOG_UPLOADER_H_ #ifndef CHROME_BROWSER_CHROMEOS_POLICY_EXTENSION_INSTALL_EVENT_LOG_UPLOADER_H_
#define CHROME_BROWSER_CHROMEOS_POLICY_EXTENSION_INSTALL_EVENT_LOG_UPLOADER_H_ #define CHROME_BROWSER_CHROMEOS_POLICY_EXTENSION_INSTALL_EVENT_LOG_UPLOADER_H_
#include <memory>
#include "base/callback.h" #include "base/callback.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "chrome/browser/chromeos/policy/install_event_log_uploader_base.h" #include "chrome/browser/chromeos/policy/install_event_log_uploader_base.h"
#include "chrome/browser/policy/messaging_layer/public/report_client.h"
#include "chrome/browser/policy/messaging_layer/public/report_queue.h"
#include "chrome/browser/policy/messaging_layer/util/status.h"
#include "chrome/browser/policy/messaging_layer/util/statusor.h"
#include "chrome/browser/policy/messaging_layer/util/task_runner_context.h"
namespace enterprise_management { namespace enterprise_management {
class ExtensionInstallReportRequest; class ExtensionInstallReportRequest;
...@@ -17,8 +24,6 @@ class Profile; ...@@ -17,8 +24,6 @@ class Profile;
namespace policy { namespace policy {
class CloudPolicyClient;
// Adapter between the system that captures and stores extension install event // Adapter between the system that captures and stores extension install event
// logs and the policy system which uploads them to the management server. // logs and the policy system which uploads them to the management server.
class ExtensionInstallEventLogUploader : public InstallEventLogUploaderBase { class ExtensionInstallEventLogUploader : public InstallEventLogUploaderBase {
...@@ -45,8 +50,7 @@ class ExtensionInstallEventLogUploader : public InstallEventLogUploaderBase { ...@@ -45,8 +50,7 @@ class ExtensionInstallEventLogUploader : public InstallEventLogUploaderBase {
virtual ~Delegate(); virtual ~Delegate();
}; };
// |client| must outlive |this|. explicit ExtensionInstallEventLogUploader(Profile* profile);
ExtensionInstallEventLogUploader(CloudPolicyClient* client, Profile* profile);
~ExtensionInstallEventLogUploader() override; ~ExtensionInstallEventLogUploader() override;
// Sets the delegate. The delegate must either outlive |this| or be explicitly // Sets the delegate. The delegate must either outlive |this| or be explicitly
...@@ -54,7 +58,81 @@ class ExtensionInstallEventLogUploader : public InstallEventLogUploaderBase { ...@@ -54,7 +58,81 @@ class ExtensionInstallEventLogUploader : public InstallEventLogUploaderBase {
// delegate cancels the pending log upload, if any. // delegate cancels the pending log upload, if any.
void SetDelegate(Delegate* delegate); void SetDelegate(Delegate* delegate);
// Sets the report queue if it is not already set.
void SetReportQueue(std::unique_ptr<reporting::ReportQueue> report_queue);
// Meant to be used in tests for creating the ReportQueueConfiguration.
void SetBuildReportQueueConfigurationForTests(const std::string& dm_token);
private: private:
// Ensures that only one ReportQueueBuilder is working at one time.
class ReportQueueBuilderLeaderTracker;
// ReportQueueBuilder builds a ReportQueue and uses |set_report_queue_cb|
// to set it in the ExtensionInstallEventLogUploader. ReportQueueBuilder
// ensures that only one ReportQueue is built for ExtensionInstallLogUploader.
class ReportQueueBuilder : public reporting::TaskRunnerContext<bool> {
public:
using SetReportQueueCallback =
base::OnceCallback<void(std::unique_ptr<reporting::ReportQueue>,
base::OnceCallback<void()>)>;
using GetReportQueueConfigCallback =
base::RepeatingCallback<reporting::StatusOr<
std::unique_ptr<reporting::ReportQueueConfiguration>>()>;
ReportQueueBuilder(
SetReportQueueCallback set_report_queue_cb,
GetReportQueueConfigCallback get_report_queue_config_cb,
scoped_refptr<ReportQueueBuilderLeaderTracker> leader_tracker,
base::OnceCallback<void(bool)> completion_cb,
scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner);
private:
~ReportQueueBuilder() override;
// |OnStart| requests leadership promotion from the provided
// |leader_tracker|. If there is already a leader, |OnStart| will exit.
// Otherwise it will call |BuildReportQueue|.
void OnStart() override;
// |BuildReportQueue| will get the |ReportQueueConfiguration| from the
// |get_report_queue_config_cb_| and call ReportClient::CreateReportQueue to
// generate a ReportQueue. Sets OnReportQueueResult as the completion
// callback for |CreateReportQueue|.
void BuildReportQueue();
// |OnReportQueueResult| will evaluate |report_queue_result|. If it is not
// an OK status, it exits the builder with a |Complete| call. On an OK
// status it |Schedule|s SetReportQueue.
void OnReportQueueResult(
reporting::StatusOr<std::unique_ptr<reporting::ReportQueue>>
report_queue_result);
// SetReportQueue will call |set_report_queue_cb_| with the provided
// |report_queue|.
void SetReportQueue(std::unique_ptr<reporting::ReportQueue> report_queue);
// |Schedules| |ReleaseLeader|.
void Complete();
// Releases the leader lock if it is held, and then calls |Response|.
void ReleaseLeader();
// Callback for setting the ReportQueue in the calling
// |ExtensionInstallEventLogUploader|.
SetReportQueueCallback set_report_queue_cb_;
// Callback for creating the |ReportQueueConfiguration|.
GetReportQueueConfigCallback get_report_queue_config_cb_;
// |leader_tracker_| is used to ensure that only one ReportQueueBuilder is
// active at a time.
scoped_refptr<ReportQueueBuilderLeaderTracker> leader_tracker_;
base::OnceCallback<void()> release_leader_cb_;
};
// InstallEventLogUploaderBase: // InstallEventLogUploaderBase:
void CheckDelegateSet() override; void CheckDelegateSet() override;
void PostTaskForStartSerialization() override; void PostTaskForStartSerialization() override;
...@@ -67,9 +145,31 @@ class ExtensionInstallEventLogUploader : public InstallEventLogUploaderBase { ...@@ -67,9 +145,31 @@ class ExtensionInstallEventLogUploader : public InstallEventLogUploaderBase {
void OnSerialized( void OnSerialized(
const enterprise_management::ExtensionInstallReportRequest* report); const enterprise_management::ExtensionInstallReportRequest* report);
// Enqueues the report for upload.
void EnqueueReport(
const enterprise_management::ExtensionInstallReportRequest& report);
// Handles the status of the report enqueue.
void OnEnqueueDone(reporting::Status status);
// The delegate that provides serialized logs to be uploaded. // The delegate that provides serialized logs to be uploaded.
Delegate* delegate_ = nullptr; Delegate* delegate_ = nullptr;
// ReportQueueBuilderLeaderTracker for building the ReportQueue, passed to
// each ReportQueueBuilder in order to track which is the leader.
scoped_refptr<ReportQueueBuilderLeaderTracker> leader_tracker_;
// SequencedTaskRunenr for building the ReportQueue.
scoped_refptr<base::SequencedTaskRunner> report_queue_builder_task_runner_;
// Callback to generate a ReportQueueConfiguration.
base::RepeatingCallback<reporting::StatusOr<
std::unique_ptr<reporting::ReportQueueConfiguration>>()>
get_report_queue_config_cb_;
// ReportQueue for uploading events.
std::unique_ptr<reporting::ReportQueue> report_queue_;
// Weak pointer factory for invalidating callbacks passed to the delegate and // Weak pointer factory for invalidating callbacks passed to the delegate and
// scheduled retries when the upload request is canceled or |this| is // scheduled retries when the upload request is canceled or |this| is
// destroyed. // destroyed.
......
...@@ -29,8 +29,14 @@ InstallEventLogUploaderBase::InstallEventLogUploaderBase( ...@@ -29,8 +29,14 @@ InstallEventLogUploaderBase::InstallEventLogUploaderBase(
client_->AddObserver(this); client_->AddObserver(this);
} }
InstallEventLogUploaderBase::InstallEventLogUploaderBase(Profile* profile)
: client_(nullptr),
profile_(profile),
retry_backoff_ms_(kMinRetryBackoffMs) {}
InstallEventLogUploaderBase::~InstallEventLogUploaderBase() { InstallEventLogUploaderBase::~InstallEventLogUploaderBase() {
client_->RemoveObserver(this); if (client_)
client_->RemoveObserver(this);
} }
void InstallEventLogUploaderBase::RequestUpload() { void InstallEventLogUploaderBase::RequestUpload() {
...@@ -39,8 +45,12 @@ void InstallEventLogUploaderBase::RequestUpload() { ...@@ -39,8 +45,12 @@ void InstallEventLogUploaderBase::RequestUpload() {
return; return;
upload_requested_ = true; upload_requested_ = true;
if (client_->is_registered())
// If the client is set - ensure that it is also registered.
// Otherwise start Serialization.
if ((client_ && client_->is_registered()) || !client_) {
StartSerialization(); StartSerialization();
}
} }
void InstallEventLogUploaderBase::CancelUpload() { void InstallEventLogUploaderBase::CancelUpload() {
...@@ -51,6 +61,7 @@ void InstallEventLogUploaderBase::CancelUpload() { ...@@ -51,6 +61,7 @@ void InstallEventLogUploaderBase::CancelUpload() {
void InstallEventLogUploaderBase::OnRegistrationStateChanged( void InstallEventLogUploaderBase::OnRegistrationStateChanged(
CloudPolicyClient* client) { CloudPolicyClient* client) {
DCHECK(client_);
if (!upload_requested_) if (!upload_requested_)
return; return;
......
...@@ -21,6 +21,17 @@ class InstallEventLogUploaderBase : public CloudPolicyClient::Observer { ...@@ -21,6 +21,17 @@ class InstallEventLogUploaderBase : public CloudPolicyClient::Observer {
public: public:
// |client| must outlive |this|. // |client| must outlive |this|.
InstallEventLogUploaderBase(CloudPolicyClient* client, Profile* profile); InstallEventLogUploaderBase(CloudPolicyClient* client, Profile* profile);
// Will construct a non-CloudPolicyClient::Observer version of
// InstallEventLogUploaderBase.
// TODO(crbug.com/1078512) This exists to support the move to using
// reporting::ReportQueue, which owns its own CloudPolicyClient. Once
// ArcInstallEventLogUploader is ready to move to using
// reporting::ReportQueue, we can likely do a small refactor removing all
// references to CloudPolicyClient from InstallEventLogUploaderBase and its
// children.
explicit InstallEventLogUploaderBase(Profile* profile);
~InstallEventLogUploaderBase() override; ~InstallEventLogUploaderBase() override;
// Requests log upload. If there is no pending upload yet, asks the delegate // Requests log upload. If there is no pending upload yet, asks the delegate
......
...@@ -271,7 +271,7 @@ void UserCloudPolicyManagerChromeOS::Connect( ...@@ -271,7 +271,7 @@ void UserCloudPolicyManagerChromeOS::Connect(
app_install_event_log_uploader_ = app_install_event_log_uploader_ =
std::make_unique<ArcAppInstallEventLogUploader>(client(), profile_); std::make_unique<ArcAppInstallEventLogUploader>(client(), profile_);
extension_install_event_log_uploader_ = extension_install_event_log_uploader_ =
std::make_unique<ExtensionInstallEventLogUploader>(client(), profile_); std::make_unique<ExtensionInstallEventLogUploader>(profile_);
// Initializes an instance of DlpRulesManager to be responsible for the rules // Initializes an instance of DlpRulesManager to be responsible for the rules
// of the data leak prevention policy. // of the data leak prevention policy.
......
...@@ -34,12 +34,18 @@ using AppInstallReportUploader = ...@@ -34,12 +34,18 @@ using AppInstallReportUploader =
using UploaderLeaderTracker = AppInstallReportHandler::UploaderLeaderTracker; using UploaderLeaderTracker = AppInstallReportHandler::UploaderLeaderTracker;
UploaderLeaderTracker::UploaderLeaderTracker(policy::CloudPolicyClient* client)
: client_(client) {}
UploaderLeaderTracker::~UploaderLeaderTracker() = default;
// static // static
scoped_refptr<UploaderLeaderTracker> UploaderLeaderTracker::Create() { scoped_refptr<UploaderLeaderTracker> UploaderLeaderTracker::Create(
return base::WrapRefCounted(new UploaderLeaderTracker()); policy::CloudPolicyClient* client) {
return base::WrapRefCounted(new UploaderLeaderTracker(client));
} }
StatusOr<AppInstallReportHandler::ReleaseLeaderCallback> StatusOr<std::unique_ptr<UploaderLeaderTracker::LeaderLock>>
UploaderLeaderTracker::RequestLeaderPromotion() { UploaderLeaderTracker::RequestLeaderPromotion() {
if (has_promoted_app_install_event_uploader_) { if (has_promoted_app_install_event_uploader_) {
return Status(error::RESOURCE_EXHAUSTED, return Status(error::RESOURCE_EXHAUSTED,
...@@ -47,26 +53,39 @@ UploaderLeaderTracker::RequestLeaderPromotion() { ...@@ -47,26 +53,39 @@ UploaderLeaderTracker::RequestLeaderPromotion() {
} }
has_promoted_app_install_event_uploader_ = true; has_promoted_app_install_event_uploader_ = true;
return base::BindOnce(&UploaderLeaderTracker::ReleaseLeader,
base::Unretained(this)); return std::make_unique<LeaderLock>(
base::BindOnce(&UploaderLeaderTracker::ReleaseLeader,
base::Unretained(this)),
client_);
} }
void UploaderLeaderTracker::ReleaseLeader() { void UploaderLeaderTracker::ReleaseLeader() {
has_promoted_app_install_event_uploader_ = false; has_promoted_app_install_event_uploader_ = false;
} }
UploaderLeaderTracker::LeaderLock::LeaderLock(
UploaderLeaderTracker::ReleaseLeaderCallback release_cb,
policy::CloudPolicyClient* client)
: client_(std::move(client)),
release_leader_callback_(std::move(release_cb)) {}
UploaderLeaderTracker::LeaderLock::~LeaderLock() {
if (release_leader_callback_) {
std::move(release_leader_callback_).Run();
}
}
AppInstallReportUploader::AppInstallReportUploader( AppInstallReportUploader::AppInstallReportUploader(
base::Value report, base::Value report,
scoped_refptr<SharedQueue<base::Value>> report_queue, scoped_refptr<SharedQueue<base::Value>> report_queue,
scoped_refptr<UploaderLeaderTracker> leader_tracker, scoped_refptr<UploaderLeaderTracker> leader_tracker,
policy::CloudPolicyClient* client,
ClientCallback client_cb, ClientCallback client_cb,
scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner) scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner)
: TaskRunnerContext<bool>(std::move(client_cb), sequenced_task_runner), : TaskRunnerContext<bool>(std::move(client_cb), sequenced_task_runner),
report_(std::move(report)), report_(std::move(report)),
report_queue_(report_queue), report_queue_(report_queue),
leader_tracker_(leader_tracker), leader_tracker_(leader_tracker) {}
client_(client) {}
AppInstallReportUploader::~AppInstallReportUploader() = default; AppInstallReportUploader::~AppInstallReportUploader() = default;
...@@ -88,7 +107,7 @@ void AppInstallReportUploader::RequestLeaderPromotion() { ...@@ -88,7 +107,7 @@ void AppInstallReportUploader::RequestLeaderPromotion() {
return; return;
} }
release_leader_cb_ = std::move(promo_result.ValueOrDie()); leader_lock_ = std::move(promo_result.ValueOrDie());
ScheduleNextPop(); ScheduleNextPop();
} }
...@@ -101,7 +120,7 @@ void AppInstallReportUploader::ScheduleNextPop() { ...@@ -101,7 +120,7 @@ void AppInstallReportUploader::ScheduleNextPop() {
void AppInstallReportUploader::OnPopResult(StatusOr<base::Value> pop_result) { void AppInstallReportUploader::OnPopResult(StatusOr<base::Value> pop_result) {
if (!pop_result.ok()) { if (!pop_result.ok()) {
// There are no more records to process - exit. // There are no more records to process - exit.
std::move(release_leader_cb_).Run(); leader_lock_.reset();
Complete(); Complete();
return; return;
} }
...@@ -120,7 +139,7 @@ void AppInstallReportUploader::StartUpload(base::Value record) { ...@@ -120,7 +139,7 @@ void AppInstallReportUploader::StartUpload(base::Value record) {
client->UploadExtensionInstallReport(std::move(record), client->UploadExtensionInstallReport(std::move(record),
std::move(cb)); std::move(cb));
}, },
client_, std::move(record), std::move(cb))); leader_lock_->client(), std::move(record), std::move(cb)));
} }
void AppInstallReportUploader::OnUploadComplete(bool success) { void AppInstallReportUploader::OnUploadComplete(bool success) {
...@@ -139,7 +158,7 @@ AppInstallReportHandler::AppInstallReportHandler( ...@@ -139,7 +158,7 @@ AppInstallReportHandler::AppInstallReportHandler(
policy::CloudPolicyClient* client) policy::CloudPolicyClient* client)
: RecordHandler(client), : RecordHandler(client),
report_queue_(SharedQueue<base::Value>::Create()), report_queue_(SharedQueue<base::Value>::Create()),
leader_tracker_(UploaderLeaderTracker::Create()), leader_tracker_(UploaderLeaderTracker::Create(client)),
sequenced_task_runner_(base::ThreadPool::CreateSequencedTaskRunner({})) {} sequenced_task_runner_(base::ThreadPool::CreateSequencedTaskRunner({})) {}
AppInstallReportHandler::~AppInstallReportHandler() = default; AppInstallReportHandler::~AppInstallReportHandler() = default;
...@@ -155,8 +174,8 @@ Status AppInstallReportHandler::HandleRecord(Record record) { ...@@ -155,8 +174,8 @@ Status AppInstallReportHandler::HandleRecord(Record record) {
// Start an uploader in case any previous uploader has finished running before // Start an uploader in case any previous uploader has finished running before
// this record was posted. // this record was posted.
Start<AppInstallReportUploader>(std::move(report), report_queue_, Start<AppInstallReportUploader>(std::move(report), report_queue_,
leader_tracker_, GetClient(), leader_tracker_, std::move(client_cb),
std::move(client_cb), sequenced_task_runner_); sequenced_task_runner_);
return Status::StatusOK(); return Status::StatusOK();
} }
...@@ -189,11 +208,4 @@ StatusOr<base::Value> AppInstallReportHandler::ConvertRecord( ...@@ -189,11 +208,4 @@ StatusOr<base::Value> AppInstallReportHandler::ConvertRecord(
return std::move(report_result.value()); return std::move(report_result.value());
} }
Status AppInstallReportHandler::ValidateClientState() const {
if (!GetClient()->is_registered()) {
return Status(error::UNAVAILABLE, "DmServer is currently unavailable");
}
return Status::StatusOK();
}
} // namespace reporting } // namespace reporting
...@@ -36,16 +36,30 @@ class AppInstallReportHandler : public DmServerUploadService::RecordHandler { ...@@ -36,16 +36,30 @@ class AppInstallReportHandler : public DmServerUploadService::RecordHandler {
// and false indicates failure. // and false indicates failure.
using ClientCallback = base::OnceCallback<void(bool status)>; using ClientCallback = base::OnceCallback<void(bool status)>;
using ReleaseLeaderCallback = base::OnceCallback<void()>;
using RequestLeaderPromotionCallback =
base::OnceCallback<StatusOr<ReleaseLeaderCallback>()>;
// Tracking the leader needs to outlive |AppInstallReportHandler| so it needs // Tracking the leader needs to outlive |AppInstallReportHandler| so it needs
// to be wrapped in a scoped_refptr. // to be wrapped in a scoped_refptr.
class UploaderLeaderTracker class UploaderLeaderTracker
: public base::RefCountedThreadSafe<UploaderLeaderTracker> { : public base::RefCountedThreadSafe<UploaderLeaderTracker> {
public: public:
static scoped_refptr<UploaderLeaderTracker> Create(); using ReleaseLeaderCallback = base::OnceCallback<void()>;
// Holds the lock on the leader, releases it upon destruction.
class LeaderLock {
public:
LeaderLock(ReleaseLeaderCallback release_cb,
policy::CloudPolicyClient* client);
virtual ~LeaderLock();
policy::CloudPolicyClient* client() { return client_; }
private:
policy::CloudPolicyClient* client_;
ReleaseLeaderCallback release_leader_callback_;
};
static scoped_refptr<UploaderLeaderTracker> Create(
policy::CloudPolicyClient* cloud_policy_client);
// If there is currently no leader // If there is currently no leader
// (|has_promoted_app_install_event_uploader_| is false), then the StatusOr // (|has_promoted_app_install_event_uploader_| is false), then the StatusOr
...@@ -53,23 +67,29 @@ class AppInstallReportHandler : public DmServerUploadService::RecordHandler { ...@@ -53,23 +67,29 @@ class AppInstallReportHandler : public DmServerUploadService::RecordHandler {
// leader an error::RESOURCE_EXHAUSTED is returned (which should be the // leader an error::RESOURCE_EXHAUSTED is returned (which should be the
// common case). This will be called on sequence from inside the // common case). This will be called on sequence from inside the
// |AppInstallReportUploader| and so needs no additional protection. // |AppInstallReportUploader| and so needs no additional protection.
StatusOr<ReleaseLeaderCallback> RequestLeaderPromotion(); StatusOr<std::unique_ptr<LeaderLock>> RequestLeaderPromotion();
private:
friend class base::RefCountedThreadSafe<UploaderLeaderTracker>;
explicit UploaderLeaderTracker(policy::CloudPolicyClient* client);
virtual ~UploaderLeaderTracker();
// Once a AppInstallEventUploader leader drains the queue of reports, it // Once a AppInstallEventUploader leader drains the queue of reports, it
// will release its leadership and return, allowing a new // will release its leadership and return, allowing a new
// AppInstallEventUploader to take leadership and upload events. // AppInstallEventUploader to take leadership and upload events.
void ReleaseLeader(); void ReleaseLeader();
private: // CloudPolicyClient allows calls to the reporting server.
friend class base::RefCountedThreadSafe<UploaderLeaderTracker>; policy::CloudPolicyClient* client_;
virtual ~UploaderLeaderTracker() = default;
UploaderLeaderTracker() = default;
// Flag indicates whether a leader has been promoted. // Flag indicates whether a leader has been promoted.
bool has_promoted_app_install_event_uploader_{false}; bool has_promoted_app_install_event_uploader_{false};
}; };
using RequestLeaderPromotionCallback =
base::OnceCallback<StatusOr<UploaderLeaderTracker::LeaderLock>()>;
// AppInstallReportUploader handles enqueuing events on the |report_queue_|, // AppInstallReportUploader handles enqueuing events on the |report_queue_|,
// and uploading those events with the |client_|. // and uploading those events with the |client_|.
class AppInstallReportUploader : public TaskRunnerContext<bool> { class AppInstallReportUploader : public TaskRunnerContext<bool> {
...@@ -78,7 +98,6 @@ class AppInstallReportHandler : public DmServerUploadService::RecordHandler { ...@@ -78,7 +98,6 @@ class AppInstallReportHandler : public DmServerUploadService::RecordHandler {
base::Value report, base::Value report,
scoped_refptr<SharedQueue<base::Value>> report_queue, scoped_refptr<SharedQueue<base::Value>> report_queue,
scoped_refptr<UploaderLeaderTracker> leader_tracker, scoped_refptr<UploaderLeaderTracker> leader_tracker,
policy::CloudPolicyClient* client,
ClientCallback client_cb, ClientCallback client_cb,
scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner); scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner);
...@@ -127,9 +146,7 @@ class AppInstallReportHandler : public DmServerUploadService::RecordHandler { ...@@ -127,9 +146,7 @@ class AppInstallReportHandler : public DmServerUploadService::RecordHandler {
base::Value report_; base::Value report_;
scoped_refptr<SharedQueue<base::Value>> report_queue_; scoped_refptr<SharedQueue<base::Value>> report_queue_;
scoped_refptr<UploaderLeaderTracker> leader_tracker_; scoped_refptr<UploaderLeaderTracker> leader_tracker_;
ReleaseLeaderCallback release_leader_cb_; std::unique_ptr<UploaderLeaderTracker::LeaderLock> leader_lock_;
policy::CloudPolicyClient* const client_;
}; };
explicit AppInstallReportHandler(policy::CloudPolicyClient* client); explicit AppInstallReportHandler(policy::CloudPolicyClient* client);
...@@ -150,9 +167,6 @@ class AppInstallReportHandler : public DmServerUploadService::RecordHandler { ...@@ -150,9 +167,6 @@ class AppInstallReportHandler : public DmServerUploadService::RecordHandler {
// Convert record into base::Value for upload (override for subclass). // Convert record into base::Value for upload (override for subclass).
virtual StatusOr<base::Value> ConvertRecord(const Record& record) const; virtual StatusOr<base::Value> ConvertRecord(const Record& record) const;
// Helper method. Validates CloudPolicyClient state.
Status ValidateClientState() const;
scoped_refptr<SharedQueue<base::Value>> report_queue_; scoped_refptr<SharedQueue<base::Value>> report_queue_;
scoped_refptr<UploaderLeaderTracker> leader_tracker_; scoped_refptr<UploaderLeaderTracker> leader_tracker_;
scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_;
......
...@@ -63,17 +63,17 @@ class TestCallbackWaiter { ...@@ -63,17 +63,17 @@ class TestCallbackWaiter {
class AppInstallReportHandlerTest : public testing::Test { class AppInstallReportHandlerTest : public testing::Test {
public: public:
AppInstallReportHandlerTest() = default; AppInstallReportHandlerTest()
: client_(std::make_unique<policy::MockCloudPolicyClient>()) {}
protected:
void SetUp() override { void SetUp() override {
client_.SetDMToken( client_->SetDMToken(
policy::DMToken::CreateValidTokenForTesting("FAKE_DM_TOKEN").value()); policy::DMToken::CreateValidTokenForTesting("FAKE_DM_TOKEN").value());
} }
content::BrowserTaskEnvironment task_environment_;
protected: std::unique_ptr<policy::MockCloudPolicyClient> client_;
content::BrowserTaskEnvironment task_envrionment_;
policy::MockCloudPolicyClient client_;
}; };
class TestRecord : public Record { class TestRecord : public Record {
...@@ -97,7 +97,7 @@ class TestRecord : public Record { ...@@ -97,7 +97,7 @@ class TestRecord : public Record {
TEST_F(AppInstallReportHandlerTest, AcceptsValidRecord) { TEST_F(AppInstallReportHandlerTest, AcceptsValidRecord) {
TestCallbackWaiter waiter; TestCallbackWaiter waiter;
TestRecord test_record; TestRecord test_record;
EXPECT_CALL(client_, EXPECT_CALL(*client_,
UploadExtensionInstallReport_(MatchValue(test_record.data()), _)) UploadExtensionInstallReport_(MatchValue(test_record.data()), _))
.WillOnce(WithArgs<1>( .WillOnce(WithArgs<1>(
Invoke([&waiter](AppInstallReportHandler::ClientCallback& callback) { Invoke([&waiter](AppInstallReportHandler::ClientCallback& callback) {
...@@ -105,15 +105,15 @@ TEST_F(AppInstallReportHandlerTest, AcceptsValidRecord) { ...@@ -105,15 +105,15 @@ TEST_F(AppInstallReportHandlerTest, AcceptsValidRecord) {
waiter.Signal(); waiter.Signal();
}))); })));
AppInstallReportHandler handler(&client_); AppInstallReportHandler handler(client_.get());
Status handle_status = handler.HandleRecord(test_record); Status handle_status = handler.HandleRecord(test_record);
EXPECT_OK(handle_status); EXPECT_OK(handle_status);
waiter.Wait(); waiter.Wait();
} }
TEST_F(AppInstallReportHandlerTest, DeniesInvalidDestination) { TEST_F(AppInstallReportHandlerTest, DeniesInvalidDestination) {
EXPECT_CALL(client_, UploadExtensionInstallReport_(_, _)).Times(0); EXPECT_CALL(*client_, UploadExtensionInstallReport_(_, _)).Times(0);
AppInstallReportHandler handler(&client_); AppInstallReportHandler handler(client_.get());
TestRecord test_record; TestRecord test_record;
test_record.set_destination(Destination::MEET_DEVICE_TELEMETRY); test_record.set_destination(Destination::MEET_DEVICE_TELEMETRY);
...@@ -124,8 +124,8 @@ TEST_F(AppInstallReportHandlerTest, DeniesInvalidDestination) { ...@@ -124,8 +124,8 @@ TEST_F(AppInstallReportHandlerTest, DeniesInvalidDestination) {
} }
TEST_F(AppInstallReportHandlerTest, DeniesInvalidData) { TEST_F(AppInstallReportHandlerTest, DeniesInvalidData) {
EXPECT_CALL(client_, UploadExtensionInstallReport_(_, _)).Times(0); EXPECT_CALL(*client_, UploadExtensionInstallReport_(_, _)).Times(0);
AppInstallReportHandler handler(&client_); AppInstallReportHandler handler(client_.get());
TestRecord test_record; TestRecord test_record;
test_record.set_data("BAD_DATA"); test_record.set_data("BAD_DATA");
...@@ -138,7 +138,7 @@ TEST_F(AppInstallReportHandlerTest, ReportsUnsuccessfulCall) { ...@@ -138,7 +138,7 @@ TEST_F(AppInstallReportHandlerTest, ReportsUnsuccessfulCall) {
TestCallbackWaiter waiter; TestCallbackWaiter waiter;
TestRecord test_record; TestRecord test_record;
EXPECT_CALL(client_, EXPECT_CALL(*client_,
UploadExtensionInstallReport_(MatchValue(test_record.data()), _)) UploadExtensionInstallReport_(MatchValue(test_record.data()), _))
.WillOnce(WithArgs<1>( .WillOnce(WithArgs<1>(
Invoke([&waiter](AppInstallReportHandler::ClientCallback& callback) { Invoke([&waiter](AppInstallReportHandler::ClientCallback& callback) {
...@@ -146,7 +146,7 @@ TEST_F(AppInstallReportHandlerTest, ReportsUnsuccessfulCall) { ...@@ -146,7 +146,7 @@ TEST_F(AppInstallReportHandlerTest, ReportsUnsuccessfulCall) {
waiter.Signal(); waiter.Signal();
}))); })));
AppInstallReportHandler handler(&client_); AppInstallReportHandler handler(client_.get());
Status handle_status = handler.HandleRecord(test_record); Status handle_status = handler.HandleRecord(test_record);
EXPECT_OK(handle_status); EXPECT_OK(handle_status);
waiter.Wait(); waiter.Wait();
...@@ -173,7 +173,7 @@ TEST_F(AppInstallReportHandlerTest, AcceptsMultipleValidRecords) { ...@@ -173,7 +173,7 @@ TEST_F(AppInstallReportHandlerTest, AcceptsMultipleValidRecords) {
TestCallbackWaiterWithCounter waiter{kExpectedCallTimes}; TestCallbackWaiterWithCounter waiter{kExpectedCallTimes};
TestRecord test_record; TestRecord test_record;
EXPECT_CALL(client_, EXPECT_CALL(*client_,
UploadExtensionInstallReport_(MatchValue(test_record.data()), _)) UploadExtensionInstallReport_(MatchValue(test_record.data()), _))
.WillRepeatedly(WithArgs<1>( .WillRepeatedly(WithArgs<1>(
Invoke([&waiter](AppInstallReportHandler::ClientCallback& callback) { Invoke([&waiter](AppInstallReportHandler::ClientCallback& callback) {
...@@ -181,7 +181,7 @@ TEST_F(AppInstallReportHandlerTest, AcceptsMultipleValidRecords) { ...@@ -181,7 +181,7 @@ TEST_F(AppInstallReportHandlerTest, AcceptsMultipleValidRecords) {
waiter.Signal(); waiter.Signal();
}))); })));
AppInstallReportHandler handler(&client_); AppInstallReportHandler handler(client_.get());
for (int i = 0; i < kExpectedCallTimes; i++) { for (int i = 0; i < kExpectedCallTimes; i++) {
Status handle_status = handler.HandleRecord(test_record); Status handle_status = handler.HandleRecord(test_record);
......
...@@ -88,11 +88,10 @@ class CollectorCallback { ...@@ -88,11 +88,10 @@ class CollectorCallback {
}; };
} // namespace } // namespace
DmServerUploadService::RecordHandler::RecordHandler(CloudPolicyClient* client) DmServerUploadService::RecordHandler::RecordHandler(
policy::CloudPolicyClient* client)
: client_(client) {} : client_(client) {}
DmServerUploadService::RecordHandler::~RecordHandler() = default;
DmServerUploader::DmServerUploader( DmServerUploader::DmServerUploader(
std::unique_ptr<std::vector<EncryptedRecord>> records, std::unique_ptr<std::vector<EncryptedRecord>> records,
scoped_refptr<SharedVector<std::unique_ptr<RecordHandler>>> handlers, scoped_refptr<SharedVector<std::unique_ptr<RecordHandler>>> handlers,
...@@ -349,9 +348,8 @@ DmServerUploadService::DmServerUploadService( ...@@ -349,9 +348,8 @@ DmServerUploadService::DmServerUploadService(
ReportSuccessfulUploadCallback upload_cb) ReportSuccessfulUploadCallback upload_cb)
: client_(std::move(client)), : client_(std::move(client)),
upload_cb_(upload_cb), upload_cb_(upload_cb),
sequenced_task_runner_(base::ThreadPool::CreateSequencedTaskRunner({})), record_handlers_(SharedVector<std::unique_ptr<RecordHandler>>::Create()),
record_handlers_(SharedVector<std::unique_ptr<RecordHandler>>::Create()) { sequenced_task_runner_(base::ThreadPool::CreateSequencedTaskRunner({})) {}
}
DmServerUploadService::~DmServerUploadService() { DmServerUploadService::~DmServerUploadService() {
if (client_) { if (client_) {
......
...@@ -52,7 +52,7 @@ class DmServerUploadService { ...@@ -52,7 +52,7 @@ class DmServerUploadService {
class RecordHandler { class RecordHandler {
public: public:
explicit RecordHandler(policy::CloudPolicyClient* client); explicit RecordHandler(policy::CloudPolicyClient* client);
virtual ~RecordHandler(); virtual ~RecordHandler() = default;
virtual Status HandleRecord(Record record) = 0; virtual Status HandleRecord(Record record) = 0;
...@@ -91,8 +91,8 @@ class DmServerUploadService { ...@@ -91,8 +91,8 @@ class DmServerUploadService {
void IsHandlerVectorEmptyCheck(bool handler_is_empty); void IsHandlerVectorEmptyCheck(bool handler_is_empty);
// ProcessRecords verifies that the records provided are parseable and sets // ProcessRecords verifies that the records provided are parseable and sets
// the |Record|s up for handling by the |RecordHandler|s. On completion, // the |Record|s up for handling by the |RecordHandlers|s. On
// ProcessRecords |Schedule|s |HandleRecords|. // completion, ProcessRecords |Schedule|s |HandleRecords|.
void ProcessRecords(); void ProcessRecords();
// HandleRecords sends the records to the |record_handlers_|, allowing them // HandleRecords sends the records to the |record_handlers_|, allowing them
...@@ -125,7 +125,7 @@ class DmServerUploadService { ...@@ -125,7 +125,7 @@ class DmServerUploadService {
SequencingInformation sequencing_information); SequencingInformation sequencing_information);
std::unique_ptr<std::vector<EncryptedRecord>> encrypted_records_; std::unique_ptr<std::vector<EncryptedRecord>> encrypted_records_;
const scoped_refptr<SharedVector<std::unique_ptr<RecordHandler>>> handlers_; scoped_refptr<SharedVector<std::unique_ptr<RecordHandler>>> handlers_;
// generation_id_ will be set to the generation of the first record in // generation_id_ will be set to the generation of the first record in
// encrypted_records_. // encrypted_records_.
...@@ -173,9 +173,9 @@ class DmServerUploadService { ...@@ -173,9 +173,9 @@ class DmServerUploadService {
std::unique_ptr<policy::CloudPolicyClient> client_; std::unique_ptr<policy::CloudPolicyClient> client_;
ReportSuccessfulUploadCallback upload_cb_; ReportSuccessfulUploadCallback upload_cb_;
scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_;
scoped_refptr<SharedVector<std::unique_ptr<RecordHandler>>> record_handlers_; scoped_refptr<SharedVector<std::unique_ptr<RecordHandler>>> record_handlers_;
scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_;
}; };
} // namespace reporting } // namespace reporting
......
...@@ -116,9 +116,7 @@ class TestCallbackWaiter { ...@@ -116,9 +116,7 @@ class TestCallbackWaiter {
class TestRecordHandler : public DmServerUploadService::RecordHandler { class TestRecordHandler : public DmServerUploadService::RecordHandler {
public: public:
explicit TestRecordHandler(policy::CloudPolicyClient* client) TestRecordHandler() : RecordHandler(/*client=*/nullptr) {}
: RecordHandler(client) {}
~TestRecordHandler() override = default; ~TestRecordHandler() override = default;
MOCK_METHOD(Status, HandleRecord, (Record)); MOCK_METHOD(Status, HandleRecord, (Record));
...@@ -132,8 +130,7 @@ class DmServerUploaderTest : public testing::Test { ...@@ -132,8 +130,7 @@ class DmServerUploaderTest : public testing::Test {
DmServerUploadService::RecordHandler>>::Create()) {} DmServerUploadService::RecordHandler>>::Create()) {}
void SetUp() override { void SetUp() override {
std::unique_ptr<TestRecordHandler> handler_ptr( std::unique_ptr<TestRecordHandler> handler_ptr(new TestRecordHandler());
new TestRecordHandler(&client_));
handler_ = handler_ptr.get(); handler_ = handler_ptr.get();
handlers_->PushBack(std::move(handler_ptr), base::DoNothing()); handlers_->PushBack(std::move(handler_ptr), base::DoNothing());
records_ = std::make_unique<std::vector<EncryptedRecord>>(); records_ = std::make_unique<std::vector<EncryptedRecord>>();
...@@ -153,9 +150,6 @@ class DmServerUploaderTest : public testing::Test { ...@@ -153,9 +150,6 @@ class DmServerUploaderTest : public testing::Test {
std::unique_ptr<std::vector<EncryptedRecord>> records_; std::unique_ptr<std::vector<EncryptedRecord>> records_;
const base::TimeDelta kMaxDelay_ = base::TimeDelta::FromSeconds(1); const base::TimeDelta kMaxDelay_ = base::TimeDelta::FromSeconds(1);
private:
policy::MockCloudPolicyClient client_;
}; };
TEST_F(DmServerUploaderTest, ProcessesRecord) { TEST_F(DmServerUploaderTest, ProcessesRecord) {
......
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