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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « extensions/renderer/resources/extensions_renderer_resources.grd ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: extensions/renderer/resources/mime_handler_private_custom_bindings.js
diff --git a/extensions/renderer/resources/mime_handler_private_custom_bindings.js b/extensions/renderer/resources/mime_handler_private_custom_bindings.js
new file mode 100644
index 0000000000000000000000000000000000000000..83a41af40a4e7c2c981de4956c5810126cf3def5
--- /dev/null
+++ b/extensions/renderer/resources/mime_handler_private_custom_bindings.js
@@ -0,0 +1,75 @@
+// 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.
+ */
+
+var binding = require('binding').Binding.create('mimeHandlerPrivate');
+
+var NO_STREAM_ERROR =
+ 'Streams are only available from a mime handler view guest.';
+var STREAM_ABORTED_ERROR = 'Stream has been aborted.';
+
+var servicePromise = Promise.all([
+ requireAsync('content/public/renderer/service_provider'),
+ requireAsync('extensions/common/api/mime_handler.mojom'),
+ requireAsync('mojo/public/js/router'),
+]).then(function(modules) {
+ var serviceProvider = modules[0];
+ var mojom = modules[1];
+ var 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.
+var streamInfoPromise;
+
+function throwNoStreamError() {
+ throw new Error(NO_STREAM_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;
+ }, throwNoStreamError);
+}
+
+function constructStreamInfoDict(streamInfo) {
+ var headers = {};
+ for (var 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) {
+ var 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(throwNoStreamError);
+ });
+});
+
+exports.binding = binding.generate();
« no previous file with comments | « extensions/renderer/resources/extensions_renderer_resources.grd ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698