Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/printing/print_dialog_cloud.h" | 5 #include "chrome/browser/printing/print_dialog_cloud.h" |
| 6 #include "chrome/browser/printing/print_dialog_cloud_internal.h" | 6 #include "chrome/browser/printing/print_dialog_cloud_internal.h" |
| 7 | 7 |
| 8 #include "app/l10n_util.h" | 8 #include "app/l10n_util.h" |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/command_line.h" | |
| 10 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 11 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
| 12 #include "base/values.h" | 13 #include "base/values.h" |
| 13 #include "chrome/browser/browser_list.h" | 14 #include "chrome/browser/browser_list.h" |
| 14 #include "chrome/browser/browser_thread.h" | 15 #include "chrome/browser/browser_thread.h" |
| 15 #include "chrome/browser/debugger/devtools_manager.h" | 16 #include "chrome/browser/debugger/devtools_manager.h" |
| 16 #include "chrome/browser/dom_ui/dom_ui.h" | 17 #include "chrome/browser/dom_ui/dom_ui.h" |
| 17 #include "chrome/browser/dom_ui/dom_ui_util.h" | 18 #include "chrome/browser/dom_ui/dom_ui_util.h" |
| 19 #if defined(TOOLKIT_GTK) | |
| 20 #include "chrome/browser/gtk/html_dialog_gtk.h" | |
| 21 #endif // defined(TOOLKIT_GTK) | |
| 18 #include "chrome/browser/prefs/pref_service.h" | 22 #include "chrome/browser/prefs/pref_service.h" |
| 19 #include "chrome/browser/printing/cloud_print/cloud_print_url.h" | 23 #include "chrome/browser/printing/cloud_print/cloud_print_url.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
| 25 #include "chrome/browser/profiles/profile_manager.h" | |
| 21 #include "chrome/browser/renderer_host/render_view_host.h" | 26 #include "chrome/browser/renderer_host/render_view_host.h" |
| 22 #include "chrome/browser/tab_contents/tab_contents.h" | 27 #include "chrome/browser/tab_contents/tab_contents.h" |
| 23 #include "chrome/browser/tab_contents/tab_contents_view.h" | 28 #include "chrome/browser/tab_contents/tab_contents_view.h" |
| 29 #if defined(TOOLKIT_VIEWS) | |
| 30 #include "chrome/browser/ui/views/browser_dialogs.h" | |
| 31 #endif // defined(TOOLKIT_VIEWS) | |
| 32 #include "chrome/common/chrome_switches.h" | |
| 24 #include "chrome/common/notification_observer.h" | 33 #include "chrome/common/notification_observer.h" |
| 25 #include "chrome/common/notification_registrar.h" | 34 #include "chrome/common/notification_registrar.h" |
| 26 #include "chrome/common/notification_source.h" | 35 #include "chrome/common/notification_source.h" |
| 27 #include "chrome/common/notification_type.h" | 36 #include "chrome/common/notification_type.h" |
| 28 #include "chrome/common/pref_names.h" | 37 #include "chrome/common/pref_names.h" |
| 29 #include "chrome/common/render_messages_params.h" | 38 #include "chrome/common/render_messages_params.h" |
| 30 #include "chrome/common/url_constants.h" | 39 #include "chrome/common/url_constants.h" |
| 31 #include "webkit/glue/webpreferences.h" | 40 #include "webkit/glue/webpreferences.h" |
| 32 | 41 |
| 33 #include "grit/generated_resources.h" | 42 #include "grit/generated_resources.h" |
| (...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 386 prefs::kCloudPrintDialogWidth, size.width()); | 395 prefs::kCloudPrintDialogWidth, size.width()); |
| 387 dom_ui_->GetProfile()->GetPrefs()->SetInteger( | 396 dom_ui_->GetProfile()->GetPrefs()->SetInteger( |
| 388 prefs::kCloudPrintDialogHeight, size.height()); | 397 prefs::kCloudPrintDialogHeight, size.height()); |
| 389 } | 398 } |
| 390 } | 399 } |
| 391 | 400 |
| 392 CloudPrintHtmlDialogDelegate::CloudPrintHtmlDialogDelegate( | 401 CloudPrintHtmlDialogDelegate::CloudPrintHtmlDialogDelegate( |
| 393 const FilePath& path_to_pdf, | 402 const FilePath& path_to_pdf, |
| 394 int width, int height, | 403 int width, int height, |
| 395 const std::string& json_arguments, | 404 const std::string& json_arguments, |
| 396 const string16& print_job_title) | 405 const string16& print_job_title, |
| 406 bool modal) | |
| 397 : flow_handler_(new CloudPrintFlowHandler(path_to_pdf, print_job_title)), | 407 : flow_handler_(new CloudPrintFlowHandler(path_to_pdf, print_job_title)), |
| 408 modal_(modal), | |
| 398 owns_flow_handler_(true) { | 409 owns_flow_handler_(true) { |
| 399 Init(width, height, json_arguments); | 410 Init(width, height, json_arguments); |
| 400 } | 411 } |
| 401 | 412 |
| 402 CloudPrintHtmlDialogDelegate::CloudPrintHtmlDialogDelegate( | 413 CloudPrintHtmlDialogDelegate::CloudPrintHtmlDialogDelegate( |
| 403 CloudPrintFlowHandler* flow_handler, | 414 CloudPrintFlowHandler* flow_handler, |
| 404 int width, int height, | 415 int width, int height, |
| 405 const std::string& json_arguments) | 416 const std::string& json_arguments, |
| 417 bool modal) | |
| 406 : flow_handler_(flow_handler), | 418 : flow_handler_(flow_handler), |
| 419 modal_(modal), | |
| 407 owns_flow_handler_(true) { | 420 owns_flow_handler_(true) { |
| 408 Init(width, height, json_arguments); | 421 Init(width, height, json_arguments); |
| 409 } | 422 } |
| 410 | 423 |
| 411 void CloudPrintHtmlDialogDelegate::Init( | 424 void CloudPrintHtmlDialogDelegate::Init( |
| 412 int width, int height, const std::string& json_arguments) { | 425 int width, int height, const std::string& json_arguments) { |
| 413 // This information is needed to show the dialog HTML content. | 426 // This information is needed to show the dialog HTML content. |
| 414 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 427 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 415 std::string cloud_print_url(chrome::kCloudPrintResourcesURL); | 428 std::string cloud_print_url(chrome::kCloudPrintResourcesURL); |
| 416 params_.url = GURL(cloud_print_url); | 429 params_.url = GURL(cloud_print_url); |
| 417 params_.height = height; | 430 params_.height = height; |
| 418 params_.width = width; | 431 params_.width = width; |
| 419 params_.json_input = json_arguments; | 432 params_.json_input = json_arguments; |
| 420 | 433 |
| 421 flow_handler_->SetDialogDelegate(this); | 434 flow_handler_->SetDialogDelegate(this); |
| 435 // We might show the dialog with no browser. | |
| 436 // We need this to keep Chrome alive while our dialog is up. | |
| 437 BrowserList::StartKeepAlive(); | |
| 422 } | 438 } |
| 423 | 439 |
| 424 CloudPrintHtmlDialogDelegate::~CloudPrintHtmlDialogDelegate() { | 440 CloudPrintHtmlDialogDelegate::~CloudPrintHtmlDialogDelegate() { |
| 425 // If the flow_handler_ is about to outlive us because we don't own | 441 // If the flow_handler_ is about to outlive us because we don't own |
| 426 // it anymore, we need to have it remove it's reference to us. | 442 // it anymore, we need to have it remove it's reference to us. |
| 427 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 443 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 428 flow_handler_->SetDialogDelegate(NULL); | 444 flow_handler_->SetDialogDelegate(NULL); |
| 429 if (owns_flow_handler_) { | 445 if (owns_flow_handler_) { |
| 430 delete flow_handler_; | 446 delete flow_handler_; |
| 431 } | 447 } |
| 432 } | 448 } |
| 433 | 449 |
| 434 bool CloudPrintHtmlDialogDelegate::IsDialogModal() const { | 450 bool CloudPrintHtmlDialogDelegate::IsDialogModal() const { |
| 435 return true; | 451 return modal_; |
| 436 } | 452 } |
| 437 | 453 |
| 438 std::wstring CloudPrintHtmlDialogDelegate::GetDialogTitle() const { | 454 std::wstring CloudPrintHtmlDialogDelegate::GetDialogTitle() const { |
| 439 return std::wstring(); | 455 return std::wstring(); |
| 440 } | 456 } |
| 441 | 457 |
| 442 GURL CloudPrintHtmlDialogDelegate::GetDialogContentURL() const { | 458 GURL CloudPrintHtmlDialogDelegate::GetDialogContentURL() const { |
| 443 return params_.url; | 459 return params_.url; |
| 444 } | 460 } |
| 445 | 461 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 458 } | 474 } |
| 459 | 475 |
| 460 std::string CloudPrintHtmlDialogDelegate::GetDialogArgs() const { | 476 std::string CloudPrintHtmlDialogDelegate::GetDialogArgs() const { |
| 461 return params_.json_input; | 477 return params_.json_input; |
| 462 } | 478 } |
| 463 | 479 |
| 464 void CloudPrintHtmlDialogDelegate::OnDialogClosed( | 480 void CloudPrintHtmlDialogDelegate::OnDialogClosed( |
| 465 const std::string& json_retval) { | 481 const std::string& json_retval) { |
| 466 // Get the final dialog size and store it. | 482 // Get the final dialog size and store it. |
| 467 flow_handler_->StoreDialogClientSize(); | 483 flow_handler_->StoreDialogClientSize(); |
| 484 // We might show the dialog with no browser. | |
| 485 // End the keep-alive so that Chrome can exit. | |
| 486 BrowserList::EndKeepAlive(); | |
| 468 delete this; | 487 delete this; |
| 469 } | 488 } |
| 470 | 489 |
| 471 void CloudPrintHtmlDialogDelegate::OnCloseContents(TabContents* source, | 490 void CloudPrintHtmlDialogDelegate::OnCloseContents(TabContents* source, |
| 472 bool* out_close_dialog) { | 491 bool* out_close_dialog) { |
| 473 if (out_close_dialog) | 492 if (out_close_dialog) |
| 474 *out_close_dialog = true; | 493 *out_close_dialog = true; |
| 475 } | 494 } |
| 476 | 495 |
| 477 bool CloudPrintHtmlDialogDelegate::ShouldShowDialogTitle() const { | 496 bool CloudPrintHtmlDialogDelegate::ShouldShowDialogTitle() const { |
| 478 return false; | 497 return false; |
| 479 } | 498 } |
| 480 | 499 |
| 481 } // end of namespace internal_cloud_print_helpers | 500 } // end of namespace internal_cloud_print_helpers |
| 482 | 501 |
| 483 // static, called on the IO thread. This is the main entry point into | 502 // static, called on the IO thread. This is the main entry point into |
| 484 // creating the dialog. | 503 // creating the dialog. |
| 485 | 504 |
| 486 // TODO(scottbyer): The signature here will need to change as the | 505 // TODO(scottbyer): The signature here will need to change as the |
| 487 // workflow through the printing code changes to allow for dynamically | 506 // workflow through the printing code changes to allow for dynamically |
| 488 // changing page setup parameters while the dialog is active. | 507 // changing page setup parameters while the dialog is active. |
| 489 void PrintDialogCloud::CreatePrintDialogForPdf(const FilePath& path_to_pdf) { | 508 void PrintDialogCloud::CreatePrintDialogForPdf(const FilePath& path_to_pdf, |
| 490 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 509 const string16& print_job_title, |
| 510 bool modal) { | |
| 511 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO) || | |
| 512 BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 491 | 513 |
| 492 BrowserThread::PostTask( | 514 BrowserThread::PostTask( |
| 493 BrowserThread::UI, FROM_HERE, | 515 BrowserThread::UI, FROM_HERE, |
| 494 NewRunnableFunction(&PrintDialogCloud::CreateDialogImpl, path_to_pdf)); | 516 NewRunnableFunction(&PrintDialogCloud::CreateDialogImpl, |
| 517 path_to_pdf, | |
| 518 print_job_title, | |
| 519 modal)); | |
| 495 } | 520 } |
| 496 | 521 |
| 497 // static, called from the UI thread. | 522 // static, called from the UI thread. |
| 498 void PrintDialogCloud::CreateDialogImpl(const FilePath& path_to_pdf) { | 523 void PrintDialogCloud::CreateDialogImpl(const FilePath& path_to_pdf, |
| 524 const string16& print_job_title, | |
| 525 bool modal) { | |
| 499 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 526 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 500 new PrintDialogCloud(path_to_pdf); | 527 new PrintDialogCloud(path_to_pdf, print_job_title, modal); |
| 501 } | 528 } |
| 502 | 529 |
| 503 // Initialize the print dialog. Called on the UI thread. | 530 // Initialize the print dialog. Called on the UI thread. |
| 504 PrintDialogCloud::PrintDialogCloud(const FilePath& path_to_pdf) | 531 PrintDialogCloud::PrintDialogCloud(const FilePath& path_to_pdf, |
| 505 : browser_(BrowserList::GetLastActive()) { | 532 const string16& print_job_title, |
| 533 bool modal) | |
| 534 : browser_(BrowserList::GetLastActive()), | |
| 535 print_job_title_(print_job_title), | |
| 536 modal_(modal) { | |
| 506 | 537 |
| 507 // TODO(scottbyer): Verify GAIA login valid, execute GAIA login if not (should | 538 // TODO(scottbyer): Verify GAIA login valid, execute GAIA login if not (should |
| 508 // be distilled out of bookmark sync.) | 539 // be distilled out of bookmark sync.) |
| 509 string16 print_job_title; | |
| 510 if (browser_ && browser_->GetSelectedTabContents()) | |
| 511 print_job_title = browser_->GetSelectedTabContents()->GetTitle(); | |
| 512 | |
| 513 const int kDefaultWidth = 497; | 540 const int kDefaultWidth = 497; |
| 514 const int kDefaultHeight = 332; | 541 const int kDefaultHeight = 332; |
| 515 | 542 Profile* profile = NULL; |
| 516 PrefService* pref_service = browser_->GetProfile()->GetPrefs(); | 543 PrefService* pref_service = NULL; |
| 544 if (modal_) { | |
|
Scott Byer
2011/01/12 23:35:02
When called to print a web page, the dialog should
| |
| 545 profile = ProfileManager::GetDefaultProfile(); | |
| 546 } else { | |
| 547 DCHECK(browser_); | |
| 548 if (print_job_title_.empty() && browser_->GetSelectedTabContents()) | |
| 549 print_job_title_ = browser_->GetSelectedTabContents()->GetTitle(); | |
| 550 profile = browser_->GetProfile(); | |
| 551 } | |
| 552 DCHECK(profile); | |
| 553 pref_service = profile->GetPrefs(); | |
| 517 DCHECK(pref_service); | 554 DCHECK(pref_service); |
| 518 if (!pref_service->FindPreference(prefs::kCloudPrintDialogWidth)) { | 555 if (!pref_service->FindPreference(prefs::kCloudPrintDialogWidth)) { |
| 519 pref_service->RegisterIntegerPref(prefs::kCloudPrintDialogWidth, | 556 pref_service->RegisterIntegerPref(prefs::kCloudPrintDialogWidth, |
| 520 kDefaultWidth); | 557 kDefaultWidth); |
| 521 } | 558 } |
| 522 if (!pref_service->FindPreference(prefs::kCloudPrintDialogHeight)) { | 559 if (!pref_service->FindPreference(prefs::kCloudPrintDialogHeight)) { |
| 523 pref_service->RegisterIntegerPref(prefs::kCloudPrintDialogHeight, | 560 pref_service->RegisterIntegerPref(prefs::kCloudPrintDialogHeight, |
| 524 kDefaultHeight); | 561 kDefaultHeight); |
| 525 } | 562 } |
| 526 | 563 |
| 527 int width = pref_service->GetInteger(prefs::kCloudPrintDialogWidth); | 564 int width = pref_service->GetInteger(prefs::kCloudPrintDialogWidth); |
| 528 int height = pref_service->GetInteger(prefs::kCloudPrintDialogHeight); | 565 int height = pref_service->GetInteger(prefs::kCloudPrintDialogHeight); |
| 566 | |
| 529 HtmlDialogUIDelegate* dialog_delegate = | 567 HtmlDialogUIDelegate* dialog_delegate = |
| 530 new internal_cloud_print_helpers::CloudPrintHtmlDialogDelegate( | 568 new internal_cloud_print_helpers::CloudPrintHtmlDialogDelegate( |
| 531 path_to_pdf, width, height, std::string(), print_job_title); | 569 path_to_pdf, width, height, std::string(), print_job_title_, modal_); |
| 532 browser_->BrowserShowHtmlDialog(dialog_delegate, NULL); | 570 if (modal_) { |
| 571 #if defined(TOOLKIT_VIEWS) | |
| 572 browser::ShowHtmlDialogView(NULL, profile, dialog_delegate); | |
| 573 #elif defined(TOOLKIT_GTK) | |
| 574 HtmlDialogGtk* html_dialog = | |
| 575 new HtmlDialogGtk(profile, dialog_delegate, NULL); | |
| 576 html_dialog->InitDialog(); | |
| 577 #endif // defined(TOOLKIT_VIEWS) | |
| 578 } else { | |
| 579 DCHECK(browser_); | |
| 580 browser_->BrowserShowHtmlDialog(dialog_delegate, NULL); | |
| 581 } | |
| 533 } | 582 } |
| 534 | 583 |
| 535 PrintDialogCloud::~PrintDialogCloud() { | 584 PrintDialogCloud::~PrintDialogCloud() { |
| 536 } | 585 } |
| OLD | NEW |