summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/loader/modulescript/worklet_module_script_fetcher.cc
blob: 4edfaeadd2d2d2d937934f7e99b2568f0d05f818 (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
57
58
59
60
61
62
63
// Copyright 2017 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/core/loader/modulescript/worklet_module_script_fetcher.h"

#include "third_party/blink/renderer/platform/loader/fetch/resource_fetcher.h"

namespace blink {

WorkletModuleScriptFetcher::WorkletModuleScriptFetcher(
    ResourceFetcher* fetcher,
    WorkletModuleResponsesMap* module_responses_map)
    : fetcher_(fetcher), module_responses_map_(module_responses_map) {}

void WorkletModuleScriptFetcher::Trace(blink::Visitor* visitor) {
  ModuleScriptFetcher::Trace(visitor);
  visitor->Trace(fetcher_);
}

void WorkletModuleScriptFetcher::Fetch(FetchParameters& fetch_params,
                                       ModuleGraphLevel level,
                                       ModuleScriptFetcher::Client* client) {
  if (module_responses_map_->GetEntry(
          fetch_params.Url(), client,
          fetcher_->Context().GetLoadingTaskRunner())) {
    return;
  }

  // TODO(japhet): This worklet global scope will drive the fetch of this
  // module. If another global scope requests the same module,
  // module_responses_map_ will ensure that it is notified when this fetch
  // completes. Currently, all worklet global scopes are destroyed when the
  // Document is destroyed, so we won't end up in a situation where this global
  // scope is being destroyed and needs to cancel the fetch, but some other
  // global scope is still alive and still wants to complete the fetch. When we
  // support worklet global scopes being created and destroyed flexibly, we'll
  // need to handle that case, maybe by having a way to restart fetches in a
  // different global scope?
  url_ = fetch_params.Url();
  ScriptResource::Fetch(fetch_params, fetcher_.Get(), this);
}

void WorkletModuleScriptFetcher::NotifyFinished(Resource* resource) {
  ClearResource();

  base::Optional<ModuleScriptCreationParams> params;
  ScriptResource* script_resource = ToScriptResource(resource);
  HeapVector<Member<ConsoleMessage>> error_messages;
  if (WasModuleLoadSuccessful(script_resource, &error_messages)) {
    params.emplace(
        script_resource->GetResponse().Url(), script_resource->SourceText(),
        script_resource->GetResourceRequest().GetFetchCredentialsMode(),
        script_resource->CalculateAccessControlStatus(
            fetcher_->Context().GetSecurityOrigin()));
  }

  // This will eventually notify |client| passed to
  // WorkletModuleScriptFetcher::Fetch().
  module_responses_map_->SetEntryParams(url_, params);
}

}  // namespace blink