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 (4a108e94d3e2)

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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
/* -*- 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 "TestCommon.h"
#include <stdio.h>
#ifdef WIN32 
#include <windows.h>
#endif
#include "nspr.h"
#include "nscore.h"
#include "nsCOMPtr.h"
#include "nsIServiceManager.h"
#include "nsIStreamListener.h"
#include "nsIInputStream.h"
#include "nsIChannel.h"
#include "nsIURL.h"
#include "nsIInterfaceRequestor.h" 
#include "nsIInterfaceRequestorUtils.h"
#include "nsIDNSService.h" 
#include "mozilla/Attributes.h"

#include "nsISimpleEnumerator.h"
#include "nsNetUtil.h"
#include "nsStringAPI.h"

static bool gError = false;
static int32_t gKeepRunning = 0;

#define NS_IEQUALS_IID \
    { 0x11c5c8ee, 0x1dd2, 0x11b2, \
      { 0xa8, 0x93, 0xbb, 0x23, 0xa1, 0xb6, 0x27, 0x76 }}

class nsIEquals : public nsISupports {
public:
    NS_DECLARE_STATIC_IID_ACCESSOR(NS_IEQUALS_IID)
    NS_IMETHOD Equals(void *aPtr, bool *_retval) = 0;
};

NS_DEFINE_STATIC_IID_ACCESSOR(nsIEquals, NS_IEQUALS_IID)

class ConsumerContext final : public nsIEquals {

    ~ConsumerContext() {}

public:
    NS_DECL_THREADSAFE_ISUPPORTS

    ConsumerContext() { }

    NS_IMETHOD Equals(void *aPtr, bool *_retval) override {
        *_retval = true;
        if (aPtr != this) *_retval = false;
        return NS_OK;
    }
};

NS_IMPL_ISUPPORTS(ConsumerContext, nsIEquals)

class Consumer : public nsIStreamListener {

    virtual ~Consumer();

public:
    NS_DECL_THREADSAFE_ISUPPORTS
    NS_DECL_NSIREQUESTOBSERVER
    NS_DECL_NSISTREAMLISTENER

    Consumer();
    nsresult Init(nsIURI *aURI, nsIChannel *aChannel, nsISupports *aContext);
    nsresult Validate(nsIRequest *request, nsISupports *aContext);

    // member data
    bool    mOnStart; // have we received an OnStart?
    bool    mOnStop;  // have we received an onStop?
    int32_t mOnDataCount; // number of times OnData was called.
    nsCOMPtr<nsIURI>     mURI;
    nsCOMPtr<nsIChannel> mChannel;
    nsCOMPtr<nsIEquals>  mContext;
};

// nsISupports implementation
NS_IMPL_ISUPPORTS(Consumer, nsIStreamListener, nsIRequestObserver)


// nsIRequestObserver implementation
NS_IMETHODIMP
Consumer::OnStartRequest(nsIRequest *request, nsISupports* aContext) {
    fprintf(stderr, "Consumer::OnStart() -> in\n\n");

    if (mOnStart) {
        fprintf(stderr, "INFO: multiple OnStarts received\n");
    }
    mOnStart = true;

    nsresult rv = Validate(request, aContext);
    if (NS_FAILED(rv)) return rv;

    fprintf(stderr, "Consumer::OnStart() -> out\n\n");
    return rv;
}

NS_IMETHODIMP
Consumer::OnStopRequest(nsIRequest *request, nsISupports *aContext,
                        nsresult aStatus) {
    fprintf(stderr, "Consumer::OnStop() -> in\n\n");

    if (!mOnStart) {
        gError = true;
        fprintf(stderr, "ERROR: No OnStart received\n");
    }

    if (mOnStop) {
        fprintf(stderr, "INFO: multiple OnStops received\n");
    }

    fprintf(stderr, "INFO: received %d OnData()s\n", mOnDataCount);

    mOnStop = true;

    nsresult rv = Validate(request, aContext);
    if (NS_FAILED(rv)) return rv;

    fprintf(stderr, "Consumer::OnStop() -> out\n\n");
    return rv;
}


// nsIStreamListener implementation
NS_IMETHODIMP
Consumer::OnDataAvailable(nsIRequest *request, nsISupports *aContext,
                          nsIInputStream *aIStream,
                          uint64_t aOffset, uint32_t aLength) {
    fprintf(stderr, "Consumer::OnData() -> in\n\n");

    if (!mOnStart) {
        gError = true;
        fprintf(stderr, "ERROR: No OnStart received\n");
    }

    mOnDataCount += 1;

    nsresult rv = Validate(request, aContext);
    if (NS_FAILED(rv)) return rv;

    fprintf(stderr, "Consumer::OnData() -> out\n\n");
    return rv;
}

// Consumer implementation
Consumer::Consumer() {
    mOnStart = mOnStop = false;
    mOnDataCount = 0;
    gKeepRunning++;
}

Consumer::~Consumer() {
    fprintf(stderr, "Consumer::~Consumer -> in\n\n");

    if (!mOnStart) {
        gError = true;
        fprintf(stderr, "ERROR: Never got an OnStart\n");
    }

    if (!mOnStop) {
        gError = true;
        fprintf(stderr, "ERROR: Never got an OnStop \n");
    }

    fprintf(stderr, "Consumer::~Consumer -> out\n\n");
    if (--gKeepRunning == 0)
      QuitPumpingEvents();
}

nsresult
Consumer::Init(nsIURI *aURI, nsIChannel* aChannel, nsISupports *aContext) {
    mURI     = aURI;
    mChannel = aChannel;
    mContext = do_QueryInterface(aContext);
    return NS_OK;
}

nsresult
Consumer::Validate(nsIRequest* request, nsISupports *aContext) {
    nsresult rv = NS_OK;
    nsCOMPtr<nsIURI> uri;
    nsCOMPtr<nsIChannel> aChannel = do_QueryInterface(request);

    rv = aChannel->GetURI(getter_AddRefs(uri));
    if (NS_FAILED(rv)) return rv;

    bool same = false;

    rv = mURI->Equals(uri, &same);
    if (NS_FAILED(rv)) return rv;

    if (!same)
        fprintf(stderr, "INFO: URIs do not match\n");

    rv = mContext->Equals((void*)aContext, &same);
    if (NS_FAILED(rv)) return rv;

    if (!same) {
        gError = true;
        fprintf(stderr, "ERROR: Contexts do not match\n");
    }
    return rv;
}

nsresult StartLoad(const char *);

int main(int argc, char *argv[]) {
    if (test_common_init(&argc, &argv) != 0)
        return -1;

    nsresult rv = NS_OK;
    bool cmdLineURL = false;

    if (argc > 1) {
        // run in signle url mode
        cmdLineURL = true;
    }

    rv = NS_InitXPCOM2(nullptr, nullptr, nullptr);
    if (NS_FAILED(rv)) return -1;

    if (cmdLineURL) {
        rv = StartLoad(argv[1]);
    } else {
        rv = StartLoad("http://badhostnamexyz/test.txt");
    }
    if (NS_FAILED(rv)) return -1;

    // Enter the message pump to allow the URL load to proceed.
    PumpEvents();

    NS_ShutdownXPCOM(nullptr);
    if (gError) {
        fprintf(stderr, "\n\n-------ERROR-------\n\n");
    }
    return 0;
}

nsresult StartLoad(const char *aURISpec) {
    nsresult rv = NS_OK;

    // create a context
    ConsumerContext *context = new ConsumerContext;
    nsCOMPtr<nsISupports> contextSup = do_QueryInterface(context, &rv);
    if (NS_FAILED(rv)) return rv;

    // create a uri
    nsCOMPtr<nsIURI> uri;
    rv = NS_NewURI(getter_AddRefs(uri), aURISpec);
    if (NS_FAILED(rv)) return rv;

    nsCOMPtr<nsIScriptSecurityManager> secman =
      do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
    if (NS_FAILED(rv)) return rv;
    nsCOMPtr<nsIPrincipal> systemPrincipal;
    rv = secman->GetSystemPrincipal(getter_AddRefs(systemPrincipal));
    if (NS_FAILED(rv)) return rv;

    // create a channel
    nsCOMPtr<nsIChannel> channel;
    rv = NS_NewChannel(getter_AddRefs(channel),
                       uri,
                       systemPrincipal,
                       nsILoadInfo::SEC_NORMAL,
                       nsIContentPolicy::TYPE_OTHER);
    if (NS_FAILED(rv)) return rv;

    Consumer *consumer = new Consumer;
    rv = consumer->Init(uri, channel, contextSup);
    if (NS_FAILED(rv)) return rv;

    // kick off the load
    nsCOMPtr<nsIRequest> request;
    return channel->AsyncOpen(static_cast<nsIStreamListener*>(consumer), contextSup);
}