Chromium Code Reviews| Index: content/browser/renderer_host/pepper/pepper_truetype_font_host.cc |
| diff --git a/content/browser/renderer_host/pepper/pepper_truetype_font_host.cc b/content/browser/renderer_host/pepper/pepper_truetype_font_host.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5e4b98754d2cee2e56a18cf6667bf9f635a8e3cb |
| --- /dev/null |
| +++ b/content/browser/renderer_host/pepper/pepper_truetype_font_host.cc |
| @@ -0,0 +1,159 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/renderer_host/pepper/pepper_truetype_font_host.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/task_runner_util.h" |
| +#include "base/threading/sequenced_worker_pool.h" |
| +#include "content/browser/renderer_host/pepper/pepper_truetype_font.h" |
| +#include "content/public/browser/browser_ppapi_host.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "ppapi/c/pp_errors.h" |
| +#include "ppapi/host/dispatch_host_message.h" |
| +#include "ppapi/host/ppapi_host.h" |
| +#include "ppapi/proxy/ppapi_messages.h" |
| + |
| +using ppapi::host::HostMessageContext; |
| +using ppapi::host::ReplyMessageContext; |
| +using ppapi::proxy::SerializedTrueTypeFontDesc; |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +int32_t DoInitialize(scoped_refptr<PepperTrueTypeFont> font, |
| + SerializedTrueTypeFontDesc* desc) { |
| + return font->Initialize(desc); |
| +} |
| + |
| +int32_t DoGetTableTags(scoped_refptr<PepperTrueTypeFont> font, |
| + std::vector<uint32_t>* tags) { |
| + return font->GetTableTags(tags); |
| +} |
| + |
| +int32_t DoGetTable(scoped_refptr<PepperTrueTypeFont> font, |
| + uint32_t table, |
| + int32_t offset, |
| + int32_t max_data_length, |
| + std::string* data) { |
| + return font->GetTable(table, offset, max_data_length, data); |
| +} |
| + |
| +void DoClose(scoped_refptr<PepperTrueTypeFont> font) { |
| + // Do nothing. If |font| is the last reference, the dtor runs on this thread. |
| +} |
| + |
| +} // namespace |
| + |
| +PepperTrueTypeFontHost::PepperTrueTypeFontHost( |
| + BrowserPpapiHost* host, |
| + PP_Instance instance, |
| + PP_Resource resource, |
| + const SerializedTrueTypeFontDesc& desc) |
| + : ResourceHost(host->GetPpapiHost(), instance, resource), |
| + weak_factory_(this) { |
| + font_ = PepperTrueTypeFont::Create(); |
| + // Initialize font on a thread that allows slow blocking operations. |
| + SerializedTrueTypeFontDesc* actual_desc = |
| + new SerializedTrueTypeFontDesc(desc); |
| + base::PostTaskAndReplyWithResult( |
| + BrowserThread::GetBlockingPool(), |
| + FROM_HERE, |
| + base::Bind(&DoInitialize, font_, actual_desc), |
| + base::Bind(&PepperTrueTypeFontHost::OnInitializeComplete, |
| + weak_factory_.GetWeakPtr(), |
| + base::Owned(actual_desc))); |
| +} |
| + |
| +PepperTrueTypeFontHost::~PepperTrueTypeFontHost() { |
| + BrowserThread::GetBlockingPool()->PostTask(FROM_HERE, |
| + base::Bind(&DoClose, font_)); |
| + // Release the only reference on the main thread. |
| + font_ = NULL; |
|
piman
2014/06/23 18:21:12
Why is this logic needed? It may not work the way
bbudge
2014/06/23 18:34:10
PepperTrueTypeFontLinux has to close its file desc
piman
2014/06/23 19:05:47
I see. IIRC close() doesn't block if you don't hav
bbudge
2014/06/24 01:50:56
Done via ReleaseSoon on the task runner. + comment
|
| +} |
| + |
| +int32_t PepperTrueTypeFontHost::OnResourceMessageReceived( |
| + const IPC::Message& msg, |
| + HostMessageContext* context) { |
| + if (!host()->permissions().HasPermission(ppapi::PERMISSION_DEV)) |
| + return PP_ERROR_FAILED; |
| + |
| + PPAPI_BEGIN_MESSAGE_MAP(PepperTrueTypeFontHost, msg) |
| + PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_TrueTypeFont_GetTableTags, |
| + OnHostMsgGetTableTags) |
| + PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_TrueTypeFont_GetTable, |
| + OnHostMsgGetTable) |
| + PPAPI_END_MESSAGE_MAP() |
| + return PP_ERROR_FAILED; |
| +} |
| + |
| +int32_t PepperTrueTypeFontHost::OnHostMsgGetTableTags( |
| + HostMessageContext* context) { |
| + if (!font_) |
| + return PP_ERROR_FAILED; |
| + |
| + // Get font data on a thread that allows slow blocking operations. |
| + std::vector<uint32_t>* tags = new std::vector<uint32_t>(); |
| + base::PostTaskAndReplyWithResult( |
| + BrowserThread::GetBlockingPool(), |
| + FROM_HERE, |
| + base::Bind(&DoGetTableTags, font_, tags), |
| + base::Bind(&PepperTrueTypeFontHost::OnGetTableTagsComplete, |
| + weak_factory_.GetWeakPtr(), |
| + base::Owned(tags), |
| + context->MakeReplyMessageContext())); |
| + |
| + return PP_OK_COMPLETIONPENDING; |
| +} |
| + |
| +int32_t PepperTrueTypeFontHost::OnHostMsgGetTable(HostMessageContext* context, |
| + uint32_t table, |
| + int32_t offset, |
| + int32_t max_data_length) { |
| + if (!font_) |
| + return PP_ERROR_FAILED; |
| + if (offset < 0 || max_data_length < 0) |
| + return PP_ERROR_BADARGUMENT; |
| + |
| + // Get font data on a thread that allows slow blocking operations. |
| + std::string* data = new std::string(); |
| + base::PostTaskAndReplyWithResult( |
| + BrowserThread::GetBlockingPool(), |
| + FROM_HERE, |
| + base::Bind(&DoGetTable, font_, table, offset, max_data_length, data), |
| + base::Bind(&PepperTrueTypeFontHost::OnGetTableComplete, |
| + weak_factory_.GetWeakPtr(), |
| + base::Owned(data), |
| + context->MakeReplyMessageContext())); |
| + |
| + return PP_OK_COMPLETIONPENDING; |
| +} |
| + |
| +void PepperTrueTypeFontHost::OnInitializeComplete( |
| + SerializedTrueTypeFontDesc* desc, |
| + int32_t result) { |
| + host()->SendUnsolicitedReply( |
| + pp_resource(), PpapiPluginMsg_TrueTypeFont_CreateReply(*desc, result)); |
| +} |
| + |
| +void PepperTrueTypeFontHost::OnGetTableTagsComplete( |
| + std::vector<uint32_t>* tags, |
| + ReplyMessageContext reply_context, |
| + int32_t result) { |
| + reply_context.params.set_result(result); |
| + host()->SendReply(reply_context, |
| + PpapiPluginMsg_TrueTypeFont_GetTableTagsReply(*tags)); |
| +} |
| + |
| +void PepperTrueTypeFontHost::OnGetTableComplete( |
| + std::string* data, |
| + ReplyMessageContext reply_context, |
| + int32_t result) { |
| + reply_context.params.set_result(result); |
| + host()->SendReply(reply_context, |
| + PpapiPluginMsg_TrueTypeFont_GetTableReply(*data)); |
| +} |
| + |
| +} // namespace content |