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}
31
32PepperTrueTypeFontHost::~PepperTrueTypeFontHost() {
33}
34
35int32_t PepperTrueTypeFontHost::OnResourceMessageReceived(
36    const IPC::Message& msg,
37    HostMessageContext* context) {
38  if (!host()->permissions().HasPermission(ppapi::PERMISSION_DEV))
39    return PP_ERROR_FAILED;
40
41  IPC_BEGIN_MESSAGE_MAP(PepperTrueTypeFontHost, msg)
42    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_TrueTypeFont_Describe,
43                                        OnHostMsgDescribe)
44    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_TrueTypeFont_GetTableTags,
45                                        OnHostMsgGetTableTags)
46    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_TrueTypeFont_GetTable,
47                                      OnHostMsgGetTable)
48  IPC_END_MESSAGE_MAP()
49  return PP_ERROR_FAILED;
50}
51
52int32_t PepperTrueTypeFontHost::OnHostMsgDescribe(HostMessageContext* context) {
53  if (!font_->IsValid())
54    return PP_ERROR_FAILED;
55
56  ppapi::proxy::SerializedTrueTypeFontDesc desc;
57  ReplyMessageContext reply_context = context->MakeReplyMessageContext();
58  reply_context.params.set_result(font_->Describe(&desc));
59  host()->SendReply(reply_context,
60                    PpapiPluginMsg_TrueTypeFont_DescribeReply(desc));
61  return PP_OK_COMPLETIONPENDING;
62}
63
64int32_t PepperTrueTypeFontHost::OnHostMsgGetTableTags(
65    HostMessageContext* context) {
66  if (!font_->IsValid())
67    return PP_ERROR_FAILED;
68
69  std::vector<uint32_t> tags;
70  ReplyMessageContext reply_context = context->MakeReplyMessageContext();
71  reply_context.params.set_result(font_->GetTableTags(&tags));
72  host()->SendReply(
73      reply_context,
74      PpapiPluginMsg_TrueTypeFont_GetTableTagsReply(tags));
75  return PP_OK_COMPLETIONPENDING;
76}
77
78int32_t PepperTrueTypeFontHost::OnHostMsgGetTable(HostMessageContext* context,
79                                                  uint32_t table,
80                                                  int32_t offset,
81                                                  int32_t max_data_length) {
82  if (!font_->IsValid())
83    return PP_ERROR_FAILED;
84  if (offset < 0 || max_data_length < 0)
85    return PP_ERROR_BADARGUMENT;
86
87  std::string data;
88  ReplyMessageContext reply_context = context->MakeReplyMessageContext();
89  reply_context.params.set_result(
90      font_->GetTable(table, offset, max_data_length, &data));
91  host()->SendReply(reply_context,
92                    PpapiPluginMsg_TrueTypeFont_GetTableReply(data));
93  return PP_OK_COMPLETIONPENDING;
94}
95
96}  // namespace content
97