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.
6 * Custom bindings for the mime handler API.
9 var binding = require('binding').Binding.create('mimeHandlerPrivate');
12 'Streams are only available from a mime handler view guest.';
13 var STREAM_ABORTED_ERROR = 'Stream has been aborted.';
15 var 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 var serviceProvider = modules[0];
21 var mojom = modules[1];
22 var routerModule = modules[2];
23 return new mojom.MimeHandlerService.proxyClass(new routerModule.Router(
24 serviceProvider.connectToService(mojom.MimeHandlerService.name)));
27 // Stores a promise to the GetStreamInfo() result to avoid making additional
28 // calls in response to getStreamInfo() calls.
29 var streamInfoPromise;
31 function throwNoStreamError() {
32 throw new Error(NO_STREAM_ERROR);
35 function createStreamInfoPromise() {
36 return servicePromise.then(function(service) {
37 return service.getStreamInfo();
38 }).then(function(result) {
39 if (!result.stream_info)
40 throw new Error(STREAM_ABORTED_ERROR);
41 return result.stream_info;
42 }, throwNoStreamError);
45 function constructStreamInfoDict(streamInfo) {
47 for (var header of streamInfo.response_headers) {
48 headers[header[0]] = header[1];
51 mimeType: streamInfo.mime_type,
52 originalUrl: streamInfo.original_url,
53 streamUrl: streamInfo.stream_url,
54 tabId: streamInfo.tab_id,
55 embedded: !!streamInfo.embedded,
56 responseHeaders: headers,
60 binding.registerCustomHook(function(bindingsAPI) {
61 var apiFunctions = bindingsAPI.apiFunctions;
62 apiFunctions.setHandleRequestWithPromise('getStreamInfo', function() {
63 if (!streamInfoPromise)
64 streamInfoPromise = createStreamInfoPromise();
65 return streamInfoPromise.then(constructStreamInfoDict);
68 apiFunctions.setHandleRequestWithPromise('abortStream', function() {
69 return servicePromise.then(function(service) {
70 return service.abortStream().then(function() {});
71 }).catch(throwNoStreamError);
75 exports.binding = binding.generate();