netwerk/streamconv/test/Converters.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/streamconv/test/Converters.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,140 @@
     1.4 +#include "Converters.h"
     1.5 +#include "nsIStringStream.h"
     1.6 +#include "nsCOMPtr.h"
     1.7 +#include "nsComponentManagerUtils.h"
     1.8 +
     1.9 +#include <stdio.h>
    1.10 +
    1.11 +//////////////////////////////////////////////////
    1.12 +// TestConverter
    1.13 +//////////////////////////////////////////////////
    1.14 +
    1.15 +#define NS_TESTCONVERTER_CID                         \
    1.16 +{ /* B8A067B0-4450-11d3-A16E-0050041CAF44 */         \
    1.17 +    0xb8a067b0,                                      \
    1.18 +    0x4450,                                          \
    1.19 +    0x11d3,                                          \
    1.20 +    {0xa1, 0x6e, 0x00, 0x50, 0x04, 0x1c, 0xaf, 0x44} \
    1.21 +}
    1.22 +
    1.23 +NS_DEFINE_CID(kTestConverterCID, NS_TESTCONVERTER_CID);
    1.24 +
    1.25 +NS_IMPL_ISUPPORTS(TestConverter,
    1.26 +                  nsIStreamConverter,
    1.27 +                  nsIStreamListener,
    1.28 +                  nsIRequestObserver)
    1.29 +
    1.30 +TestConverter::TestConverter() {
    1.31 +}
    1.32 +
    1.33 +// Convert aFromStream (of type aFromType), to _retval (nsIInputStream of type aToType).
    1.34 +// This Convert method simply converts the stream byte-by-byte, to the first character
    1.35 +// in the aToType "string".
    1.36 +NS_IMETHODIMP
    1.37 +TestConverter::Convert(nsIInputStream *aFromStream, 
    1.38 +                       const char *aFromType, 
    1.39 +                       const char *aToType, 
    1.40 +                       nsISupports *ctxt, 
    1.41 +                       nsIInputStream **_retval) {
    1.42 +    char buf[1024+1];
    1.43 +    uint32_t read;
    1.44 +    nsresult rv = aFromStream->Read(buf, 1024, &read);
    1.45 +    if (NS_FAILED(rv) || read == 0) return rv;
    1.46 +
    1.47 +    // verify that the data we're converting matches the from type
    1.48 +    // if it doesn't then we're being handed the wrong data.
    1.49 +    char fromChar = *aFromType;
    1.50 +
    1.51 +    if (fromChar != buf[0]) {
    1.52 +        printf("We're receiving %c, but are supposed to have %c.\n", buf[0], fromChar);
    1.53 +        return NS_ERROR_FAILURE;
    1.54 +    }
    1.55 +
    1.56 +
    1.57 +    // Get the first character 
    1.58 +    char toChar = *aToType;
    1.59 +
    1.60 +    for (uint32_t i = 0; i < read; i++) 
    1.61 +        buf[i] = toChar;
    1.62 +
    1.63 +    buf[read] = '\0';
    1.64 +
    1.65 +    nsCOMPtr<nsIStringInputStream> str
    1.66 +      (do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv));
    1.67 +    NS_ENSURE_SUCCESS(rv, rv);
    1.68 +
    1.69 +    rv = str->SetData(buf, read);
    1.70 +    NS_ENSURE_SUCCESS(rv, rv);
    1.71 +
    1.72 +    NS_ADDREF(*_retval = str);
    1.73 +    return NS_OK;
    1.74 +}
    1.75 +
    1.76 +/* This method initializes any internal state before the stream converter
    1.77 + * begins asynchronous conversion */
    1.78 +NS_IMETHODIMP
    1.79 +TestConverter::AsyncConvertData(const char *aFromType,
    1.80 +                                const char *aToType, 
    1.81 +                                nsIStreamListener *aListener, 
    1.82 +                                nsISupports *ctxt) {
    1.83 +    NS_ASSERTION(aListener, "null listener");
    1.84 +
    1.85 +    mListener = aListener;
    1.86 +
    1.87 +    // based on these types, setup internal state to handle the appropriate conversion.
    1.88 +    fromType = aFromType;
    1.89 +    toType = aToType;
    1.90 +
    1.91 +    return NS_OK; 
    1.92 +}
    1.93 +
    1.94 +// nsIStreamListener method
    1.95 +/* This method handles asyncronous conversion of data. */
    1.96 +NS_IMETHODIMP
    1.97 +TestConverter::OnDataAvailable(nsIRequest* request,
    1.98 +                               nsISupports *ctxt, 
    1.99 +                               nsIInputStream *inStr, 
   1.100 +                               uint64_t sourceOffset, 
   1.101 +                               uint32_t count) {
   1.102 +    nsresult rv;
   1.103 +    nsCOMPtr<nsIInputStream> convertedStream;
   1.104 +    // just make a syncronous call to the Convert() method.
   1.105 +    // Anything can happen here, I just happen to be using the sync call to 
   1.106 +    // do the actual conversion.
   1.107 +    rv = Convert(inStr, fromType.get(), toType.get(), ctxt, getter_AddRefs(convertedStream));
   1.108 +    if (NS_FAILED(rv)) return rv;
   1.109 +
   1.110 +    uint64_t len = 0;
   1.111 +    convertedStream->Available(&len);
   1.112 +
   1.113 +    uint64_t offset = sourceOffset;
   1.114 +    while (len > 0) {
   1.115 +        uint32_t count = saturated(len);
   1.116 +        rv = mListener->OnDataAvailable(request, ctxt, convertedStream, offset, count);
   1.117 +        if (NS_FAILED(rv)) return rv;
   1.118 +
   1.119 +        offset += count;
   1.120 +        len -= count;
   1.121 +    }
   1.122 +    return NS_OK;
   1.123 +}
   1.124 +
   1.125 +// nsIRequestObserver methods
   1.126 +/* These methods just pass through directly to the mListener */
   1.127 +NS_IMETHODIMP
   1.128 +TestConverter::OnStartRequest(nsIRequest* request, nsISupports *ctxt) {
   1.129 +    return mListener->OnStartRequest(request, ctxt);
   1.130 +}
   1.131 +
   1.132 +NS_IMETHODIMP
   1.133 +TestConverter::OnStopRequest(nsIRequest* request, nsISupports *ctxt, 
   1.134 +                             nsresult aStatus) {
   1.135 +    return mListener->OnStopRequest(request, ctxt, aStatus);
   1.136 +}
   1.137 +
   1.138 +nsresult
   1.139 +CreateTestConverter(nsISupports* aOuter, REFNSIID aIID, void** aResult)
   1.140 +{
   1.141 +  nsCOMPtr<nsISupports> conv = new TestConverter();
   1.142 +  return conv->QueryInterface(aIID, aResult);
   1.143 +}

mercurial