-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SHv2] Split up update tasks in a background and UI task
This CL changes the functionality of the Safety Hub service that allows splitting up the update task in one for the background thread (where the most expensive computations can take place), and one on the UI thread. The former will be a static function to ensure that it is thread-safe. The unused site permissions service is updated using this new structure accordingly. Change-Id: I9f16bf892dd04b3aae6e5842ce5d327d82989a59 Bug: 1443466 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4703064 Reviewed-by: Martin Šrámek <msramek@chromium.org> Commit-Queue: Tom Van Goethem <tov@chromium.org> Cr-Commit-Position: refs/heads/main@{#1187212}
- Loading branch information
1 parent
23b42b1
commit ac36f1e
Showing
11 changed files
with
322 additions
and
100 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,57 @@ | ||
## Services | ||
|
||
This directory contains the services related to Safety Hub. Each specific | ||
service inherits from `SafetyHubService`, as defined in `safety_hub_service.h`. | ||
The services override the `UpdateOnBackgroundThread()` and | ||
`GetRepeatedUpdateInterval()` methods. The former contains the functionality | ||
that will be periodically executed, whereas the latter determines the interval | ||
in which the function is executed. | ||
The service will be periodically updated, depending on the frequency determined | ||
by `GetRepeatedUpdateInterval()`. Additionally, an update will be launched every | ||
time that the service is started (whenever the browser is started). Hence, it's | ||
important to note that the update can be called more frequently than the | ||
interval returned by `GetRepeatedUpdateInterval()`. | ||
|
||
The update process consists of two stages, a background task and a UI thread | ||
task. | ||
|
||
The background task will be run on a separate thread in the background. The | ||
function that needs to be run in the background should contain the functionality | ||
of the update that is the most computation-heavy, to prevent blocking the UI | ||
thread and possibly freezing the browser. The background task can return an | ||
intermediate result, that will be passed along to the UI thread task. Ideally, | ||
the background task should be a static function that will be returned by | ||
`GetBackgroundTask()`. As this will be run in a thread other than the one the | ||
service runs in, any arguments that are bound to the function should be | ||
thread-safe. As the browser shuts down, references to these objects might be | ||
destroyed, possibly leading to memory issues. For instance, a reference to the | ||
service itself should NOT be bound to the function. This will result in crashes | ||
of other tests, and could cause the browser to crash when one profile is shut | ||
down. | ||
|
||
The UI thread task needs to be defined in `UpdateOnUIThread()`. It will be | ||
passed the intermediate result (a unique pointer to `SafetyHubService::Result`) | ||
that was returned by the background task. This method will be run synchronously | ||
on the UI thread after the background task has completed. The result by this UI | ||
thread task will be the final result that the observers will be notified of. | ||
`SafetyHubService::Result` will thus be used for 1) passing the intermediate | ||
result of `GetBackgroundTask()` to `UpdateOnUIThread()`, and 2) the final result | ||
that follows from running the update process of the service. To reduce | ||
unnecessary overhead, it is suggested that the final result does not contain any | ||
of the intermediate results, e.g. by creating a new `SafetyHubService::Result` | ||
in `UpdateOnUIThread()`. | ||
|
||
In summary, each Safety Hub service should implement the following functions: | ||
|
||
- `GetRepeatedUpdateInterval()`: returns a `base::TimeDelta` that determines | ||
the minimal frequency of how often the service is updated. | ||
- `GetBackgroundTask()`: returns a bound (static) function that will be | ||
executed in the background, containing the computation-heavy part of the | ||
service's update. | ||
- `UpdateOnUIThread()`: will be run synchronously on the UI thread, and will | ||
further process the intermediate result of the background task. | ||
- `GetAsWeakRef()`: returns a weak pointer to the service, from the | ||
`WeakPtrFactory` of the derived service. | ||
|
||
## Testing | ||
|
||
As updating the service will run a task both in the background as well as on the | ||
UI thread, it is advised to use the helper function | ||
`UpdateSafetyHubServiceAsync(service)`, defined in `safety_hub_test_util.h` to | ||
trigger an update of the service in tests. |
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.