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.
4 
5 #ifndef EXTENSIONS_BROWSER_API_PRINTER_PROVIDER_INTERNAL_PRINTER_PROVIDER_INTERNAL_API_H_
6 #define EXTENSIONS_BROWSER_API_PRINTER_PROVIDER_INTERNAL_PRINTER_PROVIDER_INTERNAL_API_H_
7 
8 #include <string>
9 
10 #include "base/macros.h"
11 #include "base/observer_list.h"
12 #include "extensions/browser/api/printer_provider_internal/printer_provider_internal_api_observer.h"
13 #include "extensions/browser/browser_context_keyed_api_factory.h"
14 #include "extensions/browser/extension_function.h"
15 #include "extensions/common/api/printer_provider_internal.h"
16 
17 namespace base {
18 class DictionaryValue;
19 class RefCountedMemory;
20 }
21 
22 namespace content {
23 class BlobHandle;
24 class BrowserContext;
25 }
26 
27 namespace extensions {
28 class Extension;
29 }
30 
31 namespace extensions {
32 
33 // Internal API instance. Primarily used to enable observers to watch for when
34 // printerProviderInternal API functions are called.
35 class PrinterProviderInternalAPI : public BrowserContextKeyedAPI {
36  public:
37   static BrowserContextKeyedAPIFactory<PrinterProviderInternalAPI>*
38   GetFactoryInstance();
39 
40   explicit PrinterProviderInternalAPI(content::BrowserContext* browser_context);
41   ~PrinterProviderInternalAPI() override;
42 
43   void AddObserver(PrinterProviderInternalAPIObserver* observer);
44   void RemoveObserver(PrinterProviderInternalAPIObserver* observer);
45 
46  private:
47   friend class BrowserContextKeyedAPIFactory<PrinterProviderInternalAPI>;
48   friend class PrinterProviderInternalReportPrintersFunction;
49   friend class PrinterProviderInternalReportPrinterCapabilityFunction;
50   friend class PrinterProviderInternalReportPrintResultFunction;
51   friend class PrinterProviderInternalReportUsbPrinterInfoFunction;
52 
53   // BrowserContextKeyedAPI implementation.
54   static const bool kServiceRedirectedInIncognito = true;
service_name()55   static const char* service_name() { return "PrinterProviderInternal"; }
56 
57   // Notifies observers that a printerProvider.onGetPrintersRequested callback
58   // has been called. Called from
59   // |PrinterProviderInternalReportPrintersFunction|.
60   void NotifyGetPrintersResult(
61       const Extension* extension,
62       int request_id,
63       const PrinterProviderInternalAPIObserver::PrinterInfoVector& printers);
64 
65   // Notifies observers that a printerProvider.onGetCapabilityRequested callback
66   // has been called. Called from
67   // |PrinterProviderInternalReportPrinterCapabilityFunction|.
68   void NotifyGetCapabilityResult(const Extension* extension,
69                                  int request_id,
70                                  const base::DictionaryValue& capability);
71 
72   // Notifies observers that a printerProvider.onPrintRequested callback has
73   // been called. Called from
74   // |PrinterProviderInternalReportPrintResultFunction|.
75   void NotifyPrintResult(const Extension* extension,
76                          int request_id,
77                          api::printer_provider_internal::PrintError error);
78 
79   // Notifies observers that a printerProvider.onGetUsbPrinterInfoRequested
80   // callback has been called. Called from
81   // |PrinterProviderInternalReportUsbPrinterInfoFunction|.
82   void NotifyGetUsbPrinterInfoResult(
83       const Extension* extension,
84       int request_id,
85       const api::printer_provider::PrinterInfo* printer_info);
86 
87   base::ObserverList<PrinterProviderInternalAPIObserver>::Unchecked observers_;
88 
89   DISALLOW_COPY_AND_ASSIGN(PrinterProviderInternalAPI);
90 };
91 
92 class PrinterProviderInternalReportPrintResultFunction
93     : public ExtensionFunction {
94  public:
95   PrinterProviderInternalReportPrintResultFunction();
96 
97  protected:
98   ~PrinterProviderInternalReportPrintResultFunction() override;
99 
100   ExtensionFunction::ResponseAction Run() override;
101 
102  private:
103   DECLARE_EXTENSION_FUNCTION("printerProviderInternal.reportPrintResult",
104                              PRINTERPROVIDERINTERNAL_REPORTPRINTRESULT)
105 
106   DISALLOW_COPY_AND_ASSIGN(PrinterProviderInternalReportPrintResultFunction);
107 };
108 
109 class PrinterProviderInternalReportPrinterCapabilityFunction
110     : public ExtensionFunction {
111  public:
112   PrinterProviderInternalReportPrinterCapabilityFunction();
113 
114  protected:
115   ~PrinterProviderInternalReportPrinterCapabilityFunction() override;
116 
117   ExtensionFunction::ResponseAction Run() override;
118 
119  private:
120   DECLARE_EXTENSION_FUNCTION("printerProviderInternal.reportPrinterCapability",
121                              PRINTERPROVIDERINTERNAL_REPORTPRINTERCAPABILITY)
122 
123   DISALLOW_COPY_AND_ASSIGN(
124       PrinterProviderInternalReportPrinterCapabilityFunction);
125 };
126 
127 class PrinterProviderInternalReportPrintersFunction : public ExtensionFunction {
128  public:
129   PrinterProviderInternalReportPrintersFunction();
130 
131  protected:
132   ~PrinterProviderInternalReportPrintersFunction() override;
133   ExtensionFunction::ResponseAction Run() override;
134 
135  private:
136   DECLARE_EXTENSION_FUNCTION("printerProviderInternal.reportPrinters",
137                              PRINTERPROVIDERINTERNAL_REPORTPRINTERS)
138 
139   DISALLOW_COPY_AND_ASSIGN(PrinterProviderInternalReportPrintersFunction);
140 };
141 
142 class PrinterProviderInternalGetPrintDataFunction : public ExtensionFunction {
143  public:
144   PrinterProviderInternalGetPrintDataFunction();
145 
146  protected:
147   ~PrinterProviderInternalGetPrintDataFunction() override;
148   ExtensionFunction::ResponseAction Run() override;
149 
150  private:
151   void OnBlob(const std::string& type,
152               int size,
153               const scoped_refptr<base::RefCountedMemory>& data,
154               std::unique_ptr<content::BlobHandle> blob);
155   DECLARE_EXTENSION_FUNCTION("printerProviderInternal.getPrintData",
156                              PRINTERPROVIDERINTERNAL_GETPRINTDATA)
157 
158   DISALLOW_COPY_AND_ASSIGN(PrinterProviderInternalGetPrintDataFunction);
159 };
160 
161 class PrinterProviderInternalReportUsbPrinterInfoFunction
162     : public ExtensionFunction {
163  public:
164   PrinterProviderInternalReportUsbPrinterInfoFunction();
165 
166  protected:
167   ~PrinterProviderInternalReportUsbPrinterInfoFunction() override;
168   ExtensionFunction::ResponseAction Run() override;
169 
170  private:
171   DECLARE_EXTENSION_FUNCTION("printerProviderInternal.reportUsbPrinterInfo",
172                              PRINTERPROVIDERINTERNAL_REPORTUSBPRINTERINFO)
173 
174   DISALLOW_COPY_AND_ASSIGN(PrinterProviderInternalReportUsbPrinterInfoFunction);
175 };
176 
177 }  // namespace extensions
178 
179 #endif  // EXTENSIONS_BROWSER_API_PRINTER_PROVIDER_INTERNAL_PRINTER_PROVIDER_INTERNAL_API_H_
180