1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "third_party/blink/renderer/core/workers/worker_module_tree_client.h"
6 
7 #include "third_party/blink/renderer/bindings/core/v8/script_value.h"
8 #include "third_party/blink/renderer/core/execution_context/execution_context.h"
9 #include "third_party/blink/renderer/core/script/module_script.h"
10 #include "third_party/blink/renderer/core/workers/worker_global_scope.h"
11 #include "third_party/blink/renderer/core/workers/worker_reporting_proxy.h"
12 
13 namespace blink {
14 
WorkerModuleTreeClient(ScriptState * script_state)15 WorkerModuleTreeClient::WorkerModuleTreeClient(ScriptState* script_state)
16     : script_state_(script_state) {}
17 
18 // A partial implementation of the "Processing model" algorithm in the HTML
19 // WebWorker spec:
20 // https://html.spec.whatwg.org/C/#worker-processing-model
NotifyModuleTreeLoadFinished(ModuleScript * module_script)21 void WorkerModuleTreeClient::NotifyModuleTreeLoadFinished(
22     ModuleScript* module_script) {
23   auto* worker_global_scope =
24       To<WorkerGlobalScope>(ExecutionContext::From(script_state_));
25   blink::WorkerReportingProxy& worker_reporting_proxy =
26       worker_global_scope->ReportingProxy();
27 
28   // Step 12. "If the algorithm asynchronously completes with null or with
29   // script whose error to rethrow is non-null, then:"
30   if (!module_script || module_script->HasErrorToRethrow()) {
31     // Step 12.1. "Queue a task to fire an event named error at worker."
32     // DidFailToFetchModuleScript() will asynchronously fire the event.
33     worker_reporting_proxy.DidFailToFetchModuleScript();
34 
35     // Step 12.2. "Run the environment discarding steps for inside settings."
36     // Do nothing because the HTML spec doesn't define these steps for web
37     // workers.
38 
39     // Schedule worker termination.
40     worker_global_scope->close();
41 
42     // Step 12.3. "Return."
43     return;
44   }
45   worker_reporting_proxy.DidFetchScript();
46 
47   // Step 12: "Otherwise, continue the rest of these steps after the algorithm's
48   // asynchronous completion, with script being the asynchronous completion
49   // value."
50   worker_global_scope->WorkerScriptFetchFinished(
51       *module_script, base::nullopt /* v8_inspector::V8StackTraceId */);
52 }
53 
Trace(Visitor * visitor)54 void WorkerModuleTreeClient::Trace(Visitor* visitor) {
55   visitor->Trace(script_state_);
56   ModuleTreeClient::Trace(visitor);
57 }
58 
59 }  // namespace blink
60