cefclient: Simplify ClientPrintHandlerGtk for single browser usage (see issue #2196)

This commit is contained in:
Marshall Greenblatt 2021-03-30 09:49:17 -07:00
parent 7876a2f321
commit 19ac738b77
2 changed files with 12 additions and 39 deletions

View File

@ -574,23 +574,13 @@ struct ClientPrintHandlerGtk::PrintHandler {
ClientPrintHandlerGtk::ClientPrintHandlerGtk() {} ClientPrintHandlerGtk::ClientPrintHandlerGtk() {}
ClientPrintHandlerGtk::~ClientPrintHandlerGtk() { ClientPrintHandlerGtk::~ClientPrintHandlerGtk() {
DCHECK(print_handler_map_.empty()); DCHECK(!print_handler_);
} }
void ClientPrintHandlerGtk::OnPrintStart(CefRefPtr<CefBrowser> browser) { void ClientPrintHandlerGtk::OnPrintStart(CefRefPtr<CefBrowser> browser) {
CEF_REQUIRE_UI_THREAD(); CEF_REQUIRE_UI_THREAD();
DCHECK(!print_handler_);
const int browser_id = browser->GetIdentifier(); print_handler_ = std::make_unique<PrintHandler>(browser);
#ifndef _NDEBUG
// Print handler should not already exist for the browser.
PrintHandlerMap::const_iterator it = print_handler_map_.find(browser_id);
DCHECK(it == print_handler_map_.end());
#endif
// Create a new print handler.
PrintHandler* ph = new PrintHandler(browser);
print_handler_map_.insert(std::make_pair(browser_id, ph));
} }
void ClientPrintHandlerGtk::OnPrintSettings( void ClientPrintHandlerGtk::OnPrintSettings(
@ -599,7 +589,7 @@ void ClientPrintHandlerGtk::OnPrintSettings(
bool get_defaults) { bool get_defaults) {
CEF_REQUIRE_UI_THREAD(); CEF_REQUIRE_UI_THREAD();
GetPrintHandler(browser)->OnPrintSettings(settings, get_defaults); print_handler_->OnPrintSettings(settings, get_defaults);
} }
bool ClientPrintHandlerGtk::OnPrintDialog( bool ClientPrintHandlerGtk::OnPrintDialog(
@ -608,10 +598,10 @@ bool ClientPrintHandlerGtk::OnPrintDialog(
CefRefPtr<CefPrintDialogCallback> callback) { CefRefPtr<CefPrintDialogCallback> callback) {
CEF_REQUIRE_UI_THREAD(); CEF_REQUIRE_UI_THREAD();
PrintHandler* print_handler = GetPrintHandler(browser); GetWindowAndContinue(
GetWindowAndContinue(browser, base::Bind(&PrintHandler::OnPrintDialog, browser, base::Bind(&PrintHandler::OnPrintDialog,
base::Unretained(print_handler), base::Unretained(print_handler_.get()), has_selection,
has_selection, callback)); callback));
return true; return true;
} }
@ -622,19 +612,14 @@ bool ClientPrintHandlerGtk::OnPrintJob(
CefRefPtr<CefPrintJobCallback> callback) { CefRefPtr<CefPrintJobCallback> callback) {
CEF_REQUIRE_UI_THREAD(); CEF_REQUIRE_UI_THREAD();
return GetPrintHandler(browser)->OnPrintJob(document_name, pdf_file_path, return print_handler_->OnPrintJob(document_name, pdf_file_path, callback);
callback);
} }
void ClientPrintHandlerGtk::OnPrintReset(CefRefPtr<CefBrowser> browser) { void ClientPrintHandlerGtk::OnPrintReset(CefRefPtr<CefBrowser> browser) {
CEF_REQUIRE_UI_THREAD(); CEF_REQUIRE_UI_THREAD();
// Delete the print handler. // Delete the print handler.
PrintHandlerMap::iterator it = print_handler_.reset();
print_handler_map_.find(browser->GetIdentifier());
DCHECK(it != print_handler_map_.end());
delete it->second;
print_handler_map_.erase(it);
} }
CefSize ClientPrintHandlerGtk::GetPdfPaperSize(CefRefPtr<CefBrowser> browser, CefSize ClientPrintHandlerGtk::GetPdfPaperSize(CefRefPtr<CefBrowser> browser,
@ -653,12 +638,4 @@ CefSize ClientPrintHandlerGtk::GetPdfPaperSize(CefRefPtr<CefBrowser> browser,
return CefSize(width * device_units_per_inch, height * device_units_per_inch); return CefSize(width * device_units_per_inch, height * device_units_per_inch);
} }
ClientPrintHandlerGtk::PrintHandler* ClientPrintHandlerGtk::GetPrintHandler(
CefRefPtr<CefBrowser> browser) {
PrintHandlerMap::const_iterator it =
print_handler_map_.find(browser->GetIdentifier());
DCHECK(it != print_handler_map_.end());
return it->second;
}
} // namespace client } // namespace client

View File

@ -7,7 +7,7 @@
#define CEF_TESTS_CEFCLIENT_BROWSER_PRINT_HANDLER_GTK_H_ #define CEF_TESTS_CEFCLIENT_BROWSER_PRINT_HANDLER_GTK_H_
#pragma once #pragma once
#include <map> #include <memory>
#include "include/cef_print_handler.h" #include "include/cef_print_handler.h"
@ -37,11 +37,7 @@ class ClientPrintHandlerGtk : public CefPrintHandler {
private: private:
// Print handler. // Print handler.
struct PrintHandler; struct PrintHandler;
PrintHandler* GetPrintHandler(CefRefPtr<CefBrowser> browser); std::unique_ptr<PrintHandler> print_handler_;
// Map of browser ID to print handler.
typedef std::map<int, PrintHandler*> PrintHandlerMap;
PrintHandlerMap print_handler_map_;
IMPLEMENT_REFCOUNTING(ClientPrintHandlerGtk); IMPLEMENT_REFCOUNTING(ClientPrintHandlerGtk);
DISALLOW_COPY_AND_ASSIGN(ClientPrintHandlerGtk); DISALLOW_COPY_AND_ASSIGN(ClientPrintHandlerGtk);