truetype_font_resource.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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 "ppapi/proxy/truetype_font_resource.h"
6
7#include "base/bind.h"
8#include "ipc/ipc_message.h"
9#include "ppapi/c/pp_errors.h"
10#include "ppapi/proxy/ppapi_messages.h"
11#include "ppapi/shared_impl/array_writer.h"
12#include "ppapi/shared_impl/ppapi_globals.h"
13#include "ppapi/shared_impl/resource_tracker.h"
14#include "ppapi/shared_impl/var.h"
15#include "ppapi/thunk/enter.h"
16
17using ppapi::thunk::EnterResourceNoLock;
18using ppapi::thunk::PPB_TrueTypeFont_API;
19
20namespace {
21
22}  // namespace
23
24namespace ppapi {
25namespace proxy {
26
27TrueTypeFontResource::TrueTypeFontResource(Connection connection,
28                                           PP_Instance instance,
29                                           const PP_TrueTypeFontDesc_Dev& desc)
30    : PluginResource(connection, instance),
31      create_result_(PP_OK_COMPLETIONPENDING),
32      describe_desc_(NULL) {
33  SerializedTrueTypeFontDesc serialized_desc;
34  serialized_desc.SetFromPPTrueTypeFontDesc(desc);
35  SendCreate(BROWSER, PpapiHostMsg_TrueTypeFont_Create(serialized_desc));
36}
37
38TrueTypeFontResource::~TrueTypeFontResource() {
39}
40
41PPB_TrueTypeFont_API* TrueTypeFontResource::AsPPB_TrueTypeFont_API() {
42  return this;
43}
44
45int32_t TrueTypeFontResource::Describe(
46    PP_TrueTypeFontDesc_Dev* desc,
47    scoped_refptr<TrackedCallback> callback) {
48  if (describe_callback_.get())
49    return PP_ERROR_INPROGRESS;
50
51  if (create_result_ == PP_OK) {
52    desc_.CopyToPPTrueTypeFontDesc(desc);
53  } else if (create_result_ == PP_OK_COMPLETIONPENDING) {
54    describe_desc_ = desc;
55    describe_callback_ = callback;
56  }
57
58  return create_result_;
59}
60
61int32_t TrueTypeFontResource::GetTableTags(
62    const PP_ArrayOutput& output,
63    scoped_refptr<TrackedCallback> callback) {
64  Call<PpapiPluginMsg_TrueTypeFont_GetTableTagsReply>(
65      BROWSER,
66      PpapiHostMsg_TrueTypeFont_GetTableTags(),
67      base::Bind(&TrueTypeFontResource::OnPluginMsgGetTableTagsComplete,
68                 this,
69                 callback,
70                 output));
71
72  return PP_OK_COMPLETIONPENDING;
73}
74
75int32_t TrueTypeFontResource::GetTable(
76    uint32_t table,
77    int32_t offset,
78    int32_t max_data_length,
79    const PP_ArrayOutput& output,
80    scoped_refptr<TrackedCallback> callback) {
81  Call<PpapiPluginMsg_TrueTypeFont_GetTableReply>(
82      BROWSER,
83      PpapiHostMsg_TrueTypeFont_GetTable(table, offset, max_data_length),
84      base::Bind(&TrueTypeFontResource::OnPluginMsgGetTableComplete,
85                 this,
86                 callback,
87                 output));
88
89  return PP_OK_COMPLETIONPENDING;
90}
91
92void TrueTypeFontResource::OnReplyReceived(
93    const ResourceMessageReplyParams& params,
94    const IPC::Message& msg) {
95  PPAPI_BEGIN_MESSAGE_MAP(TrueTypeFontResource, msg)
96  PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(PpapiPluginMsg_TrueTypeFont_CreateReply,
97                                      OnPluginMsgCreateComplete)
98  PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED(
99      PluginResource::OnReplyReceived(params, msg))
100  PPAPI_END_MESSAGE_MAP()
101}
102
103void TrueTypeFontResource::OnPluginMsgCreateComplete(
104    const ResourceMessageReplyParams& params,
105    const ppapi::proxy::SerializedTrueTypeFontDesc& desc,
106    int32_t result) {
107  DCHECK(result != PP_OK_COMPLETIONPENDING);
108  DCHECK(create_result_ == PP_OK_COMPLETIONPENDING);
109  create_result_ = result;
110  if (create_result_ == PP_OK)
111    desc_ = desc;
112
113  // Now complete any pending Describe operation.
114  if (TrackedCallback::IsPending(describe_callback_)) {
115    desc_.CopyToPPTrueTypeFontDesc(describe_desc_);
116    describe_desc_ = NULL;
117    scoped_refptr<TrackedCallback> callback;
118    callback.swap(describe_callback_);
119    callback->Run(create_result_ == PP_OK ? PP_OK : PP_ERROR_FAILED);
120  }
121}
122
123void TrueTypeFontResource::OnPluginMsgGetTableTagsComplete(
124    scoped_refptr<TrackedCallback> callback,
125    PP_ArrayOutput array_output,
126    const ResourceMessageReplyParams& params,
127    const std::vector<uint32_t>& tag_array) {
128  // The result code should contain the data size if it's positive.
129  int32_t result = params.result();
130  DCHECK((result < 0 && tag_array.size() == 0) ||
131         result == static_cast<int32_t>(tag_array.size()));
132
133  ArrayWriter output;
134  output.set_pp_array_output(array_output);
135  if (output.is_valid())
136    output.StoreArray(&tag_array[0], std::max(0, result));
137  else
138    result = PP_ERROR_FAILED;
139
140  callback->Run(result);
141}
142
143void TrueTypeFontResource::OnPluginMsgGetTableComplete(
144    scoped_refptr<TrackedCallback> callback,
145    PP_ArrayOutput array_output,
146    const ResourceMessageReplyParams& params,
147    const std::string& data) {
148  // The result code should contain the data size if it's positive.
149  int32_t result = params.result();
150  DCHECK((result < 0 && data.size() == 0) ||
151         result == static_cast<int32_t>(data.size()));
152
153  ArrayWriter output;
154  output.set_pp_array_output(array_output);
155  if (output.is_valid())
156    output.StoreArray(data.data(), std::max(0, result));
157  else
158    result = PP_ERROR_FAILED;
159
160  callback->Run(result);
161}
162
163}  // namespace proxy
164}  // namespace ppapi
165