Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / ppapi / proxy / pdf_resource.cc
blob50370cf7cd5a47d36b13ff379097b5b760624a07
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 CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
32 const std::string& lang = parsed_command_line.GetSwitchValueASCII("lang");
33 DCHECK(!lang.empty());
34 return lang;
37 } // namespace
39 PDFResource::PDFResource(Connection connection, PP_Instance instance)
40 : PluginResource(connection, instance) {
41 SendCreate(RENDERER, PpapiHostMsg_PDF_Create());
44 PDFResource::~PDFResource() {
47 thunk::PPB_PDF_API* PDFResource::AsPPB_PDF_API() {
48 return this;
51 PP_Var PDFResource::GetLocalizedString(PP_ResourceString string_id) {
52 std::string localized_string;
53 int32_t result = SyncCall<PpapiPluginMsg_PDF_GetLocalizedStringReply>(
54 RENDERER, PpapiHostMsg_PDF_GetLocalizedString(string_id),
55 &localized_string);
56 if (result != PP_OK)
57 return PP_MakeUndefined();
58 return ppapi::StringVar::StringToPPVar(localized_string);
61 void PDFResource::SearchString(const unsigned short* input_string,
62 const unsigned short* input_term,
63 bool case_sensitive,
64 PP_PrivateFindResult** results, int* count) {
65 if (locale_.empty())
66 locale_ = GetLocale();
67 const base::char16* string =
68 reinterpret_cast<const base::char16*>(input_string);
69 const base::char16* term =
70 reinterpret_cast<const base::char16*>(input_term);
72 UErrorCode status = U_ZERO_ERROR;
73 UStringSearch* searcher = usearch_open(term, -1, string, -1, locale_.c_str(),
74 0, &status);
75 DCHECK(status == U_ZERO_ERROR || status == U_USING_FALLBACK_WARNING ||
76 status == U_USING_DEFAULT_WARNING);
77 UCollationStrength strength = case_sensitive ? UCOL_TERTIARY : UCOL_PRIMARY;
79 UCollator* collator = usearch_getCollator(searcher);
80 if (ucol_getStrength(collator) != strength) {
81 ucol_setStrength(collator, strength);
82 usearch_reset(searcher);
85 status = U_ZERO_ERROR;
86 int match_start = usearch_first(searcher, &status);
87 DCHECK(status == U_ZERO_ERROR);
89 std::vector<PP_PrivateFindResult> pp_results;
90 while (match_start != USEARCH_DONE) {
91 size_t matched_length = usearch_getMatchedLength(searcher);
92 PP_PrivateFindResult result;
93 result.start_index = match_start;
94 result.length = matched_length;
95 pp_results.push_back(result);
96 match_start = usearch_next(searcher, &status);
97 DCHECK(status == U_ZERO_ERROR);
100 *count = pp_results.size();
101 if (*count) {
102 *results = reinterpret_cast<PP_PrivateFindResult*>(malloc(
103 *count * sizeof(PP_PrivateFindResult)));
104 memcpy(*results, &pp_results[0], *count * sizeof(PP_PrivateFindResult));
105 } else {
106 *results = NULL;
109 usearch_close(searcher);
112 void PDFResource::DidStartLoading() {
113 Post(RENDERER, PpapiHostMsg_PDF_DidStartLoading());
116 void PDFResource::DidStopLoading() {
117 Post(RENDERER, PpapiHostMsg_PDF_DidStopLoading());
120 void PDFResource::SetContentRestriction(int restrictions) {
121 Post(RENDERER, PpapiHostMsg_PDF_SetContentRestriction(restrictions));
124 void PDFResource::HistogramPDFPageCount(int count) {
125 UMA_HISTOGRAM_COUNTS_10000("PDF.PageCount", count);
128 void PDFResource::UserMetricsRecordAction(const PP_Var& action) {
129 scoped_refptr<ppapi::StringVar> action_str(
130 ppapi::StringVar::FromPPVar(action));
131 if (action_str.get()) {
132 Post(RENDERER,
133 PpapiHostMsg_PDF_UserMetricsRecordAction(action_str->value()));
137 void PDFResource::HasUnsupportedFeature() {
138 Post(RENDERER, PpapiHostMsg_PDF_HasUnsupportedFeature());
141 void PDFResource::Print() {
142 Post(RENDERER, PpapiHostMsg_PDF_Print());
145 void PDFResource::SaveAs() {
146 Post(RENDERER, PpapiHostMsg_PDF_SaveAs());
149 PP_Bool PDFResource::IsFeatureEnabled(PP_PDFFeature feature) {
150 PP_Bool result = PP_FALSE;
151 switch (feature) {
152 case PP_PDFFEATURE_HIDPI:
153 result = PP_TRUE;
154 break;
155 case PP_PDFFEATURE_PRINTING:
156 // TODO(raymes): Use PrintWebViewHelper::IsPrintingEnabled.
157 result = PP_FALSE;
158 break;
160 return result;
163 PP_Resource PDFResource::GetResourceImageForScale(PP_ResourceImage image_id,
164 float scale) {
165 IPC::Message reply;
166 ResourceMessageReplyParams reply_params;
167 int32_t result = GenericSyncCall(
168 RENDERER, PpapiHostMsg_PDF_GetResourceImage(image_id, scale), &reply,
169 &reply_params);
170 if (result != PP_OK)
171 return 0;
173 HostResource resource;
174 PP_ImageDataDesc image_desc;
175 if (!UnpackMessage<PpapiPluginMsg_PDF_GetResourceImageReply>(
176 reply, &resource, &image_desc)) {
177 return 0;
180 if (resource.is_null())
181 return 0;
182 if (!PPB_ImageData_Shared::IsImageDataDescValid(image_desc))
183 return 0;
185 base::SharedMemoryHandle handle;
186 if (!reply_params.TakeSharedMemoryHandleAtIndex(0, &handle))
187 return 0;
188 return (new SimpleImageData(resource, image_desc, handle))->GetReference();
191 PP_Resource PDFResource::GetResourceImage(PP_ResourceImage image_id) {
192 return GetResourceImageForScale(image_id, 1.0f);
195 PP_Bool PDFResource::IsOutOfProcess() {
196 return PP_TRUE;
199 void PDFResource::SetSelectedText(const char* selected_text) {
200 Post(RENDERER,
201 PpapiHostMsg_PDF_SetSelectedText(base::UTF8ToUTF16(selected_text)));
204 void PDFResource::SetLinkUnderCursor(const char* url) {
205 Post(RENDERER, PpapiHostMsg_PDF_SetLinkUnderCursor(url));
208 void PDFResource::GetV8ExternalSnapshotData(const char** natives_data_out,
209 int* natives_size_out,
210 const char** snapshot_data_out,
211 int* snapshot_size_out) {
212 gin::IsolateHolder::GetV8ExternalSnapshotData(natives_data_out,
213 natives_size_out, snapshot_data_out, snapshot_size_out);
216 } // namespace proxy
217 } // namespace ppapi