|
OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2010 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/ppb_pdf_proxy.h" | |
6 | |
7 #include <string.h> // For memcpy | |
viettrungluu
2010/11/11 20:27:51
"
| |
8 | |
9 #include <map> | |
10 | |
11 #include "base/linked_ptr.h" | |
12 #include "base/logging.h" | |
13 #include "build/build_config.h" | |
14 #include "ppapi/proxy/plugin_dispatcher.h" | |
15 #include "ppapi/proxy/plugin_resource.h" | |
16 #include "ppapi/proxy/ppapi_messages.h" | |
17 #include "webkit/glue/plugins/ppb_private.h" | |
18 | |
19 namespace pp { | |
20 namespace proxy { | |
21 | |
22 class PrivateFontFile : public PluginResource { | |
23 public: | |
24 PrivateFontFile() {} | |
25 virtual ~PrivateFontFile() {} | |
26 | |
27 // Resource overrides. | |
28 virtual PrivateFontFile* AsPrivateFontFile() { return this; } | |
29 | |
30 // Sees if we have a cache of the font table and returns a pointer to it. | |
31 // Returns NULL if we don't have it. | |
32 std::string* GetFontTable(uint32_t table) const; | |
33 | |
34 std::string* AddFontTable(uint32_t table, const std::string& contents); | |
35 | |
36 private: | |
37 typedef std::map<uint32_t, linked_ptr<std::string> > FontTableMap; | |
38 FontTableMap font_tables_; | |
39 | |
40 DISALLOW_COPY_AND_ASSIGN(PrivateFontFile); | |
41 }; | |
42 | |
43 std::string* PrivateFontFile::GetFontTable(uint32_t table) const { | |
44 FontTableMap::const_iterator found = font_tables_.find(table); | |
45 if (found == font_tables_.end()) | |
46 return NULL; | |
47 return found->second.get(); | |
48 } | |
49 | |
50 std::string* PrivateFontFile::AddFontTable(uint32_t table, | |
51 const std::string& contents) { | |
52 linked_ptr<std::string> heap_string(new std::string(contents)); | |
53 font_tables_[table] = heap_string; | |
54 return heap_string.get(); | |
55 } | |
56 | |
57 namespace { | |
58 | |
59 PP_Resource GetFontFileWithFallback( | |
60 PP_Module module_id, | |
61 const PP_FontDescription_Dev* description, | |
62 PP_PrivateFontCharset charset) { | |
63 SerializedFontDescription desc; | |
64 // TODO(brettw): serialize the description! | |
65 | |
66 PP_Resource result = 0; | |
67 PluginDispatcher::Get()->Send( | |
68 new PpapiHostMsg_PPBPdf_GetFontFileWithFallback( | |
69 INTERFACE_ID_PPB_PDF, module_id, desc, charset, &result)); | |
70 if (!result) | |
71 return 0; | |
72 | |
73 linked_ptr<PrivateFontFile> object(new PrivateFontFile); | |
74 PluginDispatcher::Get()->plugin_resource_tracker()->AddResource( | |
75 result, object); | |
76 return result; | |
77 } | |
78 | |
79 bool GetFontTableForPrivateFontFile(PP_Resource font_file, | |
80 uint32_t table, | |
81 void* output, | |
82 uint32_t* output_length) { | |
83 PrivateFontFile* object = PluginResource::GetAs<PrivateFontFile>(font_file); | |
84 if (!object) | |
85 return false; | |
86 | |
87 std::string* contents = object->GetFontTable(table); | |
88 if (!contents) { | |
89 std::string deserialized; | |
90 PluginDispatcher::Get()->Send( | |
91 new PpapiHostMsg_PPBPdf_GetFontTableForPrivateFontFile( | |
92 INTERFACE_ID_PPB_PDF, font_file, table, &deserialized)); | |
93 if (deserialized.empty()) | |
94 return false; | |
95 contents = object->AddFontTable(table, deserialized); | |
96 } | |
97 | |
98 *output_length = static_cast<uint32_t>(contents->size()); | |
99 if (output) | |
100 memcpy(output, contents->c_str(), *output_length); | |
101 return true; | |
102 } | |
103 | |
104 const PPB_Private ppb_private = { | |
105 NULL, // &GetLocalizedString, | |
viettrungluu
2010/11/11 20:27:51
Are these TODOs?
brettw
2010/11/11 20:49:19
Not unless we want to run PDF out of process.
| |
106 NULL, // &GetResourceImage, | |
107 &GetFontFileWithFallback, | |
108 &GetFontTableForPrivateFontFile, | |
109 }; | |
110 | |
111 } // namespace | |
112 | |
113 PPB_Pdf_Proxy::PPB_Pdf_Proxy(Dispatcher* dispatcher, | |
114 const void* target_interface) | |
115 : InterfaceProxy(dispatcher, target_interface) { | |
116 } | |
117 | |
118 PPB_Pdf_Proxy::~PPB_Pdf_Proxy() { | |
119 } | |
120 | |
121 const void* PPB_Pdf_Proxy::GetSourceInterface() const { | |
122 return &ppb_private; | |
123 } | |
124 | |
125 InterfaceID PPB_Pdf_Proxy::GetInterfaceId() const { | |
126 return INTERFACE_ID_PPB_PDF; | |
127 } | |
128 | |
129 void PPB_Pdf_Proxy::OnMessageReceived(const IPC::Message& msg) { | |
130 IPC_BEGIN_MESSAGE_MAP(PPB_Pdf_Proxy, msg) | |
131 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBPdf_GetFontFileWithFallback, | |
132 OnMsgGetFontFileWithFallback) | |
133 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBPdf_GetFontTableForPrivateFontFile, | |
134 OnMsgGetFontTableForPrivateFontFile) | |
135 IPC_END_MESSAGE_MAP() | |
136 // TODO(brettw): handle bad messages! | |
137 } | |
138 | |
139 void PPB_Pdf_Proxy::OnMsgGetFontFileWithFallback( | |
140 PP_Module module, | |
141 const SerializedFontDescription& in_desc, | |
142 int32_t charset, | |
143 PP_Resource* result) { | |
144 PP_FontDescription_Dev desc; | |
145 // TODO(brettw) deserialize this value! | |
146 *result = ppb_pdf_target()->GetFontFileWithFallback(module, &desc, | |
147 static_cast<PP_PrivateFontCharset>(charset)); | |
148 } | |
149 | |
150 void PPB_Pdf_Proxy::OnMsgGetFontTableForPrivateFontFile(PP_Resource font_file, | |
151 uint32_t table, | |
152 std::string* result) { | |
153 // TODO(brettw): It would be nice not to copy here. At least on Linux, | |
154 // we can map the font file into shared memory and read it that way. | |
155 uint32_t table_length = 0; | |
156 if (!ppb_pdf_target()->GetFontTableForPrivateFontFile( | |
157 font_file, table, NULL, &table_length)) | |
158 return; | |
159 | |
160 result->resize(table_length); | |
161 ppb_pdf_target()->GetFontTableForPrivateFontFile(font_file, table, | |
162 const_cast<char*>(result->c_str()), &table_length); | |
163 } | |
164 | |
165 } // namespace proxy | |
166 } // namespace pp | |
OLD | NEW |