Commit 503df9e6 authored by Andrei-Laurențiu Olteanu's avatar Andrei-Laurențiu Olteanu Committed by Commit Bot

[Telemetry SWX] Add lid events

Create and make available mojo interface for chrome://.

Add implementation in chrome://.

Add implementation in chrome-untrusted://.

Add implementation for cros_healthd fake onLidOpened event emitter.

Add browser test for lid event listener.

Bug: b:167523716
Change-Id: I4281778d451264f57154913330d8cd5ba83cb4f8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2389981Reviewed-by: default avatarTom Sepez <tsepez@chromium.org>
Reviewed-by: default avatarMaksim Ivanov <emaxx@chromium.org>
Reviewed-by: default avatarOleh Lamzin <lamzin@google.com>
Reviewed-by: default avatarMahmoud Gawad <mgawad@google.com>
Reviewed-by: default avatarGiovanni Ortuño Urquidi <ortuno@chromium.org>
Commit-Queue: Laurențiu Olteanu <lolteanu@google.com>
Cr-Commit-Position: refs/heads/master@{#812623}
parent e8290cd3
...@@ -196,6 +196,7 @@ ...@@ -196,6 +196,7 @@
#include "chromeos/components/file_manager/file_manager_ui.h" #include "chromeos/components/file_manager/file_manager_ui.h"
#include "chromeos/components/telemetry_extension_ui/mojom/diagnostics_service.mojom.h" // nogncheck crbug.com/1125897 #include "chromeos/components/telemetry_extension_ui/mojom/diagnostics_service.mojom.h" // nogncheck crbug.com/1125897
#include "chromeos/components/telemetry_extension_ui/mojom/probe_service.mojom.h" // nogncheck crbug.com/1125897 #include "chromeos/components/telemetry_extension_ui/mojom/probe_service.mojom.h" // nogncheck crbug.com/1125897
#include "chromeos/components/telemetry_extension_ui/mojom/system_events_service.mojom.h"
#include "chromeos/components/telemetry_extension_ui/telemetry_extension_ui.h" #include "chromeos/components/telemetry_extension_ui/telemetry_extension_ui.h"
#endif #endif
...@@ -726,6 +727,9 @@ void PopulateChromeWebUIFrameBinders( ...@@ -726,6 +727,9 @@ void PopulateChromeWebUIFrameBinders(
RegisterWebUIControllerInterfaceBinder< RegisterWebUIControllerInterfaceBinder<
chromeos::health::mojom::ProbeService, chromeos::TelemetryExtensionUI>( chromeos::health::mojom::ProbeService, chromeos::TelemetryExtensionUI>(
map); map);
RegisterWebUIControllerInterfaceBinder<
chromeos::health::mojom::SystemEventsService,
chromeos::TelemetryExtensionUI>(map);
} }
#endif #endif
......
...@@ -13,10 +13,14 @@ source_set("telemetry_extension_ui") { ...@@ -13,10 +13,14 @@ source_set("telemetry_extension_ui") {
"diagnostics_service.h", "diagnostics_service.h",
"diagnostics_service_converters.cc", "diagnostics_service_converters.cc",
"diagnostics_service_converters.h", "diagnostics_service_converters.h",
"lid_observer.cc",
"lid_observer.h",
"probe_service.cc", "probe_service.cc",
"probe_service.h", "probe_service.h",
"probe_service_converters.cc", "probe_service_converters.cc",
"probe_service_converters.h", "probe_service_converters.h",
"system_events_service.cc",
"system_events_service.h",
"telemetry_extension_ui.cc", "telemetry_extension_ui.cc",
"telemetry_extension_ui.h", "telemetry_extension_ui.h",
"telemetry_extension_untrusted_source.cc", "telemetry_extension_untrusted_source.cc",
...@@ -46,6 +50,7 @@ source_set("unit_tests") { ...@@ -46,6 +50,7 @@ source_set("unit_tests") {
"diagnostics_service_converters_unittest.cc", "diagnostics_service_converters_unittest.cc",
"probe_service_converters_unittest.cc", "probe_service_converters_unittest.cc",
"probe_service_unittest.cc", "probe_service_unittest.cc",
"system_events_service_unittest.cc",
] ]
deps = [ deps = [
":telemetry_extension_ui", ":telemetry_extension_ui",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/components/telemetry_extension_ui/lid_observer.h"
#include <utility>
#include "base/bind.h"
#include "chromeos/components/telemetry_extension_ui/mojom/system_events_service.mojom.h"
#include "chromeos/services/cros_healthd/public/cpp/service_connection.h"
namespace chromeos {
LidObserver::LidObserver() : receiver_{this} {
Connect();
}
LidObserver::~LidObserver() = default;
void LidObserver::AddObserver(
mojo::PendingRemote<health::mojom::LidObserver> observer) {
health::mojom::LidObserverPtr ptr{std::move(observer)};
observers_.Add(ptr.PassInterface());
}
void LidObserver::OnLidClosed() {
for (auto& observer : observers_) {
observer->OnLidClosed();
}
}
void LidObserver::OnLidOpened() {
for (auto& observer : observers_) {
observer->OnLidOpened();
}
}
void LidObserver::Connect() {
receiver_.reset();
cros_healthd::ServiceConnection::GetInstance()->AddLidObserver(
receiver_.BindNewPipeAndPassRemote());
// We try to reconnect right after disconnect because Mojo will queue the
// request and connect to cros_healthd when it becomes available.
receiver_.set_disconnect_handler(
base::BindOnce(&LidObserver::Connect, base::Unretained(this)));
}
void LidObserver::FlushForTesting() {
receiver_.FlushForTesting();
}
} // namespace chromeos
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_COMPONENTS_TELEMETRY_EXTENSION_UI_LID_OBSERVER_H_
#define CHROMEOS_COMPONENTS_TELEMETRY_EXTENSION_UI_LID_OBSERVER_H_
#if defined(OFFICIAL_BUILD)
#error Lid observer should only be included in unofficial builds.
#endif
#include "chromeos/components/telemetry_extension_ui/mojom/system_events_service.mojom-forward.h"
#include "chromeos/services/cros_healthd/public/mojom/cros_healthd_events.mojom.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote_set.h"
namespace chromeos {
class LidObserver : public cros_healthd::mojom::CrosHealthdLidObserver {
public:
LidObserver();
LidObserver(const LidObserver&) = delete;
LidObserver& operator=(const LidObserver&) = delete;
~LidObserver() override;
void AddObserver(mojo::PendingRemote<health::mojom::LidObserver> observer);
void OnLidClosed() override;
void OnLidOpened() override;
// Waits until disconnect handler will be triggered if fake cros_healthd was
// shutdown.
void FlushForTesting();
private:
void Connect();
mojo::Receiver<cros_healthd::mojom::CrosHealthdLidObserver> receiver_;
mojo::RemoteSet<health::mojom::LidObserver> observers_;
};
} // namespace chromeos
#endif // CHROMEOS_COMPONENTS_TELEMETRY_EXTENSION_UI_LID_OBSERVER_H_
...@@ -8,5 +8,6 @@ mojom("mojom") { ...@@ -8,5 +8,6 @@ mojom("mojom") {
sources = [ sources = [
"diagnostics_service.mojom", "diagnostics_service.mojom",
"probe_service.mojom", "probe_service.mojom",
"system_events_service.mojom",
] ]
} }
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Definitions for the System Events API exposed by the telemetry
// system web extension.
// This API is consumed by chrome://.
//
// This Mojo interface will go through security review before shipping.
//
// This is a subset of the cros_healthd event service interface which is
// src/chrome/services/cros_healthd/public/mojom/cros_healthd.mojom and
// src/chrome/services/cros_healthd/public/mojom/cros_healthd_events.mojom.
module chromeos.health.mojom;
// System Events interface exposed by the Telemetry SWX.
interface SystemEventsService {
// Adds an observer to be notified on lid events. The caller can remove the
// observer created by this call by closing their end of the message pipe.
//
// The request:
// * |observer| - lid observer to be added to system events service.
AddLidObserver(pending_remote<LidObserver> observer);
};
// Implemented by clients who desire lid notifications.
interface LidObserver {
// Fired when the device's lid is closed.
OnLidClosed();
// Fired when the device's lid is opened.
OnLidOpened();
};
...@@ -11,4 +11,5 @@ ...@@ -11,4 +11,5 @@
<script src="diagnostics_service.mojom-lite.js"></script> <script src="diagnostics_service.mojom-lite.js"></script>
<script src="probe_service.mojom-lite.js"></script> <script src="probe_service.mojom-lite.js"></script>
<script src="system_events_service.mojom-lite.js"></script>
<script src="trusted_scripts.js"></script> <script src="trusted_scripts.js"></script>
...@@ -43,6 +43,7 @@ dpsl_internal.Message = { ...@@ -43,6 +43,7 @@ dpsl_internal.Message = {
DIAGNOSTICS_RUN_BATTERY_CHARGE_ROUTINE: DIAGNOSTICS_RUN_BATTERY_CHARGE_ROUTINE:
'DiagnosticsService.RunBatteryChargeRoutine', 'DiagnosticsService.RunBatteryChargeRoutine',
PROBE_TELEMETRY_INFO: 'ProbeService.ProbeTelemetryInfo', PROBE_TELEMETRY_INFO: 'ProbeService.ProbeTelemetryInfo',
SYSTEM_EVENTS_SERVICE_EVENTS: 'SystemEventsService.Events',
}; };
/** /**
...@@ -189,6 +190,12 @@ dpsl_internal.DiagnosticsRunBatteryChargeRoutineRequest; ...@@ -189,6 +190,12 @@ dpsl_internal.DiagnosticsRunBatteryChargeRoutineRequest;
*/ */
dpsl_internal.DiagnosticsRunRoutineResponse; dpsl_internal.DiagnosticsRunRoutineResponse;
/**
* Event response from System Events Service.
* @typedef {{ type: !string }}
*/
dpsl_internal.Event;
/** /**
* Request message sent by the unprivileged context to request the privileged * Request message sent by the unprivileged context to request the privileged
* context to probe telemetry information * context to probe telemetry information
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
<include name="IDR_TELEMETRY_EXTENSION_TRUSTED_SCRIPTS_JS" file="trusted_scripts.js" flattenhtml="true" type="BINDATA" compress="gzip" /> <include name="IDR_TELEMETRY_EXTENSION_TRUSTED_SCRIPTS_JS" file="trusted_scripts.js" flattenhtml="true" type="BINDATA" compress="gzip" />
<include name="IDR_TELEMETRY_EXTENSION_DIAGNOSTICS_SERVICE_MOJO_LITE_JS" file="${root_gen_dir}/chromeos/components/telemetry_extension_ui/mojom/diagnostics_service.mojom-lite.js" compress="gzip" use_base_dir="false" type="BINDATA" /> <include name="IDR_TELEMETRY_EXTENSION_DIAGNOSTICS_SERVICE_MOJO_LITE_JS" file="${root_gen_dir}/chromeos/components/telemetry_extension_ui/mojom/diagnostics_service.mojom-lite.js" compress="gzip" use_base_dir="false" type="BINDATA" />
<include name="IDR_TELEMETRY_EXTENSION_PROBE_SERVICE_MOJO_LITE_JS" file="${root_gen_dir}/chromeos/components/telemetry_extension_ui/mojom/probe_service.mojom-lite.js" compress="gzip" use_base_dir="false" type="BINDATA" /> <include name="IDR_TELEMETRY_EXTENSION_PROBE_SERVICE_MOJO_LITE_JS" file="${root_gen_dir}/chromeos/components/telemetry_extension_ui/mojom/probe_service.mojom-lite.js" compress="gzip" use_base_dir="false" type="BINDATA" />
<include name="IDR_TELEMETRY_EXTENSION_SYSTEM_EVENTS_SERVICE_MOJO_LITE_JS" file="${root_gen_dir}/chromeos/components/telemetry_extension_ui/mojom/system_events_service.mojom-lite.js" compress="gzip" use_base_dir="false" type="BINDATA" />
<!-- Untrusted app host contents. --> <!-- Untrusted app host contents. -->
<include name="IDR_TELEMETRY_EXTENSION_DPSL_JS" file="dpsl.js" flattenhtml="true" type="BINDATA" compress="gzip" /> <include name="IDR_TELEMETRY_EXTENSION_DPSL_JS" file="dpsl.js" flattenhtml="true" type="BINDATA" compress="gzip" />
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
let diagnosticsService = null; let diagnosticsService = null;
let probeService = null; let probeService = null;
let systemEventsService = null;
/** /**
* Lazy creates pointer to remote implementation of diagnostics service. * Lazy creates pointer to remote implementation of diagnostics service.
...@@ -29,6 +30,18 @@ function getOrCreateProbeService() { ...@@ -29,6 +30,18 @@ function getOrCreateProbeService() {
probeService); probeService);
} }
/**
* Lazy creates pointer to remote implementation of system events service.
* @return {!chromeos.health.mojom.SystemEventsServiceRemote}
*/
function getOrCreateSystemEventsService() {
if (systemEventsService === null) {
systemEventsService = chromeos.health.mojom.SystemEventsService.getRemote();
}
return /** @type {!chromeos.health.mojom.SystemEventsServiceRemote} */ (
systemEventsService);
}
/** /**
* Alias for Mojo RunRoutine response. * Alias for Mojo RunRoutine response.
* @typedef { !Promise<{response: !chromeos.health.mojom.RunRoutineResponse}> * @typedef { !Promise<{response: !chromeos.health.mojom.RunRoutineResponse}>
...@@ -852,6 +865,50 @@ class TelemetryProxy { ...@@ -852,6 +865,50 @@ class TelemetryProxy {
const telemetryProxy = new TelemetryProxy(); const telemetryProxy = new TelemetryProxy();
/**
* Proxying event requests between SystemEventsRequester on
* chrome-untrusted:// side with WebIDL types and SystemEventsService on
* chrome:// side with Mojo types.
*/
class SystemEventsProxy {
/**
* @param {!MessagePipe} messagePipe
* @param {!Promise<undefined>} iframeReady
*/
constructor(messagePipe, iframeReady) {
this.messagePipe = messagePipe;
this.iframeReady = iframeReady;
this.events = {
ON_LID_CLOSED: 'lid-closed',
ON_LID_OPENED: 'lid-opened',
};
this.lidObserverCallbackRouter_ =
new chromeos.health.mojom.LidObserverCallbackRouter();
this.lidObserverCallbackRouter_.onLidClosed.addListener(
() => this.sendEvent(this.events.ON_LID_CLOSED));
this.lidObserverCallbackRouter_.onLidOpened.addListener(
() => this.sendEvent(this.events.ON_LID_OPENED));
getOrCreateSystemEventsService().addLidObserver(
this.lidObserverCallbackRouter_.$.bindNewPipeAndPassRemote());
}
/**
* Sends event type to chrome-untrusted://.
* @param {!string} type
*/
async sendEvent(type) {
/* Prevent events from being sent until untrusted iframe is loaded. */
await this.iframeReady;
this.messagePipe.sendMessage(
dpsl_internal.Message.SYSTEM_EVENTS_SERVICE_EVENTS,
/** @type {!dpsl_internal.Event} */ ({type: type}));
}
}
const untrustedMessagePipe = const untrustedMessagePipe =
new MessagePipe('chrome-untrusted://telemetry-extension'); new MessagePipe('chrome-untrusted://telemetry-extension');
...@@ -956,3 +1013,6 @@ const iframeReady = new Promise(resolve => { ...@@ -956,3 +1013,6 @@ const iframeReady = new Promise(resolve => {
resolve(); resolve();
}); });
}); });
const systemEventsProxy =
new SystemEventsProxy(untrustedMessagePipe, iframeReady);
...@@ -370,9 +370,17 @@ chromeos.test_support = {}; ...@@ -370,9 +370,17 @@ chromeos.test_support = {};
/** /**
* DPSL Telemetry Requester. * DPSL Telemetry Requester.
* @suppress {checkTypes}
*/ */
class TelemetryRequester { class TelemetryRequester extends EventTarget {
constructor() {} constructor() {
super();
messagePipe.registerHandler(
dpsl_internal.Message.SYSTEM_EVENTS_SERVICE_EVENTS, (message) => {
const event = /** @type {!dpsl_internal.Event} */ (message);
this.dispatchEvent(new Event(event.type));
});
}
/** /**
* Requests telemetry info. * Requests telemetry info.
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/components/telemetry_extension_ui/system_events_service.h"
#include <utility>
#include "mojo/public/cpp/bindings/pending_remote.h"
namespace chromeos {
SystemEventsService::SystemEventsService(
mojo::PendingReceiver<health::mojom::SystemEventsService> receiver)
: receiver_(this, std::move(receiver)) {}
SystemEventsService::~SystemEventsService() = default;
void SystemEventsService::AddLidObserver(
mojo::PendingRemote<health::mojom::LidObserver> observer) {
lid_observer_.AddObserver(std::move(observer));
}
void SystemEventsService::FlushForTesting() {
lid_observer_.FlushForTesting();
}
} // namespace chromeos
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_COMPONENTS_TELEMETRY_EXTENSION_UI_SYSTEM_EVENTS_SERVICE_H_
#define CHROMEOS_COMPONENTS_TELEMETRY_EXTENSION_UI_SYSTEM_EVENTS_SERVICE_H_
#if defined(OFFICIAL_BUILD)
#error System events service should only be included in unofficial builds.
#endif
#include "chromeos/components/telemetry_extension_ui/lid_observer.h"
#include "chromeos/components/telemetry_extension_ui/mojom/system_events_service.mojom.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
namespace chromeos {
class SystemEventsService : public health::mojom::SystemEventsService {
public:
explicit SystemEventsService(
mojo::PendingReceiver<health::mojom::SystemEventsService> receiver);
SystemEventsService(const SystemEventsService&) = delete;
SystemEventsService& operator=(const SystemEventsService&) = delete;
~SystemEventsService() override;
void AddLidObserver(
mojo::PendingRemote<health::mojom::LidObserver> observer) override;
void FlushForTesting();
private:
mojo::Receiver<health::mojom::SystemEventsService> receiver_;
LidObserver lid_observer_;
};
} // namespace chromeos
#endif // CHROMEOS_COMPONENTS_TELEMETRY_EXTENSION_UI_SYSTEM_EVENTS_SERVICE_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#if defined(OFFICIAL_BUILD)
#error System events service unit tests should only be included in unofficial builds.
#endif
#include "chromeos/components/telemetry_extension_ui/system_events_service.h"
#include <memory>
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "chromeos/components/telemetry_extension_ui/mojom/system_events_service.mojom.h"
#include "chromeos/dbus/cros_healthd/cros_healthd_client.h"
#include "chromeos/dbus/cros_healthd/fake_cros_healthd_client.h"
#include "chromeos/services/cros_healthd/public/cpp/service_connection.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace {
class MockLidObserver : public health::mojom::LidObserver {
public:
MockLidObserver() : receiver_{this} {}
MockLidObserver(const MockLidObserver&) = delete;
MockLidObserver& operator=(const MockLidObserver&) = delete;
mojo::PendingRemote<health::mojom::LidObserver> pending_remote() {
return receiver_.BindNewPipeAndPassRemote();
}
MOCK_METHOD(void, OnLidClosed, (), (override));
MOCK_METHOD(void, OnLidOpened, (), (override));
private:
mojo::Receiver<health::mojom::LidObserver> receiver_;
};
} // namespace
class SystemEventsServiceTest : public testing::Test {
public:
SystemEventsServiceTest() {
CrosHealthdClient::InitializeFake();
system_events_service_ = std::make_unique<SystemEventsService>(
remote_system_events_service_.BindNewPipeAndPassReceiver());
mock_lid_observer_ =
std::make_unique<testing::StrictMock<MockLidObserver>>();
// Force other tasks to be processed.
cros_healthd::ServiceConnection::GetInstance()->FlushForTesting();
}
~SystemEventsServiceTest() override {
CrosHealthdClient::Shutdown();
cros_healthd::ServiceConnection::GetInstance()->FlushForTesting();
}
health::mojom::SystemEventsServiceProxy* remote_system_events_service()
const {
return remote_system_events_service_.get();
}
SystemEventsService* system_events_service() const {
return system_events_service_.get();
}
mojo::PendingRemote<health::mojom::LidObserver> lid_observer() const {
return mock_lid_observer_->pending_remote();
}
MockLidObserver* mock_lid_observer() const {
return mock_lid_observer_.get();
}
private:
base::test::TaskEnvironment task_environment_;
mojo::Remote<health::mojom::SystemEventsService>
remote_system_events_service_;
std::unique_ptr<SystemEventsService> system_events_service_;
std::unique_ptr<testing::StrictMock<MockLidObserver>> mock_lid_observer_;
};
// Tests that in case of cros_healthd crash Lid Observer will reconnect.
TEST_F(SystemEventsServiceTest, LidObserverReconnect) {
remote_system_events_service()->AddLidObserver(lid_observer());
base::RunLoop run_loop1;
EXPECT_CALL(*mock_lid_observer(), OnLidClosed).WillOnce([&run_loop1]() {
run_loop1.Quit();
});
cros_healthd::FakeCrosHealthdClient::Get()->EmitLidClosedEventForTesting();
run_loop1.Run();
// Shutdown cros_healthd to simulate crash.
CrosHealthdClient::Shutdown();
// Ensure ServiceConnection is disconnected from cros_healthd.
cros_healthd::ServiceConnection::GetInstance()->FlushForTesting();
// Restart cros_healthd.
CrosHealthdClient::InitializeFake();
// Ensure disconnect handler is called for lid observer from System Event
// Service. After this call, we will have a Mojo pending connection task in
// Mojo message queue.
system_events_service()->FlushForTesting();
// Ensure that Mojo pending connection task from lid observer gets processed
// and observer is bound. After this call, we are sure that lid observer
// reconnected and we can safely emit events.
cros_healthd::ServiceConnection::GetInstance()->FlushForTesting();
base::RunLoop run_loop2;
EXPECT_CALL(*mock_lid_observer(), OnLidClosed).WillOnce([&run_loop2]() {
run_loop2.Quit();
});
cros_healthd::FakeCrosHealthdClient::Get()->EmitLidClosedEventForTesting();
run_loop2.Run();
}
} // namespace chromeos
...@@ -10,7 +10,9 @@ ...@@ -10,7 +10,9 @@
#include "chromeos/components/telemetry_extension_ui/diagnostics_service.h" #include "chromeos/components/telemetry_extension_ui/diagnostics_service.h"
#include "chromeos/components/telemetry_extension_ui/mojom/diagnostics_service.mojom.h" #include "chromeos/components/telemetry_extension_ui/mojom/diagnostics_service.mojom.h"
#include "chromeos/components/telemetry_extension_ui/mojom/probe_service.mojom.h" #include "chromeos/components/telemetry_extension_ui/mojom/probe_service.mojom.h"
#include "chromeos/components/telemetry_extension_ui/mojom/system_events_service.mojom.h"
#include "chromeos/components/telemetry_extension_ui/probe_service.h" #include "chromeos/components/telemetry_extension_ui/probe_service.h"
#include "chromeos/components/telemetry_extension_ui/system_events_service.h"
#include "chromeos/components/telemetry_extension_ui/telemetry_extension_untrusted_source.h" #include "chromeos/components/telemetry_extension_ui/telemetry_extension_untrusted_source.h"
#include "chromeos/components/telemetry_extension_ui/url_constants.h" #include "chromeos/components/telemetry_extension_ui/url_constants.h"
#include "chromeos/grit/chromeos_telemetry_extension_resources.h" #include "chromeos/grit/chromeos_telemetry_extension_resources.h"
...@@ -40,6 +42,9 @@ CreateTrustedTelemetryExtensionDataSource() { ...@@ -40,6 +42,9 @@ CreateTrustedTelemetryExtensionDataSource() {
trusted_source->AddResourcePath( trusted_source->AddResourcePath(
"probe_service.mojom-lite.js", "probe_service.mojom-lite.js",
IDR_TELEMETRY_EXTENSION_PROBE_SERVICE_MOJO_LITE_JS); IDR_TELEMETRY_EXTENSION_PROBE_SERVICE_MOJO_LITE_JS);
trusted_source->AddResourcePath(
"system_events_service.mojom-lite.js",
IDR_TELEMETRY_EXTENSION_SYSTEM_EVENTS_SERVICE_MOJO_LITE_JS);
#if !DCHECK_IS_ON() #if !DCHECK_IS_ON()
// If a user goes to an invalid url and non-DCHECK mode (DHECK = debug mode) // If a user goes to an invalid url and non-DCHECK mode (DHECK = debug mode)
...@@ -106,6 +111,12 @@ void TelemetryExtensionUI::BindInterface( ...@@ -106,6 +111,12 @@ void TelemetryExtensionUI::BindInterface(
probe_service_ = std::make_unique<ProbeService>(std::move(receiver)); probe_service_ = std::make_unique<ProbeService>(std::move(receiver));
} }
void TelemetryExtensionUI::BindInterface(
mojo::PendingReceiver<health::mojom::SystemEventsService> receiver) {
system_events_service_ =
std::make_unique<SystemEventsService>(std::move(receiver));
}
WEB_UI_CONTROLLER_TYPE_IMPL(TelemetryExtensionUI) WEB_UI_CONTROLLER_TYPE_IMPL(TelemetryExtensionUI)
} // namespace chromeos } // namespace chromeos
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "chromeos/components/telemetry_extension_ui/mojom/diagnostics_service.mojom-forward.h" #include "chromeos/components/telemetry_extension_ui/mojom/diagnostics_service.mojom-forward.h"
#include "chromeos/components/telemetry_extension_ui/mojom/probe_service.mojom-forward.h" #include "chromeos/components/telemetry_extension_ui/mojom/probe_service.mojom-forward.h"
#include "chromeos/components/telemetry_extension_ui/mojom/system_events_service.mojom-forward.h"
#include "mojo/public/cpp/bindings/pending_receiver.h" #include "mojo/public/cpp/bindings/pending_receiver.h"
#include "ui/webui/mojo_web_ui_controller.h" #include "ui/webui/mojo_web_ui_controller.h"
...@@ -32,10 +33,14 @@ class TelemetryExtensionUI : public ui::MojoWebUIController { ...@@ -32,10 +33,14 @@ class TelemetryExtensionUI : public ui::MojoWebUIController {
void BindInterface( void BindInterface(
mojo::PendingReceiver<health::mojom::ProbeService> receiver); mojo::PendingReceiver<health::mojom::ProbeService> receiver);
void BindInterface(
mojo::PendingReceiver<health::mojom::SystemEventsService> receiver);
private: private:
// Replaced when |BindInterface| is called. // Replaced when |BindInterface| is called.
std::unique_ptr<health::mojom::DiagnosticsService> diagnostics_service_; std::unique_ptr<health::mojom::DiagnosticsService> diagnostics_service_;
std::unique_ptr<health::mojom::ProbeService> probe_service_; std::unique_ptr<health::mojom::ProbeService> probe_service_;
std::unique_ptr<health::mojom::SystemEventsService> system_events_service_;
WEB_UI_CONTROLLER_TYPE_DECL(); WEB_UI_CONTROLLER_TYPE_DECL();
}; };
......
...@@ -5,8 +5,12 @@ ...@@ -5,8 +5,12 @@
#include "chromeos/components/telemetry_extension_ui/test/telemetry_extension_ui_browsertest.h" #include "chromeos/components/telemetry_extension_ui/test/telemetry_extension_ui_browsertest.h"
#include "base/base_paths.h" #include "base/base_paths.h"
#include "base/bind.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/location.h"
#include "base/path_service.h" #include "base/path_service.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "chrome/browser/chromeos/wilco_dtc_supportd/mojo_utils.h" #include "chrome/browser/chromeos/wilco_dtc_supportd/mojo_utils.h"
#include "chromeos/components/telemetry_extension_ui/url_constants.h" #include "chromeos/components/telemetry_extension_ui/url_constants.h"
#include "chromeos/components/web_applications/test/sandboxed_web_ui_test_base.h" #include "chromeos/components/web_applications/test/sandboxed_web_ui_test_base.h"
...@@ -288,6 +292,8 @@ void TelemetryExtensionUiBrowserTest::SetUpOnMainThread() { ...@@ -288,6 +292,8 @@ void TelemetryExtensionUiBrowserTest::SetUpOnMainThread() {
chromeos::cros_healthd::FakeCrosHealthdClient::Get() chromeos::cros_healthd::FakeCrosHealthdClient::Get()
->SetProbeTelemetryInfoResponseForTesting(telemetry_info); ->SetProbeTelemetryInfoResponseForTesting(telemetry_info);
ConfigureSystemEventsServiceToEmitEvents();
SandboxedWebUiAppTestBase::SetUpOnMainThread(); SandboxedWebUiAppTestBase::SetUpOnMainThread();
} }
...@@ -427,3 +433,18 @@ void TelemetryExtensionUiBrowserTest::ConfigureProbeServiceToReturnErrors() { ...@@ -427,3 +433,18 @@ void TelemetryExtensionUiBrowserTest::ConfigureProbeServiceToReturnErrors() {
chromeos::cros_healthd::FakeCrosHealthdClient::Get() chromeos::cros_healthd::FakeCrosHealthdClient::Get()
->SetProbeTelemetryInfoResponseForTesting(telemetry_info); ->SetProbeTelemetryInfoResponseForTesting(telemetry_info);
} }
void TelemetryExtensionUiBrowserTest::
ConfigureSystemEventsServiceToEmitEvents() {
chromeos::cros_healthd::FakeCrosHealthdClient::Get()
->EmitLidClosedEventForTesting();
chromeos::cros_healthd::FakeCrosHealthdClient::Get()
->EmitLidOpenedEventForTesting();
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&TelemetryExtensionUiBrowserTest::
ConfigureSystemEventsServiceToEmitEvents,
system_events_weak_ptr_factory_.GetWeakPtr()),
base::TimeDelta::FromSeconds(1));
}
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define CHROMEOS_COMPONENTS_TELEMETRY_EXTENSION_UI_TEST_TELEMETRY_EXTENSION_UI_BROWSERTEST_H_ #define CHROMEOS_COMPONENTS_TELEMETRY_EXTENSION_UI_TEST_TELEMETRY_EXTENSION_UI_BROWSERTEST_H_
#include "base/command_line.h" #include "base/command_line.h"
#include "base/memory/weak_ptr.h"
#include "chromeos/components/web_applications/test/sandboxed_web_ui_test_base.h" #include "chromeos/components/web_applications/test/sandboxed_web_ui_test_base.h"
class TelemetryExtensionUiBrowserTest : public SandboxedWebUiAppTestBase { class TelemetryExtensionUiBrowserTest : public SandboxedWebUiAppTestBase {
...@@ -26,6 +27,13 @@ class TelemetryExtensionUiBrowserTest : public SandboxedWebUiAppTestBase { ...@@ -26,6 +27,13 @@ class TelemetryExtensionUiBrowserTest : public SandboxedWebUiAppTestBase {
void ConfigureDiagnosticsForNonInteractiveUpdate(); void ConfigureDiagnosticsForNonInteractiveUpdate();
void ConfigureProbeServiceToReturnErrors(); void ConfigureProbeServiceToReturnErrors();
void ConfigureSystemEventsServiceToEmitEvents();
private:
// Use to post and cancel tasks for emitting system events.
base::WeakPtrFactory<TelemetryExtensionUiBrowserTest>
system_events_weak_ptr_factory_{this};
}; };
#endif // CHROMEOS_COMPONENTS_TELEMETRY_EXTENSION_UI_TEST_TELEMETRY_EXTENSION_UI_BROWSERTEST_H_ #endif // CHROMEOS_COMPONENTS_TELEMETRY_EXTENSION_UI_TEST_TELEMETRY_EXTENSION_UI_BROWSERTEST_H_
...@@ -408,6 +408,7 @@ const untrustedTests = [ ...@@ -408,6 +408,7 @@ const untrustedTests = [
['UntrustedDiagnosticsRequestRunBatteryDischargeRoutine'], ['UntrustedDiagnosticsRequestRunBatteryDischargeRoutine'],
['UntrustedDiagnosticsRequestRunBatteryChargeRoutineInvalidInput'], ['UntrustedDiagnosticsRequestRunBatteryChargeRoutineInvalidInput'],
['UntrustedDiagnosticsRequestRunBatteryChargeRoutine'], ['UntrustedDiagnosticsRequestRunBatteryChargeRoutine'],
['UntrustedLidEventListener'],
['UntrustedRequestTelemetryInfoUnknownCategory'], ['UntrustedRequestTelemetryInfoUnknownCategory'],
['UntrustedRequestTelemetryInfo'], ['UntrustedRequestTelemetryInfo'],
[ [
......
...@@ -441,6 +441,18 @@ UNTRUSTED_TEST( ...@@ -441,6 +441,18 @@ UNTRUSTED_TEST(
assertDeepEquals(response, {id: 123456789, status: 'ready'}); assertDeepEquals(response, {id: 123456789, status: 'ready'});
}); });
// Tests that addEventListener receives system lid events.
UNTRUSTED_TEST('UntrustedLidEventListener', async () => {
await Promise.all([
new Promise(
(resolve) =>
chromeos.telemetry.addEventListener('lid-closed', resolve)),
new Promise(
(resolve) =>
chromeos.telemetry.addEventListener('lid-opened', resolve)),
]);
});
// Tests that TelemetryInfo throws an error if category is unknown. // Tests that TelemetryInfo throws an error if category is unknown.
UNTRUSTED_TEST('UntrustedRequestTelemetryInfoUnknownCategory', async () => { UNTRUSTED_TEST('UntrustedRequestTelemetryInfoUnknownCategory', async () => {
let caughtError = {}; let caughtError = {};
......
...@@ -92,6 +92,13 @@ void FakeCrosHealthdClient::EmitLidClosedEventForTesting() { ...@@ -92,6 +92,13 @@ void FakeCrosHealthdClient::EmitLidClosedEventForTesting() {
fake_service_.EmitLidClosedEventForTesting(); fake_service_.EmitLidClosedEventForTesting();
} }
void FakeCrosHealthdClient::EmitLidOpenedEventForTesting() {
// Flush the receiver, so any pending observers are registered before the
// event is emitted.
receiver_.FlushForTesting();
fake_service_.EmitLidOpenedEventForTesting();
}
void FakeCrosHealthdClient::RequestNetworkHealthForTesting( void FakeCrosHealthdClient::RequestNetworkHealthForTesting(
chromeos::network_health::mojom::NetworkHealthService:: chromeos::network_health::mojom::NetworkHealthService::
GetHealthSnapshotCallback callback) { GetHealthSnapshotCallback callback) {
......
...@@ -72,6 +72,9 @@ class COMPONENT_EXPORT(CROS_HEALTHD) FakeCrosHealthdClient ...@@ -72,6 +72,9 @@ class COMPONENT_EXPORT(CROS_HEALTHD) FakeCrosHealthdClient
// Calls the lid event OnLidClosed on all registered lid observers. // Calls the lid event OnLidClosed on all registered lid observers.
void EmitLidClosedEventForTesting(); void EmitLidClosedEventForTesting();
// Calls the lid event OnLidOpened on all registered lid observers.
void EmitLidOpenedEventForTesting();
// Requests the network health state using the NetworkHealthService remote. // Requests the network health state using the NetworkHealthService remote.
void RequestNetworkHealthForTesting( void RequestNetworkHealthForTesting(
chromeos::network_health::mojom::NetworkHealthService:: chromeos::network_health::mojom::NetworkHealthService::
......
...@@ -293,6 +293,11 @@ void FakeCrosHealthdService::EmitLidClosedEventForTesting() { ...@@ -293,6 +293,11 @@ void FakeCrosHealthdService::EmitLidClosedEventForTesting() {
observer->OnLidClosed(); observer->OnLidClosed();
} }
void FakeCrosHealthdService::EmitLidOpenedEventForTesting() {
for (auto& observer : lid_observers_)
observer->OnLidOpened();
}
void FakeCrosHealthdService::RequestNetworkHealthForTesting( void FakeCrosHealthdService::RequestNetworkHealthForTesting(
chromeos::network_health::mojom::NetworkHealthService:: chromeos::network_health::mojom::NetworkHealthService::
GetHealthSnapshotCallback callback) { GetHealthSnapshotCallback callback) {
......
...@@ -152,6 +152,9 @@ class FakeCrosHealthdService final ...@@ -152,6 +152,9 @@ class FakeCrosHealthdService final
// Calls the lid event OnLidClosed for all registered lid observers. // Calls the lid event OnLidClosed for all registered lid observers.
void EmitLidClosedEventForTesting(); void EmitLidClosedEventForTesting();
// Calls the lid event OnLidOpened for all registered lid observers.
void EmitLidOpenedEventForTesting();
// Requests the network health state using the network_health_remote_. // Requests the network health state using the network_health_remote_.
void RequestNetworkHealthForTesting( void RequestNetworkHealthForTesting(
chromeos::network_health::mojom::NetworkHealthService:: chromeos::network_health::mojom::NetworkHealthService::
......
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