OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 var binding = require('binding').Binding.create('printerProvider'); | 5 var binding = require('binding').Binding.create('printerProvider'); |
6 var printerProviderInternal = require('binding').Binding.create( | 6 var printerProviderInternal = require('binding').Binding.create( |
7 'printerProviderInternal').generate(); | 7 'printerProviderInternal').generate(); |
8 var eventBindings = require('event_bindings'); | 8 var eventBindings = require('event_bindings'); |
9 var blobNatives = requireNative('blob_natives'); | |
9 | 10 |
10 var printerProviderSchema = | 11 var printerProviderSchema = |
11 requireNative('schema_registry').GetSchema('printerProvider') | 12 requireNative('schema_registry').GetSchema('printerProvider') |
12 var utils = require('utils'); | 13 var utils = require('utils'); |
13 var validate = require('schemaUtils').validate; | 14 var validate = require('schemaUtils').validate; |
14 | 15 |
15 // Custom bindings for chrome.printerProvider API. | 16 // Custom bindings for chrome.printerProvider API. |
16 // The bindings are used to implement callbacks for the API events. Internally | 17 // The bindings are used to implement callbacks for the API events. Internally |
17 // each event is passed requestId argument used to identify the callback | 18 // each event is passed requestId argument used to identify the callback |
18 // associated with the event. This argument is massaged out from the event | 19 // associated with the event. This argument is massaged out from the event |
19 // arguments before dispatching the event to consumers. A callback is appended | 20 // arguments before dispatching the event to consumers. A callback is appended |
20 // to the event arguments. The callback wraps an appropriate | 21 // to the event arguments. The callback wraps an appropriate |
21 // chrome.printerProviderInternal API function that is used to report the event | 22 // chrome.printerProviderInternal API function that is used to report the event |
22 // result from the extension. The function is passed requestId and values | 23 // result from the extension. The function is passed requestId and values |
23 // provided by the extension. It validates that the values provided by the | 24 // provided by the extension. It validates that the values provided by the |
24 // extension match chrome.printerProvider event callback schemas. It also | 25 // extension match chrome.printerProvider event callback schemas. It also |
25 // ensures that a callback is run at most once. In case there is an exception | 26 // ensures that a callback is run at most once. In case there is an exception |
26 // during event dispatching, the chrome.printerProviderInternal function | 27 // during event dispatching, the chrome.printerProviderInternal function |
27 // is called with a default error value. | 28 // is called with a default error value. |
28 // | 29 // |
29 | 30 |
30 // Handles a chrome.printerProvider event as described in the file comment. | 31 // Handles a chrome.printerProvider event as described in the file comment. |
31 // |eventName|: The event name. | 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 | |
38 // request id. | |
32 // |resultreporter|: The function that should be called to report event result. | 39 // |resultreporter|: The function that should be called to report event result. |
33 // One of chrome.printerProviderInternal API functions. | 40 // One of chrome.printerProviderInternal API functions. |
34 function handleEvent(eventName, resultReporter) { | 41 function handleEvent(eventName, prepareArgsForDispatch, resultReporter) { |
35 eventBindings.registerArgumentMassager( | 42 eventBindings.registerArgumentMassager( |
36 'printerProvider.' + eventName, | 43 'printerProvider.' + eventName, |
37 function(args, dispatch) { | 44 function(args, dispatch) { |
38 var responded = false; | 45 var responded = false; |
39 | 46 |
40 // Validates that the result passed by the extension to the event | 47 // Validates that the result passed by the extension to the event |
41 // callback matches the callback schema. Throws an exception in case of | 48 // callback matches the callback schema. Throws an exception in case of |
42 // an error. | 49 // an error. |
43 var validateResult = function(result) { | 50 var validateResult = function(result) { |
44 var eventSchema = | 51 var eventSchema = |
(...skipping 17 matching lines...) Expand all Loading... | |
62 var finalResult = null; | 69 var finalResult = null; |
63 try { | 70 try { |
64 validateResult(result); // throws on failure | 71 validateResult(result); // throws on failure |
65 finalResult = result; | 72 finalResult = result; |
66 } finally { | 73 } finally { |
67 responded = true; | 74 responded = true; |
68 resultReporter(args[0] /* requestId */, finalResult); | 75 resultReporter(args[0] /* requestId */, finalResult); |
69 } | 76 } |
70 }; | 77 }; |
71 | 78 |
72 dispatch(args.slice(1).concat(reportResult)); | 79 prepareArgsForDispatch(args, function(success) { |
80 if (!success) { | |
81 // Do not throw an exception since the extension should not yet be | |
82 // aware of the event. | |
83 resultReporter(args[0], null); | |
not at google - send to devlin
2015/03/04 21:54:49
document that args[0] is the request ID, like you
tbarzic
2015/03/04 23:24:34
Done.
| |
84 return; | |
85 } | |
86 dispatch(args.slice(1).concat(reportResult)); | |
87 }); | |
73 }); | 88 }); |
74 } | 89 } |
75 | 90 |
76 handleEvent('onGetPrintersRequested', printerProviderInternal.reportPrinters); | 91 // Sets up printJob.document property for a print request. The printJob argument |
92 // is at |args[1]|. | |
93 function createPrintRequestBlobArguments(args, callback) { | |
94 printerProviderInternal.getPrintData(args[0], function(blobInfo) { | |
not at google - send to devlin
2015/03/04 21:54:49
and here
tbarzic
2015/03/04 23:24:34
Done.
| |
95 if (chrome.runtime.lastError) { | |
96 callback(false); | |
97 return; | |
98 } | |
99 | |
100 args[1].document = blobNatives.TakeBrowserProcessBlob( | |
not at google - send to devlin
2015/03/04 21:54:49
and document what args[1] is.
tbarzic
2015/03/04 23:24:34
Done.
| |
101 blobInfo.blobUuid, blobInfo.type, blobInfo.size); | |
102 callback(true); | |
103 }); | |
104 } | |
105 | |
106 handleEvent('onGetPrintersRequested', | |
107 function(args, callback) { callback(true); }, | |
108 printerProviderInternal.reportPrinters); | |
77 | 109 |
78 handleEvent('onGetCapabilityRequested', | 110 handleEvent('onGetCapabilityRequested', |
111 function(args, callback) { callback(true); }, | |
79 printerProviderInternal.reportPrinterCapability); | 112 printerProviderInternal.reportPrinterCapability); |
80 | 113 |
81 handleEvent('onPrintRequested', printerProviderInternal.reportPrintResult); | 114 handleEvent('onPrintRequested', |
115 createPrintRequestBlobArguments, | |
116 printerProviderInternal.reportPrintResult); | |
82 | 117 |
83 exports.binding = binding.generate(); | 118 exports.binding = binding.generate(); |
OLD | NEW |