OLD | NEW |
(Empty) | |
| 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 /** |
| 6 * Custom bindings for the mime handler API. |
| 7 */ |
| 8 |
| 9 let binding = require('binding').Binding.create('mimeHandler'); |
| 10 |
| 11 const NO_STREAM_ERROR = |
| 12 'Streams are only available from a mime handler view guest.'; |
| 13 const STREAM_ABORTED_ERROR = 'Stream has been aborted.'; |
| 14 |
| 15 let servicePromise = Promise.all([ |
| 16 requireAsync('content/public/renderer/service_provider'), |
| 17 requireAsync('extensions/common/api/mime_handler.mojom'), |
| 18 requireAsync('mojo/public/js/router'), |
| 19 ]).then(function(modules) { |
| 20 let serviceProvider = modules[0]; |
| 21 let mojom = modules[1]; |
| 22 let routerModule = modules[2]; |
| 23 return new mojom.MimeHandlerService.proxyClass(new routerModule.Router( |
| 24 serviceProvider.connectToService(mojom.MimeHandlerService.name))); |
| 25 }); |
| 26 |
| 27 // Stores a promise to the GetStreamInfo() result to avoid making additional |
| 28 // calls in response to getStreamInfo() calls. |
| 29 let streamInfoPromise; |
| 30 |
| 31 function throwError(error) { |
| 32 return function() { |
| 33 throw new Error(error); |
| 34 } |
| 35 } |
| 36 |
| 37 function createStreamInfoPromise() { |
| 38 return servicePromise.then(function(service) { |
| 39 return service.getStreamInfo(); |
| 40 }).then(function(result) { |
| 41 if (!result.stream_info) |
| 42 throw new Error(STREAM_ABORTED_ERROR); |
| 43 return result.stream_info; |
| 44 }, throwError(NO_STREAM_ERROR)); |
| 45 } |
| 46 |
| 47 function constructStreamInfoDict(streamInfo) { |
| 48 let headers = {}; |
| 49 for (let header of streamInfo.response_headers) { |
| 50 headers[header[0]] = header[1]; |
| 51 } |
| 52 return { |
| 53 mimeType: streamInfo.mime_type, |
| 54 originalUrl: streamInfo.original_url, |
| 55 streamUrl: streamInfo.stream_url, |
| 56 tabId: streamInfo.tab_id, |
| 57 embedded: !!streamInfo.embedded, |
| 58 responseHeaders: headers, |
| 59 }; |
| 60 } |
| 61 |
| 62 binding.registerCustomHook(function(bindingsAPI) { |
| 63 let apiFunctions = bindingsAPI.apiFunctions; |
| 64 apiFunctions.setHandleRequestWithPromise('getStreamInfo', function() { |
| 65 if (!streamInfoPromise) |
| 66 streamInfoPromise = createStreamInfoPromise(); |
| 67 return streamInfoPromise.then(constructStreamInfoDict); |
| 68 }); |
| 69 |
| 70 apiFunctions.setHandleRequestWithPromise('abortStream', function() { |
| 71 return servicePromise.then(function(service) { |
| 72 return service.abortStream().then(function() {}); |
| 73 }).catch(throwError(NO_STREAM_ERROR)); |
| 74 }); |
| 75 }); |
| 76 |
| 77 exports.binding = binding.generate(); |
OLD | NEW |