diff options
author | vitalybuka <vitalybuka@chromium.org> | 2015-07-07 11:21:16 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-07-07 18:21:45 +0000 |
commit | 27e401c8a69340bbc6cfbae460eeae7646e2dbbf (patch) | |
tree | 0eed9783a73efa6d4f9f25edfbf3ad6ce4af368e /printing | |
parent | 5ded2f651fff2380b9bd0f3a18c114f1615ae4d1 (diff) | |
download | chromium_src-27e401c8a69340bbc6cfbae460eeae7646e2dbbf.zip chromium_src-27e401c8a69340bbc6cfbae460eeae7646e2dbbf.tar.gz chromium_src-27e401c8a69340bbc6cfbae460eeae7646e2dbbf.tar.bz2 |
Include user email into document title before spooling
Could be used to monitor print jobs on OS side.
Windows spooler "owner" cannot be changed so we should modify title.
Max title length increased from 50 to 80 better readability.
BUG=504826
Review URL: https://codereview.chromium.org/1212883003
Cr-Commit-Position: refs/heads/master@{#337647}
Diffstat (limited to 'printing')
-rw-r--r-- | printing/printing_utils.cc | 44 | ||||
-rw-r--r-- | printing/printing_utils.h | 13 | ||||
-rw-r--r-- | printing/printing_utils_unittest.cc | 38 |
3 files changed, 81 insertions, 14 deletions
diff --git a/printing/printing_utils.cc b/printing/printing_utils.cc index 3d91de1b..5f9fd2c 100644 --- a/printing/printing_utils.cc +++ b/printing/printing_utils.cc @@ -6,23 +6,57 @@ #include <algorithm> +#include "base/logging.h" + +#include "base/strings/utf_string_conversions.h" #include "third_party/icu/source/common/unicode/uchar.h" #include "ui/gfx/text_elider.h" +namespace printing { + namespace { -const int kMaxDocumentTitleLength = 50; -} -namespace printing { +const size_t kMaxDocumentTitleLength = 80; -base::string16 SimplifyDocumentTitle(const base::string16& title) { +} // namespace + +base::string16 SimplifyDocumentTitleWithLength(const base::string16& title, + size_t length) { base::string16 no_controls(title); no_controls.erase( std::remove_if(no_controls.begin(), no_controls.end(), &u_iscntrl), no_controls.end()); base::string16 result; - gfx::ElideString(no_controls, kMaxDocumentTitleLength, &result); + gfx::ElideString(no_controls, static_cast<int>(length), &result); return result; } +base::string16 FormatDocumentTitleWithOwnerAndLength( + const base::string16& owner, + const base::string16& title, + size_t length) { + const base::string16 separator = base::ASCIIToUTF16(": "); + DCHECK(separator.size() < length); + + base::string16 short_title = + SimplifyDocumentTitleWithLength(owner, length - separator.size()); + short_title += separator; + if (short_title.size() < length) { + short_title += + SimplifyDocumentTitleWithLength(title, length - short_title.size()); + } + + return short_title; +} + +base::string16 SimplifyDocumentTitle(const base::string16& title) { + return SimplifyDocumentTitleWithLength(title, kMaxDocumentTitleLength); +} + +base::string16 FormatDocumentTitleWithOwner(const base::string16& owner, + const base::string16& title) { + return FormatDocumentTitleWithOwnerAndLength(owner, title, + kMaxDocumentTitleLength); +} + } // namespace printing diff --git a/printing/printing_utils.h b/printing/printing_utils.h index 958a3f5..e307350 100644 --- a/printing/printing_utils.h +++ b/printing/printing_utils.h @@ -14,6 +14,19 @@ namespace printing { PRINTING_EXPORT base::string16 SimplifyDocumentTitle( const base::string16& title); +PRINTING_EXPORT base::string16 SimplifyDocumentTitleWithLength( + const base::string16& title, + size_t length); + +PRINTING_EXPORT base::string16 FormatDocumentTitleWithOwner( + const base::string16& owner, + const base::string16& title); + +PRINTING_EXPORT base::string16 FormatDocumentTitleWithOwnerAndLength( + const base::string16& owner, + const base::string16& title, + size_t length); + } // namespace printing #endif // PRINTING_PRINTING_UTILS_H_ diff --git a/printing/printing_utils_unittest.cc b/printing/printing_utils_unittest.cc index be66fab..331451d 100644 --- a/printing/printing_utils_unittest.cc +++ b/printing/printing_utils_unittest.cc @@ -8,18 +8,38 @@ namespace printing { -std::string Simplify(const char* title) { - return base::UTF16ToUTF8(SimplifyDocumentTitle(base::ASCIIToUTF16(title))); +namespace { + +const size_t kTestLength = 8; + +std::string Simplify(const std::string& title) { + return base::UTF16ToUTF8( + SimplifyDocumentTitleWithLength(base::UTF8ToUTF16(title), kTestLength)); +} + +std::string Format(const std::string& owner, const std::string& title) { + return base::UTF16ToUTF8(FormatDocumentTitleWithOwnerAndLength( + base::UTF8ToUTF16(owner), base::UTF8ToUTF16(title), kTestLength)); } +} // namespace + TEST(PrintingUtilsTest, SimplifyDocumentTitle) { - EXPECT_STREQ("", Simplify("").c_str()); - EXPECT_STREQ("Long string. Long string...ng string. Long string.", - Simplify("Long string. Long string. Long string. Long string. " - "Long string. Long string. Long string.").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()); + EXPECT_EQ("", Simplify("")); + EXPECT_EQ("abcdefgh", Simplify("abcdefgh")); + EXPECT_EQ("abc...ij", Simplify("abcdefghij")); + EXPECT_EQ("Controls", Simplify("C\ron\nt\15rols")); + EXPECT_EQ("", Simplify("\n\r\n\r\t\r")); +} + +TEST(PrintingUtilsTest, FormatDocumentTitleWithOwner) { + EXPECT_EQ(": ", Format("", "")); + EXPECT_EQ("abc: ", Format("abc", "")); + EXPECT_EQ(": 123", Format("", "123")); + EXPECT_EQ("abc: 123", Format("abc", "123")); + EXPECT_EQ("abc: 0.9", Format("abc", "0123456789")); + EXPECT_EQ("ab...j: ", Format("abcdefghij", "123")); + EXPECT_EQ("ab...j: ", Format("abcdefghij", "0123456789")); } } // namespace printing |