Skip to content

Commit

Permalink
Correctly build ReportQueueConfig
Browse files Browse the repository at this point in the history
In ExtensionInstallLogUploader the ReportQueueConfig wasn't
correctly building the ReportQueueConfig.

Bug: chromium:1078512
Change-Id: Ifd0b495536b3b5b5153193a4e9d29e3a1b66b0d8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2505682
Commit-Queue: Zach Trudo <zatrudo@google.com>
Reviewed-by: Swapnil Gupta <swapnilgupta@google.com>
Cr-Commit-Position: refs/heads/master@{#823191}
  • Loading branch information
zachary-trudo authored and Commit Bot committed Nov 2, 2020
1 parent 44db755 commit 0ae9dc1
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,39 @@
#include "chrome/browser/profiles/reporting_util.h"
#include "components/policy/core/common/cloud/realtime_reporting_job_configuration.h"
#include "components/policy/proto/device_management_backend.pb.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"

namespace em = enterprise_management;

namespace policy {
namespace {

base::RepeatingCallback<::reporting::StatusOr<
std::unique_ptr<::reporting::ReportQueueConfiguration>>()>
ExtensionInstallEventLogUploader::GetReportQueueConfigCallback
CreateReportQueueConfigGetter(Profile* profile) {
auto dm_token = GetDMToken(profile, /*only_affiliated=*/false);
return base::BindRepeating(
[](DMToken dm_token) {
return ::reporting::ReportQueueConfiguration::Create(
dm_token, ::reporting::Destination::UPLOAD_EVENTS,
::reporting::Priority::SLOW_BATCH, base::BindRepeating([]() {
return ::reporting::Status::StatusOK();
}));
[](Profile* profile,
ExtensionInstallEventLogUploader::ReportQueueConfigResultCallback
complete_cb) {
auto task = base::BindOnce(
[](Profile* profile,
base::OnceCallback<void(
::reporting::StatusOr<std::unique_ptr<
::reporting::ReportQueueConfiguration>>)> complete_cb) {
auto dm_token = GetDMToken(profile, /*only_affiliated=*/false);
std::move(complete_cb)
.Run(::reporting::ReportQueueConfiguration::Create(
dm_token, ::reporting::Destination::UPLOAD_EVENTS,
::reporting::Priority::SLOW_BATCH,
base::BindRepeating(
[]() { return ::reporting::Status::StatusOK(); })));
},
profile, std::move(complete_cb));

base::PostTask(FROM_HERE, {content::BrowserThread::UI},
std::move(task));
},
dm_token);
profile);
}

} // namespace
Expand Down Expand Up @@ -99,13 +113,15 @@ void ExtensionInstallEventLogUploader::SetReportQueue(
void ExtensionInstallEventLogUploader::SetBuildReportQueueConfigurationForTests(
const std::string& dm_token) {
get_report_queue_config_cb_ = base::BindRepeating(
[](const std::string& dm_token) {
return ::reporting::ReportQueueConfiguration::Create(
DMToken::CreateValidTokenForTesting(dm_token),
::reporting::Destination::UPLOAD_EVENTS,
::reporting::Priority::SLOW_BATCH, base::BindRepeating([]() {
return ::reporting::Status::StatusOK();
}));
[](const std::string& dm_token,
ReportQueueConfigResultCallback complete_cb) {
std::move(complete_cb)
.Run(::reporting::ReportQueueConfiguration::Create(
DMToken::CreateValidTokenForTesting(dm_token),
::reporting::Destination::UPLOAD_EVENTS,
::reporting::Priority::SLOW_BATCH, base::BindRepeating([]() {
return ::reporting::Status::StatusOK();
})));
},
dm_token);
}
Expand All @@ -118,9 +134,7 @@ ExtensionInstallEventLogUploader::ReportQueueBuilderLeaderTracker::
ExtensionInstallEventLogUploader::ReportQueueBuilder::ReportQueueBuilder(
base::OnceCallback<void(std::unique_ptr<::reporting::ReportQueue>,
base::OnceCallback<void()>)> set_report_queue_cb,
base::RepeatingCallback<::reporting::StatusOr<
std::unique_ptr<::reporting::ReportQueueConfiguration>>()>
get_report_queue_config_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)
Expand All @@ -143,17 +157,32 @@ void ExtensionInstallEventLogUploader::ReportQueueBuilder::OnStart() {
}

release_leader_cb_ = std::move(promo_result.ValueOrDie());
BuildReportQueue();
BuildReportQueueConfig();
}

void ExtensionInstallEventLogUploader::ReportQueueBuilder::
BuildReportQueueConfig() {
std::move(get_report_queue_config_cb_)
.Run(base::BindOnce(&ReportQueueBuilder::OnReportQueueConfigResult,
base::Unretained(this)));
}

void ExtensionInstallEventLogUploader::ReportQueueBuilder::BuildReportQueue() {
auto report_queue_config_result = get_report_queue_config_cb_.Run();
void ExtensionInstallEventLogUploader::ReportQueueBuilder::
OnReportQueueConfigResult(
ReportQueueConfigResult report_queue_config_result) {
if (!report_queue_config_result.ok()) {
Complete();
return;
}
Schedule(&ReportQueueBuilder::BuildReportQueue, base::Unretained(this),
std::move(report_queue_config_result.ValueOrDie()));
}

void ExtensionInstallEventLogUploader::ReportQueueBuilder::BuildReportQueue(
std::unique_ptr<::reporting::ReportQueueConfiguration>
report_queue_config) {
::reporting::ReportingClient::CreateReportQueue(
std::move(report_queue_config_result.ValueOrDie()),
std::move(report_queue_config),
base::BindOnce(&ReportQueueBuilder::OnReportQueueResult,
base::Unretained(this)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ namespace policy {
// logs and the policy system which uploads them to the management server.
class ExtensionInstallEventLogUploader : public InstallEventLogUploaderBase {
public:
// Result of trying to build a |ReportQueueConfiguration|.
using ReportQueueConfigResult = ::reporting::StatusOr<
std::unique_ptr<::reporting::ReportQueueConfiguration>>;

// Callback for handling a |ReportQueueConfigResult|.
using ReportQueueConfigResultCallback =
base::OnceCallback<void(ReportQueueConfigResult)>;

// Callback for getting a |ReportQueueConfiguration|.
using GetReportQueueConfigCallback =
base::RepeatingCallback<void(ReportQueueConfigResultCallback)>;

// The delegate that event logs will be retrieved from.
class Delegate {
public:
Expand Down Expand Up @@ -71,15 +83,14 @@ class ExtensionInstallEventLogUploader : public InstallEventLogUploaderBase {
// 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.
// TODO: For testing there ideally there would be a way to capture the
// ReportQueueConfiguration prior to passing it to the ReportQueue.
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,
Expand All @@ -96,11 +107,18 @@ class ExtensionInstallEventLogUploader : public InstallEventLogUploaderBase {
// 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();
// Posts the task for building the ReportQueueConfiguration used to
// instantiate the ReportQueue.
void BuildReportQueueConfig();

// Handles the result of the task posted in |BuildReportQueueConfig|.
void OnReportQueueConfigResult(ReportQueueConfigResult report_queue_result);

// |BuildReportQueue| uses the |report_queue_config| to build a ReportQueue
// with ReportClient::CreateReportQueue. Sets |OnReportQueueResult| as the
// completion callback for |CreateReportQueue|.
void BuildReportQueue(std::unique_ptr<::reporting::ReportQueueConfiguration>
report_queue_config);

// |OnReportQueueResult| will evaluate |report_queue_result|. If it is not
// an OK status, it exits the builder with a |Complete| call. On an OK
Expand Down Expand Up @@ -163,9 +181,7 @@ class ExtensionInstallEventLogUploader : public InstallEventLogUploaderBase {
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_;
GetReportQueueConfigCallback get_report_queue_config_cb_;

// ReportQueue for uploading events.
std::unique_ptr<reporting::ReportQueue> report_queue_;
Expand Down

0 comments on commit 0ae9dc1

Please sign in to comment.