netwerk/protocol/about/nsAboutProtocolUtils.h
author Randell Jesup <rjesup@jesup.org>
Sat, 01 Sep 2012 22:35:17 -0400
changeset 104468 3d9424eb6eb473cfd14309fd635ff7d6e29322c2
parent 103381 a16372ce30b5f6b747246b01fcd215a4bf3b6342
child 107183 ae68e6c539a21a7a2b65e33b1e9d7fd3401812a1
permissions -rw-r--r--
Bug 773151: Convert nsCAutoString->nsAutoCString CLOSED TREE r=bsmedberg

/* 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 "nsIURI.h"
#include "nsString.h"
#include "nsReadableUtils.h"
#include "nsIAboutModule.h"

inline nsresult
NS_GetAboutModuleName(nsIURI *aAboutURI, nsCString& aModule)
{
#ifdef DEBUG
    {
        bool isAbout;
        NS_ASSERTION(NS_SUCCEEDED(aAboutURI->SchemeIs("about", &isAbout)) &&
                     isAbout,
                     "should be used only on about: URIs");
    }
#endif

    nsresult rv = aAboutURI->GetPath(aModule);
    NS_ENSURE_SUCCESS(rv, rv);

    int32_t f = aModule.FindCharInSet(NS_LITERAL_CSTRING("#?"));
    if (f != kNotFound) {
        aModule.Truncate(f);
    }

    // convert to lowercase, as all about: modules are lowercase
    ToLowerCase(aModule);
    return NS_OK;
}

inline nsresult
NS_GetAboutModule(nsIURI *aAboutURI, nsIAboutModule** aModule)
{
  NS_PRECONDITION(aAboutURI, "Must have URI");

  nsAutoCString contractID;
  nsresult rv = NS_GetAboutModuleName(aAboutURI, contractID);
  if (NS_FAILED(rv)) return rv;

  // look up a handler to deal with "what"
  contractID.Insert(NS_LITERAL_CSTRING(NS_ABOUT_MODULE_CONTRACTID_PREFIX), 0);

  return CallGetService(contractID.get(), aModule);
}