Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: extensions/renderer/resources/mime_handler_private_custom_bindings.js

Issue 797183005: Add a mimeHandler extension API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@streams-lifetime
Patch Set: rebase Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 var binding = require('binding').Binding.create('mimeHandlerPrivate');
10
11 var NO_STREAM_ERROR =
12 'Streams are only available from a mime handler view guest.';
13 var STREAM_ABORTED_ERROR = 'Stream has been aborted.';
14
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)));
25 });
26
27 // Stores a promise to the GetStreamInfo() result to avoid making additional
28 // calls in response to getStreamInfo() calls.
29 var streamInfoPromise;
30
31 function throwError(error) {
benwells 2015/01/19 01:05:19 Sorry this code makes me feel dumb. Why does throw
Sam McNally 2015/01/19 04:29:57 Changed to not do this.
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 var headers = {};
49 for (var 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 var 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();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698