tree: 0a0854adc7af412de3c6ff60c036febad61eba47 [path history] [tgz]
  1. BUILD.gn
  2. OWNERS
  3. README.md
  4. abstract_worker.cc
  5. abstract_worker.h
  6. abstract_worker.idl
  7. dedicated_worker.cc
  8. dedicated_worker.h
  9. dedicated_worker_global_scope.cc
  10. dedicated_worker_global_scope.h
  11. dedicated_worker_global_scope.idl
  12. dedicated_worker_messaging_proxy.cc
  13. dedicated_worker_messaging_proxy.h
  14. dedicated_worker_object_proxy.cc
  15. dedicated_worker_object_proxy.h
  16. dedicated_worker_test.cc
  17. dedicated_worker_thread.cc
  18. dedicated_worker_thread.h
  19. global_scope_creation_params.cc
  20. global_scope_creation_params.h
  21. installed_scripts_manager.cc
  22. installed_scripts_manager.h
  23. main_thread_worklet_reporting_proxy.cc
  24. main_thread_worklet_reporting_proxy.h
  25. main_thread_worklet_test.cc
  26. parent_execution_context_task_runners.cc
  27. parent_execution_context_task_runners.h
  28. shared_worker.cc
  29. shared_worker.h
  30. shared_worker.idl
  31. shared_worker_client.cc
  32. shared_worker_client.h
  33. shared_worker_client_holder.cc
  34. shared_worker_client_holder.h
  35. shared_worker_content_settings_proxy.cc
  36. shared_worker_content_settings_proxy.h
  37. shared_worker_global_scope.cc
  38. shared_worker_global_scope.h
  39. shared_worker_global_scope.idl
  40. shared_worker_reporting_proxy.cc
  41. shared_worker_reporting_proxy.h
  42. shared_worker_thread.cc
  43. shared_worker_thread.h
  44. threaded_messaging_proxy_base.cc
  45. threaded_messaging_proxy_base.h
  46. threaded_object_proxy_base.cc
  47. threaded_object_proxy_base.h
  48. threaded_worklet_messaging_proxy.cc
  49. threaded_worklet_messaging_proxy.h
  50. threaded_worklet_object_proxy.cc
  51. threaded_worklet_object_proxy.h
  52. threaded_worklet_test.cc
  53. worker.idl
  54. worker_backing_thread.cc
  55. worker_backing_thread.h
  56. worker_backing_thread_startup_data.h
  57. worker_classic_script_loader.cc
  58. worker_classic_script_loader.h
  59. worker_clients.cc
  60. worker_clients.h
  61. worker_global_scope.cc
  62. worker_global_scope.h
  63. worker_global_scope.idl
  64. worker_location.h
  65. worker_location.idl
  66. worker_module_tree_client.cc
  67. worker_module_tree_client.h
  68. worker_navigator.cc
  69. worker_navigator.h
  70. worker_navigator.idl
  71. worker_options.idl
  72. worker_or_worklet_global_scope.cc
  73. worker_or_worklet_global_scope.h
  74. worker_reporting_proxy.h
  75. worker_settings.cc
  76. worker_settings.h
  77. worker_thread.cc
  78. worker_thread.h
  79. worker_thread_test.cc
  80. worker_thread_test_helper.h
  81. worklet.cc
  82. worklet.h
  83. worklet.idl
  84. worklet_global_scope.cc
  85. worklet_global_scope.h
  86. worklet_global_scope.idl
  87. worklet_global_scope_proxy.h
  88. worklet_module_responses_map.cc
  89. worklet_module_responses_map.h
  90. worklet_module_responses_map_test.cc
  91. worklet_module_tree_client.cc
  92. worklet_module_tree_client.h
  93. worklet_options.idl
  94. worklet_pending_tasks.cc
  95. worklet_pending_tasks.h
  96. worklet_thread_holder.h
third_party/blink/renderer/core/workers/README.md

This directory contains the base implementation of all worker and worklet types. Also, this contains the implementation of the Web Workers API (Dedicated Worker and Shared Worker) and Worklets API.

Worker / Worklet types

  • Workers are divided into 2 types:
    • In-process workers (Dedicated Workers): A worker of this type always runs in the same renderer process with a parent document that starts the worker.
    • Out-of-process workers (Shared Workers and Service Workers): A worker of this type may run in a different renderer process with a parent document that starts the worker.
  • Worklets are divided into 2 types:
    • Main thread worklets (Paint Worklets and Layout Worklets): A worklet of this type runs on the main thread.
    • Threaded worklets (Audio Worklets and Animation Worklets): A worklet of this type runs on a worker thread.

Worklets always run in the same renderer process with a parent document that starts the worklet like the in-process-workers.

Naming conventions

Classes in this directory are named with the following conventions, there're still some exceptions though.

  • WorkerOrWorklet prefix: Classes commonly used for workers and worklets (e.g., WorkerOrWorkletGlobalScope).
  • Worker / Worklet prefix: Classes used for workers or worklets (e.g., WorkerGlobalScope).
  • Threaded prefix: Classes used for workers and threaded worklets (e.g., ThreadedMessagingProxyBase).
  • MainThreadWorklet prefix: Classes used for main thread worklets (e.g., MainThreadWorkletReportingProxy).

Thread hopping between the main (parent) thread and a worker thread is handled by proxy classes.

  • MessagingProxy is the main (parent) thread side proxy that communicates to the worker thread.
  • ObjectProxy is the worker thread side proxy that communicates to the main (parent) thread. Object indicates a worker/worklet JavaScript object on the parent execution context.

Off-the-main-thread fetch

All worker subresources, and some of worker/worklet top-level scripts, are fetched off-the-main-thread (i.e. on the worker/worklet thread). See following docs for more details.

There are two types of network fetch on the worker/worklet thread: insideSettings and outsideSettings fetch. The terms insideSettings and outsideSettings are originated from HTML spec and Worklet spec.

insideSettings fetch

insideSettings fetch is subresource fetch.

In the spec, insideSettings corresponds to the worker environment settings object of WorkerOrWorkletGlobalScope.

In the implementation, insideSettings roughly corresponds to WorkerOrWorkletGlobalScope. WorkerOrWorkletGlobalScope::Fetcher(), its corresponding WorkerFetchContext, and WorkerOrWorkletGlobalScope::GetContentSecurityPolicy() are used.

Currently, all subresource fetches are already off-the-main-thread.

outsideSettings fetch

outsideSettings fetch is off-the-main-thread top-level worker/worklet script fetch.

In the spec, an outsideSettings is the environment settings object of worker's parent context.

In the implementation, outsideSettings should correspond to Document (or WorkerOrWorkletGlobalScope for nested workers), but the worker thread can't access these objects due to threading restriction. Therefore, we pass FetchClientSettingsObjectSnapshot that contains information of these objects across threads, and create ResourceFetcher, WorkerFetchContext and ContentSecurityPolicy (separate objects from those used for insideSettings fetch) on the worker thread. They work as if the parent context, and are used via WorkerOrWorkletGlobalScope::CreateOutsideSettingsFetcher().

Note that, where off-the-main-thread top-level fetch is NOT enabled (e.g. classic workers), the worker scripts are fetched on the main thread and thus WorkerOrWorkletGlobalScope and the worker thread are not involved.

Metrics

UseCounter

UseCounter is available in all workers and worklets. The count mechanism varies based on worker and worklet types as follows.

  • Dedicated Workers and Worklets: A feature use in a dedicated worker or worklet is propagated to the parent document, and then recorded in the document's UseCounter. For nested dedicated workers, a feature use is propagated up to the ancestor document via parent workers.
  • Shared Workers: A feature use in a shared worker is propagated to all documents connected to the shared worker via mojo calls, and then recorded in their UseCounter.
  • Service Workers: A feature use in a service worker is propagated to all documents and workers being controlled by the service worker via mojo calls, and then recorded in their UseCounter. When a feature use occurs before the service worker finishes installing, the feature use is stored in service worker storage. It is read whenever the installed service worker is started. See content/browser/service_worker/README.md for details.

WorkerOrWorkletGlobalScope::CountUse() is the common entry point. For more details, see Design of UseCounter for workers and crbug 376039.

There are some fundamental metrics.

  • WorkerStart : Counts of new DedicatedWorker() calls in Document and DedicatedWorkerGlobalScope.
  • ClassicDedicatedWorker : Counts of new DedicatedWorker() calls with { type: 'classic' } or without WorkerOptions#type argument.
  • ModuleDedicatedWorker : Counts of new DedicatedWorker() calls with { type: 'module' }.
  • NestedDedicatedWorker : Counts of new DedicatedWorker() calls in DedicatedWorkerGlobalScope.
  • SharedWorkerStart : Counts of new SharedWorker() calls in Document.
  • WorkletAddModule : Counts of Worklet#addModule() calls in Document. This includes all worklet types. Each worklet type has its own counter, too.

UMA

The UMA data is internal-only.

  • WorkerThread.ExitCode : Records the exit code of WorkerThread.

Tests

When you add a new worker or worklet type, please consider adding tests in following files and directories to check integration with the underlying worker and worklet infrastructure.

Workers and worklets interact with various features. You should also add tests in the following files and directories to avoid breakage.

References