Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / extensions / renderer / resources / app_runtime_custom_bindings.js
blob267ba0931fad9d4f164ac54d70f0d4dd58e18767
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.
5 // Custom binding for the chrome.app.runtime API.
7 var binding = require('binding').Binding.create('app.runtime');
9 var AppViewInternal =
10 require('binding').Binding.create('appViewInternal').generate();
11 var eventBindings = require('event_bindings');
12 var fileSystemHelpers = requireNative('file_system_natives');
13 var GetIsolatedFileSystem = fileSystemHelpers.GetIsolatedFileSystem;
14 var appNatives = requireNative('app_runtime');
15 var DeserializeString = appNatives.DeserializeString;
16 var SerializeToString = appNatives.SerializeToString;
17 var CreateBlob = appNatives.CreateBlob;
18 var entryIdManager = require('entryIdManager');
20 eventBindings.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);
29 appEmbeddingRequest.deny = function() {
30 AppViewInternal.denyRequest(id);
33 dispatch([appEmbeddingRequest]);
34 });
36 eventBindings.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);
49 if (--numItems === 0) {
50 var data = { isKioskSession: launchData.isKioskSession };
51 if (items.length !== 0) {
52 data.id = launchData.id;
53 data.items = items;
55 dispatch([data]);
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]);
72 });
74 exports.binding = binding.generate();