OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef EXTENSIONS_BROWSER_API_PRINTER_PROVIDER_PRINTER_PROVIDER_API_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_PRINTER_PROVIDER_PRINTER_PROVIDER_API_H_ |
6 #define EXTENSIONS_BROWSER_API_PRINTER_PROVIDER_PRINTER_PROVIDER_API_H_ | 6 #define EXTENSIONS_BROWSER_API_PRINTER_PROVIDER_PRINTER_PROVIDER_API_H_ |
7 | 7 |
8 #include <map> | |
9 #include <set> | |
10 #include <string> | 8 #include <string> |
11 | 9 |
12 #include "base/callback.h" | 10 #include "base/callback.h" |
13 #include "base/macros.h" | 11 #include "components/keyed_service/core/keyed_service.h" |
14 #include "base/memory/ref_counted_memory.h" | |
15 #include "base/scoped_observer.h" | |
16 #include "extensions/browser/api/printer_provider_internal/printer_provider_inte
rnal_api_observer.h" | |
17 #include "extensions/browser/browser_context_keyed_api_factory.h" | |
18 #include "extensions/browser/extension_registry_observer.h" | |
19 | 12 |
20 namespace base { | 13 namespace base { |
21 class DictionaryValue; | 14 class DictionaryValue; |
22 class ListValue; | 15 class ListValue; |
23 } | 16 } |
24 | 17 |
25 namespace content { | 18 namespace content { |
26 class BrowserContext; | 19 class BrowserContext; |
27 } | 20 } |
28 | 21 |
29 namespace extensions { | 22 namespace extensions { |
30 class Extension; | 23 struct PrinterProviderPrintJob; |
31 class ExtensionRegistry; | |
32 class PrinterProviderInternalAPI; | |
33 } | 24 } |
34 | 25 |
35 namespace extensions { | 26 namespace extensions { |
36 | 27 |
37 // Implements chrome.printerProvider API events. | 28 // Implements chrome.printerProvider API events. |
38 class PrinterProviderAPI : public BrowserContextKeyedAPI, | 29 class PrinterProviderAPI : public KeyedService { |
39 public PrinterProviderInternalAPIObserver, | |
40 public ExtensionRegistryObserver { | |
41 public: | 30 public: |
42 // Struct describing print job that should be forwarded to an extension via | |
43 // chrome.printerProvider.onPrintRequested event. | |
44 struct PrintJob { | |
45 PrintJob(); | |
46 ~PrintJob(); | |
47 | |
48 // The id of the printer that should handle the print job. The id is | |
49 // formatted as <extension_id>:<printer_id>, where <extension_id> is the | |
50 // id of the extension that manages the printer, and <printer_id> is | |
51 // the the printer's id within the extension (as reported via | |
52 // chrome.printerProvider.onGetPrintersRequested event callback). | |
53 std::string printer_id; | |
54 | |
55 // The print job ticket. | |
56 std::string ticket_json; | |
57 | |
58 // Content type of the document that should be printed. | |
59 std::string content_type; | |
60 | |
61 // The document data that should be printed. | |
62 scoped_refptr<base::RefCountedMemory> document_bytes; | |
63 }; | |
64 | |
65 using GetPrintersCallback = | 31 using GetPrintersCallback = |
66 base::Callback<void(const base::ListValue& printers, bool done)>; | 32 base::Callback<void(const base::ListValue& printers, bool done)>; |
67 using GetCapabilityCallback = | 33 using GetCapabilityCallback = |
68 base::Callback<void(const base::DictionaryValue& capability)>; | 34 base::Callback<void(const base::DictionaryValue& capability)>; |
69 using PrintCallback = | 35 using PrintCallback = |
70 base::Callback<void(bool success, const std::string& error)>; | 36 base::Callback<void(bool success, const std::string& error)>; |
71 | 37 |
72 static BrowserContextKeyedAPIFactory<PrinterProviderAPI>* | 38 static PrinterProviderAPI* Create(content::BrowserContext* context); |
73 GetFactoryInstance(); | |
74 | 39 |
| 40 // Returns generic error string for print request. |
75 static std::string GetDefaultPrintError(); | 41 static std::string GetDefaultPrintError(); |
76 | 42 |
77 explicit PrinterProviderAPI(content::BrowserContext* browser_context); | 43 // The API currently cannot handle very large files, so the document size that |
78 ~PrinterProviderAPI() override; | 44 // may be sent to an extension is restricted. |
| 45 // TODO(tbarzic): Fix the API to support huge documents. |
| 46 static const int kMaxDocumentSize = 50 * 1000 * 1000; |
79 | 47 |
80 // Requests list of supported printers from extensions implementing | 48 ~PrinterProviderAPI() override {} |
81 // chrome.printerProvider API. It dispatches | 49 |
82 // chrome.printerProvider.onGetPrintersRequested event. The callback is | 50 virtual void DispatchGetPrintersRequested( |
83 // called once for every extension handling the event with a list of its | 51 const GetPrintersCallback& callback) = 0; |
84 // supported printers. The printer values reported by an extension are | |
85 // added "extensionId" property that is set to the ID of the extension | |
86 // returning the list. | |
87 // Note that the "id" property of printer values reported by an extension are | |
88 // rewriten as "<extension_id>:<id>" to ensure they are unique across | |
89 // different extensions. | |
90 void DispatchGetPrintersRequested(const GetPrintersCallback& callback); | |
91 | 52 |
92 // Requests printer capability for a printer with id |printer_id|. | 53 // Requests printer capability for a printer with id |printer_id|. |
93 // |printer_id| should be one of the printer ids reported by |GetPrinters| | 54 // |printer_id| should be one of the printer ids reported by |GetPrinters| |
94 // callback. | 55 // callback. |
95 // It dispatches chrome.printerProvider.onGetCapabilityRequested event | 56 // It dispatches chrome.printerProvider.onGetCapabilityRequested event |
96 // to the extension that manages the printer (which can be determined from | 57 // to the extension that manages the printer (which can be determined from |
97 // |printer_id| value). | 58 // |printer_id| value). |
98 // |callback| is passed a dictionary value containing printer capabilities as | 59 // |callback| is passed a dictionary value containing printer capabilities as |
99 // reported by the extension. | 60 // reported by the extension. |
100 void DispatchGetCapabilityRequested(const std::string& printer_id, | 61 virtual void DispatchGetCapabilityRequested( |
101 const GetCapabilityCallback& callback); | 62 const std::string& printer_id, |
| 63 const GetCapabilityCallback& callback) = 0; |
102 | 64 |
103 // It dispatches chrome.printerProvider.onPrintRequested event with the | 65 // It dispatches chrome.printerProvider.onPrintRequested event with the |
104 // provided print job. The event is dispatched only to the extension that | 66 // provided print job. The event is dispatched only to the extension that |
105 // manages printer with id |job.printer_id|. | 67 // manages printer with id |job.printer_id|. |
106 // |callback| is passed the print status returned by the extension, and it | 68 // |callback| is passed the print status returned by the extension, and it |
107 // must not be null. | 69 // must not be null. |
108 void DispatchPrintRequested(const PrintJob& job, | 70 virtual void DispatchPrintRequested(const PrinterProviderPrintJob& job, |
109 const PrintCallback& callback); | 71 const PrintCallback& callback) = 0; |
110 | |
111 // The API currently cannot handle very large files, so the document size that | |
112 // may be sent to an extension is restricted. | |
113 // TODO(tbarzic): Fix the API to support huge documents. | |
114 static const int kMaxDocumentSize = 50 * 1000 * 1000; | |
115 | |
116 private: | |
117 friend class BrowserContextKeyedAPIFactory<PrinterProviderAPI>; | |
118 | |
119 // Holds information about a pending onGetPrintersRequested request; | |
120 // in particular, the list of extensions to which the event was dispatched but | |
121 // which haven't yet responded, and the |GetPrinters| callback associated with | |
122 // the event. | |
123 class GetPrintersRequest { | |
124 public: | |
125 explicit GetPrintersRequest(const GetPrintersCallback& callback); | |
126 ~GetPrintersRequest(); | |
127 | |
128 // Adds an extension id to the list of the extensions that need to respond | |
129 // to the event. | |
130 void AddSource(const std::string& extension_id); | |
131 | |
132 // Whether all extensions have responded to the event. | |
133 bool IsDone() const; | |
134 | |
135 // Runs the callback for an extension and removes the extension from the | |
136 // list of extensions that still have to respond to the event. | |
137 void ReportForExtension(const std::string& extension_id, | |
138 const base::ListValue& printers); | |
139 | |
140 private: | |
141 // Callback reporting event result for an extension. Called once for each | |
142 // extension. | |
143 GetPrintersCallback callback_; | |
144 | |
145 // The list of extensions that still have to respond to the event. | |
146 std::set<std::string> extensions_; | |
147 }; | |
148 | |
149 // Keeps track of pending chrome.printerProvider.onGetPrintersRequested | |
150 // requests. | |
151 class PendingGetPrintersRequests { | |
152 public: | |
153 PendingGetPrintersRequests(); | |
154 ~PendingGetPrintersRequests(); | |
155 | |
156 // Adds a new request to the set of pending requests. Returns the id | |
157 // assigned to the request. | |
158 int Add(const GetPrintersCallback& callback); | |
159 | |
160 // Completes a request for an extension. It runs the request callback with | |
161 // values reported by the extension. | |
162 bool CompleteForExtension(const std::string& extension_id, | |
163 int request_id, | |
164 const base::ListValue& result); | |
165 | |
166 // Runs callbacks for the extension for all requests that are waiting for a | |
167 // response from the extension with the provided extension id. Callbacks are | |
168 // called as if the extension reported empty set of printers. | |
169 void FailAllForExtension(const std::string& extension_id); | |
170 | |
171 // Adds an extension id to the list of the extensions that need to respond | |
172 // to the event. | |
173 bool AddSource(int request_id, const std::string& extension_id); | |
174 | |
175 private: | |
176 int last_request_id_; | |
177 std::map<int, GetPrintersRequest> pending_requests_; | |
178 | |
179 DISALLOW_COPY_AND_ASSIGN(PendingGetPrintersRequests); | |
180 }; | |
181 | |
182 // Keeps track of pending chrome.printerProvider.onGetCapabilityRequested | |
183 // requests for an extension. | |
184 class PendingGetCapabilityRequests { | |
185 public: | |
186 PendingGetCapabilityRequests(); | |
187 ~PendingGetCapabilityRequests(); | |
188 | |
189 // Adds a new request to the set. Only information needed is the callback | |
190 // associated with the request. Returns the id assigned to the request. | |
191 int Add(const GetCapabilityCallback& callback); | |
192 | |
193 // Completes the request with the provided request id. It runs the request | |
194 // callback and removes the request from the set. | |
195 bool Complete(int request_id, const base::DictionaryValue& result); | |
196 | |
197 // Runs all pending callbacks with empty capability value and clears the | |
198 // set of pending requests. | |
199 void FailAll(); | |
200 | |
201 private: | |
202 int last_request_id_; | |
203 std::map<int, GetCapabilityCallback> pending_requests_; | |
204 }; | |
205 | |
206 // Keeps track of pending chrome.printerProvider.ontPrintRequested requests | |
207 // for an extension. | |
208 class PendingPrintRequests { | |
209 public: | |
210 PendingPrintRequests(); | |
211 ~PendingPrintRequests(); | |
212 | |
213 // Adds a new request to the set. Only information needed is the callback | |
214 // associated with the request. Returns the id assigned to the request. | |
215 int Add(const PrintCallback& callback); | |
216 | |
217 // Completes the request with the provided request id. It runs the request | |
218 // callback and removes the request from the set. | |
219 bool Complete(int request_id, bool success, const std::string& result); | |
220 | |
221 // Runs all pending callbacks with ERROR_FAILED and clears the set of | |
222 // pending requests. | |
223 void FailAll(); | |
224 | |
225 private: | |
226 int last_request_id_; | |
227 std::map<int, PrintCallback> pending_requests_; | |
228 }; | |
229 | |
230 // BrowserContextKeyedAPI implementation. | |
231 static const bool kServiceRedirectedInIncognito = true; | |
232 static const char* service_name() { return "PrinterProvider"; } | |
233 | |
234 // PrinterProviderInternalAPIObserver implementation: | |
235 void OnGetPrintersResult( | |
236 const Extension* extension, | |
237 int request_id, | |
238 const PrinterProviderInternalAPIObserver::PrinterInfoVector& result) | |
239 override; | |
240 void OnGetCapabilityResult(const Extension* extension, | |
241 int request_id, | |
242 const base::DictionaryValue& result) override; | |
243 void OnPrintResult( | |
244 const Extension* extension, | |
245 int request_id, | |
246 core_api::printer_provider_internal::PrintError error) override; | |
247 | |
248 // ExtensionRegistryObserver implementation: | |
249 void OnExtensionUnloaded(content::BrowserContext* browser_context, | |
250 const Extension* extension, | |
251 UnloadedExtensionInfo::Reason reason) override; | |
252 | |
253 // Called before chrome.printerProvider.onGetPrintersRequested event is | |
254 // dispatched to an extension. It returns whether the extension is interested | |
255 // in the event. If the extension listens to the event, it's added to the set | |
256 // of |request| sources. |request| is |GetPrintersRequest| object associated | |
257 // with the event. | |
258 bool WillRequestPrinters(int request_id, | |
259 content::BrowserContext* browser_context, | |
260 const Extension* extension, | |
261 base::ListValue* args); | |
262 | |
263 content::BrowserContext* browser_context_; | |
264 | |
265 PendingGetPrintersRequests pending_get_printers_requests_; | |
266 | |
267 std::map<std::string, PendingPrintRequests> pending_print_requests_; | |
268 | |
269 std::map<std::string, PendingGetCapabilityRequests> | |
270 pending_capability_requests_; | |
271 | |
272 ScopedObserver<PrinterProviderInternalAPI, PrinterProviderInternalAPIObserver> | |
273 internal_api_observer_; | |
274 | |
275 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> | |
276 extension_registry_observer_; | |
277 | |
278 DISALLOW_COPY_AND_ASSIGN(PrinterProviderAPI); | |
279 }; | 72 }; |
280 | 73 |
281 template <> | |
282 void BrowserContextKeyedAPIFactory< | |
283 PrinterProviderAPI>::DeclareFactoryDependencies(); | |
284 | |
285 } // namespace extensions | 74 } // namespace extensions |
286 | 75 |
287 #endif // EXTENSIONS_BROWSER_API_PRINTER_PROVIDER_PRINTER_PROVIDER_API_H_ | 76 #endif // EXTENSIONS_BROWSER_API_PRINTER_PROVIDER_PRINTER_PROVIDER_API_H_ |
OLD | NEW |