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_internal/printer_provider_inte
rnal_api.h" | 5 #include "extensions/browser/api/printer_provider_internal/printer_provider_inte
rnal_api.h" |
6 | 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/guid.h" |
7 #include "base/lazy_instance.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" |
8 #include "base/values.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_process_host.h" |
| 21 #include "content/public/browser/render_view_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" |
9 #include "extensions/common/api/printer_provider.h" | 27 #include "extensions/common/api/printer_provider.h" |
10 #include "extensions/common/api/printer_provider_internal.h" | 28 #include "extensions/common/api/printer_provider_internal.h" |
11 | 29 |
12 namespace internal_api = extensions::core_api::printer_provider_internal; | 30 namespace internal_api = extensions::core_api::printer_provider_internal; |
13 | 31 |
14 namespace extensions { | 32 namespace extensions { |
15 | 33 |
16 namespace { | 34 namespace { |
17 | 35 |
18 static base::LazyInstance< | 36 static base::LazyInstance< |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 } else { | 157 } else { |
140 PrinterProviderInternalAPI::GetFactoryInstance() | 158 PrinterProviderInternalAPI::GetFactoryInstance() |
141 ->Get(browser_context()) | 159 ->Get(browser_context()) |
142 ->NotifyGetPrintersResult( | 160 ->NotifyGetPrintersResult( |
143 extension(), params->request_id, | 161 extension(), params->request_id, |
144 PrinterProviderInternalAPIObserver::PrinterInfoVector()); | 162 PrinterProviderInternalAPIObserver::PrinterInfoVector()); |
145 } | 163 } |
146 return RespondNow(NoArguments()); | 164 return RespondNow(NoArguments()); |
147 } | 165 } |
148 | 166 |
| 167 PrinterProviderInternalGetPrintDataFunction:: |
| 168 PrinterProviderInternalGetPrintDataFunction() { |
| 169 } |
| 170 |
| 171 PrinterProviderInternalGetPrintDataFunction:: |
| 172 ~PrinterProviderInternalGetPrintDataFunction() { |
| 173 } |
| 174 |
| 175 ExtensionFunction::ResponseAction |
| 176 PrinterProviderInternalGetPrintDataFunction::Run() { |
| 177 scoped_ptr<internal_api::GetPrintData::Params> params( |
| 178 internal_api::GetPrintData::Params::Create(*args_)); |
| 179 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 180 |
| 181 const PrinterProviderPrintJob* job = |
| 182 PrinterProviderAPIFactory::GetInstance() |
| 183 ->GetForBrowserContext(browser_context()) |
| 184 ->GetPrintJob(extension(), params->request_id); |
| 185 if (!job) |
| 186 return RespondNow(Error("Print request not found.")); |
| 187 |
| 188 if (job->document_bytes.get()) { |
| 189 // |job->document_bytes| are passed to the callback to make sure the ref |
| 190 // counted memory does not go away before the memory backed blob is created. |
| 191 content::BrowserContext::CreateMemoryBackedBlob( |
| 192 browser_context(), job->document_bytes->front_as<char>(), |
| 193 job->document_bytes->size(), |
| 194 base::Bind(&PrinterProviderInternalGetPrintDataFunction::OnBlob, this, |
| 195 job->content_type, job->document_bytes->size(), |
| 196 job->document_bytes)); |
| 197 } else if (!job->document_path.empty()) { |
| 198 content::BrowserContext::CreateFileBackedBlob( |
| 199 browser_context(), job->document_path, 0 /* offset */, |
| 200 job->file_info.size, job->file_info.last_modified, |
| 201 base::Bind(&PrinterProviderInternalGetPrintDataFunction::OnBlob, this, |
| 202 job->content_type, job->file_info.size, |
| 203 scoped_refptr<base::RefCountedMemory>())); |
| 204 } else { |
| 205 return RespondNow(Error("Job data not set")); |
| 206 } |
| 207 return RespondLater(); |
| 208 } |
| 209 |
| 210 void PrinterProviderInternalGetPrintDataFunction::OnBlob( |
| 211 const std::string& type, |
| 212 int size, |
| 213 const scoped_refptr<base::RefCountedMemory>& data, |
| 214 scoped_ptr<content::BlobHandle> blob) { |
| 215 if (!blob) { |
| 216 SetError("Unable to create the blob."); |
| 217 SendResponse(false); |
| 218 return; |
| 219 } |
| 220 |
| 221 internal_api::BlobInfo info; |
| 222 info.blob_uuid = blob->GetUUID(); |
| 223 info.type = type; |
| 224 info.size = size; |
| 225 |
| 226 std::vector<std::string> uuids; |
| 227 uuids.push_back(blob->GetUUID()); |
| 228 |
| 229 content::WebContents* contents = |
| 230 content::WebContents::FromRenderViewHost(render_view_host()); |
| 231 extensions::BlobHolder* holder = |
| 232 extensions::BlobHolder::FromRenderProcessHost( |
| 233 contents->GetRenderProcessHost()); |
| 234 holder->HoldBlobReference(blob.Pass()); |
| 235 |
| 236 results_ = internal_api::GetPrintData::Results::Create(info); |
| 237 SetTransferredBlobUUIDs(uuids); |
| 238 SendResponse(true); |
| 239 } |
| 240 |
149 } // namespace extensions | 241 } // namespace extensions |
OLD | NEW |