1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef EXTENSIONS_BROWSER_API_PRINTER_PROVIDER_PRINTER_PROVIDER_API_H_
6 #define EXTENSIONS_BROWSER_API_PRINTER_PROVIDER_PRINTER_PROVIDER_API_H_
12 #include "base/callback.h"
13 #include "base/macros.h"
14 #include "base/scoped_observer.h"
15 #include "extensions/browser/api/printer_provider_internal/printer_provider_internal_api_observer.h"
16 #include "extensions/browser/browser_context_keyed_api_factory.h"
19 class DictionaryValue
;
27 namespace extensions
{
29 class PrinterProviderInternalAPI
;
32 namespace extensions
{
34 // Implements chrome.printerProvider API events.
35 class PrinterProviderAPI
: public BrowserContextKeyedAPI
,
36 public PrinterProviderInternalAPIObserver
{
38 // Status returned by chrome.printerProvider.onPrintRequested event.
42 PRINT_ERROR_INVALID_TICKET
,
43 PRINT_ERROR_INVALID_DATA
46 // Struct describing print job that should be forwarded to an extension via
47 // chrome.printerProvider.onPrintRequested event.
52 // The id of the printer that should handle the print job. This identifies
53 // a printer within an extension and is provided by the extension via
54 // chrome.printerProvider.onGetPrintersRequested event callback.
55 std::string printer_id
;
57 // The print job ticket.
58 std::string ticket_json
;
60 // Content type of the document that should be printed.
61 std::string content_type
;
63 // The document data that should be printed.
64 std::string document_bytes
;
67 using PrintCallback
= base::Callback
<void(PrintError
)>;
69 static BrowserContextKeyedAPIFactory
<PrinterProviderAPI
>*
72 explicit PrinterProviderAPI(content::BrowserContext
* browser_context
);
73 ~PrinterProviderAPI() override
;
75 // It dispatches chrome.printerProvider.onPrintRequested event to the
76 // extension with id |extension_id| with the provided print job.
77 // |callback| is passed the print status returned by the extension, and it
79 void DispatchPrintRequested(const std::string
& extension_id
,
81 const PrintCallback
& callback
);
84 friend class BrowserContextKeyedAPIFactory
<PrinterProviderAPI
>;
86 // Keeps track of pending chrome.printerProvider.ontPrintRequested requests
88 class PendingPrintRequests
{
90 PendingPrintRequests();
91 ~PendingPrintRequests();
93 // Adds a new request to the set. Only information needed is the callback
94 // associated with the request. Returns the id assigned to the request.
95 int Add(const PrintCallback
& callback
);
97 // Completes the request with the provided request id. It runs the request
98 // callback and removes the request from the set.
99 bool Complete(int request_id
, PrintError result
);
102 int last_request_id_
;
103 std::map
<int, PrintCallback
> pending_requests_
;
106 // BrowserContextKeyedAPI implementation.
107 static const char* service_name() { return "PrinterProvider"; }
109 // PrinterProviderInternalAPIObserver implementation:
111 const Extension
* extension
,
113 core_api::printer_provider_internal::PrintError error
) override
;
115 content::BrowserContext
* browser_context_
;
117 std::map
<std::string
, PendingPrintRequests
> pending_print_requests_
;
119 ScopedObserver
<PrinterProviderInternalAPI
, PrinterProviderInternalAPIObserver
>
120 internal_api_observer_
;
122 DISALLOW_COPY_AND_ASSIGN(PrinterProviderAPI
);
126 void BrowserContextKeyedAPIFactory
<
127 PrinterProviderAPI
>::DeclareFactoryDependencies();
129 } // namespace extensions
131 #endif // EXTENSIONS_BROWSER_API_PRINTER_PROVIDER_PRINTER_PROVIDER_API_H_