make use of media_use_ffmpeg in BUILD.gn
[chromium-blink-merge.git] / extensions / browser / api / printer_provider_internal / printer_provider_internal_api.cc
blob6ffaa4864c6743b349c414e7f721f02b4bf40c5f
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 #include "extensions/browser/api/printer_provider_internal/printer_provider_internal_api.h"
7 #include <string>
8 #include <vector>
10 #include "base/bind.h"
11 #include "base/guid.h"
12 #include "base/lazy_instance.h"
13 #include "base/location.h"
14 #include "base/memory/ref_counted_memory.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/values.h"
17 #include "content/public/browser/blob_handle.h"
18 #include "content/public/browser/browser_context.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/render_frame_host.h"
21 #include "content/public/browser/render_process_host.h"
22 #include "content/public/browser/web_contents.h"
23 #include "extensions/browser/api/printer_provider/printer_provider_api.h"
24 #include "extensions/browser/api/printer_provider/printer_provider_api_factory.h"
25 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h"
26 #include "extensions/browser/blob_holder.h"
27 #include "extensions/common/api/printer_provider.h"
28 #include "extensions/common/api/printer_provider_internal.h"
30 namespace internal_api = extensions::api::printer_provider_internal;
32 namespace extensions {
34 namespace {
36 static base::LazyInstance<
37 BrowserContextKeyedAPIFactory<PrinterProviderInternalAPI>> g_api_factory =
38 LAZY_INSTANCE_INITIALIZER;
40 } // namespace
42 // static
43 BrowserContextKeyedAPIFactory<PrinterProviderInternalAPI>*
44 PrinterProviderInternalAPI::GetFactoryInstance() {
45 return g_api_factory.Pointer();
48 PrinterProviderInternalAPI::PrinterProviderInternalAPI(
49 content::BrowserContext* browser_context) {
52 PrinterProviderInternalAPI::~PrinterProviderInternalAPI() {
55 void PrinterProviderInternalAPI::AddObserver(
56 PrinterProviderInternalAPIObserver* observer) {
57 observers_.AddObserver(observer);
60 void PrinterProviderInternalAPI::RemoveObserver(
61 PrinterProviderInternalAPIObserver* observer) {
62 observers_.RemoveObserver(observer);
65 void PrinterProviderInternalAPI::NotifyGetPrintersResult(
66 const Extension* extension,
67 int request_id,
68 const PrinterProviderInternalAPIObserver::PrinterInfoVector& printers) {
69 FOR_EACH_OBSERVER(PrinterProviderInternalAPIObserver, observers_,
70 OnGetPrintersResult(extension, request_id, printers));
73 void PrinterProviderInternalAPI::NotifyGetCapabilityResult(
74 const Extension* extension,
75 int request_id,
76 const base::DictionaryValue& capability) {
77 FOR_EACH_OBSERVER(PrinterProviderInternalAPIObserver, observers_,
78 OnGetCapabilityResult(extension, request_id, capability));
81 void PrinterProviderInternalAPI::NotifyPrintResult(
82 const Extension* extension,
83 int request_id,
84 api::printer_provider_internal::PrintError error) {
85 FOR_EACH_OBSERVER(PrinterProviderInternalAPIObserver, observers_,
86 OnPrintResult(extension, request_id, error));
89 void PrinterProviderInternalAPI::NotifyGetUsbPrinterInfoResult(
90 const Extension* extension,
91 int request_id,
92 const api::printer_provider::PrinterInfo* printer_info) {
93 FOR_EACH_OBSERVER(
94 PrinterProviderInternalAPIObserver, observers_,
95 OnGetUsbPrinterInfoResult(extension, request_id, printer_info));
98 PrinterProviderInternalReportPrintResultFunction::
99 PrinterProviderInternalReportPrintResultFunction() {
102 PrinterProviderInternalReportPrintResultFunction::
103 ~PrinterProviderInternalReportPrintResultFunction() {
106 ExtensionFunction::ResponseAction
107 PrinterProviderInternalReportPrintResultFunction::Run() {
108 scoped_ptr<internal_api::ReportPrintResult::Params> params(
109 internal_api::ReportPrintResult::Params::Create(*args_));
110 EXTENSION_FUNCTION_VALIDATE(params.get());
112 PrinterProviderInternalAPI::GetFactoryInstance()
113 ->Get(browser_context())
114 ->NotifyPrintResult(extension(), params->request_id, params->error);
115 return RespondNow(NoArguments());
118 PrinterProviderInternalReportPrinterCapabilityFunction::
119 PrinterProviderInternalReportPrinterCapabilityFunction() {
122 PrinterProviderInternalReportPrinterCapabilityFunction::
123 ~PrinterProviderInternalReportPrinterCapabilityFunction() {
126 ExtensionFunction::ResponseAction
127 PrinterProviderInternalReportPrinterCapabilityFunction::Run() {
128 scoped_ptr<internal_api::ReportPrinterCapability::Params> params(
129 internal_api::ReportPrinterCapability::Params::Create(*args_));
130 EXTENSION_FUNCTION_VALIDATE(params.get());
132 if (params->capability) {
133 PrinterProviderInternalAPI::GetFactoryInstance()
134 ->Get(browser_context())
135 ->NotifyGetCapabilityResult(extension(), params->request_id,
136 params->capability->additional_properties);
137 } else {
138 PrinterProviderInternalAPI::GetFactoryInstance()
139 ->Get(browser_context())
140 ->NotifyGetCapabilityResult(extension(), params->request_id,
141 base::DictionaryValue());
143 return RespondNow(NoArguments());
146 PrinterProviderInternalReportPrintersFunction::
147 PrinterProviderInternalReportPrintersFunction() {
150 PrinterProviderInternalReportPrintersFunction::
151 ~PrinterProviderInternalReportPrintersFunction() {
154 ExtensionFunction::ResponseAction
155 PrinterProviderInternalReportPrintersFunction::Run() {
156 scoped_ptr<internal_api::ReportPrinters::Params> params(
157 internal_api::ReportPrinters::Params::Create(*args_));
158 EXTENSION_FUNCTION_VALIDATE(params.get());
160 base::ListValue printers;
161 if (params->printers) {
162 PrinterProviderInternalAPI::GetFactoryInstance()
163 ->Get(browser_context())
164 ->NotifyGetPrintersResult(extension(), params->request_id,
165 *params->printers);
166 } else {
167 PrinterProviderInternalAPI::GetFactoryInstance()
168 ->Get(browser_context())
169 ->NotifyGetPrintersResult(
170 extension(), params->request_id,
171 PrinterProviderInternalAPIObserver::PrinterInfoVector());
173 return RespondNow(NoArguments());
176 PrinterProviderInternalGetPrintDataFunction::
177 PrinterProviderInternalGetPrintDataFunction() {
180 PrinterProviderInternalGetPrintDataFunction::
181 ~PrinterProviderInternalGetPrintDataFunction() {
184 ExtensionFunction::ResponseAction
185 PrinterProviderInternalGetPrintDataFunction::Run() {
186 scoped_ptr<internal_api::GetPrintData::Params> params(
187 internal_api::GetPrintData::Params::Create(*args_));
188 EXTENSION_FUNCTION_VALIDATE(params.get());
190 const PrinterProviderPrintJob* job =
191 PrinterProviderAPIFactory::GetInstance()
192 ->GetForBrowserContext(browser_context())
193 ->GetPrintJob(extension(), params->request_id);
194 if (!job)
195 return RespondNow(Error("Print request not found."));
197 if (job->document_bytes.get()) {
198 // |job->document_bytes| are passed to the callback to make sure the ref
199 // counted memory does not go away before the memory backed blob is created.
200 content::BrowserContext::CreateMemoryBackedBlob(
201 browser_context(), job->document_bytes->front_as<char>(),
202 job->document_bytes->size(),
203 base::Bind(&PrinterProviderInternalGetPrintDataFunction::OnBlob, this,
204 job->content_type, job->document_bytes->size(),
205 job->document_bytes));
206 } else if (!job->document_path.empty()) {
207 content::BrowserContext::CreateFileBackedBlob(
208 browser_context(), job->document_path, 0 /* offset */,
209 job->file_info.size, job->file_info.last_modified,
210 base::Bind(&PrinterProviderInternalGetPrintDataFunction::OnBlob, this,
211 job->content_type, job->file_info.size,
212 scoped_refptr<base::RefCountedMemory>()));
213 } else {
214 return RespondNow(Error("Job data not set"));
216 return RespondLater();
219 void PrinterProviderInternalGetPrintDataFunction::OnBlob(
220 const std::string& type,
221 int size,
222 const scoped_refptr<base::RefCountedMemory>& data,
223 scoped_ptr<content::BlobHandle> blob) {
224 if (!blob) {
225 SetError("Unable to create the blob.");
226 SendResponse(false);
227 return;
230 internal_api::BlobInfo info;
231 info.blob_uuid = blob->GetUUID();
232 info.type = type;
233 info.size = size;
235 std::vector<std::string> uuids;
236 uuids.push_back(blob->GetUUID());
238 extensions::BlobHolder* holder =
239 extensions::BlobHolder::FromRenderProcessHost(
240 render_frame_host()->GetProcess());
241 holder->HoldBlobReference(blob.Pass());
243 results_ = internal_api::GetPrintData::Results::Create(info);
244 SetTransferredBlobUUIDs(uuids);
245 SendResponse(true);
248 PrinterProviderInternalReportUsbPrinterInfoFunction::
249 PrinterProviderInternalReportUsbPrinterInfoFunction() {
252 PrinterProviderInternalReportUsbPrinterInfoFunction::
253 ~PrinterProviderInternalReportUsbPrinterInfoFunction() {
256 ExtensionFunction::ResponseAction
257 PrinterProviderInternalReportUsbPrinterInfoFunction::Run() {
258 scoped_ptr<internal_api::ReportUsbPrinterInfo::Params> params(
259 internal_api::ReportUsbPrinterInfo::Params::Create(*args_));
260 EXTENSION_FUNCTION_VALIDATE(params.get());
262 PrinterProviderInternalAPI::GetFactoryInstance()
263 ->Get(browser_context())
264 ->NotifyGetUsbPrinterInfoResult(extension(), params->request_id,
265 params->printer_info.get());
266 return RespondNow(NoArguments());
269 } // namespace extensions