summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/service_worker/service_worker_module_tree_client.cc
blob: 8d60af66824114e90cf579e4b5591ca66b7d8ad2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/service_worker/service_worker_module_tree_client.h"

#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/script/module_script.h"
#include "third_party/blink/renderer/core/workers/worker_global_scope.h"
#include "third_party/blink/renderer/core/workers/worker_reporting_proxy.h"

namespace blink {

ServiceWorkerModuleTreeClient::ServiceWorkerModuleTreeClient(
    Modulator* modulator)
    : modulator_(modulator) {}

// This client is used for both new and installed scripts. In the new scripts
// case, this is a partial implementation of the custom "perform the fetch" hook
// in the spec: https://w3c.github.io/ServiceWorker/#update-algorithm For
// installed scripts, there is no corresponding specification text because there
// is no fetching process there. The service worker simply uses its associated
// script resource.
void ServiceWorkerModuleTreeClient::NotifyModuleTreeLoadFinished(
    ModuleScript* module_script) {
  auto* execution_context =
      ExecutionContext::From(modulator_->GetScriptState());
  auto* worker_global_scope = To<WorkerGlobalScope>(execution_context);
  blink::WorkerReportingProxy& worker_reporting_proxy =
      worker_global_scope->ReportingProxy();

  if (!module_script) {
    // (In the update case) Step 9: "If the algorithm asynchronously completes
    // with null, then: Invoke Reject Job Promise with job and TypeError."
    // DidFailToFetchModuleScript() signals that startup failed, which causes
    // ServiceWorkerRegisterJob to reject the job promise.
    worker_reporting_proxy.DidFailToFetchModuleScript();
    worker_global_scope->close();
    return;
  }

  // (In the update case) Step 9: "Else, continue the rest of these steps after
  // the algorithm's asynchronous completion, with script being the asynchronous
  // completion value."
  worker_reporting_proxy.WillEvaluateModuleScript();
  ScriptValue error = modulator_->ExecuteModule(
      module_script, Modulator::CaptureEvalErrorFlag::kReport);
  worker_reporting_proxy.DidEvaluateModuleScript(error.IsEmpty());
}

void ServiceWorkerModuleTreeClient::Trace(blink::Visitor* visitor) {
  visitor->Trace(modulator_);
  ModuleTreeClient::Trace(visitor);
}

}  // namespace blink