Skip to content

Commit

Permalink
Reland "CCaaS private API."
Browse files Browse the repository at this point in the history
This is a reland of commit f409dca.
Fixes MemorySanitizer use-of-uninitialized-value by initializng
`profile_is_affiliated_for_testing_`. Since this fix exposed bugs in the browser test, the browser test will be included in a subsequent change.

Original change's description:
> CCaaS private API.
>
> Add API function which will be called by the CCaaS extension to enqueue
> records to the reporting pipeline via the Missive Client.
>
> DD: go/ccaas-extension-api
>
> Change-Id: I18091c124603a715ce262df94ab4de40d66fd399
> Bug: b:216327466
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3556512
> Reviewed-by: Vignesh Shenvi <vshenvi@google.com>
> Commit-Queue: Josh Hilke <jrhilke@google.com>
> Reviewed-by: Julian Pastarmov <pastarmovj@chromium.org>
> Reviewed-by: Devlin Cronin <rdevlin.cronin@chromium.org>
> Cr-Commit-Position: refs/heads/main@{#998033}

Bug: b:216327466
Change-Id: I66222016cb8b52fe4fe6737bfb39444c50b08fc4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3621442
Reviewed-by: Devlin Cronin <rdevlin.cronin@chromium.org>
Reviewed-by: Vignesh Shenvi <vshenvi@google.com>
Commit-Queue: Josh Hilke <jrhilke@google.com>
Reviewed-by: Julian Pastarmov <pastarmovj@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1001932}
  • Loading branch information
Josh Hilke authored and Chromium LUCI CQ committed May 11, 2022
1 parent 555a0e5 commit c45121b
Show file tree
Hide file tree
Showing 7 changed files with 395 additions and 1 deletion.
1 change: 1 addition & 0 deletions chrome/browser/extensions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,7 @@ static_library("extensions") {
"//chromeos/components/quick_answers/public/cpp:prefs",
"//chromeos/crosapi/cpp",
"//chromeos/crosapi/mojom",
"//chromeos/dbus/missive:missive",
"//remoting/host/it2me:chrome_os_host",
]
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,20 @@
#include "chrome/browser/enterprise/connectors/connectors_service.h"
#include "chrome/browser/enterprise/signals/device_info_fetcher.h"
#include "chrome/browser/enterprise/signals/signals_common.h"
#include "chrome/browser/enterprise/util/affiliation.h"
#include "chrome/browser/enterprise/util/managed_browser_utils.h"
#include "chrome/browser/profiles/profile.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/policy/dm_token_utils.h"
#include "chromeos/dbus/missive/missive_client.h"
#include "components/policy/core/common/cloud/dm_token.h"
#include "components/reporting/client/report_queue_configuration.h"
#include "components/reporting/proto/synced/record.pb.h"
#include "components/reporting/proto/synced/record_constants.pb.h"
#include "components/reporting/util/statusor.h"
#endif

#include "components/content_settings/core/common/pref_names.h"
#include "components/enterprise/browser/controller/browser_dm_token_storage.h"
#include "net/cert/x509_util.h"
Expand Down Expand Up @@ -471,4 +484,123 @@ void EnterpriseReportingPrivateGetCertificateFunction::OnClientCertFetched(
Respond(OneArgument(base::Value::FromUniquePtrValue(ret.ToValue())));
}

#if BUILDFLAG(IS_CHROMEOS)

// enqueueRecord

EnterpriseReportingPrivateEnqueueRecordFunction::
EnterpriseReportingPrivateEnqueueRecordFunction() = default;

EnterpriseReportingPrivateEnqueueRecordFunction::
~EnterpriseReportingPrivateEnqueueRecordFunction() = default;

ExtensionFunction::ResponseAction
EnterpriseReportingPrivateEnqueueRecordFunction::Run() {
auto* profile = Profile::FromBrowserContext(browser_context());
DCHECK(profile);

if (!IsProfileAffiliated(profile)) {
return RespondNow(Error(kErrorProfileNotAffiliated));
}

std::unique_ptr<api::enterprise_reporting_private::EnqueueRecord::Params>
params(api::enterprise_reporting_private::EnqueueRecord::Params::Create(
args()));
EXTENSION_FUNCTION_VALIDATE(params.get());

// Parse params
const auto event_type = params->request.event_type;
::reporting::Record record;
::reporting::Priority priority;
if (!TryParseParams(std::move(params), record, priority)) {
return RespondNow(Error(kErrorInvalidEnqueueRecordRequest));
}

// Attach appropriate DM token to record
if (!TryAttachDMTokenToRecord(record, event_type)) {
return RespondNow(Error(kErrorCannotAssociateRecordWithUser));
}

// Initiate enqueue and subsequent upload
auto enqueue_completion_cb = base::BindOnce(
&EnterpriseReportingPrivateEnqueueRecordFunction::OnRecordEnqueued, this);
auto* reporting_client = ::chromeos::MissiveClient::Get();
DCHECK(reporting_client);
reporting_client->EnqueueRecord(priority, record,
std::move(enqueue_completion_cb));
return RespondLater();
}

bool EnterpriseReportingPrivateEnqueueRecordFunction::TryParseParams(
std::unique_ptr<api::enterprise_reporting_private::EnqueueRecord::Params>
params,
::reporting::Record& record,
::reporting::Priority& priority) {
if (params->request.record_data.empty()) {
return false;
}

const auto* record_data =
reinterpret_cast<const char*>(params->request.record_data.data());
if (!record.ParseFromArray(record_data, params->request.record_data.size())) {
// Invalid record payload
return false;
}

if (!::reporting::Priority_IsValid(params->request.priority) ||
!::reporting::Priority_Parse(
::reporting::Priority_Name(params->request.priority), &priority)) {
// Invalid priority
return false;
}

// Valid
return true;
}

bool EnterpriseReportingPrivateEnqueueRecordFunction::TryAttachDMTokenToRecord(
::reporting::Record& record,
api::enterprise_reporting_private::EventType event_type) {
if (event_type ==
api::enterprise_reporting_private::EventType::EVENT_TYPE_DEVICE) {
// Device DM tokens are automatically appended during uploads, so we need
// not specify them with the record.
return true;
}

auto* profile = Profile::FromBrowserContext(browser_context());

const policy::DMToken& dm_token = policy::GetDMToken(profile);
if (!dm_token.is_valid()) {
return false;
}

record.set_dm_token(dm_token.value());
return true;
}

void EnterpriseReportingPrivateEnqueueRecordFunction::OnRecordEnqueued(
::reporting::Status result) {
if (!result.ok()) {
Respond(Error(kUnexpectedErrorEnqueueRecordRequest));
return;
}

Respond(NoArguments());
}

bool EnterpriseReportingPrivateEnqueueRecordFunction::IsProfileAffiliated(
Profile* profile) {
if (profile_is_affiliated_for_testing_) {
return true;
}
return chrome::enterprise_util::IsProfileAffiliated(profile);
}

void EnterpriseReportingPrivateEnqueueRecordFunction::
SetProfileIsAffiliatedForTesting(bool is_affiliated) {
profile_is_affiliated_for_testing_ = is_affiliated;
}
#endif

} // namespace extensions
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
#include "chrome/browser/enterprise/signals/device_info_fetcher.h"
#include "chrome/browser/extensions/api/enterprise_reporting_private/chrome_desktop_report_request_helper.h"
#include "chrome/common/extensions/api/enterprise_reporting_private.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "components/reporting/proto/synced/record.pb.h"
#include "components/reporting/proto/synced/record_constants.pb.h"
#include "components/reporting/util/statusor.h"
#endif

#include "extensions/browser/extension_function.h"

namespace extensions {
Expand Down Expand Up @@ -204,6 +211,57 @@ class EnterpriseReportingPrivateGetCertificateFunction
client_cert_fetcher_;
};

#if BUILDFLAG(IS_CHROMEOS)

class EnterpriseReportingPrivateEnqueueRecordFunction
: public ExtensionFunction {
public:
inline static constexpr char kErrorInvalidEnqueueRecordRequest[] =
"Invalid request";
inline static constexpr char kUnexpectedErrorEnqueueRecordRequest[] =
"Encountered unexpected error while enqueuing record";
inline static constexpr char kErrorProfileNotAffiliated[] =
"Profile is not affiliated";
inline static constexpr char kErrorCannotAssociateRecordWithUser[] =
"Cannot associate record with user";

DECLARE_EXTENSION_FUNCTION("enterprise.reportingPrivate.enqueueRecord",
ENTERPRISEREPORTINGPRIVATE_ENQUEUERECORD)

EnterpriseReportingPrivateEnqueueRecordFunction();
EnterpriseReportingPrivateEnqueueRecordFunction(
const EnterpriseReportingPrivateEnqueueRecordFunction&) = delete;
EnterpriseReportingPrivateEnqueueRecordFunction& operator=(
const EnterpriseReportingPrivateEnqueueRecordFunction&) = delete;

void SetProfileIsAffiliatedForTesting(bool is_affiliated);

private:
~EnterpriseReportingPrivateEnqueueRecordFunction() override;

// ExtensionFunction:
ExtensionFunction::ResponseAction Run() override;

bool TryParseParams(
std::unique_ptr<api::enterprise_reporting_private::EnqueueRecord::Params>
params,
::reporting::Record& record,
::reporting::Priority& priority);

bool TryAttachDMTokenToRecord(
::reporting::Record& record,
api::enterprise_reporting_private::EventType event_type);

// Callback invoked after the record was successfully enqueued
void OnRecordEnqueued(::reporting::Status result);

bool IsProfileAffiliated(Profile* profile);

bool profile_is_affiliated_for_testing_ = false;
};

#endif

} // namespace extensions

#endif // CHROME_BROWSER_EXTENSIONS_API_ENTERPRISE_REPORTING_PRIVATE_ENTERPRISE_REPORTING_PRIVATE_API_H_

0 comments on commit c45121b

Please sign in to comment.