OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Native Client 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 "native_client/src/shared/ppapi_proxy/plugin_ppb_pdf.h" |
| 6 |
| 7 #include <stdio.h> |
| 8 #include <stdlib.h> |
| 9 #include <string.h> |
| 10 #include <sys/mman.h> |
| 11 |
| 12 #include "native_client/src/include/nacl_scoped_ptr.h" |
| 13 #include "native_client/src/include/portability.h" |
| 14 #include "native_client/src/shared/ppapi_proxy/object_serialize.h" |
| 15 #include "native_client/src/shared/ppapi_proxy/plugin_globals.h" |
| 16 #include "native_client/src/shared/ppapi_proxy/plugin_ppb_image_data.h" |
| 17 #include "native_client/src/shared/ppapi_proxy/plugin_resource.h" |
| 18 #include "native_client/src/shared/ppapi_proxy/utility.h" |
| 19 #include "native_client/src/shared/srpc/nacl_srpc.h" |
| 20 #include "ppapi/c/private/ppb_pdf.h" |
| 21 #include "srpcgen/ppb_rpc.h" |
| 22 |
| 23 namespace ppapi_proxy { |
| 24 |
| 25 namespace { |
| 26 |
| 27 const nacl_abi_size_t kPpbPrivateFindResultBytes = |
| 28 static_cast<nacl_abi_size_t>(sizeof(PP_PrivateFindResult)); |
| 29 // Limit the maximum result buffer size. |
| 30 const nacl_abi_size_t kMaxFindResultsBytes = 65536; |
| 31 const int32_t kMaxFindResults = |
| 32 kMaxFindResultsBytes / kPpbPrivateFindResultBytes; |
| 33 |
| 34 PP_Var GetLocalizedString( |
| 35 PP_Instance instance, |
| 36 PP_ResourceString string_id) { |
| 37 DebugPrintf("PPB_PDF::GetLocalizedString: " |
| 38 "instance=%"NACL_PRIu32"\n", instance); |
| 39 |
| 40 NaClSrpcChannel* channel = GetMainSrpcChannel(); |
| 41 nacl_abi_size_t string_size = kMaxVarSize; |
| 42 nacl::scoped_array<char> string_bytes(new char[kMaxVarSize]); |
| 43 NaClSrpcError srpc_result = |
| 44 PpbPdfRpcClient::PPB_PDF_GetLocalizedString( |
| 45 channel, |
| 46 instance, |
| 47 static_cast<int32_t>(string_id), |
| 48 &string_size, string_bytes.get()); |
| 49 |
| 50 PP_Var string = PP_MakeUndefined(); |
| 51 if (srpc_result == NACL_SRPC_RESULT_OK) { |
| 52 (void) DeserializeTo( |
| 53 channel, string_bytes.get(), string_size, 1, &string); |
| 54 } |
| 55 |
| 56 DebugPrintf("PPB_PDF::GetLocalizedString: %s\n", |
| 57 NaClSrpcErrorString(srpc_result)); |
| 58 return string; |
| 59 } |
| 60 |
| 61 PP_Resource GetResourceImage(PP_Instance instance, |
| 62 PP_ResourceImage image_id) { |
| 63 DebugPrintf("PPB_PDF::GetResourceImage: " |
| 64 "instance=%"NACL_PRIu32"\n", instance); |
| 65 |
| 66 PP_Resource image = kInvalidResourceId; |
| 67 NaClSrpcError srpc_result = |
| 68 PpbPdfRpcClient::PPB_PDF_GetResourceImage( |
| 69 GetMainSrpcChannel(), |
| 70 instance, |
| 71 static_cast<int32_t>(image_id), |
| 72 &image); |
| 73 |
| 74 DebugPrintf("PPB_PDF::GetResourceImage: %s\n", |
| 75 NaClSrpcErrorString(srpc_result)); |
| 76 if (srpc_result == NACL_SRPC_RESULT_OK) { |
| 77 scoped_refptr<PluginImageData> image_data = |
| 78 PluginResource::AdoptAs<PluginImageData>(image); |
| 79 if (image_data.get()) { |
| 80 return image; |
| 81 } |
| 82 } |
| 83 return kInvalidResourceId; |
| 84 } |
| 85 |
| 86 PP_Resource GetFontFileWithFallback( |
| 87 PP_Instance instance, |
| 88 const struct PP_FontDescription_Dev* description, |
| 89 PP_PrivateFontCharset charset) { |
| 90 DebugPrintf("PPB_PDF::GetFontFileWithFallback: Not Implemented\n"); |
| 91 return kInvalidResourceId; |
| 92 } |
| 93 |
| 94 bool GetFontTableForPrivateFontFile(PP_Resource font_file, |
| 95 uint32_t table, |
| 96 void* output, |
| 97 uint32_t* output_length) { |
| 98 DebugPrintf("PPB_PDF::GetFontTableForPrivateFontFile: Not Implemented\n"); |
| 99 *output_length = 0; |
| 100 return false; |
| 101 } |
| 102 |
| 103 nacl_abi_size_t utf16_length(const unsigned short* str) { |
| 104 const unsigned short* s = str; |
| 105 while (*s) |
| 106 ++s; |
| 107 return (s - str); |
| 108 } |
| 109 |
| 110 void SearchString(PP_Instance instance, |
| 111 const unsigned short* string, |
| 112 const unsigned short* term, |
| 113 bool case_sensitive, |
| 114 struct PP_PrivateFindResult** results, |
| 115 int* count) { |
| 116 DebugPrintf("PPB_PDF::SearchString: instance=%"NACL_PRIu32"\n", instance); |
| 117 |
| 118 CHECK(string != NULL); |
| 119 CHECK(term != NULL); |
| 120 CHECK(results != NULL); |
| 121 CHECK(count != NULL); |
| 122 |
| 123 nacl_abi_size_t find_results_size = kMaxFindResultsBytes; |
| 124 *results = reinterpret_cast<PP_PrivateFindResult*>(malloc(find_results_size)); |
| 125 *count = 0; |
| 126 |
| 127 nacl_abi_size_t string_length = utf16_length(string); |
| 128 nacl_abi_size_t term_length = utf16_length(term); |
| 129 if (string_length > 0 && term_length > 0) { |
| 130 NaClSrpcError srpc_result = |
| 131 PpbPdfRpcClient::PPB_PDF_SearchString( |
| 132 GetMainSrpcChannel(), |
| 133 instance, |
| 134 (string_length + 1) * sizeof(unsigned short), // include NULL |
| 135 reinterpret_cast<char*>(const_cast<unsigned short*>(string)), |
| 136 (term_length + 1) * sizeof(unsigned short), // include NULL |
| 137 reinterpret_cast<char*>(const_cast<unsigned short*>(term)), |
| 138 static_cast<int32_t>(case_sensitive), |
| 139 &find_results_size, |
| 140 reinterpret_cast<char*>(*results), |
| 141 reinterpret_cast<int32_t*>(count)); |
| 142 |
| 143 DebugPrintf("PPB_PDF::SearchString: %s\n", |
| 144 NaClSrpcErrorString(srpc_result)); |
| 145 } |
| 146 } |
| 147 |
| 148 void DidStartLoading(PP_Instance instance) { |
| 149 DebugPrintf("PPB_PDF::DidStartLoading: instance=%"NACL_PRIu32"\n", |
| 150 instance); |
| 151 |
| 152 NaClSrpcError srpc_result = |
| 153 PpbPdfRpcClient::PPB_PDF_DidStartLoading(GetMainSrpcChannel(), |
| 154 instance); |
| 155 |
| 156 DebugPrintf("PPB_PDF::DidStartLoading: %s\n", |
| 157 NaClSrpcErrorString(srpc_result)); |
| 158 } |
| 159 |
| 160 void DidStopLoading(PP_Instance instance) { |
| 161 DebugPrintf("PPB_PDF::DidStopLoading: instance=%"NACL_PRIu32"\n", |
| 162 instance); |
| 163 |
| 164 NaClSrpcError srpc_result = |
| 165 PpbPdfRpcClient::PPB_PDF_DidStopLoading(GetMainSrpcChannel(), |
| 166 instance); |
| 167 |
| 168 DebugPrintf("PPB_PDF::DidStopLoading: %s\n", |
| 169 NaClSrpcErrorString(srpc_result)); |
| 170 } |
| 171 |
| 172 void SetContentRestriction(PP_Instance instance, |
| 173 int restrictions) { |
| 174 DebugPrintf("PPB_PDF::SetContentRestriction: instance=%"NACL_PRIu32"\n", |
| 175 instance); |
| 176 |
| 177 NaClSrpcError srpc_result = |
| 178 PpbPdfRpcClient::PPB_PDF_SetContentRestriction(GetMainSrpcChannel(), |
| 179 instance, |
| 180 restrictions); |
| 181 |
| 182 DebugPrintf("PPB_PDF::SetContentRestriction: %s\n", |
| 183 NaClSrpcErrorString(srpc_result)); |
| 184 } |
| 185 |
| 186 void HistogramPDFPageCount(int count) { |
| 187 DebugPrintf("PPB_PDF::HistogramPDFPageCount/n"); |
| 188 |
| 189 NaClSrpcError srpc_result = |
| 190 PpbPdfRpcClient::PPB_PDF_HistogramPDFPageCount(GetMainSrpcChannel(), |
| 191 count); |
| 192 |
| 193 DebugPrintf("PPB_PDF::HistogramPDFPageCount: %s\n", |
| 194 NaClSrpcErrorString(srpc_result)); |
| 195 } |
| 196 |
| 197 void UserMetricsRecordAction(struct PP_Var action) { |
| 198 DebugPrintf("PPB_PDF::UserMetricsRecordAction\n"); |
| 199 |
| 200 nacl_abi_size_t action_size = kMaxVarSize; |
| 201 nacl::scoped_array<char> action_bytes( |
| 202 Serialize(&action, 1, &action_size)); |
| 203 NaClSrpcError srpc_result = |
| 204 PpbPdfRpcClient::PPB_PDF_UserMetricsRecordAction(GetMainSrpcChannel(), |
| 205 action_size, |
| 206 action_bytes.get()); |
| 207 |
| 208 DebugPrintf("PPB_PDF::UserMetricsRecordAction: %s\n", |
| 209 NaClSrpcErrorString(srpc_result)); |
| 210 } |
| 211 |
| 212 void HasUnsupportedFeature(PP_Instance instance) { |
| 213 DebugPrintf("PPB_PDF::HasUnsupportedFeature: instance=%"NACL_PRIu32"\n", |
| 214 instance); |
| 215 |
| 216 NaClSrpcError srpc_result = |
| 217 PpbPdfRpcClient::PPB_PDF_HasUnsupportedFeature(GetMainSrpcChannel(), |
| 218 instance); |
| 219 |
| 220 DebugPrintf("PPB_PDF::HasUnsupportedFeature: %s\n", |
| 221 NaClSrpcErrorString(srpc_result)); |
| 222 } |
| 223 |
| 224 void SaveAs(PP_Instance instance) { |
| 225 DebugPrintf("PPB_PDF::SaveAs: instance=%"NACL_PRIu32"\n", instance); |
| 226 |
| 227 NaClSrpcError srpc_result = |
| 228 PpbPdfRpcClient::PPB_PDF_SaveAs(GetMainSrpcChannel(), |
| 229 instance); |
| 230 |
| 231 DebugPrintf("PPB_PDF::SaveAs: %s\n", NaClSrpcErrorString(srpc_result)); |
| 232 } |
| 233 |
| 234 } // namespace |
| 235 |
| 236 const PPB_PDF* PluginPDF::GetInterface() { |
| 237 static const PPB_PDF pdf_interface = { |
| 238 GetLocalizedString, |
| 239 GetResourceImage, |
| 240 GetFontFileWithFallback, |
| 241 GetFontTableForPrivateFontFile, |
| 242 SearchString, |
| 243 DidStartLoading, |
| 244 DidStopLoading, |
| 245 SetContentRestriction, |
| 246 HistogramPDFPageCount, |
| 247 UserMetricsRecordAction, |
| 248 HasUnsupportedFeature, |
| 249 SaveAs |
| 250 }; |
| 251 return &pdf_interface; |
| 252 } |
| 253 |
| 254 } // namespace ppapi_proxy |
| 255 |
OLD | NEW |