Skip to content

Commit

Permalink
PDF Printing: embed browser user agent string in PDF metadata.
Browse files Browse the repository at this point in the history
Review URL: https://codereview.chromium.org/1397333003

Cr-Commit-Position: refs/heads/master@{#358186}
  • Loading branch information
HalCanary authored and Commit bot committed Nov 6, 2015
1 parent 738f72f commit 73b63fd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
5 changes: 5 additions & 0 deletions chrome/renderer/chrome_content_renderer_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@
#endif

#if defined(ENABLE_PRINTING)
#include "chrome/common/chrome_content_client.h"
#include "chrome/renderer/printing/chrome_print_web_view_helper_delegate.h"
#include "components/printing/renderer/print_web_view_helper.h"
#include "printing/print_settings.h"
#endif

#if defined(ENABLE_PRINT_PREVIEW)
Expand Down Expand Up @@ -355,6 +357,9 @@ ChromeContentRendererClient::ChromeContentRendererClient() {
for (size_t i = 0; i < arraysize(kPredefinedAllowedCompositorOrigins); ++i)
allowed_compositor_origins_.insert(kPredefinedAllowedCompositorOrigins[i]);
#endif
#if defined(ENABLE_PRINTING)
printing::SetAgent(GetUserAgent());
#endif
}

ChromeContentRendererClient::~ChromeContentRendererClient() {
Expand Down
7 changes: 6 additions & 1 deletion printing/pdf_metafile_skia.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "base/numerics/safe_conversions.h"
#include "base/posix/eintr_wrapper.h"
#include "base/time/time.h"
#include "printing/print_settings.h"
#include "skia/ext/refptr.h"
#include "third_party/skia/include/core/SkData.h"
#include "third_party/skia/include/core/SkDocument.h"
Expand Down Expand Up @@ -163,7 +164,11 @@ bool PdfMetafileSkia::FinishDocument() {
pdf_doc->endPage();
}
SkTArray<SkDocument::Attribute> info;
info.emplace_back(SkString("Creator"), SkString("Chromium"));
const std::string& user_agent = GetAgent();
info.emplace_back(SkString("Creator"),
user_agent.empty()
? SkString("Chromium")
: SkString(user_agent.c_str(), user_agent.size()));
SkTime::DateTime now = TimeToSkTime(base::Time::Now());
pdf_doc->setMetadata(info, &now, &now);
if (!pdf_doc->close())
Expand Down
11 changes: 11 additions & 0 deletions printing/print_settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@
#include "printing/print_settings.h"

#include "base/atomic_sequence_num.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "printing/print_job_constants.h"
#include "printing/units.h"

namespace printing {

base::LazyInstance<std::string> g_user_agent;

void SetAgent(const std::string& user_agent) {
g_user_agent.Get() = user_agent;
}

const std::string& GetAgent() {
return g_user_agent.Get();
}

#if defined(USE_CUPS)
void GetColorModelForMode(
int color_mode, std::string* color_setting_name, std::string* color_value) {
Expand Down
5 changes: 5 additions & 0 deletions printing/print_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ PRINTING_EXPORT void GetColorModelForMode(int color_mode,
std::string* color_value);
#endif

// Inform the printing system that it may embed this user-agent string
// in its output's metadata.
PRINTING_EXPORT void SetAgent(const std::string& user_agent);
PRINTING_EXPORT const std::string& GetAgent();

// OS-independent print settings.
class PRINTING_EXPORT PrintSettings {
public:
Expand Down

0 comments on commit 73b63fd

Please sign in to comment.