Commit 1378bc38 authored by tbarzic's avatar tbarzic Committed by Commit bot

Make PrinterProviderAPI a pure interface

This makes PrinterProviderAPI a pure interface, with the
implementation hidden in printer_provider.cc, and with a custom browser
context keyed service factory. This will make it easier to inject a
fake API implementation in tests (e.g. for ExtensionPrinterHandler).

BUG=461114
TEST=extensions_browsertests --gtest_filter=PrinterProviderAPI

Review URL: https://codereview.chromium.org/922833004

Cr-Commit-Position: refs/heads/master@{#318124}
parent 05b47b88
...@@ -19,6 +19,8 @@ ...@@ -19,6 +19,8 @@
#include "components/cloud_devices/common/cloud_device_description.h" #include "components/cloud_devices/common/cloud_device_description.h"
#include "components/cloud_devices/common/printer_description.h" #include "components/cloud_devices/common/printer_description.h"
#include "extensions/browser/api/printer_provider/printer_provider_api.h" #include "extensions/browser/api/printer_provider/printer_provider_api.h"
#include "extensions/browser/api/printer_provider/printer_provider_api_factory.h"
#include "extensions/browser/api/printer_provider/printer_provider_print_job.h"
#include "printing/pdf_render_settings.h" #include "printing/pdf_render_settings.h"
#include "printing/pwg_raster_settings.h" #include "printing/pwg_raster_settings.h"
...@@ -87,8 +89,8 @@ void ExtensionPrinterHandler::Reset() { ...@@ -87,8 +89,8 @@ void ExtensionPrinterHandler::Reset() {
void ExtensionPrinterHandler::StartGetPrinters( void ExtensionPrinterHandler::StartGetPrinters(
const PrinterHandler::GetPrintersCallback& callback) { const PrinterHandler::GetPrintersCallback& callback) {
extensions::PrinterProviderAPI::GetFactoryInstance() extensions::PrinterProviderAPIFactory::GetInstance()
->Get(browser_context_) ->GetForBrowserContext(browser_context_)
->DispatchGetPrintersRequested( ->DispatchGetPrintersRequested(
base::Bind(&ExtensionPrinterHandler::WrapGetPrintersCallback, base::Bind(&ExtensionPrinterHandler::WrapGetPrintersCallback,
weak_ptr_factory_.GetWeakPtr(), callback)); weak_ptr_factory_.GetWeakPtr(), callback));
...@@ -97,8 +99,8 @@ void ExtensionPrinterHandler::StartGetPrinters( ...@@ -97,8 +99,8 @@ void ExtensionPrinterHandler::StartGetPrinters(
void ExtensionPrinterHandler::StartGetCapability( void ExtensionPrinterHandler::StartGetCapability(
const std::string& destination_id, const std::string& destination_id,
const PrinterHandler::GetCapabilityCallback& callback) { const PrinterHandler::GetCapabilityCallback& callback) {
extensions::PrinterProviderAPI::GetFactoryInstance() extensions::PrinterProviderAPIFactory::GetInstance()
->Get(browser_context_) ->GetForBrowserContext(browser_context_)
->DispatchGetCapabilityRequested( ->DispatchGetCapabilityRequested(
destination_id, destination_id,
base::Bind(&ExtensionPrinterHandler::WrapGetCapabilityCallback, base::Bind(&ExtensionPrinterHandler::WrapGetCapabilityCallback,
...@@ -112,8 +114,8 @@ void ExtensionPrinterHandler::StartPrint( ...@@ -112,8 +114,8 @@ void ExtensionPrinterHandler::StartPrint(
const gfx::Size& page_size, const gfx::Size& page_size,
const scoped_refptr<base::RefCountedMemory>& print_data, const scoped_refptr<base::RefCountedMemory>& print_data,
const PrinterHandler::PrintCallback& callback) { const PrinterHandler::PrintCallback& callback) {
scoped_ptr<extensions::PrinterProviderAPI::PrintJob> print_job( scoped_ptr<extensions::PrinterProviderPrintJob> print_job(
new extensions::PrinterProviderAPI::PrintJob()); new extensions::PrinterProviderPrintJob());
print_job->printer_id = destination_id; print_job->printer_id = destination_id;
print_job->ticket_json = ticket_json; print_job->ticket_json = ticket_json;
...@@ -163,7 +165,7 @@ void ExtensionPrinterHandler::ConvertToPWGRaster( ...@@ -163,7 +165,7 @@ void ExtensionPrinterHandler::ConvertToPWGRaster(
void ExtensionPrinterHandler::DispatchPrintJob( void ExtensionPrinterHandler::DispatchPrintJob(
const PrinterHandler::PrintCallback& callback, const PrinterHandler::PrintCallback& callback,
scoped_ptr<extensions::PrinterProviderAPI::PrintJob> print_job, scoped_ptr<extensions::PrinterProviderPrintJob> print_job,
const scoped_refptr<base::RefCountedMemory>& print_data) { const scoped_refptr<base::RefCountedMemory>& print_data) {
if (!print_data) { if (!print_data) {
WrapPrintCallback(callback, false, kInvalidDataPrintError); WrapPrintCallback(callback, false, kInvalidDataPrintError);
...@@ -172,8 +174,8 @@ void ExtensionPrinterHandler::DispatchPrintJob( ...@@ -172,8 +174,8 @@ void ExtensionPrinterHandler::DispatchPrintJob(
print_job->document_bytes = print_data; print_job->document_bytes = print_data;
extensions::PrinterProviderAPI::GetFactoryInstance() extensions::PrinterProviderAPIFactory::GetInstance()
->Get(browser_context_) ->GetForBrowserContext(browser_context_)
->DispatchPrintRequested( ->DispatchPrintRequested(
*print_job, base::Bind(&ExtensionPrinterHandler::WrapPrintCallback, *print_job, base::Bind(&ExtensionPrinterHandler::WrapPrintCallback,
weak_ptr_factory_.GetWeakPtr(), callback)); weak_ptr_factory_.GetWeakPtr(), callback));
......
...@@ -73,11 +73,9 @@ class ExtensionPrinterHandler : public PrinterHandler { ...@@ -73,11 +73,9 @@ class ExtensionPrinterHandler : public PrinterHandler {
const RefCountedMemoryCallback& callback); const RefCountedMemoryCallback& callback);
// Sets print job document data and dispatches it using printerProvider API. // Sets print job document data and dispatches it using printerProvider API.
// TODO(tbarzic): Move PrinterProvider::PrintJob to it's own file so it can
// be forward-declared.
void DispatchPrintJob( void DispatchPrintJob(
const PrinterHandler::PrintCallback& callback, const PrinterHandler::PrintCallback& callback,
scoped_ptr<extensions::PrinterProviderAPI::PrintJob> print_job, scoped_ptr<extensions::PrinterProviderPrintJob> print_job,
const scoped_refptr<base::RefCountedMemory>& data); const scoped_refptr<base::RefCountedMemory>& data);
// Methods used as wrappers to callbacks for extensions::PrinterProviderAPI // Methods used as wrappers to callbacks for extensions::PrinterProviderAPI
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/browser/api/printer_provider/printer_provider_api_factory.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "extensions/browser/api/printer_provider/printer_provider_api.h"
#include "extensions/browser/api/printer_provider_internal/printer_provider_internal_api.h"
#include "extensions/browser/extension_registry_factory.h"
#include "extensions/browser/extensions_browser_client.h"
namespace {
static base::LazyInstance<extensions::PrinterProviderAPIFactory> g_api_factory =
LAZY_INSTANCE_INITIALIZER;
} // namespace
namespace extensions {
// static
PrinterProviderAPIFactory* PrinterProviderAPIFactory::GetInstance() {
return g_api_factory.Pointer();
}
PrinterProviderAPI* PrinterProviderAPIFactory::GetForBrowserContext(
content::BrowserContext* context) {
return static_cast<PrinterProviderAPI*>(
GetServiceForBrowserContext(context, true));
}
PrinterProviderAPIFactory::PrinterProviderAPIFactory()
: BrowserContextKeyedServiceFactory(
"PrinterProviderAPI",
BrowserContextDependencyManager::GetInstance()) {
DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
DependsOn(PrinterProviderInternalAPI::GetFactoryInstance());
DependsOn(ExtensionRegistryFactory::GetInstance());
}
PrinterProviderAPIFactory::~PrinterProviderAPIFactory() {
}
KeyedService* PrinterProviderAPIFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
return PrinterProviderAPI::Create(context);
}
content::BrowserContext* PrinterProviderAPIFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
return ExtensionsBrowserClient::Get()->GetOriginalContext(context);
}
} // namespace extensions
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef EXTENSIONS_BROWSER_API_PRINTER_PROVIDER_PRINTER_PROVIDER_API_FACTORY_H_
#define EXTENSIONS_BROWSER_API_PRINTER_PROVIDER_PRINTER_PROVIDER_API_FACTORY_H_
#include "base/lazy_instance.h"
#include "base/macros.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
class KeyedService;
namespace content {
class BrowserContext;
}
namespace extensions {
class PrinterProviderAPI;
}
namespace extensions {
// Factory for PrinterProviderAPI.
class PrinterProviderAPIFactory : public BrowserContextKeyedServiceFactory {
public:
static PrinterProviderAPIFactory* GetInstance();
PrinterProviderAPI* GetForBrowserContext(content::BrowserContext* context);
private:
friend struct base::DefaultLazyInstanceTraits<PrinterProviderAPIFactory>;
PrinterProviderAPIFactory();
~PrinterProviderAPIFactory() override;
// BrowserContextKeyedServiceFactory implementation:
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const override;
DISALLOW_COPY_AND_ASSIGN(PrinterProviderAPIFactory);
};
} // namespace extensions
#endif // EXTENSIONS_BROWSER_API_PRINTER_PROVIDER_PRINTER_PROVIDER_API_FACTORY_H_
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "extensions/browser/api/printer_provider/printer_provider_api.h" #include "extensions/browser/api/printer_provider/printer_provider_api.h"
#include "extensions/browser/api/printer_provider/printer_provider_api_factory.h"
#include "extensions/browser/api/printer_provider/printer_provider_print_job.h"
#include "extensions/browser/extension_registry.h" #include "extensions/browser/extension_registry.h"
#include "extensions/common/extension.h" #include "extensions/common/extension.h"
#include "extensions/shell/test/shell_apitest.h" #include "extensions/shell/test/shell_apitest.h"
...@@ -18,6 +20,7 @@ ...@@ -18,6 +20,7 @@
namespace { namespace {
using extensions::PrinterProviderAPI; using extensions::PrinterProviderAPI;
using extensions::PrinterProviderAPIFactory;
// Callback for PrinterProviderAPI::DispatchGetPrintersRequested calls. // Callback for PrinterProviderAPI::DispatchGetPrintersRequested calls.
// It appends items in |printers| to |*printers_out|. If |done| is set, it runs // It appends items in |printers| to |*printers_out|. If |done| is set, it runs
...@@ -69,14 +72,14 @@ class PrinterProviderApiTest : public extensions::ShellApiTest { ...@@ -69,14 +72,14 @@ class PrinterProviderApiTest : public extensions::ShellApiTest {
void StartGetPrintersRequest( void StartGetPrintersRequest(
const PrinterProviderAPI::GetPrintersCallback& callback) { const PrinterProviderAPI::GetPrintersCallback& callback) {
PrinterProviderAPI::GetFactoryInstance() PrinterProviderAPIFactory::GetInstance()
->Get(browser_context()) ->GetForBrowserContext(browser_context())
->DispatchGetPrintersRequested(callback); ->DispatchGetPrintersRequested(callback);
} }
void StartPrintRequest(const std::string& extension_id, void StartPrintRequest(const std::string& extension_id,
const PrinterProviderAPI::PrintCallback& callback) { const PrinterProviderAPI::PrintCallback& callback) {
PrinterProviderAPI::PrintJob job; extensions::PrinterProviderPrintJob job;
job.printer_id = extension_id + ":printer_id"; job.printer_id = extension_id + ":printer_id";
job.ticket_json = "{}"; job.ticket_json = "{}";
job.content_type = "content_type"; job.content_type = "content_type";
...@@ -84,16 +87,16 @@ class PrinterProviderApiTest : public extensions::ShellApiTest { ...@@ -84,16 +87,16 @@ class PrinterProviderApiTest : public extensions::ShellApiTest {
job.document_bytes = job.document_bytes =
new base::RefCountedBytes(kDocumentBytes, arraysize(kDocumentBytes)); new base::RefCountedBytes(kDocumentBytes, arraysize(kDocumentBytes));
PrinterProviderAPI::GetFactoryInstance() PrinterProviderAPIFactory::GetInstance()
->Get(browser_context()) ->GetForBrowserContext(browser_context())
->DispatchPrintRequested(job, callback); ->DispatchPrintRequested(job, callback);
} }
void StartCapabilityRequest( void StartCapabilityRequest(
const std::string& extension_id, const std::string& extension_id,
const PrinterProviderAPI::GetCapabilityCallback& callback) { const PrinterProviderAPI::GetCapabilityCallback& callback) {
PrinterProviderAPI::GetFactoryInstance() PrinterProviderAPIFactory::GetInstance()
->Get(browser_context()) ->GetForBrowserContext(browser_context())
->DispatchGetCapabilityRequested(extension_id + ":printer_id", ->DispatchGetCapabilityRequested(extension_id + ":printer_id",
callback); callback);
} }
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/browser/api/printer_provider/printer_provider_print_job.h"
namespace extensions {
PrinterProviderPrintJob::PrinterProviderPrintJob() {
}
PrinterProviderPrintJob::~PrinterProviderPrintJob() {
}
} // namespace extensions
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef EXTENSIONS_BROWSER_API_PRINTER_PROVIDER_PRINTER_PROVIDER_PRINT_JOB_H_
#define EXTENSIONS_BROWSER_API_PRINTER_PROVIDER_PRINTER_PROVIDER_PRINT_JOB_H_
#include <string>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/ref_counted_memory.h"
namespace extensions {
// Struct describing print job that should be forwarded to an extension via
// chrome.printerProvider.onPrintRequested event.
struct PrinterProviderPrintJob {
PrinterProviderPrintJob();
~PrinterProviderPrintJob();
// The id of the printer that should handle the print job. The id is
// formatted as <extension_id>:<printer_id>, where <extension_id> is the
// id of the extension that manages the printer, and <printer_id> is
// the the printer's id within the extension (as reported via
// chrome.printerProvider.onGetPrintersRequested event callback).
std::string printer_id;
// The print job ticket.
std::string ticket_json;
// Content type of the document that should be printed.
std::string content_type;
// The document data that should be printed.
scoped_refptr<base::RefCountedMemory> document_bytes;
private:
DISALLOW_COPY_AND_ASSIGN(PrinterProviderPrintJob);
};
} // namespace extensions
#endif // EXTENSIONS_BROWSER_API_PRINTER_PROVIDER_PRINTER_PROVIDER_PRINT_JOB_H_
...@@ -387,6 +387,10 @@ ...@@ -387,6 +387,10 @@
'browser/api/power/power_api_manager.h', 'browser/api/power/power_api_manager.h',
'browser/api/printer_provider/printer_provider_api.cc', 'browser/api/printer_provider/printer_provider_api.cc',
'browser/api/printer_provider/printer_provider_api.h', 'browser/api/printer_provider/printer_provider_api.h',
'browser/api/printer_provider/printer_provider_api_factory.cc',
'browser/api/printer_provider/printer_provider_api_factory.h',
'browser/api/printer_provider/printer_provider_print_job.cc',
'browser/api/printer_provider/printer_provider_print_job.h',
'browser/api/printer_provider_internal/printer_provider_internal_api.cc', 'browser/api/printer_provider_internal/printer_provider_internal_api.cc',
'browser/api/printer_provider_internal/printer_provider_internal_api.h', 'browser/api/printer_provider_internal/printer_provider_internal_api.h',
'browser/api/printer_provider_internal/printer_provider_internal_api_observer.h', 'browser/api/printer_provider_internal/printer_provider_internal_api_observer.h',
......
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