Bug 1661304 - Adding running() method to UserInteraction. r=chutten

Differential Revision: https://phabricator.services.mozilla.com/D93589
This commit is contained in:
Mike Conley 2020-10-19 17:42:28 +00:00
Родитель 39c88bb8d6
Коммит f4b73ffaff
5 изменённых файлов: 81 добавлений и 3 удалений

Просмотреть файл

@ -71,8 +71,22 @@ namespace UserInteraction {
*
* @returns True if the timer was successfully stopped.
*/
boolean cancel(DOMString id,
optional object? obj = null);
boolean cancel(DOMString id, optional object? obj = null);
/**
* Returns whether a UserInteraction timer is currently running.
*
* @param id - the ID of the interaction to check, as declared in
* UserInteractions.yaml.
*
* @param obj - Optional parameter which checks for a timer associated
* with this particular object. If you're checking for a running timer
* that was started with an object, you'll need to pass in that same
* object here to check its running state.
*
* @returns True if the timer exists and is currently running.
*/
boolean running(DOMString id, optional object? obj = null);
/**
* Stops the timer associated with the given UserInteraction ID

Просмотреть файл

@ -179,7 +179,9 @@ class Timers final : public BackgroundHangAnnotator {
bool StartUserInteraction(JSContext* aCx, const nsAString& aUserInteraction,
const nsACString& aValue, JS::HandleObject aObj);
bool UpdateUserInteraction(JSContext* aCx, const nsAString& id,
bool RunningUserInteraction(JSContext* aCx, const nsAString& aUserInteraction,
JS::HandleObject aObj);
bool UpdateUserInteraction(JSContext* aCx, const nsAString& aUserInteraction,
const nsACString& aValue, JS::HandleObject aObj);
bool FinishUserInteraction(JSContext* aCx, const nsAString& aUserInteraction,
JS::HandleObject aObj,
@ -435,6 +437,16 @@ bool Timers::StartUserInteraction(JSContext* aCx,
return false;
}
bool Timers::RunningUserInteraction(JSContext* aCx,
const nsAString& aUserInteraction,
JS::HandleObject aObj) {
if (RefPtr<Timer> timer =
Get(aCx, aUserInteraction, aObj, VoidString(), false /* aCreate */)) {
return timer->Started();
}
return false;
}
bool Timers::UpdateUserInteraction(JSContext* aCx,
const nsAString& aUserInteraction,
const nsACString& aValue,
@ -619,6 +631,17 @@ bool UserInteractionStopwatch::Start(const dom::GlobalObject& aGlobal,
aGlobal.Context(), aUserInteraction, aValue, aObj);
}
/* static */
bool UserInteractionStopwatch::Running(const dom::GlobalObject& aGlobal,
const nsAString& aUserInteraction,
JS::Handle<JSObject*> aObj) {
if (!NS_IsMainThread()) {
return false;
}
return Timers::Singleton().RunningUserInteraction(aGlobal.Context(),
aUserInteraction, aObj);
}
/* static */
bool UserInteractionStopwatch::Update(const dom::GlobalObject& aGlobal,
const nsAString& aUserInteraction,

Просмотреть файл

@ -63,6 +63,9 @@ class UserInteractionStopwatch {
static bool Start(const GlobalObject& aGlobal,
const nsAString& aUserInteraction, const nsACString& aValue,
JS::Handle<JSObject*> aObj);
static bool Running(const GlobalObject& aGlobal,
const nsAString& aUserInteraction,
JS::Handle<JSObject*> aObj);
static bool Update(const GlobalObject& aGlobal,
const nsAString& aUserInteraction,
const nsACString& aValue, JS::Handle<JSObject*> aObj);

Просмотреть файл

@ -232,6 +232,20 @@ Hangs that occurred before the User Interaction was cancelled will not, however,
Returns `false` and logs a message to the browser console if the cancellation cannot be completed for some reason.
``running()``
~~~~~~~~~~~~~~~~~~~~
.. code-block:: js
UserInteraction.running(id, object);
Checks to see if a UserInteraction is already running.
* ``id``: Required. A string value, limited to 80 characters. This is the category name concatenated with the User Interaction name.
* ``object``: Optional. If specified, the User Interaction is associated with this object, so multiple recordings can be done concurrently. If you're checking for a running timer that was started with an object, you'll need to pass in that same object here to check its running state.
Returns `true` if a UserInteraction is already running.
``finish()``
~~~~~~~~~~~~~~~~~~~~

Просмотреть файл

@ -21,6 +21,10 @@ function run_test() {
UserInteraction.start(TEST_USER_INTERACTION_ID, TEST_VALUE_1, obj2)
);
Assert.ok(UserInteraction.running(TEST_USER_INTERACTION_ID));
Assert.ok(UserInteraction.running(TEST_USER_INTERACTION_ID, obj1));
Assert.ok(UserInteraction.running(TEST_USER_INTERACTION_ID, obj2));
// Same UserInteraction can't be re-started before being stopped
Assert.ok(!UserInteraction.start(TEST_USER_INTERACTION_ID, TEST_VALUE_1));
Assert.ok(
@ -30,6 +34,10 @@ function run_test() {
!UserInteraction.start(TEST_USER_INTERACTION_ID, TEST_VALUE_1, obj2)
);
Assert.ok(!UserInteraction.running(TEST_USER_INTERACTION_ID));
Assert.ok(!UserInteraction.running(TEST_USER_INTERACTION_ID, obj1));
Assert.ok(!UserInteraction.running(TEST_USER_INTERACTION_ID, obj2));
// Ensure that we can't finish a UserInteraction that was accidentally started
// twice
Assert.ok(!UserInteraction.finish(TEST_USER_INTERACTION_ID));
@ -44,6 +52,9 @@ function run_test() {
Assert.ok(
!UserInteraction.start("non-existent.interaction", TEST_VALUE_1, obj2)
);
Assert.ok(!UserInteraction.running("non-existent.interaction"));
Assert.ok(!UserInteraction.running("non-existent.interaction", obj1));
Assert.ok(!UserInteraction.running("non-existent.interaction", obj2));
Assert.ok(!UserInteraction.finish("non-existent.interaction"));
Assert.ok(!UserInteraction.finish("non-existent.interaction", obj1));
Assert.ok(!UserInteraction.finish("non-existent.interaction", obj2));
@ -58,6 +69,9 @@ function run_test() {
Assert.ok(
!UserInteraction.start(TEST_USER_INTERACTION_ID, TEST_INVALID_VALUE, obj2)
);
Assert.ok(!UserInteraction.running(TEST_USER_INTERACTION_ID));
Assert.ok(!UserInteraction.running(TEST_USER_INTERACTION_ID, obj1));
Assert.ok(!UserInteraction.running(TEST_USER_INTERACTION_ID, obj2));
// Verify that they can be used again
Assert.ok(UserInteraction.start(TEST_USER_INTERACTION_ID, TEST_VALUE_2));
@ -67,6 +81,9 @@ function run_test() {
Assert.ok(
UserInteraction.start(TEST_USER_INTERACTION_ID, TEST_VALUE_2, obj2)
);
Assert.ok(UserInteraction.running(TEST_USER_INTERACTION_ID));
Assert.ok(UserInteraction.running(TEST_USER_INTERACTION_ID, obj1));
Assert.ok(UserInteraction.running(TEST_USER_INTERACTION_ID, obj2));
Assert.ok(UserInteraction.finish(TEST_USER_INTERACTION_ID));
Assert.ok(UserInteraction.finish(TEST_USER_INTERACTION_ID, obj1));
Assert.ok(UserInteraction.finish(TEST_USER_INTERACTION_ID, obj2));
@ -74,6 +91,9 @@ function run_test() {
Assert.ok(!UserInteraction.finish(TEST_USER_INTERACTION_ID));
Assert.ok(!UserInteraction.finish(TEST_USER_INTERACTION_ID, obj1));
Assert.ok(!UserInteraction.finish(TEST_USER_INTERACTION_ID, obj2));
Assert.ok(!UserInteraction.running(TEST_USER_INTERACTION_ID));
Assert.ok(!UserInteraction.running(TEST_USER_INTERACTION_ID, obj1));
Assert.ok(!UserInteraction.running(TEST_USER_INTERACTION_ID, obj2));
// Verify that they can be used again with different values.
Assert.ok(UserInteraction.start(TEST_USER_INTERACTION_ID, TEST_VALUE_1));
@ -83,6 +103,9 @@ function run_test() {
Assert.ok(
UserInteraction.start(TEST_USER_INTERACTION_ID, TEST_VALUE_1, obj2)
);
Assert.ok(UserInteraction.running(TEST_USER_INTERACTION_ID));
Assert.ok(UserInteraction.running(TEST_USER_INTERACTION_ID, obj1));
Assert.ok(UserInteraction.running(TEST_USER_INTERACTION_ID, obj2));
Assert.ok(UserInteraction.finish(TEST_USER_INTERACTION_ID));
Assert.ok(
UserInteraction.finish(
@ -102,6 +125,7 @@ function run_test() {
// Test that they can be cancelled
Assert.ok(UserInteraction.start(TEST_USER_INTERACTION_ID, TEST_VALUE_1));
Assert.ok(UserInteraction.cancel(TEST_USER_INTERACTION_ID));
Assert.ok(!UserInteraction.running(TEST_USER_INTERACTION_ID));
Assert.ok(!UserInteraction.finish(TEST_USER_INTERACTION_ID));
// Test that they cannot be cancelled twice