[email protected] | 8f857ef8 | 2014-06-04 23:46:16 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | d51bd57 | 2013-01-14 03:37:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 5 | var fileSystemNatives = requireNative('file_system_natives'); |
6 | |||||
[email protected] | d51bd57 | 2013-01-14 03:37:08 | [diff] [blame] | 7 | var nameToIds = {}; |
8 | var idsToEntries = {}; | ||||
9 | |||||
10 | function computeName(entry) { | ||||
11 | return entry.filesystem.name + ':' + entry.fullPath; | ||||
12 | } | ||||
13 | |||||
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 14 | function computeId(entry) { |
15 | var fileSystemId = fileSystemNatives.CrackIsolatedFileSystemName( | ||||
16 | entry.filesystem.name); | ||||
17 | if (!fileSystemId) | ||||
18 | return null; | ||||
19 | // Strip the leading '/' from the path. | ||||
[email protected] | 31bbfd7 | 2013-06-22 02:35:54 | [diff] [blame] | 20 | return fileSystemId + ':' + $String.slice(entry.fullPath, 1); |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 21 | } |
22 | |||||
[email protected] | d51bd57 | 2013-01-14 03:37:08 | [diff] [blame] | 23 | function registerEntry(id, entry) { |
24 | var name = computeName(entry); | ||||
25 | nameToIds[name] = id; | ||||
26 | idsToEntries[id] = entry; | ||||
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 27 | } |
[email protected] | d51bd57 | 2013-01-14 03:37:08 | [diff] [blame] | 28 | |
29 | function getEntryId(entry) { | ||||
30 | var name = null; | ||||
31 | try { | ||||
32 | name = computeName(entry); | ||||
33 | } catch(e) { | ||||
34 | return null; | ||||
35 | } | ||||
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 36 | var id = nameToIds[name]; |
37 | if (id != null) | ||||
38 | return id; | ||||
39 | |||||
40 | // If an entry has not been registered, compute its id and register it. | ||||
41 | id = computeId(entry); | ||||
42 | registerEntry(id, entry); | ||||
43 | return id; | ||||
[email protected] | d51bd57 | 2013-01-14 03:37:08 | [diff] [blame] | 44 | } |
45 | |||||
46 | function getEntryById(id) { | ||||
47 | return idsToEntries[id]; | ||||
48 | } | ||||
49 | |||||
50 | exports.registerEntry = registerEntry; | ||||
51 | exports.getEntryId = getEntryId; | ||||
52 | exports.getEntryById = getEntryById; |