netwerk/cache/nsCache.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/cache/nsCache.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,108 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + *
     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 +#include "nsCache.h"
    1.11 +#include "nsReadableUtils.h"
    1.12 +#include "nsDependentSubstring.h"
    1.13 +#include "nsString.h"
    1.14 +
    1.15 +
    1.16 +/**
    1.17 + * Cache Service Utility Functions
    1.18 + */
    1.19 +
    1.20 +#if defined(PR_LOGGING)
    1.21 +PRLogModuleInfo * gCacheLog = nullptr;
    1.22 +
    1.23 +
    1.24 +void
    1.25 +CacheLogInit()
    1.26 +{
    1.27 +    if (gCacheLog) return;
    1.28 +    gCacheLog = PR_NewLogModule("cache");
    1.29 +    NS_ASSERTION(gCacheLog, "\nfailed to allocate cache log.\n");
    1.30 +}
    1.31 +
    1.32 +
    1.33 +void
    1.34 +CacheLogPrintPath(PRLogModuleLevel level, const char * format, nsIFile * item)
    1.35 +{
    1.36 +    nsAutoCString path;
    1.37 +    nsresult rv = item->GetNativePath(path);
    1.38 +    if (NS_SUCCEEDED(rv)) {
    1.39 +        PR_LOG(gCacheLog, level, (format, path.get()));
    1.40 +    } else {
    1.41 +        PR_LOG(gCacheLog, level, ("GetNativePath failed: %x", rv));
    1.42 +    }
    1.43 +}
    1.44 +
    1.45 +#endif
    1.46 +
    1.47 +
    1.48 +uint32_t
    1.49 +SecondsFromPRTime(PRTime prTime)
    1.50 +{
    1.51 +  int64_t  microSecondsPerSecond = PR_USEC_PER_SEC;
    1.52 +  return uint32_t(prTime / microSecondsPerSecond);
    1.53 +}
    1.54 +
    1.55 +
    1.56 +PRTime
    1.57 +PRTimeFromSeconds(uint32_t seconds)
    1.58 +{
    1.59 +  int64_t intermediateResult = seconds;
    1.60 +  PRTime prTime = intermediateResult * PR_USEC_PER_SEC;
    1.61 +  return prTime;
    1.62 +}
    1.63 +
    1.64 +
    1.65 +nsresult
    1.66 +ClientIDFromCacheKey(const nsACString&  key, char ** result)
    1.67 +{
    1.68 +    nsresult  rv = NS_OK;
    1.69 +    *result = nullptr;
    1.70 +
    1.71 +    nsReadingIterator<char> colon;
    1.72 +    key.BeginReading(colon);
    1.73 +        
    1.74 +    nsReadingIterator<char> start;
    1.75 +    key.BeginReading(start);
    1.76 +        
    1.77 +    nsReadingIterator<char> end;
    1.78 +    key.EndReading(end);
    1.79 +        
    1.80 +    if (FindCharInReadable(':', colon, end)) {
    1.81 +        *result = ToNewCString( Substring(start, colon));
    1.82 +        if (!*result) rv = NS_ERROR_OUT_OF_MEMORY;
    1.83 +    } else {
    1.84 +        NS_ASSERTION(false, "FindCharInRead failed to find ':'");
    1.85 +        rv = NS_ERROR_UNEXPECTED;
    1.86 +    }
    1.87 +    return rv;
    1.88 +}
    1.89 +
    1.90 +
    1.91 +nsresult
    1.92 +ClientKeyFromCacheKey(const nsCString& key, nsACString &result)
    1.93 +{
    1.94 +    nsresult  rv = NS_OK;
    1.95 +
    1.96 +    nsReadingIterator<char> start;
    1.97 +    key.BeginReading(start);
    1.98 +        
    1.99 +    nsReadingIterator<char> end;
   1.100 +    key.EndReading(end);
   1.101 +        
   1.102 +    if (FindCharInReadable(':', start, end)) {
   1.103 +        ++start;  // advance past clientID ':' delimiter
   1.104 +        result.Assign(Substring(start, end));
   1.105 +    } else {
   1.106 +        NS_ASSERTION(false, "FindCharInRead failed to find ':'");
   1.107 +        rv = NS_ERROR_UNEXPECTED;
   1.108 +        result.Truncate(0);
   1.109 +    }
   1.110 +    return rv;
   1.111 +}

mercurial