Hook up chrome.printerProvider.onPrintRequested
[chromium-blink-merge.git] / extensions / browser / api / printer_provider / printer_provider_api.h
blob5d2a28cc68e7dc4695551a91af82226edb6dceb7
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_
8 #include <map>
9 #include <set>
10 #include <string>
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"
18 namespace base {
19 class DictionaryValue;
20 class ListValue;
23 namespace content {
24 class BrowserContext;
27 namespace extensions {
28 class Extension;
29 class PrinterProviderInternalAPI;
32 namespace extensions {
34 // Implements chrome.printerProvider API events.
35 class PrinterProviderAPI : public BrowserContextKeyedAPI,
36 public PrinterProviderInternalAPIObserver {
37 public:
38 // Status returned by chrome.printerProvider.onPrintRequested event.
39 enum PrintError {
40 PRINT_ERROR_NONE,
41 PRINT_ERROR_FAILED,
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.
48 struct PrintJob {
49 PrintJob();
50 ~PrintJob();
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>*
70 GetFactoryInstance();
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
78 // must not be null.
79 void DispatchPrintRequested(const std::string& extension_id,
80 const PrintJob& job,
81 const PrintCallback& callback);
83 private:
84 friend class BrowserContextKeyedAPIFactory<PrinterProviderAPI>;
86 // Keeps track of pending chrome.printerProvider.ontPrintRequested requests
87 // for an extension.
88 class PendingPrintRequests {
89 public:
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);
101 private:
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:
110 void OnPrintResult(
111 const Extension* extension,
112 int request_id,
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);
125 template <>
126 void BrowserContextKeyedAPIFactory<
127 PrinterProviderAPI>::DeclareFactoryDependencies();
129 } // namespace extensions
131 #endif // EXTENSIONS_BROWSER_API_PRINTER_PROVIDER_PRINTER_PROVIDER_API_H_