Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ppapi / proxy / pdf_resource.cc
blobd3aad14b3550fa1f54c1ef1a5d28112b5b0650c7
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 void PDFResource::SearchString(const unsigned short* input_string,
52 const unsigned short* input_term,
53 bool case_sensitive,
54 PP_PrivateFindResult** results, int* count) {
55 if (locale_.empty())
56 locale_ = GetLocale();
57 const base::char16* string =
58 reinterpret_cast<const base::char16*>(input_string);
59 const base::char16* term =
60 reinterpret_cast<const base::char16*>(input_term);
62 UErrorCode status = U_ZERO_ERROR;
63 UStringSearch* searcher = usearch_open(term, -1, string, -1, locale_.c_str(),
64 0, &status);
65 DCHECK(status == U_ZERO_ERROR || status == U_USING_FALLBACK_WARNING ||
66 status == U_USING_DEFAULT_WARNING);
67 UCollationStrength strength = case_sensitive ? UCOL_TERTIARY : UCOL_PRIMARY;
69 UCollator* collator = usearch_getCollator(searcher);
70 if (ucol_getStrength(collator) != strength) {
71 ucol_setStrength(collator, strength);
72 usearch_reset(searcher);
75 status = U_ZERO_ERROR;
76 int match_start = usearch_first(searcher, &status);
77 DCHECK(status == U_ZERO_ERROR);
79 std::vector<PP_PrivateFindResult> pp_results;
80 while (match_start != USEARCH_DONE) {
81 int32_t matched_length = usearch_getMatchedLength(searcher);
82 PP_PrivateFindResult result;
83 result.start_index = match_start;
84 result.length = matched_length;
85 pp_results.push_back(result);
86 match_start = usearch_next(searcher, &status);
87 DCHECK(status == U_ZERO_ERROR);
90 if (pp_results.empty() ||
91 pp_results.size() > std::numeric_limits<uint32_t>::max() ||
92 pp_results.size() > SIZE_MAX / sizeof(PP_PrivateFindResult)) {
93 *count = 0;
94 *results = nullptr;
95 } else {
96 *count = static_cast<uint32_t>(pp_results.size());
97 const size_t result_size = pp_results.size() * sizeof(PP_PrivateFindResult);
98 *results = reinterpret_cast<PP_PrivateFindResult*>(malloc(result_size));
99 memcpy(*results, &pp_results[0], result_size);
102 usearch_close(searcher);
105 void PDFResource::DidStartLoading() {
106 Post(RENDERER, PpapiHostMsg_PDF_DidStartLoading());
109 void PDFResource::DidStopLoading() {
110 Post(RENDERER, PpapiHostMsg_PDF_DidStopLoading());
113 void PDFResource::SetContentRestriction(int restrictions) {
114 Post(RENDERER, PpapiHostMsg_PDF_SetContentRestriction(restrictions));
117 void PDFResource::UserMetricsRecordAction(const PP_Var& action) {
118 scoped_refptr<ppapi::StringVar> action_str(
119 ppapi::StringVar::FromPPVar(action));
120 if (action_str.get()) {
121 Post(RENDERER,
122 PpapiHostMsg_PDF_UserMetricsRecordAction(action_str->value()));
126 void PDFResource::HasUnsupportedFeature() {
127 Post(RENDERER, PpapiHostMsg_PDF_HasUnsupportedFeature());
130 void PDFResource::Print() {
131 Post(RENDERER, PpapiHostMsg_PDF_Print());
134 void PDFResource::SaveAs() {
135 Post(RENDERER, PpapiHostMsg_PDF_SaveAs());
138 PP_Bool PDFResource::IsFeatureEnabled(PP_PDFFeature feature) {
139 PP_Bool result = PP_FALSE;
140 switch (feature) {
141 case PP_PDFFEATURE_HIDPI:
142 result = PP_TRUE;
143 break;
144 case PP_PDFFEATURE_PRINTING:
145 // TODO(raymes): Use PrintWebViewHelper::IsPrintingEnabled.
146 result = PP_FALSE;
147 break;
149 return result;
152 void PDFResource::SetSelectedText(const char* selected_text) {
153 Post(RENDERER,
154 PpapiHostMsg_PDF_SetSelectedText(base::UTF8ToUTF16(selected_text)));
157 void PDFResource::SetLinkUnderCursor(const char* url) {
158 Post(RENDERER, PpapiHostMsg_PDF_SetLinkUnderCursor(url));
161 void PDFResource::GetV8ExternalSnapshotData(const char** natives_data_out,
162 int* natives_size_out,
163 const char** snapshot_data_out,
164 int* snapshot_size_out) {
165 gin::V8Initializer::GetV8ExternalSnapshotData(
166 natives_data_out, natives_size_out, snapshot_data_out, snapshot_size_out);
169 } // namespace proxy
170 } // namespace ppapi