Unregister from GCM when the only GCM app is removed
[chromium-blink-merge.git] / extensions / browser / api / printer_provider / printer_provider_api.h
blob636fcd36607418652db5ea88d7381eccdd2c9b62
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"
17 #include "extensions/browser/extension_registry_observer.h"
19 namespace base {
20 class DictionaryValue;
21 class ListValue;
24 namespace content {
25 class BrowserContext;
28 namespace extensions {
29 class Extension;
30 class ExtensionRegistry;
31 class PrinterProviderInternalAPI;
34 namespace extensions {
36 // Implements chrome.printerProvider API events.
37 class PrinterProviderAPI : public BrowserContextKeyedAPI,
38 public PrinterProviderInternalAPIObserver,
39 public ExtensionRegistryObserver {
40 public:
41 // Status returned by chrome.printerProvider.onPrintRequested event.
42 enum PrintError {
43 PRINT_ERROR_NONE,
44 PRINT_ERROR_FAILED,
45 PRINT_ERROR_INVALID_TICKET,
46 PRINT_ERROR_INVALID_DATA
49 // Struct describing print job that should be forwarded to an extension via
50 // chrome.printerProvider.onPrintRequested event.
51 struct PrintJob {
52 PrintJob();
53 ~PrintJob();
55 // The id of the printer that should handle the print job. The id is
56 // formatted as <extension_id>:<printer_id>, where <extension_id> is the
57 // id of the extension that manages the printer, and <printer_id> is
58 // the the printer's id within the extension (as reported via
59 // chrome.printerProvider.onGetPrintersRequested event callback).
60 std::string printer_id;
62 // The print job ticket.
63 std::string ticket_json;
65 // Content type of the document that should be printed.
66 std::string content_type;
68 // The document data that should be printed.
69 std::string document_bytes;
72 using GetPrintersCallback =
73 base::Callback<void(const base::ListValue& printers, bool done)>;
74 using GetCapabilityCallback =
75 base::Callback<void(const base::DictionaryValue&)>;
76 using PrintCallback = base::Callback<void(PrintError)>;
78 static BrowserContextKeyedAPIFactory<PrinterProviderAPI>*
79 GetFactoryInstance();
81 explicit PrinterProviderAPI(content::BrowserContext* browser_context);
82 ~PrinterProviderAPI() override;
84 // Requests list of supported printers from extensions implementing
85 // chrome.printerProvider API. It dispatches
86 // chrome.printerProvider.onGetPrintersRequested event. The callback is
87 // called once for every extension handling the event with a list of its
88 // supported printers. The printer values reported by an extension are
89 // added "extensionId" property that is set to the ID of the extension
90 // returning the list.
91 // Note that the "id" property of printer values reported by an extension are
92 // rewriten as "<extension_id>:<id>" to ensure they are unique across
93 // different extensions.
94 void DispatchGetPrintersRequested(const GetPrintersCallback& callback);
96 // Requests printer capability for a printer with id |printer_id|.
97 // |printer_id| should be one of the printer ids reported by |GetPrinters|
98 // callback.
99 // It dispatches chrome.printerProvider.onGetCapabilityRequested event
100 // to the extension that manages the printer (which can be determined from
101 // |printer_id| value).
102 // |callback| is passed a dictionary value containing printer capabilities as
103 // reported by the extension.
104 void DispatchGetCapabilityRequested(const std::string& printer_id,
105 const GetCapabilityCallback& callback);
107 // It dispatches chrome.printerProvider.onPrintRequested event with the
108 // provided print job. The event is dispatched only to the extension that
109 // manages printer with id |job.printer_id|.
110 // |callback| is passed the print status returned by the extension, and it
111 // must not be null.
112 void DispatchPrintRequested(const PrintJob& job,
113 const PrintCallback& callback);
115 private:
116 friend class BrowserContextKeyedAPIFactory<PrinterProviderAPI>;
118 // Holds information about a pending onGetPrintersRequested request;
119 // in particular, the list of extensions to which the event was dispatched but
120 // which haven't yet responded, and the |GetPrinters| callback associated with
121 // the event.
122 class GetPrintersRequest {
123 public:
124 explicit GetPrintersRequest(const GetPrintersCallback& callback);
125 ~GetPrintersRequest();
127 // Adds an extension id to the list of the extensions that need to respond
128 // to the event.
129 void AddSource(const std::string& extension_id);
131 // Whether all extensions have responded to the event.
132 bool IsDone() const;
134 // Runs the callback for an extension and removes the extension from the
135 // list of extensions that still have to respond to the event.
136 void ReportForExtension(const std::string& extension_id,
137 const base::ListValue& printers);
139 private:
140 // Callback reporting event result for an extension. Called once for each
141 // extension.
142 GetPrintersCallback callback_;
144 // The list of extensions that still have to respond to the event.
145 std::set<std::string> extensions_;
147 DISALLOW_COPY_AND_ASSIGN(GetPrintersRequest);
150 // Keeps track of pending chrome.printerProvider.onGetPrintersRequested
151 // requests.
152 class PendingGetPrintersRequests {
153 public:
154 PendingGetPrintersRequests();
155 ~PendingGetPrintersRequests();
157 // Adds a new request to the set of pending requests. Returns the id
158 // assigned to the request.
159 int Add(scoped_ptr<GetPrintersRequest> request);
161 // Completes a request for an extension. It runs the request callback with
162 // values reported by the extension.
163 bool CompleteForExtension(const std::string& extension_id,
164 int request_id,
165 const base::ListValue& result);
167 // Runs callbacks for the extension for all requests that are waiting for a
168 // response from the extension with the provided extension id. Callbacks are
169 // called as if the extension reported empty set of printers.
170 void FailAllForExtension(const std::string& extension_id);
172 private:
173 int last_request_id_;
174 std::map<int, GetPrintersRequest*> pending_requests_;
176 DISALLOW_COPY_AND_ASSIGN(PendingGetPrintersRequests);
179 // Keeps track of pending chrome.printerProvider.onGetCapabilityRequested
180 // requests for an extension.
181 class PendingGetCapabilityRequests {
182 public:
183 PendingGetCapabilityRequests();
184 ~PendingGetCapabilityRequests();
186 // Adds a new request to the set. Only information needed is the callback
187 // associated with the request. Returns the id assigned to the request.
188 int Add(const GetCapabilityCallback& callback);
190 // Completes the request with the provided request id. It runs the request
191 // callback and removes the request from the set.
192 bool Complete(int request_id, const base::DictionaryValue& result);
194 // Runs all pending callbacks with empty capability value and clears the
195 // set of pending requests.
196 void FailAll();
198 private:
199 int last_request_id_;
200 std::map<int, GetCapabilityCallback> pending_requests_;
203 // Keeps track of pending chrome.printerProvider.ontPrintRequested requests
204 // for an extension.
205 class PendingPrintRequests {
206 public:
207 PendingPrintRequests();
208 ~PendingPrintRequests();
210 // Adds a new request to the set. Only information needed is the callback
211 // associated with the request. Returns the id assigned to the request.
212 int Add(const PrintCallback& callback);
214 // Completes the request with the provided request id. It runs the request
215 // callback and removes the request from the set.
216 bool Complete(int request_id, PrintError result);
218 // Runs all pending callbacks with ERROR_FAILED and clears the set of
219 // pending requests.
220 void FailAll();
222 private:
223 int last_request_id_;
224 std::map<int, PrintCallback> pending_requests_;
227 // BrowserContextKeyedAPI implementation.
228 static const bool kServiceRedirectedInIncognito = true;
229 static const char* service_name() { return "PrinterProvider"; }
231 // PrinterProviderInternalAPIObserver implementation:
232 void OnGetPrintersResult(
233 const Extension* extension,
234 int request_id,
235 const PrinterProviderInternalAPIObserver::PrinterInfoVector& result)
236 override;
237 void OnGetCapabilityResult(const Extension* extension,
238 int request_id,
239 const base::DictionaryValue& result) override;
240 void OnPrintResult(
241 const Extension* extension,
242 int request_id,
243 core_api::printer_provider_internal::PrintError error) override;
245 // ExtensionRegistryObserver implementation:
246 void OnExtensionUnloaded(content::BrowserContext* browser_context,
247 const Extension* extension,
248 UnloadedExtensionInfo::Reason reason) override;
250 // Called before chrome.printerProvider.onGetPrintersRequested event is
251 // dispatched to an extension. It returns whether the extension is interested
252 // in the event. If the extension listens to the event, it's added to the set
253 // of |request| sources. |request| is |GetPrintersRequest| object associated
254 // with the event.
255 bool WillRequestPrinters(GetPrintersRequest* request,
256 content::BrowserContext* browser_context,
257 const Extension* extension,
258 base::ListValue* args) const;
260 content::BrowserContext* browser_context_;
262 PendingGetPrintersRequests pending_get_printers_requests_;
264 std::map<std::string, PendingPrintRequests> pending_print_requests_;
266 std::map<std::string, PendingGetCapabilityRequests>
267 pending_capability_requests_;
269 ScopedObserver<PrinterProviderInternalAPI, PrinterProviderInternalAPIObserver>
270 internal_api_observer_;
272 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
273 extension_registry_observer_;
275 DISALLOW_COPY_AND_ASSIGN(PrinterProviderAPI);
278 template <>
279 void BrowserContextKeyedAPIFactory<
280 PrinterProviderAPI>::DeclareFactoryDependencies();
282 } // namespace extensions
284 #endif // EXTENSIONS_BROWSER_API_PRINTER_PROVIDER_PRINTER_PROVIDER_API_H_