Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a new method to chrome.power to report a user activity to ChromeOS. This is required in order to stop a commercial screensaver when the user performs actions not registered by ChromeOS but processed by commercial extensions. Currently badge taps are not processed by ChromeOS directly, but by the Imprivata extensions. In case a user taps their badge, the Imprivata extensions need to be able to notify ChromeOS of this and close the screensaver so that the user sees the second factor screen. Bug: 1402949 Change-Id: I7afac28749a2ed5461666a9f44b3f859c8417fb7 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4248626 Commit-Queue: Maria Petrisor <mpetrisor@chromium.org> Reviewed-by: Erik Chen <erikchen@chromium.org> Cr-Commit-Position: refs/heads/main@{#1118589}
- Loading branch information
Maria Petrisor
authored and
Chromium LUCI CQ
committed
Mar 17, 2023
1 parent
efefe2c
commit 982f041
Showing
26 changed files
with
529 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "chrome/browser/extensions/extension_apitest.h" | ||
#include "content/public/test/browser_test.h" | ||
#include "extensions/browser/api/idle/idle_manager.h" | ||
#include "extensions/browser/api/idle/idle_manager_factory.h" | ||
#include "extensions/browser/background_script_executor.h" | ||
#include "extensions/common/extension.h" | ||
#include "extensions/test/extension_test_message_listener.h" | ||
#include "extensions/test/result_catcher.h" | ||
#include "ui/base/idle/scoped_set_idle_state.h" | ||
|
||
namespace extensions { | ||
|
||
namespace { | ||
|
||
constexpr char kExtensionRelativePath[] = "power/report_activity"; | ||
constexpr char kExtensionId[] = "dibbenaepdnglcjpgjmnefmjccpinang"; | ||
|
||
} // namespace | ||
|
||
using ContextType = ExtensionBrowserTest::ContextType; | ||
|
||
class PowerApiTest : public ExtensionApiTest, | ||
public testing::WithParamInterface<ContextType> { | ||
public: | ||
PowerApiTest() : ExtensionApiTest(GetParam()) {} | ||
}; | ||
|
||
INSTANTIATE_TEST_SUITE_P(PersistentBackground, | ||
PowerApiTest, | ||
::testing::Values(ContextType::kPersistentBackground)); | ||
|
||
INSTANTIATE_TEST_SUITE_P(ServiceWorker, | ||
PowerApiTest, | ||
::testing::Values(ContextType::kServiceWorker)); | ||
|
||
// Verifies that chrome.power.reportActivity() correctly reports a user activity | ||
// by observing idle states. | ||
IN_PROC_BROWSER_TEST_P(PowerApiTest, | ||
ReportActivityChangesIdleStateFromIdleToActive) { | ||
ResultCatcher catcher; | ||
|
||
ExtensionTestMessageListener ready_listener("ready"); | ||
ready_listener.set_extension_id(kExtensionId); | ||
|
||
LoadExtension(test_data_dir_.AppendASCII(kExtensionRelativePath)); | ||
|
||
// Wait for idle state listener for be set up. | ||
ASSERT_TRUE(ready_listener.WaitUntilSatisfied()); | ||
|
||
{ | ||
// Wait for the state to change to idle. | ||
ExtensionTestMessageListener idle_listener("idle"); | ||
ui::ScopedSetIdleState idle(ui::IDLE_STATE_IDLE); | ||
ASSERT_TRUE(idle_listener.WaitUntilSatisfied()); | ||
|
||
// Allow the idle state to go out of scope to reset it. | ||
// Otherwise the QueryState() call is always going to return | ||
// the test state, even though it actually changed. | ||
} | ||
|
||
auto* idle_manager = IdleManagerFactory::GetForBrowserContext(profile()); | ||
auto threshold = idle_manager->GetThresholdForTest(kExtensionId); | ||
ASSERT_EQ(idle_manager->QueryState(threshold), ui::IDLE_STATE_IDLE); | ||
|
||
// Report activity. | ||
BackgroundScriptExecutor script_executor(profile()); | ||
constexpr char kReportActivityScript[] = R"(chrome.power.reportActivity();)"; | ||
script_executor.BackgroundScriptExecutor::ExecuteScriptAsync( | ||
kExtensionId, kReportActivityScript, | ||
BackgroundScriptExecutor::ResultCapture::kNone); | ||
|
||
// The test succeeds if the state goes from idle to active. | ||
ASSERT_TRUE(catcher.GetNextResult()); | ||
|
||
// Test that the actual state is active. | ||
ASSERT_EQ(idle_manager->QueryState(threshold), ui::IDLE_STATE_ACTIVE); | ||
} | ||
|
||
} // namespace extensions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "chrome/test/base/in_process_browser_test.h" | ||
#include "chromeos/crosapi/mojom/idle_service.mojom.h" | ||
#include "chromeos/crosapi/mojom/power.mojom.h" | ||
#include "chromeos/lacros/lacros_service.h" | ||
#include "content/public/test/browser_test.h" | ||
#include "mojo/public/cpp/bindings/receiver.h" | ||
|
||
namespace { | ||
|
||
// Verifies that all required Crosapi dependencies of the test are available. | ||
bool ValidateCrosapi() { | ||
auto* lacros_service = chromeos::LacrosService::Get(); | ||
if (!lacros_service) { | ||
return false; | ||
} | ||
|
||
if (!lacros_service->IsAvailable<crosapi::mojom::Power>()) { | ||
return false; | ||
} | ||
|
||
int interface_version = chromeos::LacrosService::Get()->GetInterfaceVersion( | ||
crosapi::mojom::Power::Uuid_); | ||
if (interface_version < | ||
int(crosapi::mojom::Power::kReportActivityMinVersion)) { | ||
return false; | ||
} | ||
|
||
if (!lacros_service->IsAvailable<crosapi::mojom::IdleService>()) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
class TestIdleInfoObserver : public crosapi::mojom::IdleInfoObserver { | ||
public: | ||
explicit TestIdleInfoObserver(base::RepeatingClosure callback) | ||
: callback_(callback) {} | ||
|
||
TestIdleInfoObserver(const TestIdleInfoObserver&) = delete; | ||
TestIdleInfoObserver& operator=(const TestIdleInfoObserver&) = delete; | ||
|
||
void Start() { | ||
chromeos::LacrosService::Get() | ||
->GetRemote<crosapi::mojom::IdleService>() | ||
->AddIdleInfoObserver(receiver_.BindNewPipeAndPassRemoteWithVersion()); | ||
} | ||
|
||
base::TimeTicks GetLastUserActivityTime() { return last_activity_time_; } | ||
|
||
private: | ||
// crosapi::mojom::IdleInfoObserver: | ||
void OnIdleInfoChanged(crosapi::mojom::IdleInfoPtr info) override { | ||
last_activity_time_ = info->last_activity_time; | ||
callback_.Run(); | ||
} | ||
|
||
base::RepeatingClosure callback_; | ||
base::TimeTicks last_activity_time_; | ||
mojo::Receiver<crosapi::mojom::IdleInfoObserver> receiver_{this}; | ||
}; | ||
|
||
class PowerApiLacrosBrowserTest : public InProcessBrowserTest { | ||
protected: | ||
PowerApiLacrosBrowserTest() = default; | ||
|
||
PowerApiLacrosBrowserTest(const PowerApiLacrosBrowserTest&) = delete; | ||
PowerApiLacrosBrowserTest& operator=(const PowerApiLacrosBrowserTest&) = | ||
delete; | ||
~PowerApiLacrosBrowserTest() override = default; | ||
}; | ||
|
||
// Calling ReportActivity() will update the last user activity. | ||
IN_PROC_BROWSER_TEST_F(PowerApiLacrosBrowserTest, | ||
ReportActivityUpdatesLastUserActivity) { | ||
if (!ValidateCrosapi()) { | ||
return; | ||
} | ||
|
||
// Report user activity and remember the time before. | ||
base::TimeTicks initial_time = base::TimeTicks::Now(); | ||
chromeos::LacrosService::Get() | ||
->GetRemote<crosapi::mojom::Power>() | ||
.get() | ||
->ReportActivity(); | ||
|
||
// TestIdleInfoObserver will receive the last user activity when added via a | ||
// call to its OnIdleInfoChanged() method. | ||
base::RunLoop run_loop; | ||
TestIdleInfoObserver observer(run_loop.QuitClosure()); | ||
observer.Start(); | ||
run_loop.Run(); | ||
|
||
// User activity was reported after calling ReportActivity(). | ||
EXPECT_GT(observer.GetLastUserActivityTime(), initial_time); | ||
} | ||
|
||
// Checks that the reported last user activity does not update without a | ||
// ReportActivity() call. | ||
IN_PROC_BROWSER_TEST_F(PowerApiLacrosBrowserTest, | ||
LastUserActivityNotUpdatedWithoutCall) { | ||
if (!ValidateCrosapi()) { | ||
return; | ||
} | ||
|
||
// Save initial time to check no activity was reported during the test. | ||
base::TimeTicks initial_time = base::TimeTicks::Now(); | ||
|
||
// TestIdleInfoObserver will receive the last user activity when added via a | ||
// call to its OnIdleInfoChanged() method. | ||
base::RunLoop run_loop; | ||
TestIdleInfoObserver observer(run_loop.QuitClosure()); | ||
observer.Start(); | ||
run_loop.Run(); | ||
|
||
// No user activity reported without calling ReportActivity(). | ||
EXPECT_LT(observer.GetLastUserActivityTime(), initial_time); | ||
} | ||
|
||
} // namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
chrome/test/data/extensions/api_test/power/report_activity/manifest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "chrome.power.reportActivity() test extension (ID dibbenaepdnglcjpgjmnefmjccpinang)", | ||
"key": "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCb0DzjK+cmP0/ULDF4z7mS8wniW1e0fo+HMhrznMqsahgrX/zYzc0fGZDX5dmzE3FQ9c1a4BdqWCaxI4q3yQmmNR+xKYUWaYuaTQAI1RPIUqZK3yps+2TUqMkfa1Da9t6pBGlzNXwKwsrvDdP55oXPlhtXiJZ0Qq+0AtRHhbi7/kzR7EpRjqhfWmJLYovSf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g1bVeuKAbYO6hXd5KnOBwq+2NFGJg1jSvAyGL5MrJcLTna/VZxCjNSdTBa1gsK1bciCV4ViYq7VCpVNhic4YhJvvwPn+eybXt4f0UwguX09s2J/KpYeVqHBrxmgQH4m349dq98hwKBgC2MFgDw9/4r8GUgYxS5bAvAtegqQv4Rl3wzTOOe/KO95zukmqbFI93fI9ksTYDZ2t5n8jE6qfKms0W7ItJ3irb0oQHcHyL8vhl8rAZVkffmDyGU7pYMm3bQRtL029fKnwJ9miySj2+hAObuFZg6feum5z8fztn2Whoz8dujCvl+", | ||
"version": "1.0", | ||
"manifest_version": 2, | ||
"description": "chrome.power.reportActivity API test", | ||
"background": { | ||
"scripts": [ | ||
"test.js" | ||
], | ||
"persistent": true | ||
}, | ||
"permissions": [ | ||
"idle", | ||
"power" | ||
] | ||
} |
19 changes: 19 additions & 0 deletions
19
chrome/test/data/extensions/api_test/power/report_activity/test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
let wasIdle = false; | ||
chrome.idle.onStateChanged.addListener((newState) => { | ||
// First state is set to be idle from the Chrome side of the test. We report | ||
// an activity once the state is idle. | ||
if (newState === 'idle') { | ||
wasIdle = true; | ||
chrome.test.sendMessage('idle'); | ||
} | ||
// Only succeed a test if we go from "idle" to "active". | ||
else if (newState === 'active' && wasIdle) { | ||
chrome.test.succeed(); | ||
} | ||
}); | ||
|
||
chrome.test.sendMessage('ready'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.