diff options
author | vitalybuka@chromium.org <vitalybuka@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-20 05:58:43 +0000 |
---|---|---|
committer | vitalybuka@chromium.org <vitalybuka@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-20 05:58:43 +0000 |
commit | 09083ebe22a6d21484b7e0e5d386642e741bfd89 (patch) | |
tree | 26077799e9b996ac5311fe1e25783bb6b593b657 | |
parent | 23150a133bc7db48ee40fe85f7af46c4ce94ca5f (diff) | |
download | chromium_src-09083ebe22a6d21484b7e0e5d386642e741bfd89.zip chromium_src-09083ebe22a6d21484b7e0e5d386642e741bfd89.tar.gz chromium_src-09083ebe22a6d21484b7e0e5d386642e741bfd89.tar.bz2 |
Simplify document title for Windows printing.
Some drivers can't handle long titles or control characters.
BUG=65469
Review URL: https://chromiumcodereview.appspot.com/10941025
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@157715 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/printing/print_job_worker.cc | 11 | ||||
-rw-r--r-- | chrome/service/cloud_print/print_system_win.cc | 4 | ||||
-rw-r--r-- | chrome/service/cloud_print/printer_job_handler.cc | 26 | ||||
-rw-r--r-- | printing/DEPS | 1 | ||||
-rw-r--r-- | printing/backend/print_backend.cc | 22 | ||||
-rw-r--r-- | printing/backend/print_backend.h | 4 | ||||
-rw-r--r-- | printing/backend/print_backend_unittest.cc | 25 | ||||
-rw-r--r-- | printing/printing.gyp | 1 | ||||
-rw-r--r-- | printing/printing_context_win.cc | 2 |
9 files changed, 87 insertions, 9 deletions
diff --git a/chrome/browser/printing/print_job_worker.cc b/chrome/browser/printing/print_job_worker.cc index 90ea961..42ebab8 100644 --- a/chrome/browser/printing/print_job_worker.cc +++ b/chrome/browser/printing/print_job_worker.cc @@ -15,9 +15,12 @@ #include "chrome/common/chrome_notification_types.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/notification_service.h" +#include "grit/generated_resources.h" +#include "printing/backend/print_backend.h" #include "printing/print_job_constants.h" #include "printing/printed_document.h" #include "printing/printed_page.h" +#include "ui/base/l10n/l10n_util.h" using content::BrowserThread; @@ -202,8 +205,14 @@ void PrintJobWorker::StartPrinting(PrintedDocument* new_document) { return; } + string16 document_name = + printing::PrintBackend::SimplifyDocumentTitle(document_->name()); + if (document_name.empty()) { + document_name = printing::PrintBackend::SimplifyDocumentTitle( + l10n_util::GetStringUTF16(IDS_DEFAULT_PRINT_DOCUMENT_TITLE)); + } PrintingContext::Result result = - printing_context_->NewDocument(document_->name()); + printing_context_->NewDocument(document_name); if (result != PrintingContext::OK) { OnFailure(); return; diff --git a/chrome/service/cloud_print/print_system_win.cc b/chrome/service/cloud_print/print_system_win.cc index 3d787e0..7cdf06f 100644 --- a/chrome/service/cloud_print/print_system_win.cc +++ b/chrome/service/cloud_print/print_system_win.cc @@ -430,7 +430,9 @@ class PrintSystemWin : public PrintSystem { hr = E_FAIL; DOCINFO di = {0}; di.cbSize = sizeof(DOCINFO); - std::wstring doc_name = UTF8ToWide(job_title); + string16 doc_name = UTF8ToUTF16(job_title); + DCHECK(printing::PrintBackend::SimplifyDocumentTitle(doc_name) == + doc_name); di.lpszDocName = doc_name.c_str(); job_id_ = StartDoc(dc, &di); if (job_id_ <= 0) diff --git a/chrome/service/cloud_print/printer_job_handler.cc b/chrome/service/cloud_print/printer_job_handler.cc index b34de25..98b4e9d 100644 --- a/chrome/service/cloud_print/printer_job_handler.cc +++ b/chrome/service/cloud_print/printer_job_handler.cc @@ -17,8 +17,11 @@ #include "chrome/service/cloud_print/cloud_print_helpers.h" #include "chrome/service/cloud_print/job_status_updater.h" #include "googleurl/src/gurl.h" +#include "grit/generated_resources.h" #include "net/http/http_response_headers.h" #include "net/http/http_status_code.h" +#include "printing/backend/print_backend.h" +#include "ui/base/l10n/l10n_util.h" PrinterJobHandler::JobDetails::JobDetails() {} @@ -676,13 +679,22 @@ void PrinterJobHandler::DoPrint(const JobDetails& job_details, const std::string& printer_name) { job_spooler_ = print_system_->CreateJobSpooler(); DCHECK(job_spooler_); - if (!job_spooler_ || !job_spooler_->Spool(job_details.print_ticket_, - job_details.print_data_file_path_, - job_details.print_data_mime_type_, - printer_name, - job_details.job_title_, - job_details.tags_, - this)) { + if (!job_spooler_) + return; + string16 document_name = + printing::PrintBackend::SimplifyDocumentTitle( + UTF8ToUTF16(job_details.job_title_)); + if (document_name.empty()) { + document_name = printing::PrintBackend::SimplifyDocumentTitle( + l10n_util::GetStringUTF16(IDS_DEFAULT_PRINT_DOCUMENT_TITLE)); + } + if (!job_spooler_->Spool(job_details.print_ticket_, + job_details.print_data_file_path_, + job_details.print_data_mime_type_, + printer_name, + UTF16ToUTF8(document_name), + job_details.tags_, + this)) { OnJobSpoolFailed(); } } diff --git a/printing/DEPS b/printing/DEPS index bd54ac8..fa7e4a7 100644 --- a/printing/DEPS +++ b/printing/DEPS @@ -1,5 +1,6 @@ include_rules = [ "+skia/ext", + "+third_party/icu", "+third_party/skia", "+ui/base/text", "+ui/gfx", diff --git a/printing/backend/print_backend.cc b/printing/backend/print_backend.cc index b993bfc..71f917d 100644 --- a/printing/backend/print_backend.cc +++ b/printing/backend/print_backend.cc @@ -4,6 +4,18 @@ #include "printing/backend/print_backend.h" +#include <algorithm> + +#include "third_party/icu/public/common/unicode/uchar.h" +#include "ui/base/text/text_elider.h" + +namespace { + +const wchar_t kDefaultDocumentTitle[] = L"Untitled Document"; +const int kMaxDocumentTitleLength = 25; + +} // namespace + namespace printing { PrinterBasicInfo::PrinterBasicInfo() @@ -26,4 +38,14 @@ PrinterCapsAndDefaults::~PrinterCapsAndDefaults() {} PrintBackend::~PrintBackend() {} +string16 PrintBackend::SimplifyDocumentTitle(const string16& title) { + string16 no_controls(title); + no_controls.erase( + std::remove_if(no_controls.begin(), no_controls.end(), &u_iscntrl), + no_controls.end()); + string16 result; + ui::ElideString(no_controls, kMaxDocumentTitleLength, &result); + return result; +} + } // namespace printing diff --git a/printing/backend/print_backend.h b/printing/backend/print_backend.h index 3cbf162..127caf5 100644 --- a/printing/backend/print_backend.h +++ b/printing/backend/print_backend.h @@ -10,6 +10,7 @@ #include <vector> #include "base/memory/ref_counted.h" +#include "base/string16.h" #include "printing/print_job_constants.h" #include "printing/printing_export.h" @@ -92,6 +93,9 @@ class PRINTING_EXPORT PrintBackend // Returns true if printer_name points to a valid printer. virtual bool IsValidPrinter(const std::string& printer_name) = 0; + // Simplify title to resolve issue with some drivers. + static string16 SimplifyDocumentTitle(const string16& title); + // Allocate a print backend. If |print_backend_settings| is NULL, default // settings will be used. // Return NULL if no print backend available. diff --git a/printing/backend/print_backend_unittest.cc b/printing/backend/print_backend_unittest.cc new file mode 100644 index 0000000..c801684 --- /dev/null +++ b/printing/backend/print_backend_unittest.cc @@ -0,0 +1,25 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "printing/backend/print_backend.h" +#include "base/utf_string_conversions.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace printing { + +std::string Simplify(const char* title) { + return UTF16ToUTF8(PrintBackend::SimplifyDocumentTitle(ASCIIToUTF16(title))); +} + +TEST(PrintBackendTest, SimplifyDocumentTitle) { + EXPECT_STREQ("", Simplify("").c_str()); + EXPECT_STREQ("Document wi...oooong name", + Simplify("Document with very looooooooooong name").c_str()); + EXPECT_STREQ("Control Characters", + Simplify("C\ron\ntrol Charac\15ters").c_str()); + EXPECT_STREQ("", Simplify("\n\r\n\r\t\r").c_str()); +} + +} // namespace printing + diff --git a/printing/printing.gyp b/printing/printing.gyp index 89d0d1d..8733e2a 100644 --- a/printing/printing.gyp +++ b/printing/printing.gyp @@ -209,6 +209,7 @@ '../ui/ui.gyp:ui', ], 'sources': [ + 'backend/print_backend_unittest.cc', 'emf_win_unittest.cc', 'printing_test.h', 'page_number_unittest.cc', diff --git a/printing/printing_context_win.cc b/printing/printing_context_win.cc index 522da3d..b5d0551 100644 --- a/printing/printing_context_win.cc +++ b/printing/printing_context_win.cc @@ -16,6 +16,7 @@ #include "base/utf_string_conversions.h" #include "base/values.h" #include "base/win/metro.h" +#include "printing/backend/print_backend.h" #include "printing/backend/win_helper.h" #include "printing/print_job_constants.h" #include "printing/print_settings_initializer_win.h" @@ -504,6 +505,7 @@ PrintingContext::Result PrintingContextWin::NewDocument( if (SP_ERROR == SetAbortProc(context_, &AbortProc)) return OnError(); + DCHECK(PrintBackend::SimplifyDocumentTitle(document_name) == document_name); DOCINFO di = { sizeof(DOCINFO) }; const std::wstring& document_name_wide = UTF16ToWide(document_name); di.lpszDocName = document_name_wide.c_str(); |