dom/workers/ChromeWorkerScope.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     4  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include "ChromeWorkerScope.h"
     8 #include "jsapi.h"
    10 #include "nsXPCOM.h"
    11 #include "nsNativeCharsetUtils.h"
    12 #include "nsString.h"
    14 #include "WorkerPrivate.h"
    16 using namespace mozilla::dom;
    17 USING_WORKERS_NAMESPACE
    19 namespace {
    21 #ifdef BUILD_CTYPES
    23 char*
    24 UnicodeToNative(JSContext* aCx, const jschar* aSource, size_t aSourceLen)
    25 {
    26   nsDependentString unicode(aSource, aSourceLen);
    28   nsAutoCString native;
    29   if (NS_FAILED(NS_CopyUnicodeToNative(unicode, native))) {
    30     JS_ReportError(aCx, "Could not convert string to native charset!");
    31     return nullptr;
    32   }
    34   char* result = static_cast<char*>(JS_malloc(aCx, native.Length() + 1));
    35   if (!result) {
    36     return nullptr;
    37   }
    39   memcpy(result, native.get(), native.Length());
    40   result[native.Length()] = 0;
    41   return result;
    42 }
    44 #endif // BUILD_CTYPES
    46 } // anonymous namespace
    48 BEGIN_WORKERS_NAMESPACE
    50 bool
    51 DefineChromeWorkerFunctions(JSContext* aCx, JS::Handle<JSObject*> aGlobal)
    52 {
    53   // Currently ctypes is the only special property given to ChromeWorkers.
    54 #ifdef BUILD_CTYPES
    55   {
    56     JS::Rooted<JS::Value> ctypes(aCx);
    57     if (!JS_InitCTypesClass(aCx, aGlobal) ||
    58         !JS_GetProperty(aCx, aGlobal, "ctypes", &ctypes)) {
    59       return false;
    60     }
    62     static JSCTypesCallbacks callbacks = {
    63       UnicodeToNative
    64     };
    66     JS_SetCTypesCallbacks(JSVAL_TO_OBJECT(ctypes), &callbacks);
    67   }
    68 #endif // BUILD_CTYPES
    70   return true;
    71 }
    73 END_WORKERS_NAMESPACE

mercurial