Bug 800493 - Remove now useless libxul code from OS.File. r=froydnj
authorDavid Rajchenbach-Teller <dteller@mozilla.com>
Tue, 16 Oct 2012 21:34:03 -0400
changeset 110626 b508524e3126f07f6940aaa86687db63603f8a31
parent 110625 70e45c7aa3ba26db59b91e24d204788b1281b5e4
child 110627 37fead15ae60d6c042be2ba35cb61786fb8d2f4b
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersfroydnj
bugs800493
milestone19.0a1
Bug 800493 - Remove now useless libxul code from OS.File. r=froydnj
toolkit/components/osfile/Makefile.in
toolkit/components/osfile/osfile_shared_allthreads.jsm
toolkit/components/osfile/osfileutils.cpp
toolkit/components/osfile/osfileutils.h
toolkit/components/osfile/tests/mochi/worker_test_osfile_front.js
--- a/toolkit/components/osfile/Makefile.in
+++ b/toolkit/components/osfile/Makefile.in
@@ -14,20 +14,16 @@ MODULE          = osfile
 LIBRARY_NAME    = osfile_s
 LIBXUL_LIBRARY  = 1
 FORCE_STATIC_LIB = 1
 
 EXTRA_PP_JS_MODULES = \
    osfile.jsm \
    $(NULL)
 
-CPPSRCS = \
-	osfileutils.cpp \
-	$(NULL)
-
 ifdef ENABLE_TESTS
 DIRS += tests
 endif
 
 include $(topsrcdir)/config/rules.mk
 
 libs::
 	$(NSINSTALL) $(srcdir)/osfile_shared_allthreads.jsm $(FINAL_TARGET)/modules/osfile
--- a/toolkit/components/osfile/osfile_shared_allthreads.jsm
+++ b/toolkit/components/osfile/osfile_shared_allthreads.jsm
@@ -832,31 +832,16 @@
 	 if (exports.OS.Shared.DEBUG) {
           LOG("Could not declare function " + symbol, x);
 	 }
          return null;
        }
      };
      exports.OS.Shared.declareFFI = declareFFI;
 
-
-     /**
-      * Libxul-based utilities, shared by all back-ends.
-      */
-
-     // Lazy getter for libxul
-     defineLazyGetter(exports.OS.Shared, "libxul",
-       function init_libxul() {
-         return ctypes.open(OS.Constants.Path.libxul);
-       });
-
-     exports.OS.Shared.Utils = {};
-
-     let Strings = exports.OS.Shared.Utils.Strings = {};
-
      // A bogus array type used to perform pointer arithmetics
      let gOffsetByType;
 
      /**
       * Advance a pointer by a number of items.
       *
       * This method implements adding an integer to a pointer in C.
       *
@@ -891,78 +876,12 @@
          if (!gOffsetByType || gOffsetByType.size <= bytes) {
            gOffsetByType = ctypes.uint8_t.array(bytes * 2);
          }
          let addr = ctypes.cast(pointer, gOffsetByType.ptr).
            contents.addressOfElement(bytes);
          return ctypes.cast(addr, type);
      };
 
-     /**
-      * Import a wide string (e.g. a |jschar.ptr|) as a string.
-      *
-      * @param {CData} wstring The C representation of a widechar string
-      * (can be jschar* or a jschar[]).
-      * @return {string} The same string, as a JavaScript String.
-      */
-     Strings.importWString = function importWString(wstring) {
-       return wstring.readString();
-     };
-
-
-     let Pointers = {};
-     defineLazyGetter(Pointers, "NS_Free",
-       function init_NS_Free() {
-         return exports.OS.Shared.libxul.declare("osfile_ns_free",
-           ctypes.default_abi,
-          /*return*/ Types.void_t.implementation,
-          /*ptr*/ Types.voidptr_t.implementation);
-       });
-
-     /**
-      * Export a string as a wide string (e.g. a |jschar.ptr|).
-      *
-      * @param {string} string A JavaScript String.
-      * @return {CData} The C representation of that string, as a |jschar*|.
-      * This value will be automatically garbage-collected once it is
-      * not referenced anymore.
-      */
-     defineLazyGetter(Strings, "exportWString",
-       function init_exportWString() {
-         return declareFFI(exports.OS.Shared.libxul,
-           "osfile_wstrdup",
-           ctypes.default_abi,
-           /*return*/ Types.out_wstring.releaseWith(Pointers.NS_Free),
-           /*ptr*/ Types.wstring);
-       });
-
 // Encodings
 
-     defineLazyGetter(Strings, "encodeAll",
-       function init_encodeAll() {
-         return declareFFI(exports.OS.Shared.libxul,
-           "osfile_EncodeAll",
-           ctypes.default_abi,
-           /*return*/     Types.void_t.out_ptr.releaseWith(Pointers.NS_Free),
-           /*encoding*/   Types.cstring,
-           /*source*/     Types.wstring,
-           /*bytes*/      Types.uint32_t.out_ptr);
-       });
-
-     defineLazyGetter(Strings, "decodeAll",
-       function init_decodeAll() {
-         let _decodeAll = declareFFI(exports.OS.Shared.libxul, "osfile_DecodeAll",
-           ctypes.default_abi,
-            /*return*/     Types.out_wstring.releaseWith(Pointers.NS_Free),
-            /*encoding*/   Types.cstring,
-            /*source*/     Types.void_t.in_ptr,
-            /*bytes*/      Types.uint32_t);
-         return function decodeAll(encoding, source, bytes) {
-           let decoded = _decodeAll(encoding, source, bytes);
-           if (!decoded) {
-             return null;
-           }
-           return Strings.importWString(decoded);
-          };
-        }
-     );
    })(this);
 }
deleted file mode 100644
--- a/toolkit/components/osfile/osfileutils.cpp
+++ /dev/null
@@ -1,194 +0,0 @@
-
-#include "mozilla/Scoped.h"
-
-#include "osfileutils.h"
-#include "nsICharsetConverterManager.h"
-#include "nsServiceManagerUtils.h"
-#include "nsCOMPtr.h"
-#include "nsCRTGlue.h"
-
-// Utilities for handling errors
-namespace {
-
-#if defined(XP_WIN)
-#include <windows.h>
-
-/**
- * Set the OS-specific error to inform the OS that
- * the last operation failed because it is not supported.
- */
-void error_not_supported() {
-  SetLastError(ERROR_NOT_SUPPORTED);
-}
-
-/**
- * Set the OS-specific error to inform the OS that
- * the last operation failed because of an invalid
- * argument.
- */
-void error_invalid_argument() {
-  SetLastError(ERROR_INVALID_DATA);
-}
-
-/**
- * Set the OS-specific error to inform the OS that
- * the last operation failed because of insufficient
- * memory.
- */
-void error_no_memory() {
-  SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-}
-
-#else
-
-#include "errno.h"
-
-/**
- * Set the OS-specific error to inform the OS that
- * the last operation failed because it is not supported.
- */
-void error_not_supported() {
-  errno = ENOTSUP;
-}
-
-/**
- * Set the OS-specific error to inform the OS that
- * the last operation failed because of an invalid
- * argument.
- */
-void error_invalid_argument() {
-  errno = EINVAL;
-}
-
-/**
- * Set the OS-specific error to inform the OS that
- * the last operation failed because of insufficient
- * memory.
- */
-void error_no_memory() {
-  errno = ENOMEM;
-}
-
-#endif // defined(XP_WIN)
-
-}
-
-
-extern "C" {
-
-// Memory utilities
-
-MOZ_EXPORT_API(void) osfile_ns_free(void* buf) {
-  NS_Free(buf);
-}
-
-// Unicode utilities
-
-MOZ_EXPORT_API(PRUnichar*) osfile_wstrdup(PRUnichar* string) {
-  return NS_strdup(string);
-}
-
-MOZ_EXPORT_API(PRUnichar*) osfile_DecodeAll(
-   const char* aEncoding,
-   const char* aSource,
-   const int32_t aBytesToDecode)
-{
-  if (!aEncoding || !aSource) {
-    error_invalid_argument();
-    return nullptr;
-  }
-
-  nsresult rv;
-  nsCOMPtr<nsICharsetConverterManager> manager =
-    do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
-  if (NS_FAILED(rv)) {
-    error_not_supported();
-    return nullptr;
-  }
-
-  nsCOMPtr<nsIUnicodeDecoder> decoder;
-  rv = manager->GetUnicodeDecoder(aEncoding, getter_AddRefs(decoder));
-  if (NS_FAILED(rv)) {
-    error_invalid_argument();
-    return nullptr;
-  }
-
-  // Compute an upper bound to the number of chars, allocate buffer
-
-  int32_t srcBytes = aBytesToDecode;
-  int32_t upperBoundChars = 0;
-  rv = decoder->GetMaxLength(aSource, srcBytes, &upperBoundChars);
-  MOZ_ASSERT(NS_SUCCEEDED(rv));
-
-  int32_t bufSize = (upperBoundChars + 1) * sizeof (PRUnichar);
-
-  mozilla::ScopedFreePtr<PRUnichar> dest((PRUnichar*)NS_Alloc(bufSize));
-  if (dest.get() == nullptr) {
-    error_no_memory();
-    return nullptr;
-  }
-
-  // Convert, add trailing \0
-
-  rv = decoder->Convert(aSource, &srcBytes, dest.rwget(), &upperBoundChars);
-  if (NS_FAILED(rv)) {
-    error_invalid_argument();
-    return nullptr;
-  }
-
-  dest.rwget()[upperBoundChars] = '\0';
-
-  return dest.forget();
-}
-
-MOZ_EXPORT_API(char*) osfile_EncodeAll(
-   const char* aEncoding,
-   const PRUnichar* aSource,
-   int32_t* aBytesProduced)
-{
-  if (!aEncoding || !aSource || !aBytesProduced) {
-    error_invalid_argument();
-    return nullptr;
-  }
-
-  nsresult rv;
-  nsCOMPtr<nsICharsetConverterManager> manager =
-    do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
-  if (NS_FAILED(rv)) {
-    error_not_supported();
-    return nullptr;
-  }
-
-  nsCOMPtr<nsIUnicodeEncoder> encoder;
-  rv = manager->GetUnicodeEncoder(aEncoding, getter_AddRefs(encoder));
-  if (NS_FAILED(rv)) {
-    error_invalid_argument();
-    return nullptr;
-  }
-
-  int32_t srcChars = NS_strlen(aSource);
-
-  int32_t upperBoundBytes = 0;
-  rv = encoder->GetMaxLength(aSource, srcChars, &upperBoundBytes);
-  MOZ_ASSERT(NS_SUCCEEDED(rv));
-
-  printf_stderr("Encoding %d chars into at up to %d bytes\n", srcChars, upperBoundBytes);
-  int32_t bufSize = upperBoundBytes;
-  mozilla::ScopedFreePtr<char> dest((char*)NS_Alloc(bufSize));
-
-  if (dest.get() == nullptr) {
-    error_no_memory();
-    return nullptr;
-  }
-
-  rv = encoder->Convert(aSource, &srcChars, dest.rwget(), &upperBoundBytes);
-  if (NS_FAILED(rv)) {
-    error_invalid_argument();
-    return nullptr;
-  }
-
-  *aBytesProduced = upperBoundBytes;
-  return dest.forget();
-}
-
-} // extern "C"
deleted file mode 100644
--- a/toolkit/components/osfile/osfileutils.h
+++ /dev/null
@@ -1,65 +0,0 @@
-#ifndef mozilla_osfileutils_h__
-#define mozilla_osfileutils_h__
-
-#include "mozilla/Types.h"
-#include "nsIUnicodeDecoder.h"
-#include "nsIUnicodeEncoder.h"
-
-extern "C" {
-
-// Memory utilities
-
-/**
- * As |NS_Free|, but exported.
- */
-MOZ_EXPORT_API(void) osfile_ns_free(void* buf);
-
-// Unicode utilities
-
-/**
- * Duplicate a Unicode string, as per wpcpy/StrDupW.
- *
- * @param source A well-formed, nul-terminated, Unicode string.
- *
- * @return Either |NULL| if there was not enough memory to copy the
- * string, or a new string with the same contents as |source|.
- * Memory MUST be released with |osfile_ns_free|.
- */
-MOZ_EXPORT_API(PRUnichar*) osfile_wstrdup(PRUnichar* source);
-
-/**
- * Decode a nul-terminated C string into a Unicode string.
- *
- * @param aEncoding The encoding to use.
- * @param aSource The C string to decode.
- * @param aBytesToDecode The number of bytes to decode from |aSource|.
- *
- * @return null in case of error, otherwise a sequence of Unicode
- *   chars, representing |aSource|. This memory MUST be released with
- *   |NS_Free|/|osfile_ns_free|.
- */
-MOZ_EXPORT_API(PRUnichar*) osfile_DecodeAll(
-   const char* aEncoding,
-   const char* aSource,
-   const int32_t aBytesToDecode);
-
-/**
- * Encode a complete Unicode string into a set of bytes.
- *
- * @param aEncoding The encoding to use.
- * @param aSource The Unicode string to encode. Must be nul-terminated.
- * @param aBytesWritten (out) The number of bytes encoded.
- *
- * @return null in case of error, otherwise a new buffer.  The
- * number of bytes actually allocated may be higher than
- * |aBytesWritten|.  The buffer MUST be released with
- * |NS_Free|/|osfile_ns_free|.
- */
-MOZ_EXPORT_API(char*) osfile_EncodeAll(
-   const char* aEncoding,
-   const PRUnichar* aSource,
-   int32_t* aBytesWritten);
-
-} // extern "C"
-
-#endif // mozilla_osfileutils_h__
--- a/toolkit/components/osfile/tests/mochi/worker_test_osfile_front.js
+++ b/toolkit/components/osfile/tests/mochi/worker_test_osfile_front.js
@@ -19,17 +19,16 @@ function should_throw(f) {
 }
 
 self.onmessage = function onmessage_start(msg) {
   self.onmessage = function onmessage_ignored(msg) {
     log("ignored message " + JSON.stringify(msg.data));
   };
   try {
     test_init();
-    test_unicode();
     test_offsetby();
     test_open_existing_file();
     test_open_non_existing_file();
     test_flush_open_file();
     test_copy_existing_file();
     test_readall_writeall_file();
     test_position();
     test_move_file();
@@ -61,40 +60,16 @@ function isnot(a, b, description) {
   send({kind: "isnot", a: a, b:b, description:description});
 }
 
 function test_init() {
   ok(true, "Starting test_init");
   importScripts("resource://gre/modules/osfile.jsm");
 }
 
-
-function test_unicode() {
-  ok(true, "Starting test_unicode");
-  function test_go_round(encoding, sentence)  {
-    let bytes = new OS.Shared.Type.uint32_t.implementation();
-    let pBytes = bytes.address();
-    ok(true, "test_unicode: testing encoding of " + sentence + " with encoding " + encoding);
-    let encoded = OS.Shared.Utils.Strings.encodeAll(encoding, sentence, pBytes);
-    let decoded = OS.Shared.Utils.Strings.decodeAll(encoding, encoded, bytes);
-    isnot(decoded, null, "test_unicode: Decoding returned a string");
-    is(decoded.length, sentence.length, "test_unicode: Decoding + encoding returns strings with the same length");
-    is(decoded, sentence, "test_unicode: Decoding + encoding returns the same string");
-  }
-  let tests = ["This is a simple test","àáâäèéêëíìîïòóôöùúûüçß","骥䥚ぶ 䤦べ祌褦鋨 きょげヒャ蟥誨 もゴ 栩を愦 堦馺ぢょ䰯蟤 禺つみゃ期楥 勩谨障り䶥 蟤れ, 訦き モじゃむ㧦ゔ 勩谨障り䶥 堥駪グェ 竨ぢゅ嶥鏧䧦 捨ヴョに䋯ざ 䦧樚 焯じゅ妦 っ勯杯 堦馺ぢょ䰯蟤 滩シャ饥鎌䧺 珦ひゃ, ざやぎ えゐ へ簯ホゥ馯夦 槎褤せ檨壌","Νισλ αλικυιδ περτινασια ναμ ετ, νε ιρασυνδια νεγλεγενθυρ ηας, νο νυμκυαμ εφφισιενδι φις. Εως μινιμυμ ελειφενδ ατ, κυωτ μαλυισετ φυλπυτατε συμ ιδ."];
-  let encodings = ["utf-8", "utf-16"];
-  for each (let encoding in encodings) {
-    for each (let i in tests) {
-      test_go_round(encoding, i);
-    }
-    test_go_round(encoding, tests.join());
-  }
-  ok(true, "test_unicode: complete");
-}
-
 function test_offsetby() {
   ok(true, "Starting test_offsetby");
 
   // Initialize one array
   let LENGTH = 1024;
   let buf = new ArrayBuffer(LENGTH);
   let view = new Uint8Array(buf);
   let i;