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 #include "extensions/browser/api/printer_provider/printer_provider_api.h" | 5 #include "extensions/browser/api/printer_provider/printer_provider_api.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "extensions/browser/api/printer_provider_internal/printer_provider_inte
rnal_api.h" | 14 #include "extensions/browser/api/printer_provider_internal/printer_provider_inte
rnal_api.h" |
15 #include "extensions/browser/event_router.h" | 15 #include "extensions/browser/event_router.h" |
16 #include "extensions/common/api/printer_provider.h" | 16 #include "extensions/common/api/printer_provider.h" |
17 #include "extensions/common/api/printer_provider_internal.h" | 17 #include "extensions/common/api/printer_provider_internal.h" |
18 #include "extensions/common/extension.h" | 18 #include "extensions/common/extension.h" |
19 | 19 |
20 namespace extensions { | 20 namespace extensions { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 static base::LazyInstance<BrowserContextKeyedAPIFactory<PrinterProviderAPI>> | 24 static base::LazyInstance<BrowserContextKeyedAPIFactory<PrinterProviderAPI>> |
25 g_api_factory = LAZY_INSTANCE_INITIALIZER; | 25 g_api_factory = LAZY_INSTANCE_INITIALIZER; |
26 | 26 |
| 27 // The separator between extension id and the extension's internal printer id |
| 28 // used when generating a printer id unique across extensions. |
| 29 const char kPrinterIdSeparator = ':'; |
| 30 |
| 31 // Given an extension ID and an ID of a printer reported by the extension, it |
| 32 // generates a ID for the printer unique across extensions (assuming that the |
| 33 // printer id is unique in the extension's space). |
| 34 std::string GeneratePrinterId(const std::string& extension_id, |
| 35 const std::string& internal_printer_id) { |
| 36 std::string result = extension_id; |
| 37 result.append(1, kPrinterIdSeparator); |
| 38 result.append(internal_printer_id); |
| 39 return result; |
| 40 } |
| 41 |
| 42 // Parses an ID created using |GeneratePrinterId| to it's components: |
| 43 // the extension ID and the printer ID internal to the extension. |
| 44 // Returns whenter the ID was succesfully parsed. |
| 45 bool ParsePrinterId(const std::string& printer_id, |
| 46 std::string* extension_id, |
| 47 std::string* internal_printer_id) { |
| 48 size_t separator = printer_id.find_first_of(kPrinterIdSeparator); |
| 49 if (separator == std::string::npos) |
| 50 return false; |
| 51 *extension_id = printer_id.substr(0, separator); |
| 52 *internal_printer_id = printer_id.substr(separator + 1); |
| 53 return true; |
| 54 } |
| 55 |
27 PrinterProviderAPI::PrintError APIPrintErrorToInternalType( | 56 PrinterProviderAPI::PrintError APIPrintErrorToInternalType( |
28 core_api::printer_provider_internal::PrintError error) { | 57 core_api::printer_provider_internal::PrintError error) { |
29 switch (error) { | 58 switch (error) { |
30 case core_api::printer_provider_internal::PRINT_ERROR_NONE: | 59 case core_api::printer_provider_internal::PRINT_ERROR_NONE: |
31 // The PrintError parameter is not set, which implies an error. | 60 // The PrintError parameter is not set, which implies an error. |
32 return PrinterProviderAPI::PRINT_ERROR_FAILED; | 61 return PrinterProviderAPI::PRINT_ERROR_FAILED; |
33 case core_api::printer_provider_internal::PRINT_ERROR_OK: | 62 case core_api::printer_provider_internal::PRINT_ERROR_OK: |
34 return PrinterProviderAPI::PRINT_ERROR_NONE; | 63 return PrinterProviderAPI::PRINT_ERROR_NONE; |
35 case core_api::printer_provider_internal::PRINT_ERROR_FAILED: | 64 case core_api::printer_provider_internal::PRINT_ERROR_FAILED: |
36 return PrinterProviderAPI::PRINT_ERROR_FAILED; | 65 return PrinterProviderAPI::PRINT_ERROR_FAILED; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 // Unretained is safe. Also, |raw_request_ptr| will stay valid at least until | 122 // Unretained is safe. Also, |raw_request_ptr| will stay valid at least until |
94 // |BroadcastEvent| finishes. | 123 // |BroadcastEvent| finishes. |
95 event->will_dispatch_callback = | 124 event->will_dispatch_callback = |
96 base::Bind(&PrinterProviderAPI::WillRequestPrinters, | 125 base::Bind(&PrinterProviderAPI::WillRequestPrinters, |
97 base::Unretained(this), raw_request_ptr); | 126 base::Unretained(this), raw_request_ptr); |
98 | 127 |
99 event_router->BroadcastEvent(event.Pass()); | 128 event_router->BroadcastEvent(event.Pass()); |
100 } | 129 } |
101 | 130 |
102 void PrinterProviderAPI::DispatchGetCapabilityRequested( | 131 void PrinterProviderAPI::DispatchGetCapabilityRequested( |
103 const std::string& extension_id, | |
104 const std::string& printer_id, | 132 const std::string& printer_id, |
105 const PrinterProviderAPI::GetCapabilityCallback& callback) { | 133 const PrinterProviderAPI::GetCapabilityCallback& callback) { |
| 134 std::string extension_id; |
| 135 std::string internal_printer_id; |
| 136 if (!ParsePrinterId(printer_id, &extension_id, &internal_printer_id)) { |
| 137 callback.Run(base::DictionaryValue()); |
| 138 return; |
| 139 } |
| 140 |
106 EventRouter* event_router = EventRouter::Get(browser_context_); | 141 EventRouter* event_router = EventRouter::Get(browser_context_); |
107 if (!event_router->ExtensionHasEventListener( | 142 if (!event_router->ExtensionHasEventListener( |
108 extension_id, | 143 extension_id, |
109 core_api::printer_provider::OnGetCapabilityRequested::kEventName)) { | 144 core_api::printer_provider::OnGetCapabilityRequested::kEventName)) { |
110 callback.Run(base::DictionaryValue()); | 145 callback.Run(base::DictionaryValue()); |
111 return; | 146 return; |
112 } | 147 } |
113 | 148 |
114 int request_id = pending_capability_requests_[extension_id].Add(callback); | 149 int request_id = pending_capability_requests_[extension_id].Add(callback); |
115 | 150 |
116 scoped_ptr<base::ListValue> internal_args(new base::ListValue); | 151 scoped_ptr<base::ListValue> internal_args(new base::ListValue); |
117 // Request id is not part of the public API, but it will be massaged out in | 152 // Request id is not part of the public API, but it will be massaged out in |
118 // custom bindings. | 153 // custom bindings. |
119 internal_args->AppendInteger(request_id); | 154 internal_args->AppendInteger(request_id); |
120 internal_args->AppendString(printer_id); | 155 internal_args->AppendString(internal_printer_id); |
121 | 156 |
122 scoped_ptr<Event> event(new Event( | 157 scoped_ptr<Event> event(new Event( |
123 core_api::printer_provider::OnGetCapabilityRequested::kEventName, | 158 core_api::printer_provider::OnGetCapabilityRequested::kEventName, |
124 internal_args.Pass())); | 159 internal_args.Pass())); |
125 | 160 |
126 event_router->DispatchEventToExtension(extension_id, event.Pass()); | 161 event_router->DispatchEventToExtension(extension_id, event.Pass()); |
127 } | 162 } |
128 | 163 |
129 void PrinterProviderAPI::DispatchPrintRequested( | 164 void PrinterProviderAPI::DispatchPrintRequested( |
130 const std::string& extension_id, | |
131 const PrinterProviderAPI::PrintJob& job, | 165 const PrinterProviderAPI::PrintJob& job, |
132 const PrinterProviderAPI::PrintCallback& callback) { | 166 const PrinterProviderAPI::PrintCallback& callback) { |
| 167 std::string extension_id; |
| 168 std::string internal_printer_id; |
| 169 if (!ParsePrinterId(job.printer_id, &extension_id, &internal_printer_id)) { |
| 170 callback.Run(PRINT_ERROR_FAILED); |
| 171 return; |
| 172 } |
| 173 |
133 EventRouter* event_router = EventRouter::Get(browser_context_); | 174 EventRouter* event_router = EventRouter::Get(browser_context_); |
134 if (!event_router->ExtensionHasEventListener( | 175 if (!event_router->ExtensionHasEventListener( |
135 extension_id, | 176 extension_id, |
136 core_api::printer_provider::OnPrintRequested::kEventName)) { | 177 core_api::printer_provider::OnPrintRequested::kEventName)) { |
137 callback.Run(PRINT_ERROR_FAILED); | 178 callback.Run(PRINT_ERROR_FAILED); |
138 return; | 179 return; |
139 } | 180 } |
140 | 181 |
141 core_api::printer_provider::PrintJob print_job; | 182 core_api::printer_provider::PrintJob print_job; |
142 print_job.printer_id = job.printer_id; | 183 print_job.printer_id = internal_printer_id; |
143 print_job.content_type = job.content_type; | 184 print_job.content_type = job.content_type; |
144 print_job.document = | 185 print_job.document = |
145 std::vector<char>(job.document_bytes.begin(), job.document_bytes.end()); | 186 std::vector<char>(job.document_bytes.begin(), job.document_bytes.end()); |
146 | 187 |
147 int request_id = pending_print_requests_[extension_id].Add(callback); | 188 int request_id = pending_print_requests_[extension_id].Add(callback); |
148 | 189 |
149 scoped_ptr<base::ListValue> internal_args(new base::ListValue); | 190 scoped_ptr<base::ListValue> internal_args(new base::ListValue); |
150 // Request id is not part of the public API and it will be massaged out in | 191 // Request id is not part of the public API and it will be massaged out in |
151 // custom bindings. | 192 // custom bindings. |
152 internal_args->AppendInteger(request_id); | 193 internal_args->AppendInteger(request_id); |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 if (it == pending_requests_.end()) | 302 if (it == pending_requests_.end()) |
262 return false; | 303 return false; |
263 | 304 |
264 PrintCallback callback = it->second; | 305 PrintCallback callback = it->second; |
265 pending_requests_.erase(it); | 306 pending_requests_.erase(it); |
266 | 307 |
267 callback.Run(response); | 308 callback.Run(response); |
268 return true; | 309 return true; |
269 } | 310 } |
270 | 311 |
271 void PrinterProviderAPI::OnGetPrintersResult(const Extension* extension, | 312 void PrinterProviderAPI::OnGetPrintersResult( |
272 int request_id, | 313 const Extension* extension, |
273 const base::ListValue& result) { | 314 int request_id, |
| 315 const PrinterProviderInternalAPIObserver::PrinterInfoVector& result) { |
| 316 base::ListValue printer_list; |
| 317 |
| 318 // Update some printer description properties to better identify the extension |
| 319 // managing the printer. |
| 320 for (size_t i = 0; i < result.size(); ++i) { |
| 321 scoped_ptr<base::DictionaryValue> printer(result[i]->ToValue()); |
| 322 std::string internal_printer_id; |
| 323 CHECK(printer->GetString("id", &internal_printer_id)); |
| 324 printer->SetString("id", |
| 325 GeneratePrinterId(extension->id(), internal_printer_id)); |
| 326 printer->SetString("extensionId", extension->id()); |
| 327 printer_list.Append(printer.release()); |
| 328 } |
| 329 |
274 pending_get_printers_requests_.CompleteForExtension(extension->id(), | 330 pending_get_printers_requests_.CompleteForExtension(extension->id(), |
275 request_id, result); | 331 request_id, printer_list); |
276 } | 332 } |
277 | 333 |
278 void PrinterProviderAPI::OnGetCapabilityResult( | 334 void PrinterProviderAPI::OnGetCapabilityResult( |
279 const Extension* extension, | 335 const Extension* extension, |
280 int request_id, | 336 int request_id, |
281 const base::DictionaryValue& result) { | 337 const base::DictionaryValue& result) { |
282 pending_capability_requests_[extension->id()].Complete(request_id, result); | 338 pending_capability_requests_[extension->id()].Complete(request_id, result); |
283 } | 339 } |
284 | 340 |
285 void PrinterProviderAPI::OnPrintResult( | 341 void PrinterProviderAPI::OnPrintResult( |
(...skipping 23 matching lines...) Expand all Loading... |
309 } | 365 } |
310 | 366 |
311 template <> | 367 template <> |
312 void BrowserContextKeyedAPIFactory< | 368 void BrowserContextKeyedAPIFactory< |
313 PrinterProviderAPI>::DeclareFactoryDependencies() { | 369 PrinterProviderAPI>::DeclareFactoryDependencies() { |
314 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | 370 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); |
315 DependsOn(PrinterProviderInternalAPI::GetFactoryInstance()); | 371 DependsOn(PrinterProviderInternalAPI::GetFactoryInstance()); |
316 } | 372 } |
317 | 373 |
318 } // namespace extensions | 374 } // namespace extensions |
OLD | NEW |