dom/workers/ChromeWorkerScope.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/workers/ChromeWorkerScope.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,73 @@
     1.4 +/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "ChromeWorkerScope.h"
    1.10 +
    1.11 +#include "jsapi.h"
    1.12 +
    1.13 +#include "nsXPCOM.h"
    1.14 +#include "nsNativeCharsetUtils.h"
    1.15 +#include "nsString.h"
    1.16 +
    1.17 +#include "WorkerPrivate.h"
    1.18 +
    1.19 +using namespace mozilla::dom;
    1.20 +USING_WORKERS_NAMESPACE
    1.21 +
    1.22 +namespace {
    1.23 +
    1.24 +#ifdef BUILD_CTYPES
    1.25 +
    1.26 +char*
    1.27 +UnicodeToNative(JSContext* aCx, const jschar* aSource, size_t aSourceLen)
    1.28 +{
    1.29 +  nsDependentString unicode(aSource, aSourceLen);
    1.30 +
    1.31 +  nsAutoCString native;
    1.32 +  if (NS_FAILED(NS_CopyUnicodeToNative(unicode, native))) {
    1.33 +    JS_ReportError(aCx, "Could not convert string to native charset!");
    1.34 +    return nullptr;
    1.35 +  }
    1.36 +
    1.37 +  char* result = static_cast<char*>(JS_malloc(aCx, native.Length() + 1));
    1.38 +  if (!result) {
    1.39 +    return nullptr;
    1.40 +  }
    1.41 +
    1.42 +  memcpy(result, native.get(), native.Length());
    1.43 +  result[native.Length()] = 0;
    1.44 +  return result;
    1.45 +}
    1.46 +
    1.47 +#endif // BUILD_CTYPES
    1.48 +
    1.49 +} // anonymous namespace
    1.50 +
    1.51 +BEGIN_WORKERS_NAMESPACE
    1.52 +
    1.53 +bool
    1.54 +DefineChromeWorkerFunctions(JSContext* aCx, JS::Handle<JSObject*> aGlobal)
    1.55 +{
    1.56 +  // Currently ctypes is the only special property given to ChromeWorkers.
    1.57 +#ifdef BUILD_CTYPES
    1.58 +  {
    1.59 +    JS::Rooted<JS::Value> ctypes(aCx);
    1.60 +    if (!JS_InitCTypesClass(aCx, aGlobal) ||
    1.61 +        !JS_GetProperty(aCx, aGlobal, "ctypes", &ctypes)) {
    1.62 +      return false;
    1.63 +    }
    1.64 +
    1.65 +    static JSCTypesCallbacks callbacks = {
    1.66 +      UnicodeToNative
    1.67 +    };
    1.68 +
    1.69 +    JS_SetCTypesCallbacks(JSVAL_TO_OBJECT(ctypes), &callbacks);
    1.70 +  }
    1.71 +#endif // BUILD_CTYPES
    1.72 +
    1.73 +  return true;
    1.74 +}
    1.75 +
    1.76 +END_WORKERS_NAMESPACE

mercurial