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

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

Issue 7038028: Initial support for cloudprint in print preview (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Resolved new conflictswq Created 9 years, 5 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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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_handler.h" 5 #include "chrome/browser/ui/webui/print_preview_handler.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/base64.h"
10 #if !defined(OS_CHROMEOS)
11 #include "base/command_line.h"
12 #endif
9 #include "base/i18n/file_util_icu.h" 13 #include "base/i18n/file_util_icu.h"
10 #include "base/json/json_reader.h" 14 #include "base/json/json_reader.h"
11 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.h"
12 #include "base/metrics/histogram.h" 16 #include "base/metrics/histogram.h"
13 #include "base/path_service.h" 17 #include "base/path_service.h"
14 #include "base/threading/thread.h" 18 #include "base/threading/thread.h"
15 #include "base/threading/thread_restrictions.h" 19 #include "base/threading/thread_restrictions.h"
16 #include "base/utf_string_conversions.h" 20 #include "base/utf_string_conversions.h"
17 #include "base/values.h" 21 #include "base/values.h"
18 #include "chrome/browser/browser_process.h" 22 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/platform_util.h" 23 #include "chrome/browser/platform_util.h"
20 #include "chrome/browser/printing/background_printing_manager.h" 24 #include "chrome/browser/printing/background_printing_manager.h"
25 #include "chrome/browser/printing/cloud_print/cloud_print_url.h"
21 #include "chrome/browser/printing/printer_manager_dialog.h" 26 #include "chrome/browser/printing/printer_manager_dialog.h"
22 #include "chrome/browser/printing/print_preview_tab_controller.h" 27 #include "chrome/browser/printing/print_preview_tab_controller.h"
23 #include "chrome/browser/sessions/restore_tab_helper.h" 28 #include "chrome/browser/sessions/restore_tab_helper.h"
24 #include "chrome/browser/tabs/tab_strip_model.h" 29 #include "chrome/browser/tabs/tab_strip_model.h"
25 #include "chrome/browser/ui/browser_list.h" 30 #include "chrome/browser/ui/browser_list.h"
26 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" 31 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
27 #include "chrome/browser/ui/webui/print_preview_ui.h" 32 #include "chrome/browser/ui/webui/print_preview_ui.h"
28 #include "chrome/common/chrome_paths.h" 33 #include "chrome/common/chrome_paths.h"
34 #if !defined(OS_CHROMEOS)
35 #include "chrome/common/chrome_switches.h"
36 #endif
29 #include "chrome/common/print_messages.h" 37 #include "chrome/common/print_messages.h"
30 #include "content/browser/browser_thread.h" 38 #include "content/browser/browser_thread.h"
31 #include "content/browser/renderer_host/render_view_host.h" 39 #include "content/browser/renderer_host/render_view_host.h"
32 #include "content/browser/tab_contents/tab_contents.h" 40 #include "content/browser/tab_contents/tab_contents.h"
33 #include "printing/backend/print_backend.h" 41 #include "printing/backend/print_backend.h"
34 #include "printing/metafile.h" 42 #include "printing/metafile.h"
35 #include "printing/metafile_impl.h" 43 #include "printing/metafile_impl.h"
36 #include "printing/page_range.h" 44 #include "printing/page_range.h"
37 #include "printing/print_job_constants.h" 45 #include "printing/print_job_constants.h"
38 46
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 printing::PrintBackend* print_backend, 177 printing::PrintBackend* print_backend,
170 bool has_logged_printers_count) 178 bool has_logged_printers_count)
171 : handler_(handler), 179 : handler_(handler),
172 print_backend_(print_backend), 180 print_backend_(print_backend),
173 has_logged_printers_count_(has_logged_printers_count) { 181 has_logged_printers_count_(has_logged_printers_count) {
174 } 182 }
175 183
176 void GetDefaultPrinter() { 184 void GetDefaultPrinter() {
177 VLOG(1) << "Get default printer start"; 185 VLOG(1) << "Get default printer start";
178 StringValue* default_printer = NULL; 186 StringValue* default_printer = NULL;
179 if (PrintPreviewHandler::last_used_printer_ == NULL) { 187 if (PrintPreviewHandler::last_used_printer_name_ == NULL) {
180 default_printer = new StringValue( 188 default_printer = new StringValue(
181 print_backend_->GetDefaultPrinterName()); 189 print_backend_->GetDefaultPrinterName());
182 } else { 190 } else {
183 default_printer = new StringValue( 191 default_printer = new StringValue(
184 *PrintPreviewHandler::last_used_printer_); 192 *PrintPreviewHandler::last_used_printer_name_);
185 } 193 }
186 std::string default_printer_string; 194 std::string default_printer_string;
187 default_printer->GetAsString(&default_printer_string); 195 default_printer->GetAsString(&default_printer_string);
188 VLOG(1) << "Get default printer finished, found: " 196 VLOG(1) << "Get default printer finished, found: "
189 << default_printer_string; 197 << default_printer_string;
190 198
199 StringValue* cloud_print_data = NULL;
200 if (PrintPreviewHandler::last_used_printer_cloud_print_data_ != NULL) {
201 cloud_print_data = new StringValue(
202 *PrintPreviewHandler::last_used_printer_cloud_print_data_);
203 } else {
204 cloud_print_data = new StringValue("");
205 }
206
191 BrowserThread::PostTask( 207 BrowserThread::PostTask(
192 BrowserThread::UI, FROM_HERE, 208 BrowserThread::UI, FROM_HERE,
193 NewRunnableMethod(this, 209 NewRunnableMethod(this,
194 &PrintSystemTaskProxy::SendDefaultPrinter, 210 &PrintSystemTaskProxy::SendDefaultPrinter,
195 default_printer)); 211 default_printer,
212 cloud_print_data));
196 } 213 }
197 214
198 void SendDefaultPrinter(StringValue* default_printer) { 215 void SendDefaultPrinter(const StringValue* default_printer,
216 const StringValue* cloud_print_data) {
199 if (handler_) 217 if (handler_)
200 handler_->SendDefaultPrinter(*default_printer); 218 handler_->SendDefaultPrinter(*default_printer, *cloud_print_data);
201 delete default_printer; 219 delete default_printer;
202 } 220 }
203 221
204 void EnumeratePrinters() { 222 void EnumeratePrinters() {
205 VLOG(1) << "Enumerate printers start"; 223 VLOG(1) << "Enumerate printers start";
206 ListValue* printers = new ListValue; 224 ListValue* printers = new ListValue;
207 225
208 printing::PrinterList printer_list; 226 printing::PrinterList printer_list;
209 print_backend_->EnumeratePrinters(&printer_list); 227 print_backend_->EnumeratePrinters(&printer_list);
210 228
(...skipping 17 matching lines...) Expand all
228 #endif 246 #endif
229 printer_info->SetString(printing::kSettingPrinterName, printerName); 247 printer_info->SetString(printing::kSettingPrinterName, printerName);
230 printer_info->SetString(printing::kSettingDeviceName, iter->printer_name); 248 printer_info->SetString(printing::kSettingDeviceName, iter->printer_name);
231 printers->Append(printer_info); 249 printers->Append(printer_info);
232 } 250 }
233 VLOG(1) << "Enumerate printers finished, found " << i << " printers"; 251 VLOG(1) << "Enumerate printers finished, found " << i << " printers";
234 252
235 BrowserThread::PostTask( 253 BrowserThread::PostTask(
236 BrowserThread::UI, FROM_HERE, 254 BrowserThread::UI, FROM_HERE,
237 NewRunnableMethod(this, 255 NewRunnableMethod(this,
238 &PrintSystemTaskProxy::SendPrinterList, 256 &PrintSystemTaskProxy::SetupPrinterList,
239 printers)); 257 printers));
240 } 258 }
241 259
242 void SendPrinterList(ListValue* printers) { 260 void SetupPrinterList(ListValue* printers) {
243 if (handler_) 261 if (handler_) {
244 handler_->SendPrinterList(*printers); 262 handler_->SetupPrinterList(*printers);
263 }
245 delete printers; 264 delete printers;
246 } 265 }
247 266
248 void GetPrinterCapabilities(const std::string& printer_name) { 267 void GetPrinterCapabilities(const std::string& printer_name) {
249 VLOG(1) << "Get printer capabilities start for " << printer_name; 268 VLOG(1) << "Get printer capabilities start for " << printer_name;
250 printing::PrinterCapsAndDefaults printer_info; 269 printing::PrinterCapsAndDefaults printer_info;
251 bool supports_color = true; 270 bool supports_color = true;
252 bool set_duplex_as_default = false; 271 bool set_duplex_as_default = false;
253 if (!print_backend_->GetPrinterCapsAndDefaults(printer_name, 272 if (!print_backend_->GetPrinterCapsAndDefaults(printer_name,
254 &printer_info)) { 273 &printer_info)) {
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 private: 378 private:
360 // The metafile holding the PDF data. 379 // The metafile holding the PDF data.
361 scoped_ptr<printing::Metafile> metafile_; 380 scoped_ptr<printing::Metafile> metafile_;
362 381
363 // The absolute path where the file will be saved. 382 // The absolute path where the file will be saved.
364 FilePath path_; 383 FilePath path_;
365 }; 384 };
366 385
367 // static 386 // static
368 FilePath* PrintPreviewHandler::last_saved_path_ = NULL; 387 FilePath* PrintPreviewHandler::last_saved_path_ = NULL;
369 std::string* PrintPreviewHandler::last_used_printer_ = NULL; 388 std::string* PrintPreviewHandler::last_used_printer_cloud_print_data_ = NULL;
389 std::string* PrintPreviewHandler::last_used_printer_name_ = NULL;
370 390
371 PrintPreviewHandler::PrintPreviewHandler() 391 PrintPreviewHandler::PrintPreviewHandler()
372 : print_backend_(printing::PrintBackend::CreateInstance(NULL)), 392 : print_backend_(printing::PrintBackend::CreateInstance(NULL)),
373 regenerate_preview_request_count_(0), 393 regenerate_preview_request_count_(0),
374 manage_printers_dialog_request_count_(0), 394 manage_printers_dialog_request_count_(0),
375 reported_failed_preview_(false), 395 reported_failed_preview_(false),
376 has_logged_printers_count_(false) { 396 has_logged_printers_count_(false) {
377 ReportUserActionHistogram(PREVIEW_STARTED); 397 ReportUserActionHistogram(PREVIEW_STARTED);
378 } 398 }
379 399
380 PrintPreviewHandler::~PrintPreviewHandler() { 400 PrintPreviewHandler::~PrintPreviewHandler() {
381 if (select_file_dialog_.get()) 401 if (select_file_dialog_.get())
382 select_file_dialog_->ListenerDestroyed(); 402 select_file_dialog_->ListenerDestroyed();
383 } 403 }
384 404
385 void PrintPreviewHandler::RegisterMessages() { 405 void PrintPreviewHandler::RegisterMessages() {
386 web_ui_->RegisterMessageCallback("getDefaultPrinter", 406 web_ui_->RegisterMessageCallback("getDefaultPrinter",
387 NewCallback(this, &PrintPreviewHandler::HandleGetDefaultPrinter)); 407 NewCallback(this, &PrintPreviewHandler::HandleGetDefaultPrinter));
388 web_ui_->RegisterMessageCallback("getPrinters", 408 web_ui_->RegisterMessageCallback("getPrinters",
389 NewCallback(this, &PrintPreviewHandler::HandleGetPrinters)); 409 NewCallback(this, &PrintPreviewHandler::HandleGetPrinters));
390 web_ui_->RegisterMessageCallback("getPreview", 410 web_ui_->RegisterMessageCallback("getPreview",
391 NewCallback(this, &PrintPreviewHandler::HandleGetPreview)); 411 NewCallback(this, &PrintPreviewHandler::HandleGetPreview));
392 web_ui_->RegisterMessageCallback("print", 412 web_ui_->RegisterMessageCallback("print",
393 NewCallback(this, &PrintPreviewHandler::HandlePrint)); 413 NewCallback(this, &PrintPreviewHandler::HandlePrint));
394 web_ui_->RegisterMessageCallback("getPrinterCapabilities", 414 web_ui_->RegisterMessageCallback("getPrinterCapabilities",
395 NewCallback(this, &PrintPreviewHandler::HandleGetPrinterCapabilities)); 415 NewCallback(this, &PrintPreviewHandler::HandleGetPrinterCapabilities));
396 web_ui_->RegisterMessageCallback("showSystemDialog", 416 web_ui_->RegisterMessageCallback("showSystemDialog",
397 NewCallback(this, &PrintPreviewHandler::HandleShowSystemDialog)); 417 NewCallback(this, &PrintPreviewHandler::HandleShowSystemDialog));
398 web_ui_->RegisterMessageCallback("managePrinters", 418 web_ui_->RegisterMessageCallback("morePrinters",
419 NewCallback(this, &PrintPreviewHandler::HandleShowSystemDialog));
420 web_ui_->RegisterMessageCallback("signIn",
421 NewCallback(this, &PrintPreviewHandler::HandleManageCloudPrint));
422 web_ui_->RegisterMessageCallback("addCloudPrinter",
423 NewCallback(this, &PrintPreviewHandler::HandleManageCloudPrint));
424 web_ui_->RegisterMessageCallback("manageCloudPrinters",
425 NewCallback(this, &PrintPreviewHandler::HandleManageCloudPrint));
426 web_ui_->RegisterMessageCallback("manageLocalPrinters",
399 NewCallback(this, &PrintPreviewHandler::HandleManagePrinters)); 427 NewCallback(this, &PrintPreviewHandler::HandleManagePrinters));
400 web_ui_->RegisterMessageCallback("closePrintPreviewTab", 428 web_ui_->RegisterMessageCallback("closePrintPreviewTab",
401 NewCallback(this, &PrintPreviewHandler::HandleClosePreviewTab)); 429 NewCallback(this, &PrintPreviewHandler::HandleClosePreviewTab));
402 web_ui_->RegisterMessageCallback("hidePreview", 430 web_ui_->RegisterMessageCallback("hidePreview",
403 NewCallback(this, &PrintPreviewHandler::HandleHidePreview)); 431 NewCallback(this, &PrintPreviewHandler::HandleHidePreview));
404 web_ui_->RegisterMessageCallback("cancelPendingPrintRequest", 432 web_ui_->RegisterMessageCallback("cancelPendingPrintRequest",
405 NewCallback(this, &PrintPreviewHandler::HandleCancelPendingPrintRequest)); 433 NewCallback(this, &PrintPreviewHandler::HandleCancelPendingPrintRequest));
434 web_ui_->RegisterMessageCallback("saveLastPrinter",
435 NewCallback(this, &PrintPreviewHandler::HandleSaveLastPrinter));
406 } 436 }
407 437
408 TabContents* PrintPreviewHandler::preview_tab() { 438 TabContents* PrintPreviewHandler::preview_tab() {
409 return web_ui_->tab_contents(); 439 return web_ui_->tab_contents();
410 } 440 }
411 441
412 void PrintPreviewHandler::HandleGetDefaultPrinter(const ListValue*) { 442 void PrintPreviewHandler::HandleGetDefaultPrinter(const ListValue*) {
413 scoped_refptr<PrintSystemTaskProxy> task = 443 scoped_refptr<PrintSystemTaskProxy> task =
414 new PrintSystemTaskProxy(AsWeakPtr(), 444 new PrintSystemTaskProxy(AsWeakPtr(),
415 print_backend_.get(), 445 print_backend_.get(),
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 TabContents* initiator_tab = GetInitiatorTab(); 499 TabContents* initiator_tab = GetInitiatorTab();
470 if (initiator_tab) { 500 if (initiator_tab) {
471 RenderViewHost* rvh = initiator_tab->render_view_host(); 501 RenderViewHost* rvh = initiator_tab->render_view_host();
472 rvh->Send(new PrintMsg_ResetScriptedPrintCount(rvh->routing_id())); 502 rvh->Send(new PrintMsg_ResetScriptedPrintCount(rvh->routing_id()));
473 } 503 }
474 504
475 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); 505 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args));
476 if (!settings.get()) 506 if (!settings.get())
477 return; 507 return;
478 508
479 // Initializing last_used_printer_ if it is not already initialized.
480 if (!last_used_printer_)
481 last_used_printer_ = new std::string();
482 // Storing last used printer.
483 settings->GetString("deviceName", last_used_printer_);
484
485 bool print_to_pdf = false; 509 bool print_to_pdf = false;
486 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); 510 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf);
487 511
488 TabContentsWrapper* preview_tab_wrapper = 512 TabContentsWrapper* preview_tab_wrapper =
489 TabContentsWrapper::GetCurrentWrapperForContents(preview_tab()); 513 TabContentsWrapper::GetCurrentWrapperForContents(preview_tab());
490 514
491 if (print_to_pdf) { 515 bool print_to_cloud = settings->HasKey(printing::kSettingCloudPrintId);
516 if (print_to_cloud) {
517 std::string print_ticket;
518 args->GetString(1, &print_ticket);
519 SendCloudPrintJob(*settings, print_ticket);
520 } else if (print_to_pdf) {
492 ReportUserActionHistogram(PRINT_TO_PDF); 521 ReportUserActionHistogram(PRINT_TO_PDF);
493 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPDF", 522 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPDF",
494 GetPageCountFromSettingsDictionary(*settings)); 523 GetPageCountFromSettingsDictionary(*settings));
495 524
496 // Pre-populating select file dialog with print job title. 525 // Pre-populating select file dialog with print job title.
497 string16 print_job_title_utf16 = 526 string16 print_job_title_utf16 =
498 preview_tab_wrapper->print_view_manager()->RenderSourceName(); 527 preview_tab_wrapper->print_view_manager()->RenderSourceName();
499 528
500 #if defined(OS_WIN) 529 #if defined(OS_WIN)
501 FilePath::StringType print_job_title(print_job_title_utf16); 530 FilePath::StringType print_job_title(print_job_title_utf16);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 Browser* browser = BrowserList::GetLastActive(); 571 Browser* browser = BrowserList::GetLastActive();
543 if (browser) 572 if (browser)
544 wrapper = browser->GetSelectedTabContentsWrapper(); 573 wrapper = browser->GetSelectedTabContentsWrapper();
545 } 574 }
546 575
547 if (wrapper) 576 if (wrapper)
548 wrapper->print_view_manager()->PreviewPrintingRequestCancelled(); 577 wrapper->print_view_manager()->PreviewPrintingRequestCancelled();
549 delete TabContentsWrapper::GetCurrentWrapperForContents(preview_tab()); 578 delete TabContentsWrapper::GetCurrentWrapperForContents(preview_tab());
550 } 579 }
551 580
581 void PrintPreviewHandler::HandleSaveLastPrinter(
582 const ListValue* args) {
583 std::string data_to_save;
584 if (args->GetString(0, &data_to_save) && !data_to_save.empty()) {
585 if (last_used_printer_name_ == NULL)
586 last_used_printer_name_ = new std::string();
587 *last_used_printer_name_ = data_to_save;
588 }
589 if (args->GetString(1, &data_to_save) && !data_to_save.empty()) {
590 if (last_used_printer_cloud_print_data_ == NULL)
591 last_used_printer_cloud_print_data_ = new std::string();
592 *last_used_printer_cloud_print_data_ = data_to_save;
593 }
594 }
595
552 void PrintPreviewHandler::HandleGetPrinterCapabilities( 596 void PrintPreviewHandler::HandleGetPrinterCapabilities(
553 const ListValue* args) { 597 const ListValue* args) {
554 std::string printer_name; 598 std::string printer_name;
555 bool ret = args->GetString(0, &printer_name); 599 bool ret = args->GetString(0, &printer_name);
556 if (!ret || printer_name.empty()) 600 if (!ret || printer_name.empty())
557 return; 601 return;
558 602
559 scoped_refptr<PrintSystemTaskProxy> task = 603 scoped_refptr<PrintSystemTaskProxy> task =
560 new PrintSystemTaskProxy(AsWeakPtr(), 604 new PrintSystemTaskProxy(AsWeakPtr(),
561 print_backend_.get(), 605 print_backend_.get(),
562 has_logged_printers_count_); 606 has_logged_printers_count_);
563 607
564 BrowserThread::PostTask( 608 BrowserThread::PostTask(
565 BrowserThread::FILE, FROM_HERE, 609 BrowserThread::FILE, FROM_HERE,
566 NewRunnableMethod(task.get(), 610 NewRunnableMethod(task.get(),
567 &PrintSystemTaskProxy::GetPrinterCapabilities, 611 &PrintSystemTaskProxy::GetPrinterCapabilities,
568 printer_name)); 612 printer_name));
569 } 613 }
570 614
615 void PrintPreviewHandler::HandleManageCloudPrint(const ListValue*) {
616 Browser* browser = BrowserList::GetLastActive();
617 browser->OpenURL(CloudPrintURL(browser->profile()).
618 GetCloudPrintServiceManageURL(),
619 GURL(),
620 NEW_FOREGROUND_TAB,
621 PageTransition::LINK);
622 }
623
571 void PrintPreviewHandler::HandleShowSystemDialog(const ListValue* args) { 624 void PrintPreviewHandler::HandleShowSystemDialog(const ListValue* args) {
572 ReportStats(); 625 ReportStats();
573 ReportUserActionHistogram(FALLBACK_TO_ADVANCED_SETTINGS_DIALOG); 626 ReportUserActionHistogram(FALLBACK_TO_ADVANCED_SETTINGS_DIALOG);
574 627
575 TabContents* initiator_tab = GetInitiatorTab(); 628 TabContents* initiator_tab = GetInitiatorTab();
576 if (!initiator_tab) 629 if (!initiator_tab)
577 return; 630 return;
578 631
579 TabContentsWrapper* wrapper = 632 TabContentsWrapper* wrapper =
580 TabContentsWrapper::GetCurrentWrapperForContents(initiator_tab); 633 TabContentsWrapper::GetCurrentWrapperForContents(initiator_tab);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
613 } 666 }
614 667
615 void PrintPreviewHandler::SendPrinterCapabilities( 668 void PrintPreviewHandler::SendPrinterCapabilities(
616 const DictionaryValue& settings_info) { 669 const DictionaryValue& settings_info) {
617 VLOG(1) << "Get printer capabilities finished"; 670 VLOG(1) << "Get printer capabilities finished";
618 web_ui_->CallJavascriptFunction("updateWithPrinterCapabilities", 671 web_ui_->CallJavascriptFunction("updateWithPrinterCapabilities",
619 settings_info); 672 settings_info);
620 } 673 }
621 674
622 void PrintPreviewHandler::SendDefaultPrinter( 675 void PrintPreviewHandler::SendDefaultPrinter(
623 const StringValue& default_printer) { 676 const StringValue& default_printer,
624 web_ui_->CallJavascriptFunction("setDefaultPrinter", default_printer); 677 const StringValue& cloud_print_data) {
678 web_ui_->CallJavascriptFunction("setDefaultPrinter",
679 default_printer,
680 cloud_print_data);
625 } 681 }
626 682
627 void PrintPreviewHandler::SendPrinterList(const ListValue& printers) { 683 void PrintPreviewHandler::SetupPrinterList(const ListValue& printers) {
628 web_ui_->CallJavascriptFunction("setPrinters", printers); 684 SendCloudPrintEnabled();
685 web_ui_->CallJavascriptFunction("setPrinters", printers,
686 *(Value::CreateBooleanValue(true)));
687 }
688
689 void PrintPreviewHandler::SendCloudPrintEnabled() {
690 #if defined(OS_CHROMEOS)
691 bool enable_cloud_print_integration = true;
692 #else
693 bool enable_cloud_print_integration =
694 CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableCloudPrint);
695 #endif
696 GURL gcp_url(CloudPrintURL(BrowserList::GetLastActive()->profile()).
697 GetCloudPrintServiceURL());
698 FundamentalValue enable(enable_cloud_print_integration);
699 StringValue gcp_url_value(gcp_url.spec());
700 web_ui_->CallJavascriptFunction("setUseCloudPrint", enable, gcp_url_value);
701 }
702
703 void PrintPreviewHandler::SendCloudPrintJob(const DictionaryValue& settings,
704 std::string print_ticket) {
705 scoped_refptr<RefCountedBytes> data;
706 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_);
707 print_preview_ui->GetPrintPreviewData(&data);
708 CHECK(data->front());
709 DCHECK_GT(data->size(), 0U);
710
711 TabContentsWrapper* wrapper =
712 TabContentsWrapper::GetCurrentWrapperForContents(preview_tab());
713 string16 print_job_title_utf16 =
714 wrapper->print_view_manager()->RenderSourceName();
715 std::string print_job_title = UTF16ToUTF8(print_job_title_utf16);
716 std::string printer_id;
717 settings.GetString("cloudPrintID", &printer_id);
718 // BASE64 encode the job data.
719 std::string raw_data(reinterpret_cast<const char*>(data->front()),
720 data->size());
721 std::string base64_data;
722 if (!base::Base64Encode(raw_data, &base64_data)) {
723 NOTREACHED() << "Base64 encoding PDF data.";
724 }
725
726 const char boundary[] = "----CloudPrintFormBoundaryjc9wuprokl8i";
727 const char prolog[] = "--%s\r\n"
728 "Content-Disposition: form-data; name=\"capabilities\"\r\n\r\n%s\r\n"
729 "--%s\r\n"
730 "Content-Disposition: form-data; name=\"contentType\"\r\n\r\ndataUrl\r\n"
731 "--%s\r\n"
732 "Content-Disposition: form-data; name=\"title\"\r\n\r\n%s\r\n"
733 "--%s\r\n"
734 "Content-Disposition: form-data; name=\"printerid\"\r\n\r\n%s\r\n"
735 "--%s\r\n"
736 "Content-Disposition: form-data; name=\"content\"\r\n\r\n"
737 "data:application/pdf;base64,%s\r\n"
738 "--%s\r\n";
739
740 // TODO(abodenha@chromium.org) This implies a large copy operation.
741 // Profile this and optimize if necessary.
742 std::string final_data;
743 base::SStringPrintf(&final_data,
744 prolog,
745 boundary,
746 print_ticket.c_str(),
747 boundary,
748 boundary,
749 print_job_title.c_str(),
750 boundary,
751 printer_id.c_str(),
752 boundary,
753 base64_data.c_str(),
754 boundary);
755
756 StringValue data_value(final_data);
757
758 web_ui_->CallJavascriptFunction("printToCloud",
759 data_value);
629 } 760 }
630 761
631 TabContents* PrintPreviewHandler::GetInitiatorTab() { 762 TabContents* PrintPreviewHandler::GetInitiatorTab() {
632 printing::PrintPreviewTabController* tab_controller = 763 printing::PrintPreviewTabController* tab_controller =
633 printing::PrintPreviewTabController::GetInstance(); 764 printing::PrintPreviewTabController::GetInstance();
634 if (!tab_controller) 765 if (!tab_controller)
635 return NULL; 766 return NULL;
636 return tab_controller->GetInitiatorTab(preview_tab()); 767 return tab_controller->GetInitiatorTab(preview_tab());
637 } 768 }
638 769
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
739 return; 870 return;
740 871
741 // We no longer require the initiator tab details. Remove those details 872 // We no longer require the initiator tab details. Remove those details
742 // associated with the preview tab to allow the initiator tab to create 873 // associated with the preview tab to allow the initiator tab to create
743 // another preview tab. 874 // another preview tab.
744 printing::PrintPreviewTabController* tab_controller = 875 printing::PrintPreviewTabController* tab_controller =
745 printing::PrintPreviewTabController::GetInstance(); 876 printing::PrintPreviewTabController::GetInstance();
746 if (tab_controller) 877 if (tab_controller)
747 tab_controller->EraseInitiatorTabInfo(preview_tab()); 878 tab_controller->EraseInitiatorTabInfo(preview_tab());
748 } 879 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/print_preview_handler.h ('k') | chrome/renderer/print_web_view_helper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698