Address some additional cleanup work needed for the component flash updates on Linux.
[chromium-blink-merge.git] / ppapi / proxy / pdf_resource.cc
blobed3dcde9b73f16f64cf571e575e11dc5fa4a57c8
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/v8_initializer.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/shared_impl/var.h"
18 #include "third_party/icu/source/i18n/unicode/usearch.h"
20 namespace ppapi {
21 namespace proxy {
23 namespace {
25 // TODO(raymes): This is just copied from render_thread_impl.cc. We should have
26 // generic code somewhere to get the locale in the plugin.
27 std::string GetLocale() {
28 // The browser process should have passed the locale to the plugin via the
29 // --lang command line flag.
30 const base::CommandLine& parsed_command_line =
31 *base::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 int32_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 if (pp_results.empty() ||
101 pp_results.size() > std::numeric_limits<uint32_t>::max() ||
102 pp_results.size() > SIZE_MAX / sizeof(PP_PrivateFindResult)) {
103 *count = 0;
104 *results = nullptr;
105 } else {
106 *count = static_cast<uint32_t>(pp_results.size());
107 const size_t result_size = pp_results.size() * sizeof(PP_PrivateFindResult);
108 *results = reinterpret_cast<PP_PrivateFindResult*>(malloc(result_size));
109 memcpy(*results, &pp_results[0], result_size);
112 usearch_close(searcher);
115 void PDFResource::DidStartLoading() {
116 Post(RENDERER, PpapiHostMsg_PDF_DidStartLoading());
119 void PDFResource::DidStopLoading() {
120 Post(RENDERER, PpapiHostMsg_PDF_DidStopLoading());
123 void PDFResource::SetContentRestriction(int restrictions) {
124 Post(RENDERER, PpapiHostMsg_PDF_SetContentRestriction(restrictions));
127 void PDFResource::UserMetricsRecordAction(const PP_Var& action) {
128 scoped_refptr<ppapi::StringVar> action_str(
129 ppapi::StringVar::FromPPVar(action));
130 if (action_str.get()) {
131 Post(RENDERER,
132 PpapiHostMsg_PDF_UserMetricsRecordAction(action_str->value()));
136 void PDFResource::HasUnsupportedFeature() {
137 Post(RENDERER, PpapiHostMsg_PDF_HasUnsupportedFeature());
140 void PDFResource::Print() {
141 Post(RENDERER, PpapiHostMsg_PDF_Print());
144 void PDFResource::SaveAs() {
145 Post(RENDERER, PpapiHostMsg_PDF_SaveAs());
148 PP_Bool PDFResource::IsFeatureEnabled(PP_PDFFeature feature) {
149 PP_Bool result = PP_FALSE;
150 switch (feature) {
151 case PP_PDFFEATURE_HIDPI:
152 result = PP_TRUE;
153 break;
154 case PP_PDFFEATURE_PRINTING:
155 // TODO(raymes): Use PrintWebViewHelper::IsPrintingEnabled.
156 result = PP_FALSE;
157 break;
159 return result;
162 void PDFResource::SetSelectedText(const char* selected_text) {
163 Post(RENDERER,
164 PpapiHostMsg_PDF_SetSelectedText(base::UTF8ToUTF16(selected_text)));
167 void PDFResource::SetLinkUnderCursor(const char* url) {
168 Post(RENDERER, PpapiHostMsg_PDF_SetLinkUnderCursor(url));
171 void PDFResource::GetV8ExternalSnapshotData(const char** natives_data_out,
172 int* natives_size_out,
173 const char** snapshot_data_out,
174 int* snapshot_size_out) {
175 gin::V8Initializer::GetV8ExternalSnapshotData(
176 natives_data_out, natives_size_out, snapshot_data_out, snapshot_size_out);
179 } // namespace proxy
180 } // namespace ppapi