tree: d73419f86fa2ab8922a31e49a83ac49f434f85b9 [path history] [tgz]
  1. abusive_notification_permissions_manager.cc
  2. abusive_notification_permissions_manager.h
  3. abusive_notification_permissions_manager_unittest.cc
  4. BUILD.gn
  5. DIR_METADATA
  6. disruptive_notification_permissions_manager.cc
  7. disruptive_notification_permissions_manager.h
  8. disruptive_notification_permissions_manager_unittest.cc
  9. extensions_result.cc
  10. extensions_result.h
  11. extensions_result_unittest.cc
  12. menu_notification.cc
  13. menu_notification.h
  14. menu_notification_service.cc
  15. menu_notification_service.h
  16. menu_notification_service_factory.cc
  17. menu_notification_service_factory.h
  18. menu_notification_service_unittest.cc
  19. menu_notification_unittest.cc
  20. mock_safe_browsing_database_manager.cc
  21. mock_safe_browsing_database_manager.h
  22. notification_permission_review_result.cc
  23. notification_permission_review_result.h
  24. notification_permission_review_result_unittest.cc
  25. notification_permission_review_service.cc
  26. notification_permission_review_service.h
  27. notification_permission_review_service_factory.cc
  28. notification_permission_review_service_factory.h
  29. notification_permission_review_service_unittest.cc
  30. notification_wrapper_android.cc
  31. notification_wrapper_android.h
  32. OWNERS
  33. password_status_check_result.cc
  34. password_status_check_result.h
  35. password_status_check_result_android.cc
  36. password_status_check_result_android.h
  37. password_status_check_result_android_unittest.cc
  38. password_status_check_result_unittest.cc
  39. password_status_check_service.cc
  40. password_status_check_service.h
  41. password_status_check_service_factory.cc
  42. password_status_check_service_factory.h
  43. password_status_check_service_unittest.cc
  44. README.md
  45. revoked_permissions_result.cc
  46. revoked_permissions_result.h
  47. revoked_permissions_result_unittest.cc
  48. revoked_permissions_service.cc
  49. revoked_permissions_service.h
  50. revoked_permissions_service_browsertest.cc
  51. revoked_permissions_service_factory.cc
  52. revoked_permissions_service_factory.h
  53. revoked_permissions_service_unittest.cc
  54. safe_browsing_result.cc
  55. safe_browsing_result.h
  56. safe_browsing_result_unittest.cc
  57. safety_hub_constants.cc
  58. safety_hub_constants.h
  59. safety_hub_hats_service.cc
  60. safety_hub_hats_service.h
  61. safety_hub_hats_service_factory.cc
  62. safety_hub_hats_service_factory.h
  63. safety_hub_hats_service_unittest.cc
  64. safety_hub_prefs.cc
  65. safety_hub_prefs.h
  66. safety_hub_result.cc
  67. safety_hub_result.h
  68. safety_hub_result_unittest.cc
  69. safety_hub_service.cc
  70. safety_hub_service.h
  71. safety_hub_service_unittest.cc
  72. safety_hub_test_util.cc
  73. safety_hub_test_util.h
  74. safety_hub_util.cc
  75. safety_hub_util.h
  76. unused_site_permissions_manager.cc
  77. unused_site_permissions_manager.h
  78. unused_site_permissions_manager_unittest.cc
chrome/browser/ui/safety_hub/README.md

Services

This directory contains the services related to Safety Hub. Each specific service inherits from SafetyHubService, as defined in safety_hub_service.h. 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 order to make the latest result of the service always available just after initialization of the service, the InitializeLatestResult() needs to be called in the constructor of the derived services. This function, which also needs to be implemented by each service, has to set the latest_result_ property.

Similarly, the StartRepeatedUpdates() function should also be called in the constructor of each service. This method will start up the timer that periodically run the update.

In summary, each Safety Hub service should implement the following functions:

  • InitializeLatestResult(): set the latest_result_ property to the latest available result.
  • 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.

Results

Each Safety Hub service has their own result type that inherits from SafetyHubService::Result. This result should include the information that is needed for displaying the information in the UI and being able to distinguish two different results. To support serialization, the ToDictValue() method needs to be implemented by the derived result classes. Furthermore, the derived classes should have a constructor that takes a base::Value::Dict as argument and restores the properties that are defined in the dictionary.

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.