1// Copyright (c) 2012 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/flash_font_file_resource.h"
6
7#include <cstring>
8
9#include "ppapi/c/dev/ppb_font_dev.h"
10#include "ppapi/c/pp_errors.h"
11#include "ppapi/proxy/ppapi_messages.h"
12
13namespace ppapi {
14namespace proxy {
15
16FlashFontFileResource::FlashFontFileResource(
17    Connection connection,
18    PP_Instance instance,
19    const PP_BrowserFont_Trusted_Description* description,
20    PP_PrivateFontCharset charset)
21    : PluginResource(connection, instance),
22      charset_(charset) {
23  description_.SetFromPPBrowserFontDescription(*description);
24}
25
26FlashFontFileResource::~FlashFontFileResource() {
27}
28
29thunk::PPB_Flash_FontFile_API*
30    FlashFontFileResource::AsPPB_Flash_FontFile_API() {
31  return this;
32}
33
34PP_Bool FlashFontFileResource::GetFontTable(uint32_t table,
35                                            void* output,
36                                            uint32_t* output_length) {
37  if (!output_length)
38    return PP_FALSE;
39
40  if (!sent_create_to_renderer()) {
41    SendCreate(
42        RENDERER, PpapiHostMsg_FlashFontFile_Create(description_, charset_));
43  }
44
45  std::string* contents = GetFontTable(table);
46  if (!contents) {
47    std::string out_contents;
48    int32_t result = SyncCall<PpapiPluginMsg_FlashFontFile_GetFontTableReply>(
49        RENDERER, PpapiHostMsg_FlashFontFile_GetFontTable(table),
50        &out_contents);
51    if (result != PP_OK)
52      return PP_FALSE;
53
54    contents = AddFontTable(table, out_contents);
55  }
56
57  // If we are going to copy the data into |output|, it must be big enough.
58  if (output && *output_length < contents->size())
59    return PP_FALSE;
60
61  *output_length = static_cast<uint32_t>(contents->size());
62  if (output)
63    memcpy(output, contents->c_str(), *output_length);
64  return PP_TRUE;
65}
66
67std::string* FlashFontFileResource::GetFontTable(uint32_t table) const {
68  FontTableMap::const_iterator found = font_tables_.find(table);
69  if (found == font_tables_.end())
70    return NULL;
71  return found->second.get();
72}
73
74std::string* FlashFontFileResource::AddFontTable(uint32_t table,
75                                                 const std::string& contents) {
76  linked_ptr<std::string> heap_string(new std::string(contents));
77  font_tables_[table] = heap_string;
78  return heap_string.get();
79}
80
81}  // namespace proxy
82}  // namespace ppapi
83