mime_handler_private_custom_bindings.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2015 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 bindings for the mime handler API.
  6. */
  7. var exceptionHandler = require('uncaught_exception_handler');
  8. var logActivity = requireNative('activityLogger');
  9. var NO_STREAM_ERROR =
  10. 'Streams are only available from a mime handler view guest.';
  11. var STREAM_ABORTED_ERROR = 'Stream has been aborted.';
  12. if ((typeof mojo === 'undefined') || !mojo.bindingsLibraryInitialized) {
  13. loadScript('mojo_bindings');
  14. }
  15. loadScript('extensions/common/api/mime_handler.mojom');
  16. // DO NOT USE. This causes problems with safe builtins, and makes migration to
  17. // native bindings more difficult.
  18. function handleRequestWithPromiseDoNotUse(
  19. binding, apiName, methodName, customizedFunction) {
  20. var fullName = apiName + '.' + methodName;
  21. var extensionId = requireNative('process').GetExtensionId();
  22. binding.setHandleRequest(methodName, function() {
  23. logActivity.LogAPICall(extensionId, fullName, $Array.slice(arguments));
  24. var stack = exceptionHandler.getExtensionStackTrace();
  25. var callback = arguments[arguments.length - 2];
  26. var args = $Array.slice(arguments, 0, arguments.length - 2);
  27. var keepAlive = require('keep_alive').createKeepAlive();
  28. $Function.apply(customizedFunction, this, args).then(function(result) {
  29. if (callback) {
  30. exceptionHandler.safeCallbackApply(
  31. fullName, callback, [result], stack);
  32. }
  33. }).catch(function(error) {
  34. if (callback) {
  35. var message = exceptionHandler.safeErrorToString(error, true);
  36. bindingUtil.runCallbackWithLastError(message, callback);
  37. }
  38. }).then(function() {
  39. keepAlive.close();
  40. });
  41. });
  42. };
  43. var servicePtr = new extensions.mimeHandler.MimeHandlerServicePtr;
  44. Mojo.bindInterface(
  45. extensions.mimeHandler.MimeHandlerService.name,
  46. mojo.makeRequest(servicePtr).handle);
  47. var beforeUnloadControlPtr =
  48. new extensions.mimeHandler.BeforeUnloadControlPtr;
  49. Mojo.bindInterface(
  50. extensions.mimeHandler.BeforeUnloadControl.name,
  51. mojo.makeRequest(beforeUnloadControlPtr).handle);
  52. // Stores a promise to the GetStreamInfo() result to avoid making additional
  53. // calls in response to getStreamInfo() calls.
  54. var streamInfoPromise;
  55. function throwNoStreamError() {
  56. throw new Error(NO_STREAM_ERROR);
  57. }
  58. function createStreamInfoPromise() {
  59. return servicePtr.getStreamInfo().then(function(result) {
  60. if (!result.streamInfo)
  61. throw new Error(STREAM_ABORTED_ERROR);
  62. return result.streamInfo;
  63. }, throwNoStreamError);
  64. }
  65. function constructStreamInfoDict(streamInfo) {
  66. var headers = {};
  67. for (var header of streamInfo.responseHeaders) {
  68. headers[header[0]] = header[1];
  69. }
  70. return {
  71. mimeType: streamInfo.mimeType,
  72. originalUrl: streamInfo.originalUrl,
  73. streamUrl: streamInfo.streamUrl,
  74. tabId: streamInfo.tabId,
  75. embedded: !!streamInfo.embedded,
  76. responseHeaders: headers,
  77. };
  78. }
  79. apiBridge.registerCustomHook(function(bindingsAPI) {
  80. var apiFunctions = bindingsAPI.apiFunctions;
  81. handleRequestWithPromiseDoNotUse(
  82. apiFunctions, 'mimeHandlerPrivate', 'getStreamInfo',
  83. function() {
  84. if (!streamInfoPromise)
  85. streamInfoPromise = createStreamInfoPromise();
  86. return streamInfoPromise.then(constructStreamInfoDict);
  87. });
  88. apiFunctions.setHandleRequest(
  89. 'setPdfPluginAttributes', function(pdfPluginAttributes) {
  90. servicePtr.setPdfPluginAttributes(pdfPluginAttributes);
  91. });
  92. handleRequestWithPromiseDoNotUse(
  93. apiFunctions, 'mimeHandlerPrivate', 'setShowBeforeUnloadDialog',
  94. function(showDialog) {
  95. return beforeUnloadControlPtr.setShowBeforeUnloadDialog(showDialog);
  96. });
  97. });