146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)var fileSystemNatives = requireNative('file_system_natives');
690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)var nameToIds = {};
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)var idsToEntries = {};
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)function computeName(entry) {
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return entry.filesystem.name + ':' + entry.fullPath;
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)function computeId(entry) {
1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  var fileSystemId = fileSystemNatives.CrackIsolatedFileSystemName(
1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      entry.filesystem.name);
1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!fileSystemId)
1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return null;
1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Strip the leading '/' from the path.
20eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return fileSystemId + ':' + $String.slice(entry.fullPath, 1);
2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)function registerEntry(id, entry) {
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  var name = computeName(entry);
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  nameToIds[name] = id;
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  idsToEntries[id] = entry;
2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)function getEntryId(entry) {
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  var name = null;
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  try {
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    name = computeName(entry);
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } catch(e) {
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return null;
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  var id = nameToIds[name];
3790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (id != null)
3890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return id;
3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // If an entry has not been registered, compute its id and register it.
4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  id = computeId(entry);
4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  registerEntry(id, entry);
4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return id;
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)function getEntryById(id) {
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return idsToEntries[id];
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)exports.registerEntry = registerEntry;
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)exports.getEntryId = getEntryId;
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)exports.getEntryById = getEntryById;
53