summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/workers/dedicated_worker_messaging_proxy.cc
blob: 448d1a2c71c9d96071d253c30bce5445f5f1e22a (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright 2015 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/workers/dedicated_worker_messaging_proxy.h"

#include <memory>
#include "base/feature_list.h"
#include "base/optional.h"
#include "services/network/public/mojom/fetch_api.mojom-blink.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/renderer/bindings/core/v8/sanitize_script_errors.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_cache_options.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_worker_options.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/events/error_event.h"
#include "third_party/blink/renderer/core/events/message_event.h"
#include "third_party/blink/renderer/core/fetch/request.h"
#include "third_party/blink/renderer/core/frame/csp/content_security_policy.h"
#include "third_party/blink/renderer/core/inspector/thread_debugger.h"
#include "third_party/blink/renderer/core/loader/worker_resource_timing_notifier_impl.h"
#include "third_party/blink/renderer/core/messaging/blink_transferable_message.h"
#include "third_party/blink/renderer/core/workers/dedicated_worker.h"
#include "third_party/blink/renderer/core/workers/dedicated_worker_object_proxy.h"
#include "third_party/blink/renderer/core/workers/dedicated_worker_thread.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"
#include "third_party/blink/renderer/platform/wtf/wtf.h"

namespace blink {

DedicatedWorkerMessagingProxy::DedicatedWorkerMessagingProxy(
    ExecutionContext* execution_context,
    DedicatedWorker* worker_object)
    : ThreadedMessagingProxyBase(execution_context),
      worker_object_(worker_object) {
  worker_object_proxy_ = std::make_unique<DedicatedWorkerObjectProxy>(
      this, GetParentExecutionContextTaskRunners());
}

DedicatedWorkerMessagingProxy::~DedicatedWorkerMessagingProxy() = default;

void DedicatedWorkerMessagingProxy::StartWorkerGlobalScope(
    std::unique_ptr<GlobalScopeCreationParams> creation_params,
    const WorkerOptions* options,
    const KURL& script_url,
    const FetchClientSettingsObjectSnapshot& outside_settings_object,
    const v8_inspector::V8StackTraceId& stack_id,
    const String& source_code,
    RejectCoepUnsafeNone reject_coep_unsafe_none) {
  DCHECK(IsParentContextThread());
  if (AskedToTerminate()) {
    // Worker.terminate() could be called from JS before the thread was
    // created.
    return;
  }

  InitializeWorkerThread(
      std::move(creation_params),
      CreateBackingThreadStartupData(GetExecutionContext()->GetIsolate()));

  // Step 13: "Obtain script by switching on the value of options's type
  // member:"
  if (options->type() == "classic") {
    // "classic: Fetch a classic worker script given url, outside settings,
    // destination, and inside settings."
    UseCounter::Count(GetExecutionContext(),
                      WebFeature::kClassicDedicatedWorker);
    if (base::FeatureList::IsEnabled(features::kPlzDedicatedWorker)) {
      auto* resource_timing_notifier =
          WorkerResourceTimingNotifierImpl::CreateForOutsideResourceFetcher(
              *GetExecutionContext());
      GetWorkerThread()->FetchAndRunClassicScript(
          script_url, outside_settings_object.CopyData(),
          resource_timing_notifier, stack_id);
    } else {
      // Legacy code path (to be deprecated, see https://crbug.com/835717):
      GetWorkerThread()->EvaluateClassicScript(
          script_url, source_code, nullptr /* cached_meta_data */, stack_id);
    }
  } else if (options->type() == "module") {
    // "module: Fetch a module worker script graph given url, outside settings,
    // destination, the value of the credentials member of options, and inside
    // settings."
    UseCounter::Count(GetExecutionContext(),
                      WebFeature::kModuleDedicatedWorker);
    base::Optional<network::mojom::CredentialsMode> credentials_mode =
        Request::ParseCredentialsMode(options->credentials());
    DCHECK(credentials_mode);

    auto* resource_timing_notifier =
        WorkerResourceTimingNotifierImpl::CreateForOutsideResourceFetcher(
            *GetExecutionContext());
    GetWorkerThread()->FetchAndRunModuleScript(
        script_url, outside_settings_object.CopyData(),
        resource_timing_notifier, *credentials_mode, reject_coep_unsafe_none);
  } else {
    NOTREACHED();
  }
}

void DedicatedWorkerMessagingProxy::PostMessageToWorkerGlobalScope(
    BlinkTransferableMessage message) {
  DCHECK(IsParentContextThread());
  if (AskedToTerminate())
    return;
  if (!was_script_evaluated_) {
    queued_early_tasks_.push_back(std::move(message));
    return;
  }
  PostCrossThreadTask(
      *GetWorkerThread()->GetTaskRunner(TaskType::kPostedMessage), FROM_HERE,
      CrossThreadBindOnce(
          &DedicatedWorkerObjectProxy::ProcessMessageFromWorkerObject,
          CrossThreadUnretained(&WorkerObjectProxy()),
          WTF::Passed(std::move(message)),
          CrossThreadUnretained(GetWorkerThread())));
}

bool DedicatedWorkerMessagingProxy::HasPendingActivity() const {
  DCHECK(IsParentContextThread());
  return !AskedToTerminate();
}

void DedicatedWorkerMessagingProxy::DidFailToFetchScript() {
  DCHECK(IsParentContextThread());
  if (!worker_object_ || AskedToTerminate())
    return;
  worker_object_->DispatchErrorEventForScriptFetchFailure();
}

void DedicatedWorkerMessagingProxy::Freeze() {
  DCHECK(IsParentContextThread());
  auto* worker_thread = GetWorkerThread();
  if (AskedToTerminate() || !worker_thread)
    return;
  worker_thread->Freeze();
}

void DedicatedWorkerMessagingProxy::Resume() {
  DCHECK(IsParentContextThread());
  auto* worker_thread = GetWorkerThread();
  if (AskedToTerminate() || !worker_thread)
    return;
  worker_thread->Resume();
}

void DedicatedWorkerMessagingProxy::DidEvaluateScript(bool success) {
  DCHECK(IsParentContextThread());
  was_script_evaluated_ = true;

  Vector<BlinkTransferableMessage> tasks;
  queued_early_tasks_.swap(tasks);

  // The worker thread can already be terminated.
  if (!GetWorkerThread()) {
    DCHECK(AskedToTerminate());
    return;
  }

  // Post all queued tasks to the worker.
  // TODO(nhiroki): Consider whether to post the queued tasks to the worker when
  // |success| is false.
  for (auto& task : tasks) {
    PostCrossThreadTask(
        *GetWorkerThread()->GetTaskRunner(TaskType::kPostedMessage), FROM_HERE,
        CrossThreadBindOnce(
            &DedicatedWorkerObjectProxy::ProcessMessageFromWorkerObject,
            CrossThreadUnretained(&WorkerObjectProxy()),
            WTF::Passed(std::move(task)),
            CrossThreadUnretained(GetWorkerThread())));
  }
}

void DedicatedWorkerMessagingProxy::PostMessageToWorkerObject(
    BlinkTransferableMessage message) {
  DCHECK(IsParentContextThread());
  if (!worker_object_ || AskedToTerminate())
    return;

  ThreadDebugger* debugger =
      ThreadDebugger::From(GetExecutionContext()->GetIsolate());
  MessagePortArray* ports = MessagePort::EntanglePorts(
      *GetExecutionContext(), std::move(message.ports));
  debugger->ExternalAsyncTaskStarted(message.sender_stack_trace_id);
  worker_object_->DispatchEvent(
      *MessageEvent::Create(ports, std::move(message.message)));
  debugger->ExternalAsyncTaskFinished(message.sender_stack_trace_id);
}

void DedicatedWorkerMessagingProxy::DispatchErrorEvent(
    const String& error_message,
    std::unique_ptr<SourceLocation> location,
    int exception_id) {
  DCHECK(IsParentContextThread());
  if (!worker_object_)
    return;

  // We don't bother checking the AskedToTerminate() flag for dispatching the
  // event on the owner context, because exceptions should *always* be reported
  // even if the thread is terminated as the spec says:
  //
  // "Thus, error reports propagate up to the chain of dedicated workers up to
  // the original Document, even if some of the workers along this chain have
  // been terminated and garbage collected."
  // https://html.spec.whatwg.org/C/#runtime-script-errors-2
  ErrorEvent* event =
      ErrorEvent::Create(error_message, location->Clone(), nullptr);
  if (worker_object_->DispatchEvent(*event) !=
      DispatchEventResult::kNotCanceled)
    return;

  // The worker thread can already be terminated.
  if (!GetWorkerThread()) {
    DCHECK(AskedToTerminate());
    return;
  }

  // The HTML spec requires to queue an error event using the DOM manipulation
  // task source.
  // https://html.spec.whatwg.org/C/#runtime-script-errors-2
  PostCrossThreadTask(
      *GetWorkerThread()->GetTaskRunner(TaskType::kDOMManipulation), FROM_HERE,
      CrossThreadBindOnce(
          &DedicatedWorkerObjectProxy::ProcessUnhandledException,
          CrossThreadUnretained(worker_object_proxy_.get()), exception_id,
          CrossThreadUnretained(GetWorkerThread())));

  // Propagate an unhandled error to the parent context.
  const auto mute_script_errors = SanitizeScriptErrors::kDoNotSanitize;
  GetExecutionContext()->DispatchErrorEvent(event, mute_script_errors);
}

void DedicatedWorkerMessagingProxy::Trace(Visitor* visitor) {
  visitor->Trace(worker_object_);
  ThreadedMessagingProxyBase::Trace(visitor);
}

base::Optional<WorkerBackingThreadStartupData>
DedicatedWorkerMessagingProxy::CreateBackingThreadStartupData(
    v8::Isolate* isolate) {
  using HeapLimitMode = WorkerBackingThreadStartupData::HeapLimitMode;
  using AtomicsWaitMode = WorkerBackingThreadStartupData::AtomicsWaitMode;
  return WorkerBackingThreadStartupData(
      isolate->IsHeapLimitIncreasedForDebugging()
          ? HeapLimitMode::kIncreasedForDebugging
          : HeapLimitMode::kDefault,
      AtomicsWaitMode::kAllow);
}

std::unique_ptr<WorkerThread>
DedicatedWorkerMessagingProxy::CreateWorkerThread() {
  return std::make_unique<DedicatedWorkerThread>(GetExecutionContext(),
                                                 WorkerObjectProxy());
}

}  // namespace blink