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.
5 var binding = require('binding').Binding.create('printerProvider');
6 var printerProviderInternal = require('binding').Binding.create(
7 'printerProviderInternal').generate();
8 var eventBindings = require('event_bindings');
9 var blobNatives = requireNative('blob_natives');
11 var printerProviderSchema =
12 requireNative('schema_registry').GetSchema('printerProvider')
13 var utils = require('utils');
14 var validate = require('schemaUtils').validate;
16 // Custom bindings for chrome.printerProvider API.
17 // The bindings are used to implement callbacks for the API events. Internally
18 // each event is passed requestId argument used to identify the callback
19 // associated with the event. This argument is massaged out from the event
20 // arguments before dispatching the event to consumers. A callback is appended
21 // to the event arguments. The callback wraps an appropriate
22 // chrome.printerProviderInternal API function that is used to report the event
23 // result from the extension. The function is passed requestId and values
24 // provided by the extension. It validates that the values provided by the
25 // extension match chrome.printerProvider event callback schemas. It also
26 // ensures that a callback is run at most once. In case there is an exception
27 // during event dispatching, the chrome.printerProviderInternal function
28 // is called with a default error value.
31 // Handles a chrome.printerProvider event as described in the file comment.
32 // |eventName|: The event name.
33 // |prepareArgsForDispatch|: Function called before dispatching the event to
34 // the extension. It's called with original event |args| list and callback
35 // that should be called when the |args| are ready for dispatch. The
36 // callbacks should report whether the argument preparation was successful.
37 // The function should not change the first argument, which contains the
39 // |resultreporter|: The function that should be called to report event result.
40 // One of chrome.printerProviderInternal API functions.
41 function handleEvent(eventName, prepareArgsForDispatch, resultReporter) {
42 eventBindings.registerArgumentMassager(
43 'printerProvider.' + eventName,
44 function(args, dispatch) {
45 var responded = false;
47 // Validates that the result passed by the extension to the event
48 // callback matches the callback schema. Throws an exception in case of
50 var validateResult = function(result) {
52 utils.lookup(printerProviderSchema.events, 'name', eventName);
54 utils.lookup(eventSchema.parameters, 'type', 'function');
56 validate([result], callbackSchema.parameters);
59 // Function provided to the extension as the event callback argument.
60 // It makes sure that the event result hasn't previously been returned
61 // and that the provided result matches the callback schema. In case of
62 // an error it throws an exception.
63 var reportResult = function(result) {
66 'Event callback must not be called more than once.');
69 var finalResult = null;
71 validateResult(result); // throws on failure
75 resultReporter(args[0] /* requestId */, finalResult);
79 prepareArgsForDispatch(args, function(success) {
81 // Do not throw an exception since the extension should not yet be
82 // aware of the event.
83 resultReporter(args[0] /* requestId */, null);
86 dispatch(args.slice(1).concat(reportResult));
91 // Sets up printJob.document property for a print request.
92 function createPrintRequestBlobArguments(args, callback) {
93 printerProviderInternal.getPrintData(args[0] /* requestId */,
95 if (chrome.runtime.lastError) {
100 // |args[1]| is printJob.
101 args[1].document = blobNatives.TakeBrowserProcessBlob(
102 blobInfo.blobUuid, blobInfo.type, blobInfo.size);
107 handleEvent('onGetPrintersRequested',
108 function(args, callback) { callback(true); },
109 printerProviderInternal.reportPrinters);
111 handleEvent('onGetCapabilityRequested',
112 function(args, callback) { callback(true); },
113 printerProviderInternal.reportPrinterCapability);
115 handleEvent('onPrintRequested',
116 createPrintRequestBlobArguments,
117 printerProviderInternal.reportPrintResult);
119 handleEvent('onGetUsbPrinterInfoRequested',
120 function(args, callback) { callback(true); },
121 printerProviderInternal.reportUsbPrinterInfo);
123 exports.binding = binding.generate();