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_singleton_resource.h" 6 7#include "base/bind.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" 15 16namespace ppapi { 17namespace proxy { 18 19TrueTypeFontSingletonResource::TrueTypeFontSingletonResource( 20 Connection connection, 21 PP_Instance instance) 22 : PluginResource(connection, instance) { 23 SendCreate(BROWSER, PpapiHostMsg_TrueTypeFontSingleton_Create()); 24} 25 26TrueTypeFontSingletonResource::~TrueTypeFontSingletonResource() { 27} 28 29thunk::PPB_TrueTypeFont_Singleton_API* 30TrueTypeFontSingletonResource::AsPPB_TrueTypeFont_Singleton_API() { 31 return this; 32} 33 34int32_t TrueTypeFontSingletonResource::GetFontFamilies( 35 PP_Instance instance, 36 const PP_ArrayOutput& output, 37 const scoped_refptr<TrackedCallback>& callback) { 38 Call<PpapiPluginMsg_TrueTypeFontSingleton_GetFontFamiliesReply>(BROWSER, 39 PpapiHostMsg_TrueTypeFontSingleton_GetFontFamilies(), 40 base::Bind( 41 &TrueTypeFontSingletonResource::OnPluginMsgGetFontFamiliesComplete, 42 this, callback, output)); 43 return PP_OK_COMPLETIONPENDING; 44} 45 46int32_t TrueTypeFontSingletonResource::GetFontsInFamily( 47 PP_Instance instance, 48 PP_Var family, 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()), 57 base::Bind( 58 &TrueTypeFontSingletonResource::OnPluginMsgGetFontsInFamilyComplete, 59 this, callback, output)); 60 return PP_OK_COMPLETIONPENDING; 61} 62 63void 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)) 69 return; 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())); 74 75 ArrayWriter output; 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); 83 } else { 84 result = PP_ERROR_FAILED; 85 } 86 87 callback->Run(result); 88} 89 90void 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)) 96 return; 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())); 101 ArrayWriter output; 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]); 109 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); 113 } 114 } else { 115 result = PP_ERROR_FAILED; 116 } 117 118 callback->Run(result); 119} 120 121} // namespace proxy 122} // namespace ppapi 123