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