Skip to content

Commit

Permalink
Include user email into document title before spooling
Browse files Browse the repository at this point in the history
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}
  • Loading branch information
vitalybuka authored and Commit bot committed Jul 7, 2015
1 parent 5ded2f6 commit 27e401c
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 20 deletions.
1 change: 1 addition & 0 deletions chrome/common/cloud_print/cloud_print_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const char kFileUrlValue[] = "fileUrl";
const char kPrinterListValue[] = "printers";
const char kJobListValue[] = "jobs";
const char kTitleValue[] = "title";
const char kOwnerValue[] = "ownerId";
const char kPrinterCapsHashValue[] = "capsHash";
const char kTagsValue[] = "tags";
const char kXMPPJidValue[] = "xmpp_jid";
Expand Down
1 change: 1 addition & 0 deletions chrome/common/cloud_print/cloud_print_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ extern const char kFileUrlValue[];
extern const char kPrinterListValue[];
extern const char kJobListValue[];
extern const char kTitleValue[];
extern const char kOwnerValue[];
extern const char kPrinterCapsHashValue[];
extern const char kTagsValue[];
extern const char kXMPPJidValue[];
Expand Down
15 changes: 9 additions & 6 deletions chrome/service/cloud_print/printer_job_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -792,12 +792,15 @@ void PrinterJobHandler::DoPrint(const JobDetails& job_details,
DCHECK(job_spooler_.get());
if (!job_spooler_.get())
return;
base::string16 document_name = printing::SimplifyDocumentTitle(
base::UTF8ToUTF16(job_details.job_title_));
if (document_name.empty()) {
document_name = printing::SimplifyDocumentTitle(
l10n_util::GetStringUTF16(IDS_DEFAULT_PRINT_DOCUMENT_TITLE));
}

base::string16 document_name =
job_details.job_title_.empty()
? l10n_util::GetStringUTF16(IDS_DEFAULT_PRINT_DOCUMENT_TITLE)
: base::UTF8ToUTF16(job_details.job_title_);

document_name = printing::FormatDocumentTitleWithOwner(
base::UTF8ToUTF16(job_details.job_owner_), document_name);

UMA_HISTOGRAM_ENUMERATION("CloudPrint.JobHandlerEvent",
JOB_HANDLER_START_SPOOLING, JOB_HANDLER_MAX);
spooling_start_time_ = base::Time::Now();
Expand Down
2 changes: 2 additions & 0 deletions chrome/service/cloud_print/printer_job_queue_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ JobDetails::~JobDetails() {}
void JobDetails::Clear() {
job_id_.clear();
job_title_.clear();
job_owner_.clear();
print_ticket_.clear();
print_ticket_mime_type_.clear();
print_data_mime_type_.clear();
Expand Down Expand Up @@ -58,6 +59,7 @@ void PrinterJobQueueHandler::ConstructJobDetailsFromJson(

job_data->GetString(kIdValue, &job_details->job_id_);
job_data->GetString(kTitleValue, &job_details->job_title_);
job_data->GetString(kOwnerValue, &job_details->job_owner_);

job_data->GetString(kTicketUrlValue, &job_details->print_ticket_url_);
job_data->GetString(kFileUrlValue, &job_details->print_data_url_);
Expand Down
1 change: 1 addition & 0 deletions chrome/service/cloud_print/printer_job_queue_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct JobDetails {

std::string job_id_;
std::string job_title_;
std::string job_owner_;

std::string print_ticket_url_;
std::string print_data_url_;
Expand Down
44 changes: 39 additions & 5 deletions printing/printing_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions printing/printing_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
38 changes: 29 additions & 9 deletions printing/printing_utils_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 27e401c

Please sign in to comment.