| 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 "build/build_config.h" | |
| 6 | |
| 7 #include "webkit/glue/plugins/pepper_private.h" | |
| 8 | |
| 9 #include "unicode/usearch.h" | |
| 10 | |
| 11 #include "app/resource_bundle.h" | |
| 12 #include "base/metrics/histogram.h" | |
| 13 #include "base/utf_string_conversions.h" | |
| 14 #include "grit/webkit_resources.h" | |
| 15 #include "grit/webkit_strings.h" | |
| 16 #include "skia/ext/platform_canvas.h" | |
| 17 #include "ppapi/c/pp_resource.h" | |
| 18 #include "third_party/skia/include/core/SkBitmap.h" | |
| 19 #include "webkit/glue/webkit_glue.h" | |
| 20 #include "webkit/glue/plugins/pepper_image_data.h" | |
| 21 #include "webkit/glue/plugins/pepper_plugin_delegate.h" | |
| 22 #include "webkit/glue/plugins/pepper_plugin_instance.h" | |
| 23 #include "webkit/glue/plugins/pepper_plugin_module.h" | |
| 24 #include "webkit/glue/plugins/pepper_var.h" | |
| 25 #include "webkit/glue/plugins/ppb_private.h" | |
| 26 #include "webkit/glue/plugins/pepper_var.h" | |
| 27 | |
| 28 namespace pepper { | |
| 29 | |
| 30 #if defined(OS_LINUX) | |
| 31 class PrivateFontFile : public Resource { | |
| 32 public: | |
| 33 PrivateFontFile(PluginModule* module, int fd) | |
| 34 : Resource(module), | |
| 35 fd_(fd) { | |
| 36 } | |
| 37 virtual ~PrivateFontFile() { | |
| 38 } | |
| 39 | |
| 40 // Resource overrides. | |
| 41 PrivateFontFile* AsPrivateFontFile() { return this; } | |
| 42 | |
| 43 bool GetFontTable(uint32_t table, | |
| 44 void* output, | |
| 45 uint32_t* output_length); | |
| 46 | |
| 47 private: | |
| 48 int fd_; | |
| 49 }; | |
| 50 #endif | |
| 51 | |
| 52 namespace { | |
| 53 | |
| 54 struct ResourceImageInfo { | |
| 55 PP_ResourceImage pp_id; | |
| 56 int res_id; | |
| 57 }; | |
| 58 | |
| 59 static const ResourceImageInfo kResourceImageMap[] = { | |
| 60 { PP_RESOURCEIMAGE_PDF_BUTTON_FTH, IDR_PDF_BUTTON_FTH }, | |
| 61 { PP_RESOURCEIMAGE_PDF_BUTTON_FTH_HOVER, IDR_PDF_BUTTON_FTH_HOVER }, | |
| 62 { PP_RESOURCEIMAGE_PDF_BUTTON_FTH_PRESSED, IDR_PDF_BUTTON_FTH_PRESSED }, | |
| 63 { PP_RESOURCEIMAGE_PDF_BUTTON_FTW, IDR_PDF_BUTTON_FTW }, | |
| 64 { PP_RESOURCEIMAGE_PDF_BUTTON_FTW_HOVER, IDR_PDF_BUTTON_FTW_HOVER }, | |
| 65 { PP_RESOURCEIMAGE_PDF_BUTTON_FTW_PRESSED, IDR_PDF_BUTTON_FTW_PRESSED }, | |
| 66 { PP_RESOURCEIMAGE_PDF_BUTTON_ZOOMIN, IDR_PDF_BUTTON_ZOOMIN }, | |
| 67 { PP_RESOURCEIMAGE_PDF_BUTTON_ZOOMIN_HOVER, IDR_PDF_BUTTON_ZOOMIN_HOVER }, | |
| 68 { PP_RESOURCEIMAGE_PDF_BUTTON_ZOOMIN_PRESSED, IDR_PDF_BUTTON_ZOOMIN_PRESSED }, | |
| 69 { PP_RESOURCEIMAGE_PDF_BUTTON_ZOOMOUT, IDR_PDF_BUTTON_ZOOMOUT }, | |
| 70 { PP_RESOURCEIMAGE_PDF_BUTTON_ZOOMOUT_HOVER, IDR_PDF_BUTTON_ZOOMOUT_HOVER }, | |
| 71 { PP_RESOURCEIMAGE_PDF_BUTTON_ZOOMOUT_PRESSED, | |
| 72 IDR_PDF_BUTTON_ZOOMOUT_PRESSED }, | |
| 73 { PP_RESOURCEIMAGE_PDF_BUTTON_THUMBNAIL_0, IDR_PDF_THUMBNAIL_0 }, | |
| 74 { PP_RESOURCEIMAGE_PDF_BUTTON_THUMBNAIL_1, IDR_PDF_THUMBNAIL_1 }, | |
| 75 { PP_RESOURCEIMAGE_PDF_BUTTON_THUMBNAIL_2, IDR_PDF_THUMBNAIL_2 }, | |
| 76 { PP_RESOURCEIMAGE_PDF_BUTTON_THUMBNAIL_3, IDR_PDF_THUMBNAIL_3 }, | |
| 77 { PP_RESOURCEIMAGE_PDF_BUTTON_THUMBNAIL_4, IDR_PDF_THUMBNAIL_4 }, | |
| 78 { PP_RESOURCEIMAGE_PDF_BUTTON_THUMBNAIL_5, IDR_PDF_THUMBNAIL_5 }, | |
| 79 { PP_RESOURCEIMAGE_PDF_BUTTON_THUMBNAIL_6, IDR_PDF_THUMBNAIL_6 }, | |
| 80 { PP_RESOURCEIMAGE_PDF_BUTTON_THUMBNAIL_7, IDR_PDF_THUMBNAIL_7 }, | |
| 81 { PP_RESOURCEIMAGE_PDF_BUTTON_THUMBNAIL_8, IDR_PDF_THUMBNAIL_8 }, | |
| 82 { PP_RESOURCEIMAGE_PDF_BUTTON_THUMBNAIL_9, IDR_PDF_THUMBNAIL_9 }, | |
| 83 { PP_RESOURCEIMAGE_PDF_BUTTON_THUMBNAIL_NUM_BACKGROUND, | |
| 84 IDR_PDF_THUMBNAIL_NUM_BACKGROUND }, | |
| 85 }; | |
| 86 | |
| 87 PP_Var GetLocalizedString(PP_Module module_id, PP_ResourceString string_id) { | |
| 88 PluginModule* module = ResourceTracker::Get()->GetModule(module_id); | |
| 89 if (!module) | |
| 90 return PP_MakeUndefined(); | |
| 91 | |
| 92 std::string rv; | |
| 93 if (string_id == PP_RESOURCESTRING_PDFGETPASSWORD) { | |
| 94 rv = UTF16ToUTF8(webkit_glue::GetLocalizedString(IDS_PDF_NEED_PASSWORD)); | |
| 95 } else if (string_id == PP_RESOURCESTRING_PDFLOADING) { | |
| 96 rv = UTF16ToUTF8(webkit_glue::GetLocalizedString(IDS_PDF_PAGE_LOADING)); | |
| 97 } else if (string_id == PP_RESOURCESTRING_PDFLOAD_FAILED) { | |
| 98 rv = UTF16ToUTF8(webkit_glue::GetLocalizedString(IDS_PDF_PAGE_LOAD_FAILED)); | |
| 99 } else { | |
| 100 NOTREACHED(); | |
| 101 } | |
| 102 | |
| 103 return StringVar::StringToPPVar(module, rv); | |
| 104 } | |
| 105 | |
| 106 PP_Resource GetResourceImage(PP_Module module_id, PP_ResourceImage image_id) { | |
| 107 int res_id = 0; | |
| 108 for (size_t i = 0; i < arraysize(kResourceImageMap); ++i) { | |
| 109 if (kResourceImageMap[i].pp_id == image_id) { | |
| 110 res_id = kResourceImageMap[i].res_id; | |
| 111 break; | |
| 112 } | |
| 113 } | |
| 114 if (res_id == 0) | |
| 115 return 0; | |
| 116 | |
| 117 SkBitmap* res_bitmap = | |
| 118 ResourceBundle::GetSharedInstance().GetBitmapNamed(res_id); | |
| 119 | |
| 120 PluginModule* module = ResourceTracker::Get()->GetModule(module_id); | |
| 121 if (!module) | |
| 122 return 0; | |
| 123 scoped_refptr<pepper::ImageData> image_data(new pepper::ImageData(module)); | |
| 124 if (!image_data->Init(ImageData::GetNativeImageDataFormat(), | |
| 125 res_bitmap->width(), res_bitmap->height(), false)) { | |
| 126 return 0; | |
| 127 } | |
| 128 | |
| 129 ImageDataAutoMapper mapper(image_data); | |
| 130 if (!mapper.is_valid()) | |
| 131 return 0; | |
| 132 | |
| 133 skia::PlatformCanvas* canvas = image_data->mapped_canvas(); | |
| 134 SkBitmap& ret_bitmap = | |
| 135 const_cast<SkBitmap&>(canvas->getTopPlatformDevice().accessBitmap(true)); | |
| 136 if (!res_bitmap->copyTo(&ret_bitmap, SkBitmap::kARGB_8888_Config, NULL)) { | |
| 137 return 0; | |
| 138 } | |
| 139 | |
| 140 return image_data->GetReference(); | |
| 141 } | |
| 142 | |
| 143 PP_Resource GetFontFileWithFallback( | |
| 144 PP_Module module_id, | |
| 145 const PP_FontDescription_Dev* description, | |
| 146 PP_PrivateFontCharset charset) { | |
| 147 #if defined(OS_LINUX) | |
| 148 PluginModule* module = ResourceTracker::Get()->GetModule(module_id); | |
| 149 if (!module) | |
| 150 return 0; | |
| 151 | |
| 152 scoped_refptr<StringVar> face_name(StringVar::FromPPVar(description->face)); | |
| 153 if (!face_name) | |
| 154 return 0; | |
| 155 | |
| 156 int fd = webkit_glue::MatchFontWithFallback( | |
| 157 face_name->value().c_str(), | |
| 158 description->weight >= PP_FONTWEIGHT_BOLD, | |
| 159 description->italic, | |
| 160 charset); | |
| 161 if (fd == -1) | |
| 162 return 0; | |
| 163 | |
| 164 scoped_refptr<PrivateFontFile> font(new PrivateFontFile(module, fd)); | |
| 165 | |
| 166 return font->GetReference(); | |
| 167 #else | |
| 168 // For trusted pepper plugins, this is only needed in Linux since font loading | |
| 169 // on Windows and Mac works through the renderer sandbox. | |
| 170 return 0; | |
| 171 #endif | |
| 172 } | |
| 173 | |
| 174 bool GetFontTableForPrivateFontFile(PP_Resource font_file, | |
| 175 uint32_t table, | |
| 176 void* output, | |
| 177 uint32_t* output_length) { | |
| 178 #if defined(OS_LINUX) | |
| 179 scoped_refptr<PrivateFontFile> font( | |
| 180 Resource::GetAs<PrivateFontFile>(font_file)); | |
| 181 if (!font.get()) | |
| 182 return false; | |
| 183 return font->GetFontTable(table, output, output_length); | |
| 184 #else | |
| 185 return false; | |
| 186 #endif | |
| 187 } | |
| 188 | |
| 189 void SearchString(PP_Module module, | |
| 190 const unsigned short* input_string, | |
| 191 const unsigned short* input_term, | |
| 192 bool case_sensitive, | |
| 193 PP_PrivateFindResult** results, | |
| 194 int* count) { | |
| 195 const char16* string = reinterpret_cast<const char16*>(input_string); | |
| 196 const char16* term = reinterpret_cast<const char16*>(input_term); | |
| 197 | |
| 198 UErrorCode status = U_ZERO_ERROR; | |
| 199 UStringSearch* searcher = usearch_open( | |
| 200 term, -1, string, -1, webkit_glue::GetWebKitLocale().c_str(), 0, | |
| 201 &status); | |
| 202 DCHECK(status == U_ZERO_ERROR || status == U_USING_FALLBACK_WARNING || | |
| 203 status == U_USING_DEFAULT_WARNING); | |
| 204 UCollationStrength strength = case_sensitive ? UCOL_TERTIARY : UCOL_PRIMARY; | |
| 205 | |
| 206 UCollator* collator = usearch_getCollator(searcher); | |
| 207 if (ucol_getStrength(collator) != strength) { | |
| 208 ucol_setStrength(collator, strength); | |
| 209 usearch_reset(searcher); | |
| 210 } | |
| 211 | |
| 212 status = U_ZERO_ERROR; | |
| 213 int match_start = usearch_first(searcher, &status); | |
| 214 DCHECK(status == U_ZERO_ERROR); | |
| 215 | |
| 216 std::vector<PP_PrivateFindResult> pp_results; | |
| 217 while (match_start != USEARCH_DONE) { | |
| 218 size_t matched_length = usearch_getMatchedLength(searcher); | |
| 219 PP_PrivateFindResult result; | |
| 220 result.start_index = match_start; | |
| 221 result.length = matched_length; | |
| 222 pp_results.push_back(result); | |
| 223 match_start = usearch_next(searcher, &status); | |
| 224 DCHECK(status == U_ZERO_ERROR); | |
| 225 } | |
| 226 | |
| 227 *count = pp_results.size(); | |
| 228 if (*count) { | |
| 229 *results = reinterpret_cast<PP_PrivateFindResult*>( | |
| 230 malloc(*count * sizeof(PP_PrivateFindResult))); | |
| 231 memcpy(*results, &pp_results[0], *count * sizeof(PP_PrivateFindResult)); | |
| 232 } else { | |
| 233 *results = NULL; | |
| 234 } | |
| 235 | |
| 236 usearch_close(searcher); | |
| 237 } | |
| 238 | |
| 239 void DidStartLoading(PP_Instance instance_id) { | |
| 240 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); | |
| 241 if (!instance) | |
| 242 return; | |
| 243 instance->delegate()->DidStartLoading(); | |
| 244 } | |
| 245 | |
| 246 void DidStopLoading(PP_Instance instance_id) { | |
| 247 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); | |
| 248 if (!instance) | |
| 249 return; | |
| 250 instance->delegate()->DidStopLoading(); | |
| 251 } | |
| 252 | |
| 253 void SetContentRestriction(PP_Instance instance_id, int restrictions) { | |
| 254 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); | |
| 255 if (!instance) | |
| 256 return; | |
| 257 instance->delegate()->SetContentRestriction(restrictions); | |
| 258 } | |
| 259 | |
| 260 void HistogramPDFPageCount(int count) { | |
| 261 UMA_HISTOGRAM_COUNTS_10000("PDF.PageCount", count); | |
| 262 } | |
| 263 | |
| 264 void UserMetricsRecordAction(PP_Var action) { | |
| 265 scoped_refptr<StringVar> action_str(StringVar::FromPPVar(action)); | |
| 266 if (action_str) | |
| 267 webkit_glue::UserMetricsRecordAction(action_str->value()); | |
| 268 } | |
| 269 | |
| 270 const PPB_Private ppb_private = { | |
| 271 &GetLocalizedString, | |
| 272 &GetResourceImage, | |
| 273 &GetFontFileWithFallback, | |
| 274 &GetFontTableForPrivateFontFile, | |
| 275 &SearchString, | |
| 276 &DidStartLoading, | |
| 277 &DidStopLoading, | |
| 278 &SetContentRestriction, | |
| 279 &HistogramPDFPageCount, | |
| 280 &UserMetricsRecordAction | |
| 281 }; | |
| 282 | |
| 283 } // namespace | |
| 284 | |
| 285 // static | |
| 286 const PPB_Private* Private::GetInterface() { | |
| 287 return &ppb_private; | |
| 288 } | |
| 289 | |
| 290 #if defined(OS_LINUX) | |
| 291 bool PrivateFontFile::GetFontTable(uint32_t table, | |
| 292 void* output, | |
| 293 uint32_t* output_length) { | |
| 294 size_t temp_size = static_cast<size_t>(*output_length); | |
| 295 bool rv = webkit_glue::GetFontTable( | |
| 296 fd_, table, static_cast<uint8_t*>(output), &temp_size); | |
| 297 *output_length = static_cast<uint32_t>(temp_size); | |
| 298 return rv; | |
| 299 } | |
| 300 #endif | |
| 301 | |
| 302 } // namespace pepper | |
| OLD | NEW |