Port Android relocation packer to chromium build
[chromium-blink-merge.git] / ppapi / proxy / pdf_resource.cc
blobe9d3ae893d718db7ffd8e0041c97a6802cf8c298
1 // Copyright (c) 2013 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/pdf_resource.h"
7 #include <stdlib.h>
8 #include <string.h>
10 #include "base/command_line.h"
11 #include "base/metrics/histogram.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "gin/public/isolate_holder.h"
14 #include "ppapi/c/pp_errors.h"
15 #include "ppapi/c/private/ppb_pdf.h"
16 #include "ppapi/proxy/ppapi_messages.h"
17 #include "ppapi/proxy/ppb_image_data_proxy.h"
18 #include "ppapi/shared_impl/var.h"
19 #include "third_party/icu/source/i18n/unicode/usearch.h"
21 namespace ppapi {
22 namespace proxy {
24 namespace {
26 // TODO(raymes): This is just copied from render_thread_impl.cc. We should have
27 // generic code somewhere to get the locale in the plugin.
28 std::string GetLocale() {
29 // The browser process should have passed the locale to the plugin via the
30 // --lang command line flag.
31 const base::CommandLine& parsed_command_line =
32 *base::CommandLine::ForCurrentProcess();
33 const std::string& lang = parsed_command_line.GetSwitchValueASCII("lang");
34 DCHECK(!lang.empty());
35 return lang;
38 } // namespace
40 PDFResource::PDFResource(Connection connection, PP_Instance instance)
41 : PluginResource(connection, instance) {
42 SendCreate(RENDERER, PpapiHostMsg_PDF_Create());
45 PDFResource::~PDFResource() {
48 thunk::PPB_PDF_API* PDFResource::AsPPB_PDF_API() {
49 return this;
52 PP_Var PDFResource::GetLocalizedString(PP_ResourceString string_id) {
53 std::string localized_string;
54 int32_t result = SyncCall<PpapiPluginMsg_PDF_GetLocalizedStringReply>(
55 RENDERER, PpapiHostMsg_PDF_GetLocalizedString(string_id),
56 &localized_string);
57 if (result != PP_OK)
58 return PP_MakeUndefined();
59 return ppapi::StringVar::StringToPPVar(localized_string);
62 void PDFResource::SearchString(const unsigned short* input_string,
63 const unsigned short* input_term,
64 bool case_sensitive,
65 PP_PrivateFindResult** results, int* count) {
66 if (locale_.empty())
67 locale_ = GetLocale();
68 const base::char16* string =
69 reinterpret_cast<const base::char16*>(input_string);
70 const base::char16* term =
71 reinterpret_cast<const base::char16*>(input_term);
73 UErrorCode status = U_ZERO_ERROR;
74 UStringSearch* searcher = usearch_open(term, -1, string, -1, locale_.c_str(),
75 0, &status);
76 DCHECK(status == U_ZERO_ERROR || status == U_USING_FALLBACK_WARNING ||
77 status == U_USING_DEFAULT_WARNING);
78 UCollationStrength strength = case_sensitive ? UCOL_TERTIARY : UCOL_PRIMARY;
80 UCollator* collator = usearch_getCollator(searcher);
81 if (ucol_getStrength(collator) != strength) {
82 ucol_setStrength(collator, strength);
83 usearch_reset(searcher);
86 status = U_ZERO_ERROR;
87 int match_start = usearch_first(searcher, &status);
88 DCHECK(status == U_ZERO_ERROR);
90 std::vector<PP_PrivateFindResult> pp_results;
91 while (match_start != USEARCH_DONE) {
92 int32_t matched_length = usearch_getMatchedLength(searcher);
93 PP_PrivateFindResult result;
94 result.start_index = match_start;
95 result.length = matched_length;
96 pp_results.push_back(result);
97 match_start = usearch_next(searcher, &status);
98 DCHECK(status == U_ZERO_ERROR);
101 *count = static_cast<uint32_t>(pp_results.size());
102 if (*count) {
103 *results = reinterpret_cast<PP_PrivateFindResult*>(malloc(
104 *count * sizeof(PP_PrivateFindResult)));
105 memcpy(*results, &pp_results[0], *count * sizeof(PP_PrivateFindResult));
106 } else {
107 *results = NULL;
110 usearch_close(searcher);
113 void PDFResource::DidStartLoading() {
114 Post(RENDERER, PpapiHostMsg_PDF_DidStartLoading());
117 void PDFResource::DidStopLoading() {
118 Post(RENDERER, PpapiHostMsg_PDF_DidStopLoading());
121 void PDFResource::SetContentRestriction(int restrictions) {
122 Post(RENDERER, PpapiHostMsg_PDF_SetContentRestriction(restrictions));
125 void PDFResource::HistogramPDFPageCount(int count) {
126 UMA_HISTOGRAM_COUNTS_10000("PDF.PageCount", count);
129 void PDFResource::UserMetricsRecordAction(const PP_Var& action) {
130 scoped_refptr<ppapi::StringVar> action_str(
131 ppapi::StringVar::FromPPVar(action));
132 if (action_str.get()) {
133 Post(RENDERER,
134 PpapiHostMsg_PDF_UserMetricsRecordAction(action_str->value()));
138 void PDFResource::HasUnsupportedFeature() {
139 Post(RENDERER, PpapiHostMsg_PDF_HasUnsupportedFeature());
142 void PDFResource::Print() {
143 Post(RENDERER, PpapiHostMsg_PDF_Print());
146 void PDFResource::SaveAs() {
147 Post(RENDERER, PpapiHostMsg_PDF_SaveAs());
150 PP_Bool PDFResource::IsFeatureEnabled(PP_PDFFeature feature) {
151 PP_Bool result = PP_FALSE;
152 switch (feature) {
153 case PP_PDFFEATURE_HIDPI:
154 result = PP_TRUE;
155 break;
156 case PP_PDFFEATURE_PRINTING:
157 // TODO(raymes): Use PrintWebViewHelper::IsPrintingEnabled.
158 result = PP_FALSE;
159 break;
161 return result;
164 PP_Resource PDFResource::GetResourceImageForScale(PP_ResourceImage image_id,
165 float scale) {
166 IPC::Message reply;
167 ResourceMessageReplyParams reply_params;
168 int32_t result = GenericSyncCall(
169 RENDERER, PpapiHostMsg_PDF_GetResourceImage(image_id, scale), &reply,
170 &reply_params);
171 if (result != PP_OK)
172 return 0;
174 HostResource resource;
175 PP_ImageDataDesc image_desc;
176 if (!UnpackMessage<PpapiPluginMsg_PDF_GetResourceImageReply>(
177 reply, &resource, &image_desc)) {
178 return 0;
181 if (resource.is_null())
182 return 0;
183 if (!PPB_ImageData_Shared::IsImageDataDescValid(image_desc))
184 return 0;
186 base::SharedMemoryHandle handle;
187 if (!reply_params.TakeSharedMemoryHandleAtIndex(0, &handle))
188 return 0;
189 return (new SimpleImageData(resource, image_desc, handle))->GetReference();
192 PP_Resource PDFResource::GetResourceImage(PP_ResourceImage image_id) {
193 return GetResourceImageForScale(image_id, 1.0f);
196 PP_Bool PDFResource::IsOutOfProcess() {
197 return PP_TRUE;
200 void PDFResource::SetSelectedText(const char* selected_text) {
201 Post(RENDERER,
202 PpapiHostMsg_PDF_SetSelectedText(base::UTF8ToUTF16(selected_text)));
205 void PDFResource::SetLinkUnderCursor(const char* url) {
206 Post(RENDERER, PpapiHostMsg_PDF_SetLinkUnderCursor(url));
209 void PDFResource::GetV8ExternalSnapshotData(const char** natives_data_out,
210 int* natives_size_out,
211 const char** snapshot_data_out,
212 int* snapshot_size_out) {
213 gin::IsolateHolder::GetV8ExternalSnapshotData(natives_data_out,
214 natives_size_out, snapshot_data_out, snapshot_size_out);
217 } // namespace proxy
218 } // namespace ppapi