1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 // Fake data similar to a file system structure.
8 var MODIFICATION_DATE = new Date();
9 var SHORT_CONTENTS = 'Just another example.';
10 var LONGER_CONTENTS = 'It works!\nEverything gets displayed correctly.';
13 '/': {isDirectory: true, name: '', size: 0,
14 modificationTime: MODIFICATION_DATE},
15 '/file1.txt': {isDirectory: false, name: 'file1.txt',
16 size: LONGER_CONTENTS.length, modificationTime: MODIFICATION_DATE,
17 contents: LONGER_CONTENTS},
18 '/file2': {isDirectory: false, name: 'file2', size: 150,
19 modificationTime: MODIFICATION_DATE},
20 '/dir': {isDirectory: true, name: 'dir', size: 0,
21 modificationTime: MODIFICATION_DATE},
22 '/dir/file3.txt': {isDirectory: false, name: 'file3.txt',
23 size: SHORT_CONTENTS.length, modificationTime: MODIFICATION_DATE,
24 contents: SHORT_CONTENTS}};
26 // A map with currently opened files. As key it has requestId of
27 // openFileRequested and as a value the file path.
30 function onGetMetadataRequested(options, onSuccess, onError) {
31 if (!METADATA[options.entryPath])
34 onSuccess(METADATA[options.entryPath]);
37 function onReadDirectoryRequested(options, onSuccess, onError) {
38 if (!METADATA[options.directoryPath]) {
42 if (!METADATA[options.directoryPath].isDirectory) {
43 onError('NOT_A_DIRECTORY');
47 // Retrieve directory contents from METADATA.
49 for (var entry in METADATA) {
50 // Do not add itself on the list.
51 if (entry == options.directoryPath)
53 // Check if the entry is a child of the requested directory.
54 if (entry.indexOf(options.directoryPath) != 0)
56 // Restrict to direct children only.
57 if (entry.substring(options.directoryPath.length + 1).indexOf('/') != -1)
60 entries.push(METADATA[entry]);
62 onSuccess(entries, false /* Last call. */);
65 function onOpenFileRequested(options, onSuccess, onError) {
66 if (options.mode != 'READ' || options.create) {
67 onError('INVALID_OPERATION');
69 openedFiles[options.requestId] = options.filePath;
74 function onCloseFileRequested(options, onSuccess, onError) {
75 if (!openedFiles[options.openRequestId]) {
76 onError('INVALID_OPERATION');
78 delete openedFiles[options.openRequestId];
83 function onReadFileRequested(options, onSuccess, onError) {
84 if (!openedFiles[options.openRequestId]) {
85 onError('INVALID_OPERATION');
90 METADATA[openedFiles[options.openRequestId]].contents;
92 // Write the contents as ASCII text.
93 var buffer = new ArrayBuffer(options.length);
94 var bufferView = new Uint8Array(buffer);
95 for (var i = 0; i < options.length; i++) {
96 bufferView[i] = contents.charCodeAt(i);
99 onSuccess(buffer, false /* Last call. */);
102 function onMountRequested(onSuccess, onError) {
103 chrome.fileSystemProvider.mount(
104 {fileSystemId: 'sample-file-system', displayName: 'Sample File System'},
106 if (chrome.runtime.lastError) {
107 onError(chrome.runtime.lastError.message);
108 console.error('Failed to mount because of: ' +
109 chrome.runtime.lastError.message);
116 function onUnmountRequested(options, onSuccess, onError) {
117 chrome.fileSystemProvider.unmount(
118 {fileSystemId: options.fileSystemId},
120 if (chrome.runtime.lastError) {
121 onError(chrome.runtime.lastError.message);
122 console.error('Failed to unmount because of: ' +
123 chrome.runtime.lastError.message);
130 chrome.fileSystemProvider.onGetMetadataRequested.addListener(
131 onGetMetadataRequested);
132 chrome.fileSystemProvider.onReadDirectoryRequested.addListener(
133 onReadDirectoryRequested);
134 chrome.fileSystemProvider.onOpenFileRequested.addListener(onOpenFileRequested);
135 chrome.fileSystemProvider.onCloseFileRequested.addListener(
136 onCloseFileRequested);
137 chrome.fileSystemProvider.onReadFileRequested.addListener(onReadFileRequested);
138 chrome.fileSystemProvider.onMountRequested.addListener(onMountRequested);
139 chrome.fileSystemProvider.onUnmountRequested.addListener(onUnmountRequested);