tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

XPCString.cpp (3830B)


      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 /*
      8 * Infrastructure for sharing DOMString data with JSStrings.
      9 *
     10 * Importing an nsAString into JS:
     11 * If possible (GetSharedBufferHandle works) use the external string support in
     12 * JS to create a JSString that points to the readable's buffer.  We keep a
     13 * reference to the buffer handle until the JSString is finalized.
     14 *
     15 * Exporting a JSString as an nsAReadable:
     16 * Wrap the JSString with a root-holding XPCJSReadableStringWrapper, which roots
     17 * the string and exposes its buffer via the nsAString interface, as
     18 * well as providing refcounting support.
     19 */
     20 
     21 #include "nscore.h"
     22 #include "nsString.h"
     23 #include "mozilla/StringBuffer.h"
     24 #include "jsapi.h"
     25 #include "xpcpublic.h"
     26 
     27 using namespace JS;
     28 using mozilla::StringBuffer;
     29 
     30 const XPCStringConvert::LiteralExternalString
     31    XPCStringConvert::sLiteralExternalString;
     32 
     33 void XPCStringConvert::LiteralExternalString::finalize(
     34    JS::Latin1Char* aChars) const {
     35  // Nothing to do.
     36 }
     37 
     38 void XPCStringConvert::LiteralExternalString::finalize(char16_t* aChars) const {
     39  // Nothing to do.
     40 }
     41 
     42 size_t XPCStringConvert::LiteralExternalString::sizeOfBuffer(
     43    const JS::Latin1Char* aChars, mozilla::MallocSizeOf aMallocSizeOf) const {
     44  // This string's buffer is not heap-allocated, so its malloc size is 0.
     45  return 0;
     46 }
     47 
     48 size_t XPCStringConvert::LiteralExternalString::sizeOfBuffer(
     49    const char16_t* aChars, mozilla::MallocSizeOf aMallocSizeOf) const {
     50  // This string's buffer is not heap-allocated, so its malloc size is 0.
     51  return 0;
     52 }
     53 
     54 // convert a string to a JSString, either copying or sharing string data
     55 // static
     56 bool xpc::NonVoidStringToJsval(JSContext* cx, const nsAString& readable,
     57                               MutableHandleValue vp) {
     58  uint32_t length = readable.Length();
     59 
     60  if (readable.IsLiteral()) {
     61    return XPCStringConvert::StringLiteralToJSVal(cx, readable.BeginReading(),
     62                                                  length, vp);
     63  }
     64 
     65  if (StringBuffer* buf = readable.GetStringBuffer()) {
     66    return XPCStringConvert::UCStringBufferToJSVal(cx, buf, length, vp);
     67  }
     68 
     69  // blech, have to copy.
     70  JSString* str = JS_NewUCStringCopyN(cx, readable.BeginReading(), length);
     71  if (!str) {
     72    return false;
     73  }
     74  vp.setString(str);
     75  return true;
     76 }
     77 
     78 bool xpc::NonVoidLatin1StringToJsval(JSContext* cx, const nsACString& latin1,
     79                                     MutableHandleValue vp) {
     80  uint32_t length = latin1.Length();
     81 
     82  if (latin1.IsLiteral()) {
     83    return XPCStringConvert::StringLiteralToJSVal(
     84        cx, reinterpret_cast<const JS::Latin1Char*>(latin1.BeginReading()),
     85        length, vp);
     86  }
     87 
     88  if (StringBuffer* buf = latin1.GetStringBuffer()) {
     89    return XPCStringConvert::Latin1StringBufferToJSVal(cx, buf, length, vp);
     90  }
     91 
     92  JSString* str = JS_NewStringCopyN(cx, latin1.BeginReading(), length);
     93  if (!str) {
     94    return false;
     95  }
     96  vp.setString(str);
     97  return true;
     98 }
     99 
    100 bool xpc::NonVoidUTF8StringToJsval(JSContext* cx, const nsACString& utf8,
    101                                   MutableHandleValue vp) {
    102  uint32_t length = utf8.Length();
    103 
    104  if (utf8.IsLiteral()) {
    105    return XPCStringConvert::UTF8StringLiteralToJSVal(
    106        cx, JS::UTF8Chars(utf8.BeginReading(), length), vp);
    107  }
    108 
    109  if (StringBuffer* buf = utf8.GetStringBuffer()) {
    110    return XPCStringConvert::UTF8StringBufferToJSVal(cx, buf, length, vp);
    111  }
    112 
    113  JSString* str =
    114      JS_NewStringCopyUTF8N(cx, JS::UTF8Chars(utf8.BeginReading(), length));
    115  if (!str) {
    116    return false;
    117  }
    118  vp.setString(str);
    119  return true;
    120 }