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 (d1ed7de67f5a)

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
/* 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/. */

"use strict";

const Cu = Components.utils;
const Cc = Components.classes;
const Ci = Components.interfaces;
const CC = Components.Constructor;

this.EXPORTED_SYMBOLS = ["OfflineCacheInstaller"];

Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/AppsUtils.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");

let Namespace = CC('@mozilla.org/network/application-cache-namespace;1',
                   'nsIApplicationCacheNamespace',
                   'init');
let makeFile = CC('@mozilla.org/file/local;1',
                'nsIFile',
                'initWithPath');
let MutableArray = CC('@mozilla.org/array;1', 'nsIMutableArray');

let {LoadContextInfo} = Cu.import("resource://gre/modules/LoadContextInfo.jsm", {});

const nsICacheStorage = Ci.nsICacheStorage;
const nsIApplicationCache = Ci.nsIApplicationCache;
const applicationCacheService =
  Cc['@mozilla.org/network/application-cache-service;1']
    .getService(Ci.nsIApplicationCacheService);


function debug(aMsg) {
  //dump("-*-*- OfflineCacheInstaller.jsm : " + aMsg + "\n");
}


function enableOfflineCacheForApp(origin, appId) {
  let principal = Services.scriptSecurityManager.getAppCodebasePrincipal(
                    origin, appId, false);
  Services.perms.addFromPrincipal(principal, 'offline-app',
                                  Ci.nsIPermissionManager.ALLOW_ACTION);
  // Prevent cache from being evicted:
  Services.perms.addFromPrincipal(principal, 'pin-app',
                                  Ci.nsIPermissionManager.ALLOW_ACTION);
}


function storeCache(applicationCache, url, file, itemType) {
  let storage =
    Services.cache2.appCacheStorage(LoadContextInfo.default, applicationCache);
  let uri = Services.io.newURI(url, null, null);
  storage.asyncOpenURI(uri, "", nsICacheStorage.OPEN_TRUNCATE, {
    onCacheEntryAvailable:
      function (cacheEntry, isNew, appCache, result) {
        cacheEntry.setMetaDataElement('request-method', 'GET');
        cacheEntry.setMetaDataElement('response-head', 'HTTP/1.1 200 OK\r\n');

        let outputStream = cacheEntry.openOutputStream(0);

        // Input-Output stream machinery in order to push nsIFile content into cache
        let inputStream = Cc['@mozilla.org/network/file-input-stream;1']
                            .createInstance(Ci.nsIFileInputStream);
        inputStream.init(file, 1, -1, null);
        let bufferedOutputStream = Cc['@mozilla.org/network/buffered-output-stream;1']
                                     .createInstance(Ci.nsIBufferedOutputStream);
        bufferedOutputStream.init(outputStream, 1024);
        bufferedOutputStream.writeFrom(inputStream, inputStream.available());
        bufferedOutputStream.flush();
        bufferedOutputStream.close();
        inputStream.close();

        cacheEntry.markValid();
        debug (file.path + ' -> ' + url + ' (' + itemType + ')');
        applicationCache.markEntry(url, itemType);
        cacheEntry.close();
      }
  });
}

function readFile(aFile, aCallback) {
  let channel = NetUtil.newChannel(aFile);
  channel.contentType = "plain/text";
  NetUtil.asyncFetch(channel, function(aStream, aResult) {
    if (!Components.isSuccessCode(aResult)) {
      Cu.reportError("OfflineCacheInstaller: Could not read file " + aFile.path);
      if (aCallback)
        aCallback(null);
      return;
    }

    // Obtain a converter to read from a UTF-8 encoded input stream.
    let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
                      .createInstance(Ci.nsIScriptableUnicodeConverter);
    converter.charset = "UTF-8";

    let data = NetUtil.readInputStreamToString(aStream,
                                               aStream.available());
    aCallback(converter.ConvertToUnicode(data));
  });
}

function parseCacheLine(app, urls, line) {
  try {
    let url = Services.io.newURI(line, null, app.origin);
    urls.push(url.spec);
  } catch(e) {
    throw new Error('Unable to parse cache line: ' + line + '(' + e + ')');
  }
}

function parseFallbackLine(app, urls, namespaces, fallbacks, line) {
  let split = line.split(/[ \t]+/);
  if (split.length != 2) {
    throw new Error('Should be made of two URLs seperated with spaces')
  }
  let type = Ci.nsIApplicationCacheNamespace.NAMESPACE_FALLBACK;
  let [ namespace, fallback ] = split;

  // Prepend webapp origin in case of absolute path
  try {
    namespace = Services.io.newURI(namespace, null, app.origin).spec;
    fallback = Services.io.newURI(fallback, null, app.origin).spec;
  } catch(e) {
    throw new Error('Unable to parse fallback line: ' + line + '(' + e + ')');
  }

  namespaces.push([type, namespace, fallback]);
  fallbacks.push(fallback);
  urls.push(fallback);
}

function parseNetworkLine(namespaces, line) {
  let type = Ci.nsIApplicationCacheNamespace.NAMESPACE_BYPASS;
  if (line[0] == '*' && (line.length == 1 || line[1] == ' '
                                          || line[1] == '\t')) {
    namespaces.push([type, '', '']);
  } else {
    namespaces.push([type, namespace, '']);
  }
}

function parseAppCache(app, path, content) {
  let lines = content.split(/\r?\n/);

  let urls = [];
  let namespaces = [];
  let fallbacks = [];

  let currentSection = 'CACHE';
  for (let i = 0; i < lines.length; i++) {
    let line = lines[i];

    // Ignore comments
    if (/^#/.test(line) || !line.length)
      continue;

    // Process section headers
    if (line == 'CACHE MANIFEST')
      continue;
    if (line == 'CACHE:') {
      currentSection = 'CACHE';
      continue;
    } else if (line == 'NETWORK:') {
      currentSection = 'NETWORK';
      continue;
    } else if (line == 'FALLBACK:') {
      currentSection = 'FALLBACK';
      continue;
    }

    // Process cache, network and fallback rules
    try {
      if (currentSection == 'CACHE') {
        parseCacheLine(app, urls, line);
      } else if (currentSection == 'NETWORK') {
        parseNetworkLine(namespaces, line);
      } else if (currentSection == 'FALLBACK') {
        parseFallbackLine(app, urls, namespaces, fallbacks, line);
      }
    } catch(e) {
      throw new Error('Invalid ' + currentSection + ' line in appcache ' +
                      'manifest:\n' + e.message +
                      '\nFrom: ' + path +
                      '\nLine ' + i + ': ' + line);
    }
  }

  return {
    urls: urls,
    namespaces: namespaces,
    fallbacks: fallbacks
  };
}

function installCache(app) {
  if (!app.cachePath) {
    return;
  }

  let cacheDir = makeFile(app.cachePath)
  cacheDir.append(app.appId);
  cacheDir.append('cache');
  if (!cacheDir.exists())
    return;

  let cacheManifest = cacheDir.clone();
  cacheManifest.append('manifest.appcache');
  if (!cacheManifest.exists())
    return;

  enableOfflineCacheForApp(app.origin, app.localId);

  // Get the url for the manifest.
  let appcacheURL = app.appcache_path;

  // The group ID contains application id and 'f' for not being hosted in
  // a browser element, but a mozbrowser iframe.
  // See netwerk/cache/nsDiskCacheDeviceSQL.cpp: AppendJARIdentifier
  let groupID = appcacheURL + '#' + app.localId+ '+f';
  let applicationCache = applicationCacheService.createApplicationCache(groupID);
  applicationCache.activate();

  readFile(cacheManifest, function readAppCache(content) {
    let entries = parseAppCache(app, cacheManifest.path, content);

    entries.urls.forEach(function processCachedFile(url) {
      // Get this nsIFile from cache folder for this URL
      // We have absolute urls, so remove the origin part to locate the
      // files.
      let path = url.replace(app.origin.spec, '');
      let file = cacheDir.clone();
      let paths = path.split('/');
      paths.forEach(file.append);

      if (!file.exists()) {
        let msg = 'File ' + file.path + ' exists in the manifest but does ' +
                  'not points to a real file.';
        throw new Error(msg);
      }

      let itemType = nsIApplicationCache.ITEM_EXPLICIT;
      if (entries.fallbacks.indexOf(url) > -1) {
        debug('add fallback: ' + url + '\n');
        itemType |= nsIApplicationCache.ITEM_FALLBACK;
      }
      storeCache(applicationCache, url, file, itemType);
    });

    let array = new MutableArray();
    entries.namespaces.forEach(function processNamespace([type, spec, data]) {
      debug('add namespace: ' + type + ' - ' + spec + ' - ' + data + '\n');
      array.appendElement(new Namespace(type, spec, data), false);
    });
    applicationCache.addNamespaces(array);

    storeCache(applicationCache, appcacheURL, cacheManifest,
               nsIApplicationCache.ITEM_MANIFEST);
  });
}


// Public API

this.OfflineCacheInstaller = {
  installCache: installCache
};