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"
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"
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());
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() {
51 void PDFResource::SearchString(const unsigned short* input_string
,
52 const unsigned short* input_term
,
54 PP_PrivateFindResult
** results
, int* count
) {
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(),
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
)) {
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()) {
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
;
141 case PP_PDFFEATURE_HIDPI
:
144 case PP_PDFFEATURE_PRINTING
:
145 // TODO(raymes): Use PrintWebViewHelper::IsPrintingEnabled.
152 void PDFResource::SetSelectedText(const char* selected_text
) {
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
);