DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (5b81998bb7ab)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <stdio.h>

#include "nsIServiceManager.h"
#include "nsIEventQueueService.h"
#include "nsIOutputStream.h"
#include "nsIStreamListener.h"
#include "nsITransport.h"
#include "nsIInputStream.h"
#include "nsIOutputStream.h"
#include "nsCOMPtr.h"
#include "plstr.h"
#include "prprf.h"

#ifndef USE_CREATE_INSTANCE
#include "nsICacheService.h"
#include "nsICacheSession.h"
#include "nsICacheEntryDescriptor.h"
#include "nsNetCID.h"
static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID);
static nsICacheSession *session = nullptr;
static nsICacheEntryDescriptor *desc = nullptr;
#endif

/**
 * This test program exercises the memory cache's nsITransport implementation.
 *
 * This test program loads a file into the memory cache (using OpenOutputStream),
 * and then reads the file back out (using AsyncRead).  The data read from the
 * memory cache is written to a new file (with .out added as a suffix to the file
 * name).
 */

static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
static nsIEventQueue *gEventQ = nullptr;

class TestListener : public nsIStreamListener
{
public:
    NS_DECL_ISUPPORTS
    NS_DECL_NSISTREAMLISTENER
    NS_DECL_NSIREQUESTOBSERVER

    TestListener(char *);
    virtual ~TestListener();

private:
    char *mFilename;
    FILE *mFile;
};

NS_IMPL_ISUPPORTS2(TestListener,
                   nsIStreamListener,
                   nsIRequestObserver)

TestListener::TestListener(char *filename)
    : mFilename(filename)
    , mFile(nullptr)
{
}

TestListener::~TestListener()
{
}

NS_IMETHODIMP
TestListener::OnStartRequest(nsIRequest *req, nsISupports *ctx)
{
    printf("OnStartRequest\n");

    mFile = fopen(mFilename, "w");
    if (!mFile)
        return NS_ERROR_FAILURE;

    return NS_OK;
}

NS_IMETHODIMP
TestListener::OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status)
{
    printf("OnStopRequest: status=%x\n", status);

    if (mFile)
        fclose(mFile);

    return NS_OK;
}

NS_IMETHODIMP
TestListener::OnDataAvailable(nsIRequest *req, nsISupports *ctx,
                              nsIInputStream *is,
                              uint32_t offset, uint32_t count)
{
    printf("OnDataAvailable: offset=%u count=%u\n", offset, count);

    if (!mFile) return NS_ERROR_FAILURE;

    char buf[128];
    nsresult rv;
    uint32_t nread = 0;

    while (count) {
        uint32_t amount = NS_MIN<uint32_t>(count, sizeof(buf));

        rv = is->Read(buf, amount, &nread);
        if (NS_FAILED(rv)) return rv;

        fwrite(buf, nread, 1, mFile);
        count -= nread;
    }
    return NS_OK;
}

nsresult TestMCTransport(const char *filename)
{
    nsresult rv = NS_OK;
    nsCOMPtr<nsITransport> transport;

#ifdef USE_CREATE_INSTANCE
    transport = do_CreateInstance(
            "@mozilla.org/network/memory-cache-transport;1", &rv);
    if (NS_FAILED(rv))
        return rv;
#else
    nsCOMPtr<nsICacheService> serv(do_GetService(kCacheServiceCID, &rv));
    if (NS_FAILED(rv)) return rv;

    rv = serv->CreateSession("TestMCTransport",
                             nsICache::STORE_IN_MEMORY, true,
                             &session);
    if (NS_FAILED(rv)) return rv;

    rv = session->OpenCacheEntry(nsDependentCString(filename),
                                 nsICache::ACCESS_READ_WRITE,
                                 nsICache::BLOCKING,
                                 &desc);
    if (NS_FAILED(rv)) return rv;

    rv = desc->MarkValid();
    if (NS_FAILED(rv)) return rv;

    rv = desc->GetTransport(getter_AddRefs(transport));
    if (NS_FAILED(rv)) return rv;
#endif

    nsCOMPtr<nsIOutputStream> os;
    rv = transport->OpenOutputStream(0, (uint32_t) -1, 0, getter_AddRefs(os));
    if (NS_FAILED(rv)) return rv;

    char *out = PR_smprintf("%s.out", filename);
    nsCOMPtr<nsIStreamListener> listener = new TestListener(out);
    if (!listener)
        return NS_ERROR_OUT_OF_MEMORY;

    nsCOMPtr<nsIRequest> req;
    rv = transport->AsyncRead(listener, nullptr, 0, (uint32_t) -1, 0, getter_AddRefs(req));
    if (NS_FAILED(rv)) return rv;

    FILE *file = fopen(filename, "r");
    if (!file)
        return NS_ERROR_FILE_NOT_FOUND;

    char buf[256];
    uint32_t count, total=0;

    while ((count = fread(buf, 1, sizeof(buf), file)) > 0) {
        printf("writing %u bytes\n", count);
        total += count;
        rv = os->Write(buf, count, &count);
        if (NS_FAILED(rv)) return rv;

        // process an event
        PLEvent *event = nullptr;
        gEventQ->GetEvent(&event);
        if (event) gEventQ->HandleEvent(event);
    }

    printf("wrote %u bytes\n", total);

    return rv;
}

int main(int argc, char **argv)
{
    nsresult rv;

    if (argc < 2) {
        printf("usage: %s filename\n", argv[0]);
        return -1;
    }

    nsCOMPtr<nsIEventQueueService> eqs = 
             do_GetService(kEventQueueServiceCID, &rv);
    if (NS_FAILED(rv)) {
        printf("failed to create event queue service: rv=%x\n", rv);
        return -1;
    }

    rv = eqs->CreateMonitoredThreadEventQueue();
    if (NS_FAILED(rv)) {
        printf("failed to create monitored event queue: rv=%x\n", rv);
        return -1;
    }

    rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
    if (NS_FAILED(rv)) {
        printf("failed to get thread event queue: %x\n", rv);
        return -1;
    }

    rv = TestMCTransport(argv[1]);
    printf("TestMCTransport returned %x\n", rv);

    gEventQ->ProcessPendingEvents();

#ifndef USE_CREATE_INSTANCE
    NS_IF_RELEASE(desc);
    NS_IF_RELEASE(session);
#endif
    return 0;
}