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

Side by Side Diff: chrome/browser/ui/webui/print_preview/print_preview_handler.cc

Issue 9703013: Printing: Catch more error conditions and remove more cruft. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 9 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
« no previous file with comments | « chrome/browser/printing/print_view_manager.cc ('k') | chrome/renderer/print_web_view_helper.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 #include "chrome/browser/ui/webui/print_preview/print_preview_handler.h" 5 #include "chrome/browser/ui/webui/print_preview/print_preview_handler.h"
6 6
7 #include <ctype.h> 7 #include <ctype.h>
8 8
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 // locale. 113 // locale.
114 const char kMeasurementSystem[] = "measurementSystem"; 114 const char kMeasurementSystem[] = "measurementSystem";
115 // Name of a dictionary field holding the number format according to the locale. 115 // Name of a dictionary field holding the number format according to the locale.
116 const char kNumberFormat[] = "numberFormat"; 116 const char kNumberFormat[] = "numberFormat";
117 // Name of a dictionary field specifying whether to print automatically in 117 // Name of a dictionary field specifying whether to print automatically in
118 // kiosk mode. See http://crbug.com/31395. 118 // kiosk mode. See http://crbug.com/31395.
119 const char kPrintAutomaticallyInKioskMode[] = "printAutomaticallyInKioskMode"; 119 const char kPrintAutomaticallyInKioskMode[] = "printAutomaticallyInKioskMode";
120 120
121 121
122 // Get the print job settings dictionary from |args|. The caller takes 122 // Get the print job settings dictionary from |args|. The caller takes
123 // ownership of the returned DictionaryValue. Returns NULL on failure. 123 // ownership of the returned DictionaryValue.
124 DictionaryValue* GetSettingsDictionary(const ListValue* args) { 124 DictionaryValue* GetSettingsDictionary(const ListValue* args) {
125 std::string json_str; 125 std::string json_str;
126 if (!args->GetString(0, &json_str)) { 126 CHECK(args->GetString(0, &json_str));
127 NOTREACHED() << "Could not read JSON argument"; 127 CHECK(!json_str.empty());
kmadhusu 2012/03/14 18:22:20 'if' condition is better than CHECK because we are
Lei Zhang 2012/03/14 20:09:11 But we want to crash if this ever happens, so we k
kmadhusu 2012/03/14 20:28:33 Sorry, you are adding ~15 CHECK conditions here. I
128 return NULL;
129 }
130 if (json_str.empty()) {
131 NOTREACHED() << "Empty print job settings";
132 return NULL;
133 }
134 scoped_ptr<DictionaryValue> settings(static_cast<DictionaryValue*>( 128 scoped_ptr<DictionaryValue> settings(static_cast<DictionaryValue*>(
135 base::JSONReader::Read(json_str, false))); 129 base::JSONReader::Read(json_str, false)));
136 if (!settings.get() || !settings->IsType(Value::TYPE_DICTIONARY)) { 130 CHECK(settings.get() && settings->IsType(Value::TYPE_DICTIONARY));
137 NOTREACHED() << "Print job settings must be a dictionary."; 131 CHECK(!settings->empty());
138 return NULL;
139 }
140
141 if (settings->empty()) {
142 NOTREACHED() << "Print job settings dictionary is empty";
143 return NULL;
144 }
145
146 return settings.release(); 132 return settings.release();
147 } 133 }
148 134
149 int GetPageCountFromSettingsDictionary(const DictionaryValue& settings) { 135 int GetPageCountFromSettingsDictionary(const DictionaryValue& settings) {
150 int count = 0; 136 int count = 0;
151 ListValue* page_range_array; 137 ListValue* page_range_array;
152 if (settings.GetList(printing::kSettingPageRange, &page_range_array)) { 138 if (settings.GetList(printing::kSettingPageRange, &page_range_array)) {
153 for (size_t index = 0; index < page_range_array->GetSize(); ++index) { 139 for (size_t index = 0; index < page_range_array->GetSize(); ++index) {
154 DictionaryValue* dict; 140 DictionaryValue* dict;
155 if (!page_range_array->GetDictionary(index, &dict)) 141 if (!page_range_array->GetDictionary(index, &dict))
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 print_backend_.get(), 262 print_backend_.get(),
277 has_logged_printers_count_); 263 has_logged_printers_count_);
278 has_logged_printers_count_ = true; 264 has_logged_printers_count_ = true;
279 265
280 BrowserThread::PostTask( 266 BrowserThread::PostTask(
281 BrowserThread::FILE, FROM_HERE, 267 BrowserThread::FILE, FROM_HERE,
282 base::Bind(&PrintSystemTaskProxy::EnumeratePrinters, task.get())); 268 base::Bind(&PrintSystemTaskProxy::EnumeratePrinters, task.get()));
283 } 269 }
284 270
285 void PrintPreviewHandler::HandleGetPreview(const ListValue* args) { 271 void PrintPreviewHandler::HandleGetPreview(const ListValue* args) {
286 DCHECK_EQ(3U, args->GetSize()); 272 CHECK_EQ(3U, args->GetSize());
287 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); 273 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args));
288 if (!settings.get())
289 return;
290 int request_id = -1; 274 int request_id = -1;
291 if (!settings->GetInteger(printing::kPreviewRequestID, &request_id)) 275 if (!settings->GetInteger(printing::kPreviewRequestID, &request_id))
292 return; 276 return;
293 277
294 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( 278 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(
295 web_ui()->GetController()); 279 web_ui()->GetController());
296 print_preview_ui->OnPrintPreviewRequest(request_id); 280 print_preview_ui->OnPrintPreviewRequest(request_id);
297 // Add an additional key in order to identify |print_preview_ui| later on 281 // Add an additional key in order to identify |print_preview_ui| later on
298 // when calling PrintPreviewUI::GetCurrentPrintPreviewStatus() on the IO 282 // when calling PrintPreviewUI::GetCurrentPrintPreviewStatus() on the IO
299 // thread. 283 // thread.
300 settings->SetString(printing::kPreviewUIAddr, 284 settings->SetString(printing::kPreviewUIAddr,
301 print_preview_ui->GetPrintPreviewUIAddress()); 285 print_preview_ui->GetPrintPreviewUIAddress());
302 286
303 // Increment request count. 287 // Increment request count.
304 ++regenerate_preview_request_count_; 288 ++regenerate_preview_request_count_;
305 289
306 TabContentsWrapper* initiator_tab = GetInitiatorTab(); 290 TabContentsWrapper* initiator_tab = GetInitiatorTab();
307 if (!initiator_tab) { 291 if (!initiator_tab) {
308 ReportUserActionHistogram(INITIATOR_TAB_CLOSED); 292 ReportUserActionHistogram(INITIATOR_TAB_CLOSED);
309 print_preview_ui->OnClosePrintPreviewTab(); 293 print_preview_ui->OnClosePrintPreviewTab();
310 return; 294 return;
311 } 295 }
312 296
313 // Retrieve the page title and url and send it to the renderer process if 297 // Retrieve the page title and url and send it to the renderer process if
314 // headers and footers are to be displayed. 298 // headers and footers are to be displayed.
315 bool display_header_footer = false; 299 bool display_header_footer = false;
316 if (!settings->GetBoolean(printing::kSettingHeaderFooterEnabled, 300 CHECK(settings->GetBoolean(printing::kSettingHeaderFooterEnabled,
317 &display_header_footer)) { 301 &display_header_footer));
318 NOTREACHED();
319 }
320 if (display_header_footer) { 302 if (display_header_footer) {
321 settings->SetString(printing::kSettingHeaderFooterTitle, 303 settings->SetString(printing::kSettingHeaderFooterTitle,
322 initiator_tab->web_contents()->GetTitle()); 304 initiator_tab->web_contents()->GetTitle());
323 std::string url; 305 std::string url;
324 NavigationEntry* entry = 306 NavigationEntry* entry =
325 initiator_tab->web_contents()->GetController().GetActiveEntry(); 307 initiator_tab->web_contents()->GetController().GetActiveEntry();
326 if (entry) 308 if (entry)
327 url = entry->GetVirtualURL().spec(); 309 url = entry->GetVirtualURL().spec();
328 settings->SetString(printing::kSettingHeaderFooterURL, url); 310 settings->SetString(printing::kSettingHeaderFooterURL, url);
329 } 311 }
330 312
331 bool generate_draft_data = false; 313 bool generate_draft_data = false;
332 bool success = settings->GetBoolean(printing::kSettingGenerateDraftData, 314 CHECK(settings->GetBoolean(printing::kSettingGenerateDraftData,
333 &generate_draft_data); 315 &generate_draft_data));
334 DCHECK(success);
335 316
336 if (!generate_draft_data) { 317 if (!generate_draft_data) {
337 double draft_page_count_double = -1; 318 double draft_page_count_double = -1;
338 success = args->GetDouble(1, &draft_page_count_double); 319 CHECK(args->GetDouble(1, &draft_page_count_double));
339 DCHECK(success);
340 int draft_page_count = static_cast<int>(draft_page_count_double); 320 int draft_page_count = static_cast<int>(draft_page_count_double);
341 321
342 bool preview_modifiable = false; 322 bool preview_modifiable = false;
343 success = args->GetBoolean(2, &preview_modifiable); 323 CHECK(args->GetBoolean(2, &preview_modifiable));
344 DCHECK(success);
345 324
346 if (draft_page_count != -1 && preview_modifiable && 325 if (draft_page_count != -1 && preview_modifiable &&
347 print_preview_ui->GetAvailableDraftPageCount() != draft_page_count) { 326 print_preview_ui->GetAvailableDraftPageCount() != draft_page_count) {
348 settings->SetBoolean(printing::kSettingGenerateDraftData, true); 327 settings->SetBoolean(printing::kSettingGenerateDraftData, true);
349 } 328 }
350 } 329 }
351 330
352 VLOG(1) << "Print preview request start"; 331 VLOG(1) << "Print preview request start";
353 RenderViewHost* rvh = initiator_tab->web_contents()->GetRenderViewHost(); 332 RenderViewHost* rvh = initiator_tab->web_contents()->GetRenderViewHost();
354 rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), *settings)); 333 rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), *settings));
355 } 334 }
356 335
357 void PrintPreviewHandler::HandlePrint(const ListValue* args) { 336 void PrintPreviewHandler::HandlePrint(const ListValue* args) {
337 CHECK_EQ(2U, args->GetSize());
338
358 ReportStats(); 339 ReportStats();
359 340
360 // Record the number of times the user requests to regenerate preview data 341 // Record the number of times the user requests to regenerate preview data
361 // before printing. 342 // before printing.
362 UMA_HISTOGRAM_COUNTS("PrintPreview.RegeneratePreviewRequest.BeforePrint", 343 UMA_HISTOGRAM_COUNTS("PrintPreview.RegeneratePreviewRequest.BeforePrint",
363 regenerate_preview_request_count_); 344 regenerate_preview_request_count_);
364 345
365 TabContentsWrapper* initiator_tab = GetInitiatorTab(); 346 TabContentsWrapper* initiator_tab = GetInitiatorTab();
366 if (initiator_tab) { 347 if (initiator_tab) {
367 RenderViewHost* rvh = initiator_tab->web_contents()->GetRenderViewHost(); 348 RenderViewHost* rvh = initiator_tab->web_contents()->GetRenderViewHost();
368 rvh->Send(new PrintMsg_ResetScriptedPrintCount(rvh->GetRoutingID())); 349 rvh->Send(new PrintMsg_ResetScriptedPrintCount(rvh->GetRoutingID()));
369 } 350 }
370 351
371 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); 352 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args));
372 if (!settings.get())
373 return;
374 353
375 // Storing last used settings. 354 // Storing last used settings.
376 GetStickySettings()->Store(*settings); 355 GetStickySettings()->Store(*settings);
377 356
378 // Never try to add headers/footers here. It's already in the generated PDF. 357 // Never try to add headers/footers here. It's already in the generated PDF.
379 settings->SetBoolean(printing::kSettingHeaderFooterEnabled, false); 358 settings->SetBoolean(printing::kSettingHeaderFooterEnabled, false);
380 359
381 bool print_to_pdf = false; 360 bool print_to_pdf = false;
382 bool is_cloud_printer = false; 361 bool is_cloud_printer = false;
383 bool is_cloud_dialog = false; 362 bool is_cloud_dialog = false;
384 363
385 bool open_pdf_in_preview = false; 364 bool open_pdf_in_preview = false;
386 #if defined(OS_MACOSX) 365 #if defined(OS_MACOSX)
387 open_pdf_in_preview = settings->HasKey(printing::kSettingOpenPDFInPreview); 366 open_pdf_in_preview = settings->HasKey(printing::kSettingOpenPDFInPreview);
388 #endif 367 #endif
389 368
390 if (!open_pdf_in_preview) { 369 if (!open_pdf_in_preview) {
391 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); 370 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf);
392 settings->GetBoolean(printing::kSettingCloudPrintDialog, &is_cloud_dialog); 371 settings->GetBoolean(printing::kSettingCloudPrintDialog, &is_cloud_dialog);
393 is_cloud_printer = settings->HasKey(printing::kSettingCloudPrintId); 372 is_cloud_printer = settings->HasKey(printing::kSettingCloudPrintId);
394 } 373 }
395 374
396 if (is_cloud_printer) { 375 if (is_cloud_printer) {
397 std::string print_ticket; 376 std::string print_ticket;
398 bool res = args->GetString(1, &print_ticket); 377 CHECK(args->GetString(1, &print_ticket));
399 DCHECK(res);
400 SendCloudPrintJob(*settings, print_ticket); 378 SendCloudPrintJob(*settings, print_ticket);
401 } else if (print_to_pdf) { 379 } else if (print_to_pdf) {
402 HandlePrintToPdf(*settings); 380 HandlePrintToPdf(*settings);
403 } else if (is_cloud_dialog) { 381 } else if (is_cloud_dialog) {
404 HandlePrintWithCloudPrint(); 382 HandlePrintWithCloudPrint();
405 } else { 383 } else {
406 ReportPrintSettingsStats(*settings); 384 ReportPrintSettingsStats(*settings);
407 ReportUserActionHistogram(PRINT_TO_PRINTER); 385 ReportUserActionHistogram(PRINT_TO_PRINTER);
408 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPrinter", 386 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPrinter",
409 GetPageCountFromSettingsDictionary(*settings)); 387 GetPageCountFromSettingsDictionary(*settings));
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
484 if (browser) 462 if (browser)
485 initiator_tab = browser->GetSelectedTabContentsWrapper(); 463 initiator_tab = browser->GetSelectedTabContentsWrapper();
486 } 464 }
487 465
488 if (initiator_tab) 466 if (initiator_tab)
489 initiator_tab->print_view_manager()->PreviewPrintingRequestCancelled(); 467 initiator_tab->print_view_manager()->PreviewPrintingRequestCancelled();
490 delete preview_tab_wrapper(); 468 delete preview_tab_wrapper();
491 } 469 }
492 470
493 void PrintPreviewHandler::HandleSaveLastPrinter(const ListValue* args) { 471 void PrintPreviewHandler::HandleSaveLastPrinter(const ListValue* args) {
472 CHECK_EQ(2U, args->GetSize());
494 std::string data_to_save; 473 std::string data_to_save;
495 if (args->GetString(0, &data_to_save) && !data_to_save.empty()) 474 if (args->GetString(0, &data_to_save) && !data_to_save.empty())
496 GetStickySettings()->StorePrinterName(data_to_save); 475 GetStickySettings()->StorePrinterName(data_to_save);
497 476
498 if (args->GetString(1, &data_to_save) && !data_to_save.empty()) 477 if (args->GetString(1, &data_to_save) && !data_to_save.empty())
499 GetStickySettings()->StoreCloudPrintData(data_to_save); 478 GetStickySettings()->StoreCloudPrintData(data_to_save);
500 } 479 }
501 480
502 void PrintPreviewHandler::HandleGetPrinterCapabilities(const ListValue* args) { 481 void PrintPreviewHandler::HandleGetPrinterCapabilities(const ListValue* args) {
482 CHECK_EQ(1U, args->GetSize());
503 std::string printer_name; 483 std::string printer_name;
504 bool ret = args->GetString(0, &printer_name); 484 CHECK(args->GetString(0, &printer_name));
505 if (!ret || printer_name.empty()) 485 CHECK(!printer_name.empty());
506 return;
507 486
508 scoped_refptr<PrintSystemTaskProxy> task = 487 scoped_refptr<PrintSystemTaskProxy> task =
509 new PrintSystemTaskProxy(AsWeakPtr(), 488 new PrintSystemTaskProxy(AsWeakPtr(),
510 print_backend_.get(), 489 print_backend_.get(),
511 has_logged_printers_count_); 490 has_logged_printers_count_);
512 491
513 BrowserThread::PostTask( 492 BrowserThread::PostTask(
514 BrowserThread::FILE, FROM_HERE, 493 BrowserThread::FILE, FROM_HERE,
515 base::Bind(&PrintSystemTaskProxy::GetPrinterCapabilities, task.get(), 494 base::Bind(&PrintSystemTaskProxy::GetPrinterCapabilities, task.get(),
516 printer_name)); 495 printer_name));
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after
863 return; 842 return;
864 843
865 // We no longer require the initiator tab details. Remove those details 844 // We no longer require the initiator tab details. Remove those details
866 // associated with the preview tab to allow the initiator tab to create 845 // associated with the preview tab to allow the initiator tab to create
867 // another preview tab. 846 // another preview tab.
868 printing::PrintPreviewTabController* tab_controller = 847 printing::PrintPreviewTabController* tab_controller =
869 printing::PrintPreviewTabController::GetInstance(); 848 printing::PrintPreviewTabController::GetInstance();
870 if (tab_controller) 849 if (tab_controller)
871 tab_controller->EraseInitiatorTabInfo(preview_tab_wrapper()); 850 tab_controller->EraseInitiatorTabInfo(preview_tab_wrapper());
872 } 851 }
OLDNEW
« no previous file with comments | « chrome/browser/printing/print_view_manager.cc ('k') | chrome/renderer/print_web_view_helper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698