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_singleton_resource.h"
8 #include "ppapi/proxy/ppapi_messages.h"
9 #include "ppapi/proxy/serialized_structs.h"
10 #include "ppapi/shared_impl/array_writer.h"
11 #include "ppapi/shared_impl/ppapi_globals.h"
12 #include "ppapi/shared_impl/tracked_callback.h"
13 #include "ppapi/shared_impl/var.h"
14 #include "ppapi/shared_impl/var_tracker.h"
19 TrueTypeFontSingletonResource::TrueTypeFontSingletonResource(
20 Connection connection,
22 : PluginResource(connection, instance) {
23 SendCreate(BROWSER, PpapiHostMsg_TrueTypeFontSingleton_Create());
26 TrueTypeFontSingletonResource::~TrueTypeFontSingletonResource() {
29 thunk::PPB_TrueTypeFont_Singleton_API*
30 TrueTypeFontSingletonResource::AsPPB_TrueTypeFont_Singleton_API() {
34 int32_t TrueTypeFontSingletonResource::GetFontFamilies(
36 const PP_ArrayOutput& output,
37 const scoped_refptr<TrackedCallback>& callback) {
38 Call<PpapiPluginMsg_TrueTypeFontSingleton_GetFontFamiliesReply>(BROWSER,
39 PpapiHostMsg_TrueTypeFontSingleton_GetFontFamilies(),
41 &TrueTypeFontSingletonResource::OnPluginMsgGetFontFamiliesComplete,
42 this, callback, output));
43 return PP_OK_COMPLETIONPENDING;
46 int32_t TrueTypeFontSingletonResource::GetFontsInFamily(
49 const PP_ArrayOutput& output,
50 const scoped_refptr<TrackedCallback>& callback) {
51 scoped_refptr<StringVar> family_var = StringVar::FromPPVar(family);
52 const uint32_t kMaxFamilySizeInBytes = 1024;
53 if (!family_var.get() || family_var->value().size() > kMaxFamilySizeInBytes)
54 return PP_ERROR_BADARGUMENT;
55 Call<PpapiPluginMsg_TrueTypeFontSingleton_GetFontsInFamilyReply>(BROWSER,
56 PpapiHostMsg_TrueTypeFontSingleton_GetFontsInFamily(family_var->value()),
58 &TrueTypeFontSingletonResource::OnPluginMsgGetFontsInFamilyComplete,
59 this, callback, output));
60 return PP_OK_COMPLETIONPENDING;
63 void TrueTypeFontSingletonResource::OnPluginMsgGetFontFamiliesComplete(
64 scoped_refptr<TrackedCallback> callback,
65 PP_ArrayOutput array_output,
66 const ResourceMessageReplyParams& params,
67 const std::vector<std::string>& font_families) {
68 if (!TrackedCallback::IsPending(callback))
70 // The result code should contain the data size if it's positive.
71 int32_t result = params.result();
72 DCHECK((result < 0 && font_families.size() == 0) ||
73 result == static_cast<int32_t>(font_families.size()));
76 output.set_pp_array_output(array_output);
77 if (output.is_valid()) {
78 std::vector< scoped_refptr<Var> > font_family_vars;
79 for (size_t i = 0; i < font_families.size(); i++)
80 font_family_vars.push_back(
81 scoped_refptr<Var>(new StringVar(font_families[i])));
82 output.StoreVarVector(font_family_vars);
84 result = PP_ERROR_FAILED;
87 callback->Run(result);
90 void TrueTypeFontSingletonResource::OnPluginMsgGetFontsInFamilyComplete(
91 scoped_refptr<TrackedCallback> callback,
92 PP_ArrayOutput array_output,
93 const ResourceMessageReplyParams& params,
94 const std::vector<SerializedTrueTypeFontDesc>& fonts) {
95 if (!TrackedCallback::IsPending(callback))
97 // The result code should contain the data size if it's positive.
98 int32_t result = params.result();
99 DCHECK((result < 0 && fonts.size() == 0) ||
100 result == static_cast<int32_t>(fonts.size()));
102 output.set_pp_array_output(array_output);
103 if (output.is_valid()) {
104 // Convert the message data to an array of PP_TrueTypeFontDesc_Dev structs.
105 // Each desc has an embedded PP_Var containing the family name.
106 std::vector<PP_TrueTypeFontDesc_Dev> pp_fonts(fonts.size());
107 for (size_t i = 0; i < fonts.size(); i++)
108 fonts[i].CopyToPPTrueTypeFontDesc(&pp_fonts[i]);
110 if (!output.StoreVector(pp_fonts)) {
111 for (size_t i = 0; i < pp_fonts.size(); i++)
112 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(pp_fonts[i].family);
115 result = PP_ERROR_FAILED;
118 callback->Run(result);