| 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/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/browser_list.h" | 13 #include "chrome/browser/browser_list.h" |
| 14 #include "chrome/browser/browser_thread.h" | 14 #include "chrome/browser/browser_thread.h" |
| 15 #include "chrome/browser/debugger/devtools_manager.h" | 15 #include "chrome/browser/debugger/devtools_manager.h" |
| 16 #include "chrome/browser/dom_ui/dom_ui.h" | 16 #include "chrome/browser/dom_ui/dom_ui.h" |
| 17 #include "chrome/browser/dom_ui/dom_ui_util.h" | 17 #include "chrome/browser/dom_ui/dom_ui_util.h" |
| 18 #include "chrome/browser/dom_ui/html_dialog_ui.h" | 18 #include "chrome/browser/prefs/pref_service.h" |
| 19 #include "chrome/browser/printing/cloud_print/cloud_print_url.h" | 19 #include "chrome/browser/printing/cloud_print/cloud_print_url.h" |
| 20 #include "chrome/browser/profile.h" |
| 20 #include "chrome/browser/renderer_host/render_view_host.h" | 21 #include "chrome/browser/renderer_host/render_view_host.h" |
| 21 #include "chrome/browser/tab_contents/tab_contents.h" | 22 #include "chrome/browser/tab_contents/tab_contents.h" |
| 23 #include "chrome/browser/tab_contents/tab_contents_view.h" |
| 22 #include "chrome/common/notification_observer.h" | 24 #include "chrome/common/notification_observer.h" |
| 23 #include "chrome/common/notification_registrar.h" | 25 #include "chrome/common/notification_registrar.h" |
| 24 #include "chrome/common/notification_source.h" | 26 #include "chrome/common/notification_source.h" |
| 25 #include "chrome/common/notification_type.h" | 27 #include "chrome/common/notification_type.h" |
| 28 #include "chrome/common/pref_names.h" |
| 26 #include "chrome/common/render_messages_params.h" | 29 #include "chrome/common/render_messages_params.h" |
| 27 #include "chrome/common/url_constants.h" | 30 #include "chrome/common/url_constants.h" |
| 28 #include "webkit/glue/webpreferences.h" | 31 #include "webkit/glue/webpreferences.h" |
| 29 | 32 |
| 30 #include "grit/generated_resources.h" | 33 #include "grit/generated_resources.h" |
| 31 | 34 |
| 32 // This module implements the UI support in Chrome for cloud printing. | 35 // This module implements the UI support in Chrome for cloud printing. |
| 33 // This means hosting a dialog containing HTML/JavaScript and using | 36 // This means hosting a dialog containing HTML/JavaScript and using |
| 34 // the published cloud print user interface integration APIs to get | 37 // the published cloud print user interface integration APIs to get |
| 35 // page setup settings from the dialog contents and provide the | 38 // page setup settings from the dialog contents and provide the |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 // When the dialog contents are finished (by either being cancelled or | 88 // When the dialog contents are finished (by either being cancelled or |
| 86 // hitting the print button), the delegate is notified, and responds | 89 // hitting the print button), the delegate is notified, and responds |
| 87 // that the dialog should be closed, at which point things are torn | 90 // that the dialog should be closed, at which point things are torn |
| 88 // down and released. | 91 // down and released. |
| 89 | 92 |
| 90 // TODO(scottbyer): | 93 // TODO(scottbyer): |
| 91 // http://code.google.com/p/chromium/issues/detail?id=44093 The | 94 // http://code.google.com/p/chromium/issues/detail?id=44093 The |
| 92 // high-level flow (where the PDF data is generated before even | 95 // high-level flow (where the PDF data is generated before even |
| 93 // bringing up the dialog) isn't what we want. | 96 // bringing up the dialog) isn't what we want. |
| 94 | 97 |
| 95 | |
| 96 namespace internal_cloud_print_helpers { | 98 namespace internal_cloud_print_helpers { |
| 97 | 99 |
| 98 bool GetRealOrInt(const DictionaryValue& dictionary, | 100 bool GetRealOrInt(const DictionaryValue& dictionary, |
| 99 const std::string& path, | 101 const std::string& path, |
| 100 double* out_value) { | 102 double* out_value) { |
| 101 if (!dictionary.GetReal(path, out_value)) { | 103 if (!dictionary.GetReal(path, out_value)) { |
| 102 int int_value = 0; | 104 int int_value = 0; |
| 103 if (!dictionary.GetInteger(path, &int_value)) | 105 if (!dictionary.GetInteger(path, &int_value)) |
| 104 return false; | 106 return false; |
| 105 *out_value = int_value; | 107 *out_value = int_value; |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 // reference to it, so when the task that is calling it finishes and | 240 // reference to it, so when the task that is calling it finishes and |
| 239 // removes it's reference, it goes away. | 241 // removes it's reference, it goes away. |
| 240 void CloudPrintFlowHandler::CancelAnyRunningTask() { | 242 void CloudPrintFlowHandler::CancelAnyRunningTask() { |
| 241 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 242 if (print_data_sender_.get()) { | 244 if (print_data_sender_.get()) { |
| 243 print_data_sender_->CancelPrintDataFile(); | 245 print_data_sender_->CancelPrintDataFile(); |
| 244 print_data_sender_ = NULL; | 246 print_data_sender_ = NULL; |
| 245 } | 247 } |
| 246 } | 248 } |
| 247 | 249 |
| 248 | |
| 249 void CloudPrintFlowHandler::RegisterMessages() { | 250 void CloudPrintFlowHandler::RegisterMessages() { |
| 250 if (!dom_ui_) | 251 if (!dom_ui_) |
| 251 return; | 252 return; |
| 252 | 253 |
| 253 // TODO(scottbyer) - This is where we will register messages for the | 254 // TODO(scottbyer) - This is where we will register messages for the |
| 254 // UI JS to use. Needed: Call to update page setup parameters. | 255 // UI JS to use. Needed: Call to update page setup parameters. |
| 255 dom_ui_->RegisterMessageCallback( | 256 dom_ui_->RegisterMessageCallback( |
| 256 "ShowDebugger", | 257 "ShowDebugger", |
| 257 NewCallback(this, &CloudPrintFlowHandler::HandleShowDebugger)); | 258 NewCallback(this, &CloudPrintFlowHandler::HandleShowDebugger)); |
| 258 dom_ui_->RegisterMessageCallback( | 259 dom_ui_->RegisterMessageCallback( |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 371 return; | 372 return; |
| 372 } | 373 } |
| 373 | 374 |
| 374 // TODO(scottbyer) - Here is where we would kick the originating | 375 // TODO(scottbyer) - Here is where we would kick the originating |
| 375 // renderer thread with these new parameters in order to get it to | 376 // renderer thread with these new parameters in order to get it to |
| 376 // re-generate the PDF and hand it back to us. window.print() is | 377 // re-generate the PDF and hand it back to us. window.print() is |
| 377 // currently synchronous, so there's a lot of work to do to get to | 378 // currently synchronous, so there's a lot of work to do to get to |
| 378 // that point. | 379 // that point. |
| 379 } | 380 } |
| 380 | 381 |
| 382 void CloudPrintFlowHandler::StoreDialogClientSize() const { |
| 383 if (dom_ui_ && dom_ui_->tab_contents() && dom_ui_->tab_contents()->view()) { |
| 384 gfx::Size size = dom_ui_->tab_contents()->view()->GetContainerSize(); |
| 385 dom_ui_->GetProfile()->GetPrefs()->SetInteger( |
| 386 prefs::kCloudPrintDialogWidth, size.width()); |
| 387 dom_ui_->GetProfile()->GetPrefs()->SetInteger( |
| 388 prefs::kCloudPrintDialogHeight, size.height()); |
| 389 } |
| 390 } |
| 391 |
| 381 CloudPrintHtmlDialogDelegate::CloudPrintHtmlDialogDelegate( | 392 CloudPrintHtmlDialogDelegate::CloudPrintHtmlDialogDelegate( |
| 382 const FilePath& path_to_pdf, | 393 const FilePath& path_to_pdf, |
| 383 int width, int height, | 394 int width, int height, |
| 384 const std::string& json_arguments, | 395 const std::string& json_arguments, |
| 385 const string16& print_job_title) | 396 const string16& print_job_title) |
| 386 : flow_handler_(new CloudPrintFlowHandler(path_to_pdf, print_job_title)), | 397 : flow_handler_(new CloudPrintFlowHandler(path_to_pdf, print_job_title)), |
| 387 owns_flow_handler_(true) { | 398 owns_flow_handler_(true) { |
| 388 Init(width, height, json_arguments); | 399 Init(width, height, json_arguments); |
| 389 } | 400 } |
| 390 | 401 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 418 if (owns_flow_handler_) { | 429 if (owns_flow_handler_) { |
| 419 delete flow_handler_; | 430 delete flow_handler_; |
| 420 } | 431 } |
| 421 } | 432 } |
| 422 | 433 |
| 423 bool CloudPrintHtmlDialogDelegate::IsDialogModal() const { | 434 bool CloudPrintHtmlDialogDelegate::IsDialogModal() const { |
| 424 return true; | 435 return true; |
| 425 } | 436 } |
| 426 | 437 |
| 427 std::wstring CloudPrintHtmlDialogDelegate::GetDialogTitle() const { | 438 std::wstring CloudPrintHtmlDialogDelegate::GetDialogTitle() const { |
| 428 return l10n_util::GetString(IDS_CLOUD_PRINT_TITLE); | 439 return std::wstring(); |
| 429 } | 440 } |
| 430 | 441 |
| 431 GURL CloudPrintHtmlDialogDelegate::GetDialogContentURL() const { | 442 GURL CloudPrintHtmlDialogDelegate::GetDialogContentURL() const { |
| 432 return params_.url; | 443 return params_.url; |
| 433 } | 444 } |
| 434 | 445 |
| 435 void CloudPrintHtmlDialogDelegate::GetDOMMessageHandlers( | 446 void CloudPrintHtmlDialogDelegate::GetDOMMessageHandlers( |
| 436 std::vector<DOMMessageHandler*>* handlers) const { | 447 std::vector<DOMMessageHandler*>* handlers) const { |
| 437 handlers->push_back(flow_handler_); | 448 handlers->push_back(flow_handler_); |
| 438 // We don't own flow_handler_ anymore, but it sticks around until at | 449 // We don't own flow_handler_ anymore, but it sticks around until at |
| 439 // least right after OnDialogClosed() is called (and this object is | 450 // least right after OnDialogClosed() is called (and this object is |
| 440 // destroyed). | 451 // destroyed). |
| 441 owns_flow_handler_ = false; | 452 owns_flow_handler_ = false; |
| 442 } | 453 } |
| 443 | 454 |
| 444 void CloudPrintHtmlDialogDelegate::GetDialogSize(gfx::Size* size) const { | 455 void CloudPrintHtmlDialogDelegate::GetDialogSize(gfx::Size* size) const { |
| 445 size->set_width(params_.width); | 456 size->set_width(params_.width); |
| 446 size->set_height(params_.height); | 457 size->set_height(params_.height); |
| 447 } | 458 } |
| 448 | 459 |
| 449 std::string CloudPrintHtmlDialogDelegate::GetDialogArgs() const { | 460 std::string CloudPrintHtmlDialogDelegate::GetDialogArgs() const { |
| 450 return params_.json_input; | 461 return params_.json_input; |
| 451 } | 462 } |
| 452 | 463 |
| 453 void CloudPrintHtmlDialogDelegate::OnDialogClosed( | 464 void CloudPrintHtmlDialogDelegate::OnDialogClosed( |
| 454 const std::string& json_retval) { | 465 const std::string& json_retval) { |
| 466 // Get the final dialog size and store it. |
| 467 flow_handler_->StoreDialogClientSize(); |
| 455 delete this; | 468 delete this; |
| 456 } | 469 } |
| 457 | 470 |
| 458 void CloudPrintHtmlDialogDelegate::OnCloseContents(TabContents* source, | 471 void CloudPrintHtmlDialogDelegate::OnCloseContents(TabContents* source, |
| 459 bool* out_close_dialog) { | 472 bool* out_close_dialog) { |
| 460 if (out_close_dialog) | 473 if (out_close_dialog) |
| 461 *out_close_dialog = true; | 474 *out_close_dialog = true; |
| 462 } | 475 } |
| 463 | 476 |
| 477 bool CloudPrintHtmlDialogDelegate::ShouldShowDialogTitle() const { |
| 478 return false; |
| 479 } |
| 480 |
| 464 } // end of namespace internal_cloud_print_helpers | 481 } // end of namespace internal_cloud_print_helpers |
| 465 | 482 |
| 466 // static, called on the IO thread. This is the main entry point into | 483 // static, called on the IO thread. This is the main entry point into |
| 467 // creating the dialog. | 484 // creating the dialog. |
| 468 | 485 |
| 469 // TODO(scottbyer): The signature here will need to change as the | 486 // TODO(scottbyer): The signature here will need to change as the |
| 470 // workflow through the printing code changes to allow for dynamically | 487 // workflow through the printing code changes to allow for dynamically |
| 471 // changing page setup parameters while the dialog is active. | 488 // changing page setup parameters while the dialog is active. |
| 472 void PrintDialogCloud::CreatePrintDialogForPdf(const FilePath& path_to_pdf) { | 489 void PrintDialogCloud::CreatePrintDialogForPdf(const FilePath& path_to_pdf) { |
| 473 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 490 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 486 // Initialize the print dialog. Called on the UI thread. | 503 // Initialize the print dialog. Called on the UI thread. |
| 487 PrintDialogCloud::PrintDialogCloud(const FilePath& path_to_pdf) | 504 PrintDialogCloud::PrintDialogCloud(const FilePath& path_to_pdf) |
| 488 : browser_(BrowserList::GetLastActive()) { | 505 : browser_(BrowserList::GetLastActive()) { |
| 489 | 506 |
| 490 // TODO(scottbyer): Verify GAIA login valid, execute GAIA login if not (should | 507 // TODO(scottbyer): Verify GAIA login valid, execute GAIA login if not (should |
| 491 // be distilled out of bookmark sync.) | 508 // be distilled out of bookmark sync.) |
| 492 string16 print_job_title; | 509 string16 print_job_title; |
| 493 if (browser_ && browser_->GetSelectedTabContents()) | 510 if (browser_ && browser_->GetSelectedTabContents()) |
| 494 print_job_title = browser_->GetSelectedTabContents()->GetTitle(); | 511 print_job_title = browser_->GetSelectedTabContents()->GetTitle(); |
| 495 | 512 |
| 496 // TODO(scottbyer): Get the dialog width, height from the dialog | 513 const int kDefaultWidth = 497; |
| 497 // contents, and take the screen size into account. | 514 const int kDefaultHeight = 332; |
| 515 |
| 516 PrefService* pref_service = browser_->GetProfile()->GetPrefs(); |
| 517 DCHECK(pref_service); |
| 518 if (!pref_service->FindPreference(prefs::kCloudPrintDialogWidth)) { |
| 519 pref_service->RegisterIntegerPref(prefs::kCloudPrintDialogWidth, |
| 520 kDefaultWidth); |
| 521 } |
| 522 if (!pref_service->FindPreference(prefs::kCloudPrintDialogHeight)) { |
| 523 pref_service->RegisterIntegerPref(prefs::kCloudPrintDialogHeight, |
| 524 kDefaultHeight); |
| 525 } |
| 526 |
| 527 int width = pref_service->GetInteger(prefs::kCloudPrintDialogWidth); |
| 528 int height = pref_service->GetInteger(prefs::kCloudPrintDialogHeight); |
| 498 HtmlDialogUIDelegate* dialog_delegate = | 529 HtmlDialogUIDelegate* dialog_delegate = |
| 499 new internal_cloud_print_helpers::CloudPrintHtmlDialogDelegate( | 530 new internal_cloud_print_helpers::CloudPrintHtmlDialogDelegate( |
| 500 path_to_pdf, 500, 400, std::string(), print_job_title); | 531 path_to_pdf, width, height, std::string(), print_job_title); |
| 501 browser_->BrowserShowHtmlDialog(dialog_delegate, NULL); | 532 browser_->BrowserShowHtmlDialog(dialog_delegate, NULL); |
| 502 } | 533 } |
| 503 | 534 |
| 504 PrintDialogCloud::~PrintDialogCloud() { | 535 PrintDialogCloud::~PrintDialogCloud() { |
| 505 } | 536 } |
| OLD | NEW |