Index: extensions/renderer/resources/mime_handler_custom_bindings.js |
diff --git a/extensions/renderer/resources/mime_handler_custom_bindings.js b/extensions/renderer/resources/mime_handler_custom_bindings.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..dab18426040f6ec7d992d40f9a022de99a74ecf9 |
--- /dev/null |
+++ b/extensions/renderer/resources/mime_handler_custom_bindings.js |
@@ -0,0 +1,77 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+/** |
+ * Custom bindings for the mime handler API. |
+ */ |
+ |
+let binding = require('binding').Binding.create('mimeHandler'); |
+ |
+const NO_STREAM_ERROR = |
+ 'Streams are only available from a mime handler view guest.'; |
+const STREAM_ABORTED_ERROR = 'Stream has been aborted.'; |
+ |
+let servicePromise = Promise.all([ |
+ requireAsync('content/public/renderer/service_provider'), |
+ requireAsync('extensions/common/api/mime_handler.mojom'), |
+ requireAsync('mojo/public/js/router'), |
+]).then(function(modules) { |
+ let serviceProvider = modules[0]; |
+ let mojom = modules[1]; |
+ let routerModule = modules[2]; |
+ return new mojom.MimeHandlerService.proxyClass(new routerModule.Router( |
+ serviceProvider.connectToService(mojom.MimeHandlerService.name))); |
+}); |
+ |
+// Stores a promise to the GetStreamInfo() result to avoid making additional |
+// calls in response to getStreamInfo() calls. |
+let streamInfoPromise; |
+ |
+function throwError(error) { |
+ return function() { |
+ throw new Error(error); |
+ } |
+} |
+ |
+function createStreamInfoPromise() { |
+ return servicePromise.then(function(service) { |
+ return service.getStreamInfo(); |
+ }).then(function(result) { |
+ if (!result.stream_info) |
+ throw new Error(STREAM_ABORTED_ERROR); |
+ return result.stream_info; |
+ }, throwError(NO_STREAM_ERROR)); |
+} |
+ |
+function constructStreamInfoDict(streamInfo) { |
+ let headers = {}; |
+ for (let header of streamInfo.response_headers) { |
+ headers[header[0]] = header[1]; |
+ } |
+ return { |
+ mimeType: streamInfo.mime_type, |
+ originalUrl: streamInfo.original_url, |
+ streamUrl: streamInfo.stream_url, |
+ tabId: streamInfo.tab_id, |
+ embedded: !!streamInfo.embedded, |
+ responseHeaders: headers, |
+ }; |
+} |
+ |
+binding.registerCustomHook(function(bindingsAPI) { |
+ let apiFunctions = bindingsAPI.apiFunctions; |
+ apiFunctions.setHandleRequestWithPromise('getStreamInfo', function() { |
+ if (!streamInfoPromise) |
+ streamInfoPromise = createStreamInfoPromise(); |
+ return streamInfoPromise.then(constructStreamInfoDict); |
+ }); |
+ |
+ apiFunctions.setHandleRequestWithPromise('abortStream', function() { |
+ return servicePromise.then(function(service) { |
+ return service.abortStream().then(function() {}); |
+ }).catch(throwError(NO_STREAM_ERROR)); |
+ }); |
+}); |
+ |
+exports.binding = binding.generate(); |