mac: Fix DownloadItemController unit tests on Yosemite.
[chromium-blink-merge.git] / ppapi / proxy / truetype_font_resource.cc
blobe4b851ec5798df8bf65a61b45737fe99b0fa7602
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.
5 #include "ppapi/proxy/truetype_font_resource.h"
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"
17 using ppapi::thunk::EnterResourceNoLock;
18 using ppapi::thunk::PPB_TrueTypeFont_API;
20 namespace {
22 } // namespace
24 namespace ppapi {
25 namespace proxy {
27 TrueTypeFontResource::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));
38 TrueTypeFontResource::~TrueTypeFontResource() {
41 PPB_TrueTypeFont_API* TrueTypeFontResource::AsPPB_TrueTypeFont_API() {
42 return this;
45 int32_t TrueTypeFontResource::Describe(
46 PP_TrueTypeFontDesc_Dev* desc,
47 scoped_refptr<TrackedCallback> callback) {
48 if (describe_callback_.get())
49 return PP_ERROR_INPROGRESS;
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;
58 return create_result_;
61 int32_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));
72 return PP_OK_COMPLETIONPENDING;
75 int32_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));
89 return PP_OK_COMPLETIONPENDING;
92 void 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()
103 void 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;
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);
123 void 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()));
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;
140 callback->Run(result);
143 void 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()));
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;
160 callback->Run(result);
163 } // namespace proxy
164 } // namespace ppapi