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. 4 5 // Custom binding for the chrome.app.runtime API. 6 7 var binding = require('binding').Binding.create('app.runtime'); 8 9 var eventBindings = require('event_bindings'); 10 var fileSystemHelpers = requireNative('file_system_natives'); 11 var GetIsolatedFileSystem = fileSystemHelpers.GetIsolatedFileSystem; 12 var appNatives = requireNative('app_runtime'); 13 var DeserializeString = appNatives.DeserializeString; 14 var SerializeToString = appNatives.SerializeToString; 15 var CreateBlob = appNatives.CreateBlob; 16 var entryIdManager = require('entryIdManager'); 17 18 eventBindings.registerArgumentMassager('app.runtime.onLaunched', 19 function(args, dispatch) { 20 var launchData = args[0]; 21 if (launchData.items) { 22 // An onLaunched corresponding to file_handlers in the app's manifest. 23 var items = []; 24 var numItems = launchData.items.length; 25 var itemLoaded = function(err, item) { 26 if (err) { 27 console.error('Error getting fileEntry, code: ' + err.code); 28 } else { 29 $Array.push(items, item); 30 } 31 if (--numItems === 0) { 32 var data = { isKioskSession: launchData.isKioskSession }; 33 if (items.length !== 0) { 34 data.id = launchData.id; 35 data.items = items; 36 } 37 dispatch([data]); 38 } 39 }; 40 $Array.forEach(launchData.items, function(item) { 41 var fs = GetIsolatedFileSystem(item.fileSystemId); 42 fs.root.getFile(item.baseName, {}, function(fileEntry) { 43 entryIdManager.registerEntry(item.entryId, fileEntry); 44 itemLoaded(null, { entry: fileEntry, type: item.mimeType }); 45 }, function(fileError) { 46 itemLoaded(fileError); 47 }); 48 }); 49 } else { 50 // Default case. This currently covers an onLaunched corresponding to 51 // url_handlers in the app's manifest. 52 dispatch([launchData]); 53 } 54 }); 55 56 exports.binding = binding.generate(); 57