pepper_truetype_font_host.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
1// Copyright (c) 2013 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.
4
5#include "content/renderer/pepper/pepper_truetype_font_host.h"
6
7#include "base/bind.h"
8#include "content/public/renderer/renderer_ppapi_host.h"
9#include "content/renderer/pepper/pepper_truetype_font.h"
10#include "ppapi/c/pp_errors.h"
11#include "ppapi/host/dispatch_host_message.h"
12#include "ppapi/host/host_message_context.h"
13#include "ppapi/host/ppapi_host.h"
14#include "ppapi/proxy/ppapi_messages.h"
15
16using ppapi::host::HostMessageContext;
17using ppapi::host::ReplyMessageContext;
18
19namespace content {
20
21PepperTrueTypeFontHost::PepperTrueTypeFontHost(
22    RendererPpapiHost* host,
23    PP_Instance instance,
24    PP_Resource resource,
25    const ppapi::proxy::SerializedTrueTypeFontDesc& desc)
26    : ResourceHost(host->GetPpapiHost(), instance, resource),
27      renderer_ppapi_host_(host),
28      font_(PepperTrueTypeFont::Create(desc)),
29      weak_factory_(this) {}
30
31PepperTrueTypeFontHost::~PepperTrueTypeFontHost() {}
32
33int32_t PepperTrueTypeFontHost::OnResourceMessageReceived(
34    const IPC::Message& msg,
35    HostMessageContext* context) {
36  if (!host()->permissions().HasPermission(ppapi::PERMISSION_DEV))
37    return PP_ERROR_FAILED;
38
39  PPAPI_BEGIN_MESSAGE_MAP(PepperTrueTypeFontHost, msg)
40    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_TrueTypeFont_Describe,
41                                        OnHostMsgDescribe)
42    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_TrueTypeFont_GetTableTags,
43                                        OnHostMsgGetTableTags)
44    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_TrueTypeFont_GetTable,
45                                      OnHostMsgGetTable)
46  PPAPI_END_MESSAGE_MAP()
47  return PP_ERROR_FAILED;
48}
49
50int32_t PepperTrueTypeFontHost::OnHostMsgDescribe(HostMessageContext* context) {
51  if (!font_->IsValid())
52    return PP_ERROR_FAILED;
53
54  ppapi::proxy::SerializedTrueTypeFontDesc desc;
55  ReplyMessageContext reply_context = context->MakeReplyMessageContext();
56  reply_context.params.set_result(font_->Describe(&desc));
57  host()->SendReply(reply_context,
58                    PpapiPluginMsg_TrueTypeFont_DescribeReply(desc));
59  return PP_OK_COMPLETIONPENDING;
60}
61
62int32_t PepperTrueTypeFontHost::OnHostMsgGetTableTags(
63    HostMessageContext* context) {
64  if (!font_->IsValid())
65    return PP_ERROR_FAILED;
66
67  std::vector<uint32_t> tags;
68  ReplyMessageContext reply_context = context->MakeReplyMessageContext();
69  reply_context.params.set_result(font_->GetTableTags(&tags));
70  host()->SendReply(reply_context,
71                    PpapiPluginMsg_TrueTypeFont_GetTableTagsReply(tags));
72  return PP_OK_COMPLETIONPENDING;
73}
74
75int32_t PepperTrueTypeFontHost::OnHostMsgGetTable(HostMessageContext* context,
76                                                  uint32_t table,
77                                                  int32_t offset,
78                                                  int32_t max_data_length) {
79  if (!font_->IsValid())
80    return PP_ERROR_FAILED;
81  if (offset < 0 || max_data_length < 0)
82    return PP_ERROR_BADARGUMENT;
83
84  std::string data;
85  ReplyMessageContext reply_context = context->MakeReplyMessageContext();
86  reply_context.params.set_result(
87      font_->GetTable(table, offset, max_data_length, &data));
88  host()->SendReply(reply_context,
89                    PpapiPluginMsg_TrueTypeFont_GetTableReply(data));
90  return PP_OK_COMPLETIONPENDING;
91}
92
93}  // namespace content
94