1 // Copyright 2014 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 "content/browser/renderer_host/pepper/pepper_truetype_font_host.h"
8 #include "base/task_runner_util.h"
9 #include "base/threading/sequenced_worker_pool.h"
10 #include "content/browser/renderer_host/pepper/pepper_truetype_font.h"
11 #include "content/public/browser/browser_ppapi_host.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "ppapi/c/pp_errors.h"
14 #include "ppapi/host/dispatch_host_message.h"
15 #include "ppapi/host/ppapi_host.h"
16 #include "ppapi/proxy/ppapi_messages.h"
18 using ppapi::host::HostMessageContext;
19 using ppapi::host::ReplyMessageContext;
20 using ppapi::proxy::SerializedTrueTypeFontDesc;
24 PepperTrueTypeFontHost::PepperTrueTypeFontHost(
25 BrowserPpapiHost* host,
28 const SerializedTrueTypeFontDesc& desc)
29 : ResourceHost(host->GetPpapiHost(), instance, resource),
30 initialize_completed_(false),
32 font_ = PepperTrueTypeFont::Create();
33 // Initialize the font on a blocking pool thread. This must complete before
35 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
36 task_runner_ = pool->GetSequencedTaskRunner(pool->GetSequenceToken());
37 SerializedTrueTypeFontDesc* actual_desc =
38 new SerializedTrueTypeFontDesc(desc);
39 base::PostTaskAndReplyWithResult(
42 base::Bind(&PepperTrueTypeFont::Initialize, font_, actual_desc),
43 base::Bind(&PepperTrueTypeFontHost::OnInitializeComplete,
44 weak_factory_.GetWeakPtr(),
45 base::Owned(actual_desc)));
48 PepperTrueTypeFontHost::~PepperTrueTypeFontHost() {
50 // Release the font on the task runner in case the implementation requires
51 // long blocking operations.
53 PepperTrueTypeFont* raw_font = font_.get();
55 task_runner_->ReleaseSoon(FROM_HERE, raw_font);
59 int32_t PepperTrueTypeFontHost::OnResourceMessageReceived(
60 const IPC::Message& msg,
61 HostMessageContext* context) {
62 if (!host()->permissions().HasPermission(ppapi::PERMISSION_DEV))
63 return PP_ERROR_FAILED;
65 PPAPI_BEGIN_MESSAGE_MAP(PepperTrueTypeFontHost, msg)
66 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_TrueTypeFont_GetTableTags,
67 OnHostMsgGetTableTags)
68 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_TrueTypeFont_GetTable,
70 PPAPI_END_MESSAGE_MAP()
71 return PP_ERROR_FAILED;
74 int32_t PepperTrueTypeFontHost::OnHostMsgGetTableTags(
75 HostMessageContext* context) {
77 return PP_ERROR_FAILED;
79 // Get font data on a thread that allows slow blocking operations.
80 std::vector<uint32_t>* tags = new std::vector<uint32_t>();
81 base::PostTaskAndReplyWithResult(
84 base::Bind(&PepperTrueTypeFont::GetTableTags, font_, tags),
85 base::Bind(&PepperTrueTypeFontHost::OnGetTableTagsComplete,
86 weak_factory_.GetWeakPtr(),
88 context->MakeReplyMessageContext()));
90 return PP_OK_COMPLETIONPENDING;
93 int32_t PepperTrueTypeFontHost::OnHostMsgGetTable(HostMessageContext* context,
96 int32_t max_data_length) {
98 return PP_ERROR_FAILED;
99 if (offset < 0 || max_data_length < 0)
100 return PP_ERROR_BADARGUMENT;
102 // Get font data on a thread that allows slow blocking operations.
103 std::string* data = new std::string();
104 base::PostTaskAndReplyWithResult(
107 base::Bind(&PepperTrueTypeFont::GetTable,
113 base::Bind(&PepperTrueTypeFontHost::OnGetTableComplete,
114 weak_factory_.GetWeakPtr(),
116 context->MakeReplyMessageContext()));
118 return PP_OK_COMPLETIONPENDING;
121 void PepperTrueTypeFontHost::OnInitializeComplete(
122 SerializedTrueTypeFontDesc* desc,
124 DCHECK(!initialize_completed_);
125 initialize_completed_ = true;
126 // Release the font if there was an error, so future calls will fail.
129 host()->SendUnsolicitedReply(
130 pp_resource(), PpapiPluginMsg_TrueTypeFont_CreateReply(*desc, result));
133 void PepperTrueTypeFontHost::OnGetTableTagsComplete(
134 std::vector<uint32_t>* tags,
135 ReplyMessageContext reply_context,
137 DCHECK(initialize_completed_);
138 // It's possible that Initialize failed and that |font_| is NULL. Check that
139 // the font implementation doesn't return PP_OK in that case.
140 DCHECK(font_.get() || result != PP_OK);
141 reply_context.params.set_result(result);
142 host()->SendReply(reply_context,
143 PpapiPluginMsg_TrueTypeFont_GetTableTagsReply(*tags));
146 void PepperTrueTypeFontHost::OnGetTableComplete(
148 ReplyMessageContext reply_context,
150 DCHECK(initialize_completed_);
151 // It's possible that Initialize failed and that |font_| is NULL. Check that
152 // the font implementation doesn't return PP_OK in that case.
153 DCHECK(font_.get() || result != PP_OK);
154 reply_context.params.set_result(result);
155 host()->SendReply(reply_context,
156 PpapiPluginMsg_TrueTypeFont_GetTableReply(*data));
159 } // namespace content