Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | #include "Converters.h" |
michael@0 | 2 | #include "nsIStringStream.h" |
michael@0 | 3 | #include "nsCOMPtr.h" |
michael@0 | 4 | #include "nsComponentManagerUtils.h" |
michael@0 | 5 | |
michael@0 | 6 | #include <stdio.h> |
michael@0 | 7 | |
michael@0 | 8 | ////////////////////////////////////////////////// |
michael@0 | 9 | // TestConverter |
michael@0 | 10 | ////////////////////////////////////////////////// |
michael@0 | 11 | |
michael@0 | 12 | #define NS_TESTCONVERTER_CID \ |
michael@0 | 13 | { /* B8A067B0-4450-11d3-A16E-0050041CAF44 */ \ |
michael@0 | 14 | 0xb8a067b0, \ |
michael@0 | 15 | 0x4450, \ |
michael@0 | 16 | 0x11d3, \ |
michael@0 | 17 | {0xa1, 0x6e, 0x00, 0x50, 0x04, 0x1c, 0xaf, 0x44} \ |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | NS_DEFINE_CID(kTestConverterCID, NS_TESTCONVERTER_CID); |
michael@0 | 21 | |
michael@0 | 22 | NS_IMPL_ISUPPORTS(TestConverter, |
michael@0 | 23 | nsIStreamConverter, |
michael@0 | 24 | nsIStreamListener, |
michael@0 | 25 | nsIRequestObserver) |
michael@0 | 26 | |
michael@0 | 27 | TestConverter::TestConverter() { |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | // Convert aFromStream (of type aFromType), to _retval (nsIInputStream of type aToType). |
michael@0 | 31 | // This Convert method simply converts the stream byte-by-byte, to the first character |
michael@0 | 32 | // in the aToType "string". |
michael@0 | 33 | NS_IMETHODIMP |
michael@0 | 34 | TestConverter::Convert(nsIInputStream *aFromStream, |
michael@0 | 35 | const char *aFromType, |
michael@0 | 36 | const char *aToType, |
michael@0 | 37 | nsISupports *ctxt, |
michael@0 | 38 | nsIInputStream **_retval) { |
michael@0 | 39 | char buf[1024+1]; |
michael@0 | 40 | uint32_t read; |
michael@0 | 41 | nsresult rv = aFromStream->Read(buf, 1024, &read); |
michael@0 | 42 | if (NS_FAILED(rv) || read == 0) return rv; |
michael@0 | 43 | |
michael@0 | 44 | // verify that the data we're converting matches the from type |
michael@0 | 45 | // if it doesn't then we're being handed the wrong data. |
michael@0 | 46 | char fromChar = *aFromType; |
michael@0 | 47 | |
michael@0 | 48 | if (fromChar != buf[0]) { |
michael@0 | 49 | printf("We're receiving %c, but are supposed to have %c.\n", buf[0], fromChar); |
michael@0 | 50 | return NS_ERROR_FAILURE; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | |
michael@0 | 54 | // Get the first character |
michael@0 | 55 | char toChar = *aToType; |
michael@0 | 56 | |
michael@0 | 57 | for (uint32_t i = 0; i < read; i++) |
michael@0 | 58 | buf[i] = toChar; |
michael@0 | 59 | |
michael@0 | 60 | buf[read] = '\0'; |
michael@0 | 61 | |
michael@0 | 62 | nsCOMPtr<nsIStringInputStream> str |
michael@0 | 63 | (do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv)); |
michael@0 | 64 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 65 | |
michael@0 | 66 | rv = str->SetData(buf, read); |
michael@0 | 67 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 68 | |
michael@0 | 69 | NS_ADDREF(*_retval = str); |
michael@0 | 70 | return NS_OK; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | /* This method initializes any internal state before the stream converter |
michael@0 | 74 | * begins asynchronous conversion */ |
michael@0 | 75 | NS_IMETHODIMP |
michael@0 | 76 | TestConverter::AsyncConvertData(const char *aFromType, |
michael@0 | 77 | const char *aToType, |
michael@0 | 78 | nsIStreamListener *aListener, |
michael@0 | 79 | nsISupports *ctxt) { |
michael@0 | 80 | NS_ASSERTION(aListener, "null listener"); |
michael@0 | 81 | |
michael@0 | 82 | mListener = aListener; |
michael@0 | 83 | |
michael@0 | 84 | // based on these types, setup internal state to handle the appropriate conversion. |
michael@0 | 85 | fromType = aFromType; |
michael@0 | 86 | toType = aToType; |
michael@0 | 87 | |
michael@0 | 88 | return NS_OK; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | // nsIStreamListener method |
michael@0 | 92 | /* This method handles asyncronous conversion of data. */ |
michael@0 | 93 | NS_IMETHODIMP |
michael@0 | 94 | TestConverter::OnDataAvailable(nsIRequest* request, |
michael@0 | 95 | nsISupports *ctxt, |
michael@0 | 96 | nsIInputStream *inStr, |
michael@0 | 97 | uint64_t sourceOffset, |
michael@0 | 98 | uint32_t count) { |
michael@0 | 99 | nsresult rv; |
michael@0 | 100 | nsCOMPtr<nsIInputStream> convertedStream; |
michael@0 | 101 | // just make a syncronous call to the Convert() method. |
michael@0 | 102 | // Anything can happen here, I just happen to be using the sync call to |
michael@0 | 103 | // do the actual conversion. |
michael@0 | 104 | rv = Convert(inStr, fromType.get(), toType.get(), ctxt, getter_AddRefs(convertedStream)); |
michael@0 | 105 | if (NS_FAILED(rv)) return rv; |
michael@0 | 106 | |
michael@0 | 107 | uint64_t len = 0; |
michael@0 | 108 | convertedStream->Available(&len); |
michael@0 | 109 | |
michael@0 | 110 | uint64_t offset = sourceOffset; |
michael@0 | 111 | while (len > 0) { |
michael@0 | 112 | uint32_t count = saturated(len); |
michael@0 | 113 | rv = mListener->OnDataAvailable(request, ctxt, convertedStream, offset, count); |
michael@0 | 114 | if (NS_FAILED(rv)) return rv; |
michael@0 | 115 | |
michael@0 | 116 | offset += count; |
michael@0 | 117 | len -= count; |
michael@0 | 118 | } |
michael@0 | 119 | return NS_OK; |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | // nsIRequestObserver methods |
michael@0 | 123 | /* These methods just pass through directly to the mListener */ |
michael@0 | 124 | NS_IMETHODIMP |
michael@0 | 125 | TestConverter::OnStartRequest(nsIRequest* request, nsISupports *ctxt) { |
michael@0 | 126 | return mListener->OnStartRequest(request, ctxt); |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | NS_IMETHODIMP |
michael@0 | 130 | TestConverter::OnStopRequest(nsIRequest* request, nsISupports *ctxt, |
michael@0 | 131 | nsresult aStatus) { |
michael@0 | 132 | return mListener->OnStopRequest(request, ctxt, aStatus); |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | nsresult |
michael@0 | 136 | CreateTestConverter(nsISupports* aOuter, REFNSIID aIID, void** aResult) |
michael@0 | 137 | { |
michael@0 | 138 | nsCOMPtr<nsISupports> conv = new TestConverter(); |
michael@0 | 139 | return conv->QueryInterface(aIID, aResult); |
michael@0 | 140 | } |