From 8688c3a0638c4fa48181851bb26b29a2cb881b48 Mon Sep 17 00:00:00 2001 From: "cmihail@chromium.org" Date: Fri, 27 Jun 2014 05:05:17 +0000 Subject: [PATCH] Demonstrate the basic functionality of the File System Provider API. Mount a sample file system on installing an extension. The file system is made of fake content and if offers functionality like directory listening and opening, reading and closing files supplied with fake content. BUG=388080 Review URL: https://codereview.chromium.org/351553005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@280247 0039d316-1c4b-4281-b951-d872f2087c98 --- .../api/fileSystemProvider/basic/background.js | 119 +++++++++++++++++++++ .../api/fileSystemProvider/basic/manifest.json | 17 +++ 2 files changed, 136 insertions(+) create mode 100644 chrome/common/extensions/docs/examples/api/fileSystemProvider/basic/background.js create mode 100644 chrome/common/extensions/docs/examples/api/fileSystemProvider/basic/manifest.json diff --git a/chrome/common/extensions/docs/examples/api/fileSystemProvider/basic/background.js b/chrome/common/extensions/docs/examples/api/fileSystemProvider/basic/background.js new file mode 100644 index 000000000000..f95a098f6d8f --- /dev/null +++ b/chrome/common/extensions/docs/examples/api/fileSystemProvider/basic/background.js @@ -0,0 +1,119 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +'use strict'; + +// Fake data similar to a file system structure. +var MODIFICATION_DATE = new Date(); +var SHORT_CONTENTS = 'Just another example.'; +var LONGER_CONTENTS = 'It works!\nEverything gets displayed correctly.'; + +var METADATA = { + '/': {isDirectory: true, name: '/', size: 0, + modificationTime: MODIFICATION_DATE}, + '/file1.txt': {isDirectory: false, name: 'file1.txt', + size: LONGER_CONTENTS.length, modificationTime: MODIFICATION_DATE, + contents: LONGER_CONTENTS}, + '/file2': {isDirectory: false, name: 'file2', size: 150, + modificationTime: MODIFICATION_DATE}, + '/dir': {isDirectory: true, name: 'dir', size: 0, + modificationTime: MODIFICATION_DATE}, + '/dir/file3.txt': {isDirectory: false, name: 'file3.txt', + size: SHORT_CONTENTS.length, modificationTime: MODIFICATION_DATE, + contents: SHORT_CONTENTS}}; + +// A map with currently opened files. As key it has requestId of +// openFileRequested and as a value the file path. +var openedFiles = {}; + +function onGetMetadataRequested(options, onSuccess, onError) { + if (!METADATA[options.entryPath]) + onError('NOT_FOUND'); + else + onSuccess(METADATA[options.entryPath]); +} + +function onReadDirectoryRequested(options, onSuccess, onError) { + if (!METADATA[options.directoryPath]) { + onError('NOT_FOUND'); + return; + } + if (!METADATA[options.directoryPath].isDirectory) { + onError('NOT_A_DIRECTORY'); + return; + } + + // Retrieve directory contents from METADATA. + var entries = []; + for (var entry in METADATA) { + // Do not add itself on the list. + if (entry == options.directoryPath) + continue; + // Check if the entry is a child of the requested directory. + if (entry.indexOf(options.directoryPath) != 0) + continue; + // Restrict to direct children only. + if (entry.substring(options.directoryPath.length + 1).indexOf('/') != -1) + continue; + + entries.push(METADATA[entry]); + } + onSuccess(entries, false /* Last call. */); +} + +function onOpenFileRequested(options, onSuccess, onError) { + if (options.mode != 'READ' || options.create) { + onError('INVALID_OPERATION'); + } else { + openedFiles[options.requestId] = options.filePath; + onSuccess(); + } +} + +function onCloseFileRequested(options, onSuccess, onError) { + if (!openedFiles[options.openRequestId]) { + onError('INVALID_OPERATION'); + } else { + delete openedFiles[options.openRequestId]; + onSuccess(); + } +} + +function onReadFileRequested(options, onSuccess, onError) { + if (!openedFiles[options.openRequestId]) { + onError('INVALID_OPERATION'); + return; + } + + var contents = + METADATA[openedFiles[options.openRequestId]].contents; + + // Write the contents as ASCII text. + var buffer = new ArrayBuffer(options.length); + var bufferView = new Uint8Array(buffer); + for (var i = 0; i < options.length; i++) { + bufferView[i] = contents.charCodeAt(i); + } + + onSuccess(buffer, false /* Last call. */); +} + +// Mount the file system. +chrome.runtime.onInstalled.addListener(function(details) { + chrome.fileSystemProvider.mount( + {fileSystemId: 'sample-file-system', displayName: 'Sample File System'}, + function() {}, + function() { console.error('Failed to mount.'); }); +}); + +chrome.fileSystemProvider.onGetMetadataRequested.addListener( + onGetMetadataRequested); +chrome.fileSystemProvider.onReadDirectoryRequested.addListener( + onReadDirectoryRequested); +chrome.fileSystemProvider.onOpenFileRequested.addListener( + onOpenFileRequested); +chrome.fileSystemProvider.onCloseFileRequested.addListener( + onCloseFileRequested); +chrome.fileSystemProvider.onReadFileRequested.addListener( + onReadFileRequested); diff --git a/chrome/common/extensions/docs/examples/api/fileSystemProvider/basic/manifest.json b/chrome/common/extensions/docs/examples/api/fileSystemProvider/basic/manifest.json new file mode 100644 index 000000000000..fb71b39bb833 --- /dev/null +++ b/chrome/common/extensions/docs/examples/api/fileSystemProvider/basic/manifest.json @@ -0,0 +1,17 @@ +{ + "name": "File System Provider API Example", + "version": "0.1", + "manifest_version": 2, + "description": + "Demonstrate features of the API like mounting, listing directories, etc", + "permissions": [ + "fileSystemProvider" + ], + "app": { + "background": { + "scripts": [ + "background.js" + ] + } + } +} -- 2.11.4.GIT