xpcom/io/nsStringStream.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/io/nsStringStream.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,407 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* vim:set ts=4 sts=4 sw=4 cin et: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/**
    1.11 + * Based on original code from nsIStringStream.cpp
    1.12 + */
    1.13 +
    1.14 +#include "ipc/IPCMessageUtils.h"
    1.15 +
    1.16 +#include "nsStringStream.h"
    1.17 +#include "nsStreamUtils.h"
    1.18 +#include "nsReadableUtils.h"
    1.19 +#include "nsISeekableStream.h"
    1.20 +#include "nsISupportsPrimitives.h"
    1.21 +#include "nsCRT.h"
    1.22 +#include "prerror.h"
    1.23 +#include "plstr.h"
    1.24 +#include "nsIClassInfoImpl.h"
    1.25 +#include "mozilla/Attributes.h"
    1.26 +#include "mozilla/ipc/InputStreamUtils.h"
    1.27 +#include "nsIIPCSerializableInputStream.h"
    1.28 +
    1.29 +using namespace mozilla::ipc;
    1.30 +
    1.31 +//-----------------------------------------------------------------------------
    1.32 +// nsIStringInputStream implementation
    1.33 +//-----------------------------------------------------------------------------
    1.34 +
    1.35 +class nsStringInputStream MOZ_FINAL : public nsIStringInputStream
    1.36 +                                    , public nsISeekableStream
    1.37 +                                    , public nsISupportsCString
    1.38 +                                    , public nsIIPCSerializableInputStream
    1.39 +{
    1.40 +public:
    1.41 +    NS_DECL_THREADSAFE_ISUPPORTS
    1.42 +    NS_DECL_NSIINPUTSTREAM
    1.43 +    NS_DECL_NSISTRINGINPUTSTREAM
    1.44 +    NS_DECL_NSISEEKABLESTREAM
    1.45 +    NS_DECL_NSISUPPORTSPRIMITIVE
    1.46 +    NS_DECL_NSISUPPORTSCSTRING
    1.47 +    NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
    1.48 +
    1.49 +    nsStringInputStream()
    1.50 +    {
    1.51 +        Clear();
    1.52 +    }
    1.53 +
    1.54 +private:
    1.55 +    ~nsStringInputStream()
    1.56 +    {}
    1.57 +
    1.58 +    uint32_t Length() const
    1.59 +    {
    1.60 +        return mData.Length();
    1.61 +    }
    1.62 +
    1.63 +    uint32_t LengthRemaining() const
    1.64 +    {
    1.65 +        return Length() - mOffset;
    1.66 +    }
    1.67 +
    1.68 +    void Clear()
    1.69 +    {
    1.70 +        mData.SetIsVoid(true);
    1.71 +    }
    1.72 +
    1.73 +    bool Closed()
    1.74 +    {
    1.75 +        return mData.IsVoid();
    1.76 +    }
    1.77 +
    1.78 +    nsDependentCSubstring mData;
    1.79 +    uint32_t mOffset;
    1.80 +};
    1.81 +
    1.82 +// This class needs to support threadsafe refcounting since people often
    1.83 +// allocate a string stream, and then read it from a background thread.
    1.84 +NS_IMPL_ADDREF(nsStringInputStream)
    1.85 +NS_IMPL_RELEASE(nsStringInputStream)
    1.86 +
    1.87 +NS_IMPL_CLASSINFO(nsStringInputStream, nullptr, nsIClassInfo::THREADSAFE,
    1.88 +                  NS_STRINGINPUTSTREAM_CID)
    1.89 +NS_IMPL_QUERY_INTERFACE_CI(nsStringInputStream,
    1.90 +                           nsIStringInputStream,
    1.91 +                           nsIInputStream,
    1.92 +                           nsISupportsCString,
    1.93 +                           nsISeekableStream,
    1.94 +                           nsIIPCSerializableInputStream)
    1.95 +NS_IMPL_CI_INTERFACE_GETTER(nsStringInputStream,
    1.96 +                            nsIStringInputStream,
    1.97 +                            nsIInputStream,
    1.98 +                            nsISupportsCString,
    1.99 +                            nsISeekableStream)
   1.100 +
   1.101 +/////////
   1.102 +// nsISupportsCString implementation
   1.103 +/////////
   1.104 +
   1.105 +NS_IMETHODIMP
   1.106 +nsStringInputStream::GetType(uint16_t *type)
   1.107 +{
   1.108 +    *type = TYPE_CSTRING;
   1.109 +    return NS_OK;
   1.110 +}
   1.111 +
   1.112 +NS_IMETHODIMP
   1.113 +nsStringInputStream::GetData(nsACString &data)
   1.114 +{
   1.115 +    // The stream doesn't have any data when it is closed.  We could fake it
   1.116 +    // and return an empty string here, but it seems better to keep this return
   1.117 +    // value consistent with the behavior of the other 'getter' methods.
   1.118 +    if (NS_WARN_IF(Closed()))
   1.119 +        return NS_BASE_STREAM_CLOSED;
   1.120 +
   1.121 +    data.Assign(mData);
   1.122 +    return NS_OK;
   1.123 +}
   1.124 +
   1.125 +NS_IMETHODIMP
   1.126 +nsStringInputStream::SetData(const nsACString &data)
   1.127 +{
   1.128 +    mData.Assign(data);
   1.129 +    mOffset = 0;
   1.130 +    return NS_OK;
   1.131 +}
   1.132 +
   1.133 +NS_IMETHODIMP
   1.134 +nsStringInputStream::ToString(char **result)
   1.135 +{
   1.136 +    // NOTE: This method may result in data loss, so we do not implement it.
   1.137 +    return NS_ERROR_NOT_IMPLEMENTED;
   1.138 +}
   1.139 +
   1.140 +/////////
   1.141 +// nsIStringInputStream implementation
   1.142 +/////////
   1.143 +
   1.144 +NS_IMETHODIMP
   1.145 +nsStringInputStream::SetData(const char *data, int32_t dataLen)
   1.146 +{
   1.147 +    if (NS_WARN_IF(!data))
   1.148 +        return NS_ERROR_INVALID_ARG;
   1.149 +    mData.Assign(data, dataLen);
   1.150 +    mOffset = 0;
   1.151 +    return NS_OK;
   1.152 +}
   1.153 +
   1.154 +NS_IMETHODIMP
   1.155 +nsStringInputStream::AdoptData(char *data, int32_t dataLen)
   1.156 +{
   1.157 +    if (NS_WARN_IF(!data))
   1.158 +        return NS_ERROR_INVALID_ARG;
   1.159 +    mData.Adopt(data, dataLen);
   1.160 +    mOffset = 0;
   1.161 +    return NS_OK;
   1.162 +}
   1.163 +
   1.164 +NS_IMETHODIMP
   1.165 +nsStringInputStream::ShareData(const char *data, int32_t dataLen)
   1.166 +{
   1.167 +    if (NS_WARN_IF(!data))
   1.168 +        return NS_ERROR_INVALID_ARG;
   1.169 +
   1.170 +    if (dataLen < 0)
   1.171 +        dataLen = strlen(data);
   1.172 +
   1.173 +    mData.Rebind(data, dataLen);
   1.174 +    mOffset = 0;
   1.175 +    return NS_OK;
   1.176 +}
   1.177 +
   1.178 +/////////
   1.179 +// nsIInputStream implementation
   1.180 +/////////
   1.181 +
   1.182 +NS_IMETHODIMP
   1.183 +nsStringInputStream::Close()
   1.184 +{
   1.185 +    Clear();
   1.186 +    return NS_OK;
   1.187 +}
   1.188 +    
   1.189 +NS_IMETHODIMP
   1.190 +nsStringInputStream::Available(uint64_t *aLength)
   1.191 +{
   1.192 +    NS_ASSERTION(aLength, "null ptr");
   1.193 +
   1.194 +    if (Closed())
   1.195 +        return NS_BASE_STREAM_CLOSED;
   1.196 +
   1.197 +    *aLength = LengthRemaining();
   1.198 +    return NS_OK;
   1.199 +}
   1.200 +
   1.201 +NS_IMETHODIMP
   1.202 +nsStringInputStream::Read(char* aBuf, uint32_t aCount, uint32_t *aReadCount)
   1.203 +{
   1.204 +    NS_ASSERTION(aBuf, "null ptr");
   1.205 +    return ReadSegments(NS_CopySegmentToBuffer, aBuf, aCount, aReadCount);
   1.206 +}
   1.207 +
   1.208 +NS_IMETHODIMP
   1.209 +nsStringInputStream::ReadSegments(nsWriteSegmentFun writer, void *closure,
   1.210 +                                  uint32_t aCount, uint32_t *result)
   1.211 +{
   1.212 +    NS_ASSERTION(result, "null ptr");
   1.213 +    NS_ASSERTION(Length() >= mOffset, "bad stream state");
   1.214 +
   1.215 +    if (Closed())
   1.216 +        return NS_BASE_STREAM_CLOSED;
   1.217 +
   1.218 +    // We may be at end-of-file
   1.219 +    uint32_t maxCount = LengthRemaining();
   1.220 +    if (maxCount == 0) {
   1.221 +        *result = 0;
   1.222 +        return NS_OK;
   1.223 +    }
   1.224 +
   1.225 +    if (aCount > maxCount)
   1.226 +        aCount = maxCount;
   1.227 +    nsresult rv = writer(this, closure, mData.BeginReading() + mOffset, 0, aCount, result);
   1.228 +    if (NS_SUCCEEDED(rv)) {
   1.229 +        NS_ASSERTION(*result <= aCount,
   1.230 +                     "writer should not write more than we asked it to write");
   1.231 +        mOffset += *result;
   1.232 +    }
   1.233 +
   1.234 +    // errors returned from the writer end here!
   1.235 +    return NS_OK;
   1.236 +}
   1.237 +    
   1.238 +NS_IMETHODIMP
   1.239 +nsStringInputStream::IsNonBlocking(bool *aNonBlocking)
   1.240 +{
   1.241 +    *aNonBlocking = true;
   1.242 +    return NS_OK;
   1.243 +}
   1.244 +
   1.245 +/////////
   1.246 +// nsISeekableStream implementation
   1.247 +/////////
   1.248 +
   1.249 +NS_IMETHODIMP 
   1.250 +nsStringInputStream::Seek(int32_t whence, int64_t offset)
   1.251 +{
   1.252 +    if (Closed())
   1.253 +        return NS_BASE_STREAM_CLOSED;
   1.254 +
   1.255 +    // Compute new stream position.  The given offset may be a negative value.
   1.256 + 
   1.257 +    int64_t newPos = offset;
   1.258 +    switch (whence) {
   1.259 +    case NS_SEEK_SET:
   1.260 +        break;
   1.261 +    case NS_SEEK_CUR:
   1.262 +        newPos += mOffset;
   1.263 +        break;
   1.264 +    case NS_SEEK_END:
   1.265 +        newPos += Length();
   1.266 +        break;
   1.267 +    default:
   1.268 +        NS_ERROR("invalid whence");
   1.269 +        return NS_ERROR_INVALID_ARG;
   1.270 +    }
   1.271 +
   1.272 +    if (NS_WARN_IF(newPos < 0) || NS_WARN_IF(newPos > Length()))
   1.273 +        return NS_ERROR_INVALID_ARG;
   1.274 +
   1.275 +    mOffset = (uint32_t)newPos;
   1.276 +    return NS_OK;
   1.277 +}
   1.278 +
   1.279 +NS_IMETHODIMP
   1.280 +nsStringInputStream::Tell(int64_t* outWhere)
   1.281 +{
   1.282 +    if (Closed())
   1.283 +        return NS_BASE_STREAM_CLOSED;
   1.284 +
   1.285 +    *outWhere = mOffset;
   1.286 +    return NS_OK;
   1.287 +}
   1.288 +
   1.289 +NS_IMETHODIMP
   1.290 +nsStringInputStream::SetEOF()
   1.291 +{
   1.292 +    if (Closed())
   1.293 +        return NS_BASE_STREAM_CLOSED;
   1.294 +
   1.295 +    mOffset = Length();
   1.296 +    return NS_OK;
   1.297 +}
   1.298 +
   1.299 +void
   1.300 +nsStringInputStream::Serialize(InputStreamParams& aParams,
   1.301 +                               FileDescriptorArray& /* aFDs */)
   1.302 +{
   1.303 +    StringInputStreamParams params;
   1.304 +    params.data() = PromiseFlatCString(mData);
   1.305 +    aParams = params;
   1.306 +}
   1.307 +
   1.308 +bool
   1.309 +nsStringInputStream::Deserialize(const InputStreamParams& aParams,
   1.310 +                                 const FileDescriptorArray& /* aFDs */)
   1.311 +{
   1.312 +    if (aParams.type() != InputStreamParams::TStringInputStreamParams) {
   1.313 +        NS_ERROR("Received unknown parameters from the other process!");
   1.314 +        return false;
   1.315 +    }
   1.316 +
   1.317 +    const StringInputStreamParams& params =
   1.318 +        aParams.get_StringInputStreamParams();
   1.319 +
   1.320 +    if (NS_FAILED(SetData(params.data()))) {
   1.321 +        NS_WARNING("SetData failed!");
   1.322 +        return false;
   1.323 +    }
   1.324 +
   1.325 +    return true;
   1.326 +}
   1.327 +
   1.328 +nsresult
   1.329 +NS_NewByteInputStream(nsIInputStream** aStreamResult,
   1.330 +                      const char* aStringToRead, int32_t aLength,
   1.331 +                      nsAssignmentType aAssignment)
   1.332 +{
   1.333 +    NS_PRECONDITION(aStreamResult, "null out ptr");
   1.334 +
   1.335 +    nsStringInputStream* stream = new nsStringInputStream();
   1.336 +    if (! stream)
   1.337 +        return NS_ERROR_OUT_OF_MEMORY;
   1.338 +
   1.339 +    NS_ADDREF(stream);
   1.340 +
   1.341 +    nsresult rv;
   1.342 +    switch (aAssignment) {
   1.343 +    case NS_ASSIGNMENT_COPY:
   1.344 +        rv = stream->SetData(aStringToRead, aLength);
   1.345 +        break;
   1.346 +    case NS_ASSIGNMENT_DEPEND:
   1.347 +        rv = stream->ShareData(aStringToRead, aLength);
   1.348 +        break;
   1.349 +    case NS_ASSIGNMENT_ADOPT:
   1.350 +        rv = stream->AdoptData(const_cast<char*>(aStringToRead), aLength);
   1.351 +        break;
   1.352 +    default:
   1.353 +        NS_ERROR("invalid assignment type");
   1.354 +        rv = NS_ERROR_INVALID_ARG;
   1.355 +    }
   1.356 +    
   1.357 +    if (NS_FAILED(rv)) {
   1.358 +        NS_RELEASE(stream);
   1.359 +        return rv;
   1.360 +    }
   1.361 +    
   1.362 +    *aStreamResult = stream;
   1.363 +    return NS_OK;
   1.364 +}
   1.365 +
   1.366 +nsresult
   1.367 +NS_NewStringInputStream(nsIInputStream** aStreamResult,
   1.368 +                        const nsAString& aStringToRead)
   1.369 +{
   1.370 +    NS_LossyConvertUTF16toASCII data(aStringToRead); // truncates high-order bytes
   1.371 +    return NS_NewCStringInputStream(aStreamResult, data);
   1.372 +}
   1.373 +
   1.374 +nsresult
   1.375 +NS_NewCStringInputStream(nsIInputStream** aStreamResult,
   1.376 +                         const nsACString& aStringToRead)
   1.377 +{
   1.378 +    NS_PRECONDITION(aStreamResult, "null out ptr");
   1.379 +
   1.380 +    nsStringInputStream* stream = new nsStringInputStream();
   1.381 +    if (! stream)
   1.382 +        return NS_ERROR_OUT_OF_MEMORY;
   1.383 +
   1.384 +    NS_ADDREF(stream);
   1.385 +
   1.386 +    stream->SetData(aStringToRead);
   1.387 +
   1.388 +    *aStreamResult = stream;
   1.389 +    return NS_OK;
   1.390 +}
   1.391 +
   1.392 +// factory method for constructing a nsStringInputStream object
   1.393 +nsresult
   1.394 +nsStringInputStreamConstructor(nsISupports *outer, REFNSIID iid, void **result)
   1.395 +{
   1.396 +    *result = nullptr;
   1.397 +
   1.398 +    if (NS_WARN_IF(outer))
   1.399 +        return NS_ERROR_NO_AGGREGATION;
   1.400 +
   1.401 +    nsStringInputStream *inst = new nsStringInputStream();
   1.402 +    if (!inst)
   1.403 +        return NS_ERROR_OUT_OF_MEMORY;
   1.404 +
   1.405 +    NS_ADDREF(inst);
   1.406 +    nsresult rv = inst->QueryInterface(iid, result);
   1.407 +    NS_RELEASE(inst);
   1.408 +
   1.409 +    return rv;
   1.410 +}

mercurial