Commit bdd6381b authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Add PrintedDocument::HasDebugDumpPath().

Unlike the current checks for the debug dump path, HasDebugDumpPath()
is truly read only and won't create the base::FilePath when accessing
the |g_debug_dump_info| base::LazyInstance.

Update code to use HasDebugDumpPath() instead of accessing
|g_debug_dump_info| directly, and only do that when getting / setting
the path value.

Change-Id: I200664a9cb70289883627c8dc7f85c6f02edd45b
Reviewed-on: https://chromium-review.googlesource.com/821370Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#523356}
parent bbd67f76
...@@ -1708,7 +1708,8 @@ int ChromeBrowserMainParts::PreMainMessageLoopRunImpl() { ...@@ -1708,7 +1708,8 @@ int ChromeBrowserMainParts::PreMainMessageLoopRunImpl() {
if (parsed_command_line().HasSwitch(switches::kDebugPrint)) { if (parsed_command_line().HasSwitch(switches::kDebugPrint)) {
base::FilePath path = base::FilePath path =
parsed_command_line().GetSwitchValuePath(switches::kDebugPrint); parsed_command_line().GetSwitchValuePath(switches::kDebugPrint);
printing::PrintedDocument::set_debug_dump_path(path); if (!path.empty())
printing::PrintedDocument::SetDebugDumpPath(path);
} }
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) && !defined(OFFICIAL_BUILD) #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) && !defined(OFFICIAL_BUILD)
......
...@@ -219,7 +219,8 @@ void PrintViewManagerBase::UpdateForPrintedPage( ...@@ -219,7 +219,8 @@ void PrintViewManagerBase::UpdateForPrintedPage(
reinterpret_cast<const unsigned char*>(shared_buf->memory()), reinterpret_cast<const unsigned char*>(shared_buf->memory()),
shared_buf->mapped_size())); shared_buf->mapped_size()));
document->DebugDumpData(bytes.get(), FILE_PATH_LITERAL(".pdf")); if (PrintedDocument::HasDebugDumpPath())
document->DebugDumpData(bytes.get(), FILE_PATH_LITERAL(".pdf"));
const auto& settings = document->settings(); const auto& settings = document->settings();
if (settings.printer_is_textonly()) { if (settings.printer_is_textonly()) {
......
...@@ -50,7 +50,7 @@ void DebugDumpPageTask(const base::string16& doc_name, ...@@ -50,7 +50,7 @@ void DebugDumpPageTask(const base::string16& doc_name,
const PrintedPage* page) { const PrintedPage* page) {
base::AssertBlockingAllowed(); base::AssertBlockingAllowed();
DCHECK(!g_debug_dump_info.Get().empty()); DCHECK(PrintedDocument::HasDebugDumpPath());
static constexpr base::FilePath::CharType kExtension[] = static constexpr base::FilePath::CharType kExtension[] =
FILE_PATH_LITERAL(".emf"); FILE_PATH_LITERAL(".emf");
...@@ -67,7 +67,7 @@ void DebugDumpTask(const base::string16& doc_name, ...@@ -67,7 +67,7 @@ void DebugDumpTask(const base::string16& doc_name,
const MetafilePlayer* metafile) { const MetafilePlayer* metafile) {
base::AssertBlockingAllowed(); base::AssertBlockingAllowed();
DCHECK(!g_debug_dump_info.Get().empty()); DCHECK(PrintedDocument::HasDebugDumpPath());
static constexpr base::FilePath::CharType kExtension[] = static constexpr base::FilePath::CharType kExtension[] =
FILE_PATH_LITERAL(".pdf"); FILE_PATH_LITERAL(".pdf");
...@@ -124,7 +124,7 @@ PrintedDocument::PrintedDocument(const PrintSettings& settings, ...@@ -124,7 +124,7 @@ PrintedDocument::PrintedDocument(const PrintSettings& settings,
} }
} }
if (!g_debug_dump_info.Get().empty()) if (HasDebugDumpPath())
DebugDumpSettings(name, settings); DebugDumpSettings(name, settings);
} }
...@@ -146,7 +146,7 @@ void PrintedDocument::SetPage(int page_number, ...@@ -146,7 +146,7 @@ void PrintedDocument::SetPage(int page_number,
mutable_.pages_[page_number] = page; mutable_.pages_[page_number] = page;
} }
if (!g_debug_dump_info.Get().empty()) { if (HasDebugDumpPath()) {
base::PostTaskWithTraits( base::PostTaskWithTraits(
FROM_HERE, {base::TaskPriority::BACKGROUND, base::MayBlock()}, FROM_HERE, {base::TaskPriority::BACKGROUND, base::MayBlock()},
base::BindOnce(&DebugDumpPageTask, name(), base::RetainedRef(page))); base::BindOnce(&DebugDumpPageTask, name(), base::RetainedRef(page)));
...@@ -177,7 +177,7 @@ void PrintedDocument::SetDocument(std::unique_ptr<MetafilePlayer> metafile, ...@@ -177,7 +177,7 @@ void PrintedDocument::SetDocument(std::unique_ptr<MetafilePlayer> metafile,
#endif #endif
} }
if (!g_debug_dump_info.Get().empty()) { if (HasDebugDumpPath()) {
base::PostTaskWithTraits( base::PostTaskWithTraits(
FROM_HERE, {base::TaskPriority::BACKGROUND, base::MayBlock()}, FROM_HERE, {base::TaskPriority::BACKGROUND, base::MayBlock()},
base::BindOnce(&DebugDumpTask, name(), mutable_.metafile_.get())); base::BindOnce(&DebugDumpTask, name(), mutable_.metafile_.get()));
...@@ -235,16 +235,22 @@ int PrintedDocument::expected_page_count() const { ...@@ -235,16 +235,22 @@ int PrintedDocument::expected_page_count() const {
return mutable_.expected_page_count_; return mutable_.expected_page_count_;
} }
void PrintedDocument::set_debug_dump_path( // static
const base::FilePath& debug_dump_path) { void PrintedDocument::SetDebugDumpPath(const base::FilePath& debug_dump_path) {
DCHECK(!debug_dump_path.empty());
g_debug_dump_info.Get() = debug_dump_path; g_debug_dump_info.Get() = debug_dump_path;
} }
// static
bool PrintedDocument::HasDebugDumpPath() {
return g_debug_dump_info.IsCreated();
}
// static
base::FilePath PrintedDocument::CreateDebugDumpPath( base::FilePath PrintedDocument::CreateDebugDumpPath(
const base::string16& document_name, const base::string16& document_name,
const base::FilePath::StringType& extension) { const base::FilePath::StringType& extension) {
if (!g_debug_dump_info.Get().empty()) DCHECK(HasDebugDumpPath());
return base::FilePath();
// Create a filename. // Create a filename.
base::string16 filename; base::string16 filename;
...@@ -259,15 +265,15 @@ base::FilePath PrintedDocument::CreateDebugDumpPath( ...@@ -259,15 +265,15 @@ base::FilePath PrintedDocument::CreateDebugDumpPath(
system_filename = base::UTF16ToUTF8(filename); system_filename = base::UTF16ToUTF8(filename);
#endif // OS_WIN #endif // OS_WIN
base::i18n::ReplaceIllegalCharactersInPath(&system_filename, '_'); base::i18n::ReplaceIllegalCharactersInPath(&system_filename, '_');
return g_debug_dump_info.Get().Append(system_filename).AddExtension( const auto& dump_path = g_debug_dump_info.Get();
extension); DCHECK(!dump_path.empty());
return dump_path.Append(system_filename).AddExtension(extension);
} }
void PrintedDocument::DebugDumpData( void PrintedDocument::DebugDumpData(
const base::RefCountedMemory* data, const base::RefCountedMemory* data,
const base::FilePath::StringType& extension) { const base::FilePath::StringType& extension) {
if (g_debug_dump_info.Get().empty()) DCHECK(HasDebugDumpPath());
return;
base::PostTaskWithTraits(FROM_HERE, base::PostTaskWithTraits(FROM_HERE,
{base::TaskPriority::BACKGROUND, base::MayBlock()}, {base::TaskPriority::BACKGROUND, base::MayBlock()},
base::BindOnce(&DebugDumpDataTask, name(), extension, base::BindOnce(&DebugDumpDataTask, name(), extension,
......
...@@ -101,18 +101,22 @@ class PRINTING_EXPORT PrintedDocument ...@@ -101,18 +101,22 @@ class PRINTING_EXPORT PrintedDocument
const base::string16& name() const { return immutable_.name_; } const base::string16& name() const { return immutable_.name_; }
int cookie() const { return immutable_.cookie_; } int cookie() const { return immutable_.cookie_; }
// Sets a path where to dump printing output files for debugging. If never set // Sets a path where to dump printing output files for debugging. If never
// no files are generated. // set, no files are generated. |debug_dump_path| must not be empty.
static void set_debug_dump_path(const base::FilePath& debug_dump_path); static void SetDebugDumpPath(const base::FilePath& debug_dump_path);
// Returns true if SetDebugDumpPath() has been called.
static bool HasDebugDumpPath();
// Creates debug file name from given |document_name| and |extension|. // Creates debug file name from given |document_name| and |extension|.
// |extension| should include '.', example ".pdf" // |extension| should include the leading dot. e.g. ".pdf"
// Returns empty |base::FilePath| if debug dumps is not enabled. // Should only be called when debug dumps are enabled.
static base::FilePath CreateDebugDumpPath( static base::FilePath CreateDebugDumpPath(
const base::string16& document_name, const base::string16& document_name,
const base::FilePath::StringType& extension); const base::FilePath::StringType& extension);
// Dump data on blocking task runner if debug dumps enabled. // Dump data on blocking task runner.
// Should only be called when debug dumps are enabled.
void DebugDumpData(const base::RefCountedMemory* data, void DebugDumpData(const base::RefCountedMemory* data,
const base::FilePath::StringType& extension); const base::FilePath::StringType& extension);
......
...@@ -263,11 +263,12 @@ PrintingContext::Result PrintingContextWin::NewDocument( ...@@ -263,11 +263,12 @@ PrintingContext::Result PrintingContextWin::NewDocument(
di.lpszDocName = document_name.c_str(); di.lpszDocName = document_name.c_str();
// Is there a debug dump directory specified? If so, force to print to a file. // Is there a debug dump directory specified? If so, force to print to a file.
base::string16 debug_dump_path = if (PrintedDocument::HasDebugDumpPath()) {
PrintedDocument::CreateDebugDumpPath(document_name, base::FilePath debug_dump_path = PrintedDocument::CreateDebugDumpPath(
FILE_PATH_LITERAL(".prn")).value(); document_name, FILE_PATH_LITERAL(".prn"));
if (!debug_dump_path.empty()) if (!debug_dump_path.empty())
di.lpszOutput = debug_dump_path.c_str(); di.lpszOutput = debug_dump_path.value().c_str();
}
// No message loop running in unit tests. // No message loop running in unit tests.
DCHECK(!base::MessageLoop::current() || DCHECK(!base::MessageLoop::current() ||
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment