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

Side by Side Diff: chrome/browser/resources/print_preview/native_layer.js

Issue 10108001: Refactor print preview web ui (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove extra files Created 8 years, 8 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 cr.define('print_preview', function() {
6 'use strict';
7
8 /**
9 * An interface to the native Chromium printing system layer.
10 *
11 * @constructor
12 * @extends {cr.EventTarget}
13 */
14 function NativeLayer() {
15 cr.EventTarget.call(this);
16 };
17
18 /**
19 * Events dispatched from the Chromium native layer.
20 * @enum {string}
21 * @const
22 */
23 NativeLayer.Event = {
24 CAPABILITIES_SET: 'print_preview.NativeLayer.CAPABILITIES_SET',
25 CLOUD_PRINT_ENABLE: 'print_preview.NativeLayer.CLOUD_PRINT_ENABLE',
26 DESTINATIONS_RELOAD: 'print_preview.NativeLayer.DESTINATIONS_RELOAD',
27 FILE_SELECTION_CANCEL: 'print_preview.NativeLayer.FILE_SELECTION_CANCEL',
28 FILE_SELECTION_COMPLETE:
29 'print_preview.NativeLayer.FILE_SELECTION_COMPLETE',
30 INITIAL_SETTINGS_SET: 'print_preview.NativeLayer.INITIAL_SETTINGS_SET',
31 LOCAL_DESTINATIONS_SET: 'print_preview.NativeLayer.LOCAL_DESTINATIONS_SET',
32 PAGE_COUNT_CHANGE: 'print_preview.NativeLayer.PAGE_COUNT_CHANGE',
33 PAGE_LAYOUT_CHANGE: 'print_preview.NativeLayer.PAGE_LAYOUT_CHANGE',
34 PAGE_PREVIEW_READY: 'print_preview.NativeLayer.PAGE_PREVIEW_READY',
35 PREVIEW_GENERATION_DONE:
36 'print_preview.NativeLayer.PREVIEW_GENERATION_DONE',
37 PREVIEW_GENERATION_FAIL:
38 'print_preview.NativeLayer.PREVIEW_GENERATION_FAIL',
39 PREVIEW_RELOAD: 'print_preview.NativeLayer.PREVIEW_RELOAD',
40 PRINT_TO_CLOUD: 'print_preview.NativeLayer.PRINT_TO_CLOUD',
41 SETTINGS_INVALID: 'print_preview.NativeLayer.SETTINGS_INVALID'
42 };
43
44 NativeLayer.prototype = {
45 __proto__: cr.EventTarget.prototype,
46
47 // TODO Remove me
48 dispatchEvent: function(evt) {
49 log(evt.type);
50 cr.EventTarget.prototype.dispatchEvent.call(this, evt);
51 },
52
53 /** Gets the initial settings to initialize the print preview with. */
54 startGetInitialSettings: function() {
55 chrome.send('getInitialSettings');
56 },
57
58 /**
59 * Requests the system's local print destinations. A LOCAL_DESTINATIONS_SET
60 * event will be dispatched in response.
61 */
62 startGetLocalDestinations: function() {
63 chrome.send('getPrinters');
64 },
65
66 /**
67 * Requests the destination's printing capabilities. A CAPABILITIES_SET
68 * event will be dispatched in response.
69 * @param {string} destinationId ID of the destination.
70 */
71 startGetLocalDestinationCapabilities: function(destinationId) {
72 chrome.send('getPrinterCapabilities', [destinationId]);
73 },
74
75 /**
76 * Requests that a preview be generated. The following events may be
77 * dispatched in response:
78 * - PAGE_COUNT_CHANGE
79 * - PAGE_LAYOUT_CHANGE
80 * - PAGE_PREVIEW_READY
81 * - PREVIEW_GENERATION_DONE
82 * - PREVIEW_GENERATION_FAIL
83 * - PREVIEW_RELOAD
84 * @param {print_preview.Destination?} destination Destination to print to.
85 * @param {print_preview.PrintTicketStore!} printTicketStore Used to get the
86 * state of the print ticket.
87 * @param {number} ID of the preview request.
88 */
89 startGetPreview: function(destination, printTicketStore, requestId) {
90 if (!printTicketStore.isTicketValid()) {
91 throw Error('Trying to generate preview when ticket is not valid');
92 }
93
94 var ticket = {
95 'pageRange': [], // TODO this.ticket_.pageRangeSequence,
96 'landscape': printTicketStore.isLandscapeEnabled(),
97 'color': printTicketStore.getColorMode(),
98 'headerFooterEnabled': printTicketStore.isHeaderFooterEnabled(),
99 'marginsType': printTicketStore.getMarginsType(),
100 'isFirstRequest': requestId == 0,
101 'requestID': requestId,
102 'previewModifiable': printTicketStore.isDocumentModifiable,
103 'printToPDF': destination != null && destination.isPrintToPdf,
104 'printWithCloudPrint': destination != null && !destination.isLocal,
105 'deviceName': destination == null ? 'foo' : destination.id,
106 'cloudPrintID': destination == null ? 'foo' : destination.id,
107
108 // This field seems to issue a reloadPreviewPages call when true.
109 'generateDraftData': true, // TODO What should this value be?
110
111 // NOTE: Even though the following fields don't directly relate to the
112 // preview, they still need to be included.
113 'duplex': printTicketStore.getDuplexMode(),
114 'copies': printTicketStore.getCopies(),
115 'collate': printTicketStore.isCollateEnabled()
116 };
117
118 if (printTicketStore.getMarginsType() ==
119 print_preview.Margins.Type.CUSTOM) {
120 var customMargins = printTicketStore.getCustomMargins();
121 ticket['marginsCustom'] = {
122 'marginTop': customMargins.top,
123 'marginRight': customMargins.right,
124 'marginBottom': customMargins.bottom,
125 'marginLeft': customMargins.left
126 };
127 }
128
129 var pageCount = requestId == 0 ? -1 : printTicketStore.pageCount;
130 // TODO this.printTicketStore_.getPageNumberSet().size,
131 chrome.send(
132 'getPreview',
133 [JSON.stringify(ticket),
134 pageCount,
135 printTicketStore.isDocumentModifiable]);
136 },
137
138 /**
139 * Persists the selected destination and print ticket for the next print
140 * session.
141 * @param {string} destinatId ID of the destination.
142 * @param {string} serializedTicket Serialized form of the print ticket.
143 */
144 startSaveDestinationAndTicket: function(destinationId, serializedTicket) {
145 chrome.send('saveLastPrinter', [destinationId, serializedTicket]);
146 },
147
148 /**
149 * Requests that the document be printed.
150 * @param {print_preview.Destination!} destination Destination to print to.
151 * @param {print_preview.PrintTicketStore!} printTicketStore Used to get the
152 * state of the print ticket.
153 * @param {print_preview.CloudPrintInterface?} cloudPrintInterface Interface
154 * to Google Cloud Print.
155 */
156 startPrint: function(destination, printTicketStore, cloudPrintInterface) {
157 if (!printTicketStore.isTicketValid()) {
158 throw Error(
159 'Requesting ticket for preview generator when ticket is not valid');
160 }
161
162 var ticket = {
163 'pageRange': [], // TODO this.ticket_.pageRangeSequence,
164 'landscape': printTicketStore.isLandscapeEnabled(),
165 'color': printTicketStore.getColorMode(),
166 'headerFooterEnabled': printTicketStore.isHeaderFooterEnabled(),
167 'marginsType': printTicketStore.getMarginsType(),
168 'generateDraftData': true, // TODO What should this value be?
169 'duplex': printTicketStore.getDuplexMode(),
170 'copies': printTicketStore.getCopies(),
171 'collate': printTicketStore.isCollateEnabled(),
172 'previewModifiable': printTicketStore.isDocumentModifiable,
173 'printToPDF': destination.isPrintToPdf,
174 'printWithCloudPrint': !destination.isLocal,
175 'deviceName': destination.id,
176 'isFirstRequest': false,
177 'requestID': -1
178 };
179
180 if (!destination.isLocal && !destination.isPrintWithCloudPrint) {
181 // We can't set cloudPrintID if the destination is "Print with Cloud
182 // Print" because the native system will try to print to Google Cloud
183 // Print with this ID instead of opening a Google Cloud Print dialog.
184 ticket['cloudPrintID'] = destination.id;
185 }
186
187 if (printTicketStore.getMarginsType() ==
188 print_preview.Margins.Type.CUSTOM) {
189 var customMargins = printTicketStore.getCustomMargins();
190 ticket['marginsCustom'] = {
191 'marginTop': customMargins.top,
192 'marginRight': customMargins.right,
193 'marginBottom': customMargins.bottom,
194 'marginLeft': customMargins.left
195 };
196 }
197
198 var cloudTicket = null;
199 if (!destination.isLocal) {
200 if (!cloudPrintInterface) {
201 throw Error(
202 'Trying to print to a cloud destination but Google Cloud Print ' +
203 'integration is disabled');
204 }
205 cloudTicket = cloudPrintInterface.createPrintTicket(
206 destination, printTicketStore);
207 cloudTicket = JSON.stringify(cloudTicket);
208 }
209
210 chrome.send('print', [JSON.stringify(ticket), cloudTicket]);
211 },
212
213 /** Requests that the current pending print request be cancelled. */
214 startCancelPendingPrint: function() {
215 chrome.send('cancelPendingPrintRequest');
216 },
217
218 /** Shows the system's native printing dialog. */
219 startShowSystemDialog: function() {
220 chrome.send('showSystemDialog');
221 },
222
223 /** Closes the print preview dialog. */
224 startCloseDialog: function() {
225 chrome.send('closePrintPreviewTab');
226 chrome.send('DialogClose');
227 },
228
229 /**
230 * Opens the Google Cloud Print sign-in dialog. The DESTINATIONS_RELOAD
231 * event will be dispatched in response.
232 */
233 startCloudPrintSignIn: function() {
234 chrome.send('signIn');
235 },
236
237 /** Navigates the user to the system printer settings interface. */
238 startManageLocalPrinters: function() {
239 chrome.send('manageLocalPrinters');
240 },
241
242 /** Navigates the user to the Google Cloud Print management page. */
243 startManageCloudPrinters: function() {
244 chrome.send('manageCloudPrinters');
245 }
246 };
247
248 return {
249 NativeLayer: NativeLayer
250 };
251 });
252
253 var nativeLayer = new print_preview.NativeLayer();
254
255 /** @param {object} initialSettings Object containing all initial settings. */
256 function setInitialSettings(initialSettings) {
257 var initialSettingsSetEvt = new cr.Event(
258 print_preview.NativeLayer.Event.INITIAL_SETTINGS_SET);
259 initialSettingsSetEvt.initialSettings = initialSettings;
260 nativeLayer.dispatchEvent(initialSettingsSetEvt);
261 }
262
263 /**
264 * Turn on the integration of Cloud Print.
265 * @param {string} cloudPrintURL The URL to use for cloud print servers.
266 */
267 function setUseCloudPrint(cloudPrintURL) {
268 var cloudPrintEnableEvt = new cr.Event(
269 print_preview.NativeLayer.Event.CLOUD_PRINT_ENABLE);
270 cloudPrintEnableEvt.baseCloudPrintUrl = cloudPrintURL;
271 nativeLayer.dispatchEvent(cloudPrintEnableEvt);
272 }
273
274 /**
275 * Updates the print preview with local printers.
276 * Called from PrintPreviewHandler::SetupPrinterList().
277 * @param {Array} printers Array of printer info objects.
278 */
279 function setPrinters(printers) {
280 var localDestsSetEvt = new cr.Event(
281 print_preview.NativeLayer.Event.LOCAL_DESTINATIONS_SET);
282 localDestsSetEvt.destinationInfos = printers;
283 nativeLayer.dispatchEvent(localDestsSetEvt);
284 }
285
286 /**
287 * Called when native layer gets settings information for a requested local
288 * destination.
289 * @param {Object} settingsInfo printer setting information.
290 */
291 function updateWithPrinterCapabilities(settingsInfo) {
292 var capsSetEvt = new cr.Event(
293 print_preview.NativeLayer.Event.CAPABILITIES_SET);
294 capsSetEvt.settingsInfo = settingsInfo;
295 nativeLayer.dispatchEvent(capsSetEvt);
296 }
297
298 /** Reloads the printer list. */
299 function reloadPrintersList() {
300 cr.dispatchSimpleEvent(
301 nativeLayer, print_preview.NativeLayer.Event.DESTINATIONS_RELOAD);
302 }
303
304 /**
305 * Called from the C++ layer.
306 * Take the PDF data handed to us and submit it to the cloud, closing the print
307 * preview tab once the upload is successful.
308 * @param {string} data Data to send as the print job.
309 */
310 function printToCloud(data) {
311 var printToCloudEvt = new cr.Event(
312 print_preview.NativeLayer.Event.PRINT_TO_CLOUD);
313 printToCloudEvt.data = data;
314 nativeLayer.dispatchEvent(printToCloudEvt);
315 }
316
317 /**
318 * Called from PrintPreviewUI::OnFileSelectionCancelled to notify the print
319 * preview tab regarding the file selection cancel event.
320 */
321 function fileSelectionCancelled() {
322 cr.dispatchSimpleEvent(
323 nativeLayer, print_preview.NativeLayer.Event.FILE_SELECTION_CANCEL);
324 }
325
326 /**
327 * Called from PrintPreviewUI::OnFileSelectionCompleted to notify the print
328 * preview tab regarding the file selection completed event.
329 */
330 function fileSelectionCompleted() {
331 // If the file selection is completed and the tab is not already closed it
332 // means that a pending print to pdf request exists.
333 cr.dispatchSimpleEvent(
334 nativeLayer, print_preview.NativeLayer.Event.FILE_SELECTION_COMPLETE);
335 }
336
337 /**
338 * Display an error message when print preview fails.
339 * Called from PrintPreviewMessageHandler::OnPrintPreviewFailed().
340 */
341 function printPreviewFailed() {
342 cr.dispatchSimpleEvent(
343 nativeLayer, print_preview.NativeLayer.Event.PREVIEW_GENERATION_FAIL);
344 }
345
346 /**
347 * Display an error message when encountered invalid printer settings.
348 * Called from PrintPreviewMessageHandler::OnInvalidPrinterSettings().
349 */
350 function invalidPrinterSettings() {
351 cr.dispatchSimpleEvent(
352 nativeLayer, print_preview.NativeLayer.Event.SETTINGS_INVALID);
353 }
354
355 /**
356 * @param {{contentWidth: number, contentHeight: number, marginLeft: number,
357 * marginRight: number, marginTop: number, marginBottom: number,
358 * printableAreaX: number, printableAreaY: number,
359 * printableAreaWidth: number, printableAreaHeight: number}} pageLayout
360 * Specifies default page layout details in points.
361 * @param {boolean} hasCustomPageSizeStyle Indicates whether the previewed
362 * document has a custom page size style.
363 */
364 function onDidGetDefaultPageLayout(pageLayout, hasCustomPageSizeStyle) {
365 var pageLayoutChangeEvt = new cr.Event(
366 print_preview.NativeLayer.Event.PAGE_LAYOUT_CHANGE);
367 pageLayoutChangeEvt.pageLayout = pageLayout;
368 pageLayoutChangeEvt.hasCustomPageSizeStyle = hasCustomPageSizeStyle;
369 nativeLayer.dispatchEvent(pageLayoutChangeEvt);
370 }
371
372 /**
373 * Update the page count and check the page range.
374 * Called from PrintPreviewUI::OnDidGetPreviewPageCount().
375 * @param {number} pageCount The number of pages.
376 * @param {number} previewResponseId The preview request id that resulted in
377 * this response.
378 */
379 function onDidGetPreviewPageCount(pageCount, previewResponseId) {
380 var pageCountChangeEvt = new cr.Event(
381 print_preview.NativeLayer.Event.PAGE_COUNT_CHANGE);
382 pageCountChangeEvt.pageCount = pageCount;
383 pageCountChangeEvt.previewResponseId = previewResponseId;
384 nativeLayer.dispatchEvent(pageCountChangeEvt);
385 }
386
387 /**
388 * Called when no pipelining previewed pages.
389 * @param {string} previewUid Preview unique identifier.
390 * @param {number} previewResponseId The preview request id that resulted in
391 * this response.
392 */
393 function reloadPreviewPages(previewUid, previewResponseId) {
394 var previewReloadEvt = new cr.Event(
395 print_preview.NativeLayer.Event.PREVIEW_RELOAD);
396 previewReloadEvt.previewUid = previewUid;
397 previewReloadEvt.previewResponseId = previewResponseId;
398 nativeLayer.dispatchEvent(previewReloadEvt);
399 }
400
401 /**
402 * Notification that a print preview page has been rendered.
403 * Check if the settings have changed and request a regeneration if needed.
404 * Called from PrintPreviewUI::OnDidPreviewPage().
405 * @param {number} pageNumber The page number, 0-based.
406 * @param {string} previewUid Preview unique identifier.
407 * @param {number} previewResponseId The preview request id that resulted in
408 * this response.
409 */
410 function onDidPreviewPage(pageNumber, previewUid, previewResponseId) {
411 var pagePreviewGenEvt = new cr.Event(
412 print_preview.NativeLayer.Event.PAGE_PREVIEW_READY);
413 pagePreviewGenEvt.pageIndex = pageNumber;
414 pagePreviewGenEvt.previewUid = previewUid;
415 pagePreviewGenEvt.previewResponseId = previewResponseId;
416 nativeLayer.dispatchEvent(pagePreviewGenEvt);
417 }
418
419 /**
420 * Update the print preview when new preview data is available.
421 * Create the PDF plugin as needed.
422 * Called from PrintPreviewUI::PreviewDataIsAvailable().
423 * @param {string} previewUid Preview unique identifier.
424 * @param {number} previewResponseId The preview request id that resulted in
425 * this response.
426 */
427 function updatePrintPreview(previewUid, previewResponseId) {
428 var previewGenDoneEvt = new cr.Event(
429 print_preview.NativeLayer.Event.PREVIEW_GENERATION_DONE);
430 previewGenDoneEvt.previewUid = previewUid;
431 previewGenDoneEvt.previewResponseId = previewResponseId;
432 nativeLayer.dispatchEvent(previewGenDoneEvt);
433 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698