diff options
author | tbarzic <tbarzic@chromium.org> | 2015-01-16 21:15:18 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-01-17 05:16:42 +0000 |
commit | e3bca061090d0f9abaea3fe07677b8418681a361 (patch) | |
tree | 59d6abcc3f0f13e4b21a9f7adcf39d03737210dd /extensions | |
parent | 064949f95f66d4a56160c4fa377df48ae5d1215a (diff) | |
download | chromium_src-e3bca061090d0f9abaea3fe07677b8418681a361.zip chromium_src-e3bca061090d0f9abaea3fe07677b8418681a361.tar.gz chromium_src-e3bca061090d0f9abaea3fe07677b8418681a361.tar.bz2 |
Add custom bindings for chrome.printerProvider API
The bindings are used to implement callbacks for the API events.
The event arguments as sent from C++ are massaged before dispatching them
the event listener to remove the requestId argument provided by
printerProvider implementation and add a callback that calls
chrome.printerProviderInternal API function (with requestId and the value
provided by the extension). The internal API is supposed to handle the
callback (still to be implemented).
BUG=408772
TEST=None (will be added in subsequent cls, with the C++ side API
implementations)
Review URL: https://codereview.chromium.org/845403008
Cr-Commit-Position: refs/heads/master@{#312022}
Diffstat (limited to 'extensions')
4 files changed, 93 insertions, 3 deletions
diff --git a/extensions/common/api/printer_provider_internal.idl b/extensions/common/api/printer_provider_internal.idl index c2a962f..702cc99 100644 --- a/extensions/common/api/printer_provider_internal.idl +++ b/extensions/common/api/printer_provider_internal.idl @@ -13,24 +13,28 @@ // the argument list before the event actually reaches the event listeners). The // requestId is forwarded to the chrome.printerProviderInternal API functions. namespace printerProviderInternal { + // Same as in printerProvider.PrintError enum API. + enum PrintError { OK, FAILED, INVALID_TICKET, INVALID_DATA }; + interface Functions { // Runs callback to printerProvider.onGetPrintersRequested event. // |requestId|: Parameter identifying the event instance for which the // callback is run. // |printers|: List of printers reported by the extension. - void reportPrinters(long requestId, printerProvider.PrinterInfo[] printers); + void reportPrinters(long requestId, + optional printerProvider.PrinterInfo[] printers); // Runs callback to printerProvider.onGetCapabilityRequested event. // |requestId|: Parameter identifying the event instance for which the // callback is run. // |error|: The printer capability returned by the extension. - void reportPrinterCapability(long request_id, object capability); + void reportPrinterCapability(long request_id, optional object capability); // Runs callback to printerProvider.onPrintRequested event. // |requestId|: Parameter identifying the event instance for which the // callback is run. // |error|: The requested print job result. - void reportPrintResult(long request_id, printerProvider.PrintError error); + void reportPrintResult(long request_id, optional PrintError error); }; }; diff --git a/extensions/renderer/dispatcher.cc b/extensions/renderer/dispatcher.cc index 2126c28..0d635af 100644 --- a/extensions/renderer/dispatcher.cc +++ b/extensions/renderer/dispatcher.cc @@ -613,6 +613,8 @@ std::vector<std::pair<std::string, int> > Dispatcher::GetJsResources() { std::make_pair("mojoPrivate", IDR_MOJO_PRIVATE_CUSTOM_BINDINGS_JS)); resources.push_back( std::make_pair("permissions", IDR_PERMISSIONS_CUSTOM_BINDINGS_JS)); + resources.push_back(std::make_pair("printerProvider", + IDR_PRINTER_PROVIDER_CUSTOM_BINDINGS_JS)); resources.push_back( std::make_pair("runtime", IDR_RUNTIME_CUSTOM_BINDINGS_JS)); resources.push_back(std::make_pair("windowControls", IDR_WINDOW_CONTROLS_JS)); diff --git a/extensions/renderer/resources/extensions_renderer_resources.grd b/extensions/renderer/resources/extensions_renderer_resources.grd index 331755bc..3a8bedc 100644 --- a/extensions/renderer/resources/extensions_renderer_resources.grd +++ b/extensions/renderer/resources/extensions_renderer_resources.grd @@ -61,6 +61,7 @@ <include name="IDR_I18N_CUSTOM_BINDINGS_JS" file="i18n_custom_bindings.js" type="BINDATA" /> <include name="IDR_MOJO_PRIVATE_CUSTOM_BINDINGS_JS" file="mojo_private_custom_bindings.js" type="BINDATA" /> <include name="IDR_PERMISSIONS_CUSTOM_BINDINGS_JS" file="permissions_custom_bindings.js" type="BINDATA" /> + <include name="IDR_PRINTER_PROVIDER_CUSTOM_BINDINGS_JS" file="printer_provider_custom_bindings.js" type="BINDATA" /> <include name="IDR_RUNTIME_CUSTOM_BINDINGS_JS" file="runtime_custom_bindings.js" type="BINDATA" /> <include name="IDR_WEB_REQUEST_CUSTOM_BINDINGS_JS" file="web_request_custom_bindings.js" type="BINDATA" /> <include name="IDR_WEB_REQUEST_INTERNAL_CUSTOM_BINDINGS_JS" file="web_request_internal_custom_bindings.js" type="BINDATA" /> diff --git a/extensions/renderer/resources/printer_provider_custom_bindings.js b/extensions/renderer/resources/printer_provider_custom_bindings.js new file mode 100644 index 0000000..9bf9fa2 --- /dev/null +++ b/extensions/renderer/resources/printer_provider_custom_bindings.js @@ -0,0 +1,83 @@ +// 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. + +var binding = require('binding').Binding.create('printerProvider'); +var printerProviderInternal = require('binding').Binding.create( + 'printerProviderInternal').generate(); +var eventBindings = require('event_bindings'); + +var printerProviderSchema = + requireNative('schema_registry').GetSchema('printerProvider') +var utils = require('utils'); +var validate = require('schemaUtils').validate; + +// Custom bindings for chrome.printerProvider API. +// The bindings are used to implement callbacks for the API events. Internally +// each event is passed requestId argument used to identify the callback +// associated with the event. This argument is massaged out from the event +// arguments before dispatching the event to consumers. A callback is appended +// to the event arguments. The callback wraps an appropriate +// chrome.printerProviderInternal API function that is used to report the event +// result from the extension. The function is passed requestId and values +// provided by the extension. It validates that the values provided by the +// extension match chrome.printerProvider event callback schemas. It also +// ensures that a callback is run at most once. In case there is an exception +// during event dispatching, the chrome.printerProviderInternal function +// is called with a default error value. +// + +// Handles a chrome.printerProvider event as described in the file comment. +// |eventName|: The event name. +// |resultreporter|: The function that should be called to report event result. +// One of chrome.printerProviderInternal API functions. +function handleEvent(eventName, resultReporter) { + eventBindings.registerArgumentMassager( + 'printerProvider.' + eventName, + function(args, dispatch) { + var responded = false; + + // Validates that the result passed by the extension to the event + // callback matches the callback schema. Throws an exception in case of + // an error. + var validateResult = function(result) { + var eventSchema = + utils.lookup(printerProviderSchema.events, 'name', eventName); + var callbackSchema = + utils.lookup(eventSchema.parameters, 'type', 'function'); + + validate([result], callbackSchema.parameters); + }; + + // Function provided to the extension as the event callback argument. + // It makes sure that the event result hasn't previously been returned + // and that the provided result matches the callback schema. In case of + // an error it throws an exception. + var reportResult = function(result) { + if (responded) { + throw new Error( + 'Event callback must not be called more than once.'); + } + + var finalResult = null; + try { + validateResult(result); // throws on failure + finalResult = result; + } finally { + responded = true; + resultReporter(args[0] /* requestId */, finalResult); + } + }; + + dispatch(args.slice(1).concat(reportResult)); + }); +} + +handleEvent('onGetPrintersRequested', printerProviderInternal.reportPrinters); + +handleEvent('onGetCapabilityRequested', + printerProviderInternal.reportPrinterCapability); + +handleEvent('onPrintRequested', printerProviderInternal.reportPrintResult); + +exports.binding = binding.generate(); |