Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/workers/ChromeWorkerScope.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 file,
5
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "ChromeWorkerScope.h"
8
9
#include "jsapi.h"
10
11
#include "nsXPCOM.h"
12
#include "nsNativeCharsetUtils.h"
13
#include "nsString.h"
14
15
#include "WorkerPrivate.h"
16
17
namespace mozilla {
18
namespace dom {
19
20
namespace {
21
22
#ifdef BUILD_CTYPES
23
24
char*
25
UnicodeToNative(JSContext* aCx, const char16_t* aSource, size_t aSourceLen)
26
0
{
27
0
  nsDependentString unicode(aSource, aSourceLen);
28
0
29
0
  nsAutoCString native;
30
0
  if (NS_FAILED(NS_CopyUnicodeToNative(unicode, native))) {
31
0
    JS_ReportErrorASCII(aCx, "Could not convert string to native charset!");
32
0
    return nullptr;
33
0
  }
34
0
35
0
  char* result = static_cast<char*>(JS_malloc(aCx, native.Length() + 1));
36
0
  if (!result) {
37
0
    return nullptr;
38
0
  }
39
0
40
0
  memcpy(result, native.get(), native.Length());
41
0
  result[native.Length()] = 0;
42
0
  return result;
43
0
}
44
45
#endif // BUILD_CTYPES
46
47
} // namespace
48
49
bool
50
DefineChromeWorkerFunctions(JSContext* aCx, JS::Handle<JSObject*> aGlobal)
51
0
{
52
0
  // Currently ctypes is the only special property given to ChromeWorkers.
53
0
#ifdef BUILD_CTYPES
54
0
  {
55
0
    JS::Rooted<JS::Value> ctypes(aCx);
56
0
    if (!JS_InitCTypesClass(aCx, aGlobal) ||
57
0
        !JS_GetProperty(aCx, aGlobal, "ctypes", &ctypes)) {
58
0
      return false;
59
0
    }
60
0
61
0
    static const JSCTypesCallbacks callbacks = {
62
0
      UnicodeToNative
63
0
    };
64
0
65
0
    JS_SetCTypesCallbacks(ctypes.toObjectOrNull(), &callbacks);
66
0
  }
67
0
#endif // BUILD_CTYPES
68
0
69
0
  return true;
70
0
}
71
72
} // dom namespace
73
} // mozilla namespace