dom/encoding/TextDecoder.h

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 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #ifndef mozilla_dom_textdecoder_h_
     6 #define mozilla_dom_textdecoder_h_
     8 #include "mozilla/dom/NonRefcountedDOMObject.h"
     9 #include "mozilla/dom/TextDecoderBinding.h"
    10 #include "mozilla/dom/TypedArray.h"
    11 #include "nsIUnicodeDecoder.h"
    13 namespace mozilla {
    15 class ErrorResult;
    17 namespace dom {
    19 class TextDecoder MOZ_FINAL
    20   : public NonRefcountedDOMObject
    21 {
    22 public:
    23   // The WebIDL constructor.
    24   static TextDecoder*
    25   Constructor(const GlobalObject& aGlobal,
    26               const nsAString& aEncoding,
    27               const TextDecoderOptions& aOptions,
    28               ErrorResult& aRv)
    29   {
    30     nsAutoPtr<TextDecoder> txtDecoder(new TextDecoder());
    31     txtDecoder->Init(aEncoding, aOptions.mFatal, aRv);
    32     if (aRv.Failed()) {
    33       return nullptr;
    34     }
    35     return txtDecoder.forget();
    36   }
    38   TextDecoder()
    39     : mFatal(false)
    40   {
    41     MOZ_COUNT_CTOR(TextDecoder);
    42   }
    44   ~TextDecoder()
    45   {
    46     MOZ_COUNT_DTOR(TextDecoder);
    47   }
    49   JSObject* WrapObject(JSContext* aCx, bool* aTookOwnership)
    50   {
    51     return TextDecoderBinding::Wrap(aCx, this, aTookOwnership);
    52   }
    54   /**
    55    * Validates provided label and throws an exception if invalid label.
    56    *
    57    * @param aLabel       The encoding label (case insensitive) provided.
    58    * @param aFatal       indicates whether to throw an 'EncodingError'
    59    *                     exception or not when decoding.
    60    * @return aRv         EncodingError exception else null.
    61    */
    62   void Init(const nsAString& aLabel, const bool aFatal, ErrorResult& aRv);
    64   /**
    65    * Performs initialization with a Gecko-canonical encoding name (as opposed
    66    * to a label.)
    67    *
    68    * @param aEncoding    A Gecko-canonical encoding name
    69    * @param aFatal       indicates whether to throw an 'EncodingError'
    70    *                     exception or not when decoding.
    71    */
    72   void InitWithEncoding(const nsACString& aEncoding, const bool aFatal);
    74   /**
    75    * Return the encoding name.
    76    *
    77    * @param aEncoding, current encoding.
    78    */
    79   void GetEncoding(nsAString& aEncoding);
    81   /**
    82    * Decodes incoming byte stream of characters in charset indicated by
    83    * encoding.
    84    *
    85    * The encoding algorithm state is reset if aOptions.mStream is not set.
    86    *
    87    * If the fatal flag is set then a decoding error will throw EncodingError.
    88    * Else the decoder will return a decoded string with replacement
    89    * character(s) for unidentified character(s).
    90    *
    91    * @param      aView, incoming byte stream of characters to be decoded to
    92    *                    to UTF-16 code points.
    93    * @param      aOptions, indicates if streaming or not.
    94    * @param      aOutDecodedString, decoded string of UTF-16 code points.
    95    * @param      aRv, error result.
    96    */
    97   void Decode(const char* aInput, const int32_t aLength,
    98               const bool aStream, nsAString& aOutDecodedString,
    99               ErrorResult& aRv);
   101   void Decode(nsAString& aOutDecodedString,
   102               ErrorResult& aRv) {
   103     Decode(nullptr, 0, false, aOutDecodedString, aRv);
   104   }
   106   void Decode(const ArrayBufferView& aView,
   107               const TextDecodeOptions& aOptions,
   108               nsAString& aOutDecodedString,
   109               ErrorResult& aRv) {
   110     aView.ComputeLengthAndData();
   111     Decode(reinterpret_cast<char*>(aView.Data()), aView.Length(),
   112            aOptions.mStream, aOutDecodedString, aRv);
   113   }
   115 private:
   116   nsCString mEncoding;
   117   nsCOMPtr<nsIUnicodeDecoder> mDecoder;
   118   bool mFatal;
   119 };
   121 } // dom
   122 } // mozilla
   124 #endif // mozilla_dom_textdecoder_h_

mercurial