Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/workers/ChromeWorker.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "ChromeWorker.h"
8
9
#include "mozilla/dom/WorkerBinding.h"
10
#include "nsContentUtils.h"
11
#include "WorkerPrivate.h"
12
13
namespace mozilla {
14
namespace dom {
15
16
/* static */ already_AddRefed<ChromeWorker>
17
ChromeWorker::Constructor(const GlobalObject& aGlobal,
18
                          const nsAString& aScriptURL,
19
                          ErrorResult& aRv)
20
0
{
21
0
  JSContext* cx = aGlobal.Context();
22
0
23
0
  RefPtr<WorkerPrivate> workerPrivate =
24
0
    WorkerPrivate::Constructor(cx, aScriptURL, true /* aIsChromeWorker */,
25
0
                               WorkerTypeDedicated, EmptyString(),
26
0
                               VoidCString(), nullptr /*aLoadInfo */, aRv);
27
0
  if (NS_WARN_IF(aRv.Failed())) {
28
0
    return nullptr;
29
0
  }
30
0
31
0
  nsCOMPtr<nsIGlobalObject> globalObject =
32
0
    do_QueryInterface(aGlobal.GetAsSupports());
33
0
34
0
  RefPtr<ChromeWorker> worker =
35
0
    new ChromeWorker(globalObject, workerPrivate.forget());
36
0
  return worker.forget();
37
0
}
38
39
/* static */ bool
40
ChromeWorker::WorkerAvailable(JSContext* aCx, JSObject* /* unused */)
41
0
{
42
0
  // Chrome is always allowed to use workers, and content is never
43
0
  // allowed to use ChromeWorker, so all we have to check is the
44
0
  // caller.  However, chrome workers apparently might not have a
45
0
  // system principal, so we have to check for them manually.
46
0
  if (NS_IsMainThread()) {
47
0
    return nsContentUtils::IsSystemCaller(aCx);
48
0
  }
49
0
50
0
  return GetWorkerPrivateFromContext(aCx)->IsChromeWorker();
51
0
}
52
53
ChromeWorker::ChromeWorker(nsIGlobalObject* aGlobalObject,
54
                           already_AddRefed<WorkerPrivate> aWorkerPrivate)
55
  : Worker(aGlobalObject, std::move(aWorkerPrivate))
56
0
{}
57
58
0
ChromeWorker::~ChromeWorker() = default;
59
60
JSObject*
61
ChromeWorker::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
62
0
{
63
0
  JS::Rooted<JSObject*> wrapper(aCx,
64
0
    ChromeWorker_Binding::Wrap(aCx, this, aGivenProto));
65
0
  if (wrapper) {
66
0
    // Most DOM objects don't assume they have a reflector. If they don't have
67
0
    // one and need one, they create it. But in workers code, we assume that the
68
0
    // reflector is always present.  In order to guarantee that it's always
69
0
    // present, we have to preserve it. Otherwise the GC will happily collect it
70
0
    // as needed.
71
0
    MOZ_ALWAYS_TRUE(TryPreserveWrapper(wrapper));
72
0
  }
73
0
74
0
  return wrapper;
75
0
}
76
77
} // dom namespace
78
} // mozilla namespace