Fix bug in load time stats.
[chromium-blink-merge.git] / ppapi / proxy / ppb_pdf_proxy.cc
blobaee308f38a885ae8539ee9b206346adf5c55aa9a
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.
5 #include "ppapi/proxy/ppb_pdf_proxy.h"
7 #include <string.h> // For memcpy.
9 #include <map>
11 #include "base/logging.h"
12 #include "base/memory/linked_ptr.h"
13 #include "build/build_config.h"
14 #include "ppapi/c/private/ppb_pdf.h"
15 #include "ppapi/proxy/plugin_dispatcher.h"
16 #include "ppapi/proxy/plugin_resource_tracker.h"
17 #include "ppapi/proxy/ppapi_messages.h"
18 #include "ppapi/thunk/enter.h"
19 #include "ppapi/thunk/ppb_pdf_api.h"
21 using ppapi::thunk::PPB_PDFFont_API;
22 using ppapi::thunk::EnterResource;
24 namespace ppapi {
25 namespace proxy {
27 class PrivateFontFile : public Resource,
28 public PPB_PDFFont_API {
29 public:
30 PrivateFontFile(const HostResource& resource)
31 : Resource(OBJECT_IS_PROXY, resource) {
33 virtual ~PrivateFontFile() {}
35 PPB_PDFFont_API* AsPPB_PDFFont_API() { return this; }
37 // Sees if we have a cache of the font table and returns a pointer to it.
38 // Returns NULL if we don't have it.
39 std::string* GetFontTable(uint32_t table) const;
41 std::string* AddFontTable(uint32_t table, const std::string& contents);
43 private:
44 typedef std::map<uint32_t, linked_ptr<std::string> > FontTableMap;
45 FontTableMap font_tables_;
47 DISALLOW_COPY_AND_ASSIGN(PrivateFontFile);
50 std::string* PrivateFontFile::GetFontTable(uint32_t table) const {
51 FontTableMap::const_iterator found = font_tables_.find(table);
52 if (found == font_tables_.end())
53 return NULL;
54 return found->second.get();
57 std::string* PrivateFontFile::AddFontTable(uint32_t table,
58 const std::string& contents) {
59 linked_ptr<std::string> heap_string(new std::string(contents));
60 font_tables_[table] = heap_string;
61 return heap_string.get();
64 namespace {
66 PP_Resource GetFontFileWithFallback(
67 PP_Instance instance,
68 const PP_FontDescription_Dev* description,
69 PP_PrivateFontCharset charset) {
70 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
71 if (!dispatcher)
72 return 0;
74 SerializedFontDescription desc;
75 desc.SetFromPPFontDescription(dispatcher, *description, true);
77 HostResource result;
78 dispatcher->Send(new PpapiHostMsg_PPBPDF_GetFontFileWithFallback(
79 API_ID_PPB_PDF, instance, desc, charset, &result));
80 if (result.is_null())
81 return 0;
82 return (new PrivateFontFile(result))->GetReference();
85 bool GetFontTableForPrivateFontFile(PP_Resource font_file,
86 uint32_t table,
87 void* output,
88 uint32_t* output_length) {
89 EnterResource<PPB_PDFFont_API> enter(font_file, true);
90 if (enter.failed())
91 return false;
93 PrivateFontFile* object = static_cast<PrivateFontFile*>(enter.object());
94 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(
95 object->pp_instance());
96 if (!dispatcher)
97 return false;
99 std::string* contents = object->GetFontTable(table);
100 if (!contents) {
101 std::string deserialized;
102 dispatcher->Send(new PpapiHostMsg_PPBPDF_GetFontTableForPrivateFontFile(
103 API_ID_PPB_PDF, object->host_resource(), table, &deserialized));
104 if (deserialized.empty())
105 return false;
106 contents = object->AddFontTable(table, deserialized);
109 *output_length = static_cast<uint32_t>(contents->size());
110 if (output)
111 memcpy(output, contents->c_str(), *output_length);
112 return true;
115 const PPB_PDF pdf_interface = {
116 NULL, // &GetLocalizedString,
117 NULL, // &GetResourceImage,
118 &GetFontFileWithFallback,
119 &GetFontTableForPrivateFontFile,
122 InterfaceProxy* CreatePDFProxy(Dispatcher* dispatcher) {
123 return new PPB_PDF_Proxy(dispatcher);
126 } // namespace
128 PPB_PDF_Proxy::PPB_PDF_Proxy(Dispatcher* dispatcher)
129 : InterfaceProxy(dispatcher),
130 ppb_pdf_impl_(NULL) {
131 if (!dispatcher->IsPlugin()) {
132 ppb_pdf_impl_ = static_cast<const PPB_PDF*>(
133 dispatcher->local_get_interface()(PPB_PDF_INTERFACE));
137 PPB_PDF_Proxy::~PPB_PDF_Proxy() {
140 // static
141 const InterfaceProxy::Info* PPB_PDF_Proxy::GetInfo() {
142 static const Info info = {
143 &pdf_interface,
144 PPB_PDF_INTERFACE,
145 API_ID_PPB_PDF,
146 true,
147 &CreatePDFProxy,
149 return &info;
152 bool PPB_PDF_Proxy::OnMessageReceived(const IPC::Message& msg) {
153 bool handled = true;
154 IPC_BEGIN_MESSAGE_MAP(PPB_PDF_Proxy, msg)
155 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBPDF_GetFontFileWithFallback,
156 OnMsgGetFontFileWithFallback)
157 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBPDF_GetFontTableForPrivateFontFile,
158 OnMsgGetFontTableForPrivateFontFile)
159 IPC_MESSAGE_UNHANDLED(handled = false)
160 IPC_END_MESSAGE_MAP()
161 // TODO(brettw): handle bad messages!
162 return handled;
165 void PPB_PDF_Proxy::OnMsgGetFontFileWithFallback(
166 PP_Instance instance,
167 const SerializedFontDescription& in_desc,
168 int32_t charset,
169 HostResource* result) {
170 PP_FontDescription_Dev desc;
171 in_desc.SetToPPFontDescription(dispatcher(), &desc, false);
172 result->SetHostResource(instance,
173 ppb_pdf_impl_->GetFontFileWithFallback(
174 instance, &desc, static_cast<PP_PrivateFontCharset>(charset)));
177 void PPB_PDF_Proxy::OnMsgGetFontTableForPrivateFontFile(
178 const HostResource& font_file,
179 uint32_t table,
180 std::string* result) {
181 // TODO(brettw): It would be nice not to copy here. At least on Linux,
182 // we can map the font file into shared memory and read it that way.
183 uint32_t table_length = 0;
184 if (!ppb_pdf_impl_->GetFontTableForPrivateFontFile(
185 font_file.host_resource(), table, NULL, &table_length))
186 return;
188 result->resize(table_length);
189 ppb_pdf_impl_->GetFontTableForPrivateFontFile(font_file.host_resource(),
190 table, const_cast<char*>(result->c_str()), &table_length);
193 } // namespace proxy
194 } // namespace ppapi