app_runtime_custom_bindings.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. // Custom binding for the chrome.app.runtime API.
  5. var AppViewGuestInternal;
  6. // appViewGuestInternal isn't available in lock screen contexts.
  7. if (requireNative('v8_context').GetAvailability('appViewGuestInternal').
  8. is_available) {
  9. AppViewGuestInternal = getInternalApi('appViewGuestInternal');
  10. }
  11. var fileSystemHelpers = requireNative('file_system_natives');
  12. var GetIsolatedFileSystem = fileSystemHelpers.GetIsolatedFileSystem;
  13. var entryIdManager = require('entryIdManager');
  14. if (AppViewGuestInternal) {
  15. bindingUtil.registerEventArgumentMassager('app.runtime.onEmbedRequested',
  16. function(args, dispatch) {
  17. var appEmbeddingRequest = args[0];
  18. var id = appEmbeddingRequest.guestInstanceId;
  19. delete appEmbeddingRequest.guestInstanceId;
  20. appEmbeddingRequest.allow = function(url) {
  21. AppViewGuestInternal.attachFrame(url, id);
  22. };
  23. appEmbeddingRequest.deny = function() {
  24. AppViewGuestInternal.denyRequest(id);
  25. };
  26. dispatch([appEmbeddingRequest]);
  27. });
  28. }
  29. bindingUtil.registerEventArgumentMassager('app.runtime.onLaunched',
  30. function(args, dispatch) {
  31. var launchData = args[0];
  32. if (launchData.items) {
  33. // An onLaunched corresponding to file_handlers in the app's manifest.
  34. var items = [];
  35. var numItems = launchData.items.length;
  36. var itemLoaded = function(err, item) {
  37. if (err) {
  38. console.error('Error getting fileEntry, code: ' + err.code);
  39. } else {
  40. $Array.push(items, item);
  41. }
  42. if (--numItems === 0) {
  43. var data = {
  44. isDemoSession: launchData.isDemoSession,
  45. isKioskSession: launchData.isKioskSession,
  46. isPublicSession: launchData.isPublicSession,
  47. source: launchData.source,
  48. actionData: launchData.actionData
  49. };
  50. if (items.length !== 0) {
  51. data.id = launchData.id;
  52. data.items = items;
  53. }
  54. dispatch([data]);
  55. }
  56. };
  57. $Array.forEach(launchData.items, function(item) {
  58. var fs = GetIsolatedFileSystem(item.fileSystemId);
  59. if (item.isDirectory) {
  60. fs.root.getDirectory(item.baseName, {}, function(dirEntry) {
  61. entryIdManager.registerEntry(item.entryId, dirEntry);
  62. itemLoaded(null, {entry: dirEntry});
  63. }, function(fileError) {
  64. itemLoaded(fileError);
  65. });
  66. } else {
  67. fs.root.getFile(item.baseName, {}, function(fileEntry) {
  68. entryIdManager.registerEntry(item.entryId, fileEntry);
  69. itemLoaded(null, {entry: fileEntry, type: item.mimeType});
  70. }, function(fileError) {
  71. itemLoaded(fileError);
  72. });
  73. }
  74. });
  75. } else {
  76. // Default case. This currently covers an onLaunched corresponding to
  77. // url_handlers in the app's manifest.
  78. dispatch([launchData]);
  79. }
  80. });