chromium/printing/printed_document.cc

// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "printing/printed_document.h"

#include <algorithm>
#include <set>
#include <string>
#include <utility>
#include <vector>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/i18n/file_util_icu.h"
#include "base/i18n/time_formatting.h"
#include "base/json/json_writer.h"
#include "base/lazy_instance.h"
#include "base/memory/ref_counted_memory.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "base/values.h"
#include "build/build_config.h"
#include "printing/metafile.h"
#include "printing/page_number.h"
#include "printing/print_settings_conversion.h"
#include "printing/printing_context.h"
#include "printing/units.h"
#include "ui/gfx/font.h"
#include "ui/gfx/text_elider.h"

#if BUILDFLAG(IS_WIN)
#include "printing/printed_page_win.h"
#endif

namespace printing {

namespace {

base::LazyInstance<base::FilePath>::Leaky g_debug_dump_info =;

#if BUILDFLAG(IS_WIN)
void DebugDumpPageTask(const std::u16string& doc_name,
                       const PrintedPage* page) {
  DCHECK(PrintedDocument::HasDebugDumpPath());

  static constexpr base::FilePath::CharType kExtension[] =
      FILE_PATH_LITERAL(".emf");

  std::u16string name = doc_name;
  name += base::ASCIIToUTF16(base::StringPrintf("_%04d", page->page_number()));
  base::FilePath path = PrintedDocument::CreateDebugDumpPath(name, kExtension);
  base::File file(path,
                  base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
  page->metafile()->SaveTo(&file);
}
#endif  // BUILDFLAG(IS_WIN)

void DebugDumpTask(const std::u16string& doc_name,
                   const MetafilePlayer* metafile) {}

void DebugDumpDataTask(const std::u16string& doc_name,
                       const base::FilePath::StringType& extension,
                       const base::RefCountedMemory* data) {}

void DebugDumpSettings(const std::u16string& doc_name,
                       const PrintSettings& settings) {}

}  // namespace

PrintedDocument::PrintedDocument(std::unique_ptr<PrintSettings> settings,
                                 const std::u16string& name,
                                 int cookie)
    :{}

PrintedDocument::~PrintedDocument() = default;

#if BUILDFLAG(IS_WIN)
void PrintedDocument::SetConvertingPdf() {
  base::AutoLock lock(lock_);
  mutable_.converting_pdf_ = true;
}

void PrintedDocument::SetPage(uint32_t page_number,
                              std::unique_ptr<MetafilePlayer> metafile,
                              float shrink,
                              const gfx::Size& page_size,
                              const gfx::Rect& page_content_rect) {
  // Notice the page_number + 1, the reason is that this is the value that will
  // be shown. Users dislike 0-based counting.
  auto page = base::MakeRefCounted<PrintedPage>(
      page_number + 1, std::move(metafile), page_size, page_content_rect);
  page->set_shrink_factor(shrink);
  {
    base::AutoLock lock(lock_);
    mutable_.pages_[page_number] = page;
  }

  if (HasDebugDumpPath()) {
    base::ThreadPool::PostTask(
        FROM_HERE, {base::TaskPriority::BEST_EFFORT, base::MayBlock()},
        base::BindOnce(&DebugDumpPageTask, name(), base::RetainedRef(page)));
  }
}

scoped_refptr<PrintedPage> PrintedDocument::GetPage(uint32_t page_number) {
  scoped_refptr<PrintedPage> page;
  {
    base::AutoLock lock(lock_);
    PrintedPages::const_iterator it = mutable_.pages_.find(page_number);
    if (it != mutable_.pages_.end())
      page = it->second;
  }
  return page;
}

void PrintedDocument::RemovePage(const PrintedPage* page) {
  base::AutoLock lock(lock_);
  PrintedPages::const_iterator it =
      mutable_.pages_.find(page->page_number() - 1);
  CHECK(it != mutable_.pages_.end());
  DCHECK_EQ(page, it->second.get());
  mutable_.pages_.erase(it);
}
#endif  // BUILDFLAG(IS_WIN)

void PrintedDocument::SetDocument(std::unique_ptr<MetafilePlayer> metafile) {}

const MetafilePlayer* PrintedDocument::GetMetafile() {}

mojom::ResultCode PrintedDocument::RenderPrintedDocument(
    PrintingContext* context) {}

bool PrintedDocument::IsComplete() const {}

void PrintedDocument::set_page_count(uint32_t max_page) {}

uint32_t PrintedDocument::page_count() const {}

uint32_t PrintedDocument::expected_page_count() const {}

// static
void PrintedDocument::SetDebugDumpPath(const base::FilePath& debug_dump_path) {}

// static
bool PrintedDocument::HasDebugDumpPath() {}

// static
base::FilePath PrintedDocument::CreateDebugDumpPath(
    const std::u16string& document_name,
    const base::FilePath::StringType& extension) {}

void PrintedDocument::DebugDumpData(
    const base::RefCountedMemory* data,
    const base::FilePath::StringType& extension) {}

PrintedDocument::Mutable::Mutable() = default;

PrintedDocument::Mutable::~Mutable() = default;

PrintedDocument::Immutable::Immutable(std::unique_ptr<PrintSettings> settings,
                                      const std::u16string& name,
                                      int cookie)
    :{}

PrintedDocument::Immutable::~Immutable() = default;

}  // namespace printing