1 // Copyright 2014 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 "components/pdf/renderer/pepper_pdf_host.h"
7 #include "components/pdf/common/pdf_messages.h"
8 #include "components/strings/grit/components_strings.h"
9 #include "content/public/common/referrer.h"
10 #include "content/public/renderer/pepper_plugin_instance.h"
11 #include "content/public/renderer/render_thread.h"
12 #include "content/public/renderer/render_view.h"
13 #include "content/public/renderer/renderer_ppapi_host.h"
14 #include "ppapi/host/dispatch_host_message.h"
15 #include "ppapi/host/host_message_context.h"
16 #include "ppapi/host/ppapi_host.h"
17 #include "ppapi/proxy/host_dispatcher.h"
18 #include "ppapi/proxy/ppapi_messages.h"
19 #include "ppapi/proxy/ppb_image_data_proxy.h"
20 #include "ppapi/shared_impl/ppb_image_data_shared.h"
21 #include "ppapi/shared_impl/scoped_pp_resource.h"
22 #include "ppapi/thunk/enter.h"
23 #include "ppapi/thunk/ppb_image_data_api.h"
24 #include "third_party/WebKit/public/web/WebDocument.h"
25 #include "third_party/WebKit/public/web/WebElement.h"
26 #include "third_party/WebKit/public/web/WebLocalFrame.h"
27 #include "third_party/WebKit/public/web/WebPluginContainer.h"
28 #include "third_party/WebKit/public/web/WebView.h"
29 #include "ui/base/l10n/l10n_util.h"
30 #include "ui/base/layout.h"
31 #include "ui/base/resource/resource_bundle.h"
32 #include "ui/gfx/geometry/point.h"
37 // --single-process model may fail in CHECK(!g_print_client) if there exist
38 // more than two RenderThreads, so here we use TLS for g_print_client.
39 // See http://crbug.com/457580.
40 base::LazyInstance
<base::ThreadLocalPointer
<PepperPDFHost::PrintClient
>>::Leaky
41 g_print_client_tls
= LAZY_INSTANCE_INITIALIZER
;
43 std::string
GetStringResource(PP_ResourceString string_id
) {
46 case PP_RESOURCESTRING_PDFGETPASSWORD
:
47 resource_id
= IDS_PDF_NEED_PASSWORD
;
49 case PP_RESOURCESTRING_PDFLOADING
:
50 resource_id
= IDS_PDF_PAGE_LOADING
;
52 case PP_RESOURCESTRING_PDFLOAD_FAILED
:
53 resource_id
= IDS_PDF_PAGE_LOAD_FAILED
;
55 case PP_RESOURCESTRING_PDFPROGRESSLOADING
:
56 resource_id
= IDS_PDF_PROGRESS_LOADING
;
60 return l10n_util::GetStringUTF8(resource_id
);
65 PepperPDFHost::PepperPDFHost(content::RendererPpapiHost
* host
,
68 : ppapi::host::ResourceHost(host
->GetPpapiHost(), instance
, resource
),
71 PepperPDFHost::~PepperPDFHost() {}
74 bool PepperPDFHost::InvokePrintingForInstance(PP_Instance instance_id
) {
75 return g_print_client_tls
.Pointer()->Get()
76 ? g_print_client_tls
.Pointer()->Get()->Print(instance_id
)
81 void PepperPDFHost::SetPrintClient(PepperPDFHost::PrintClient
* client
) {
82 CHECK(!g_print_client_tls
.Pointer()->Get())
83 << "There should only be a single PrintClient for one RenderThread.";
84 g_print_client_tls
.Pointer()->Set(client
);
87 int32_t PepperPDFHost::OnResourceMessageReceived(
88 const IPC::Message
& msg
,
89 ppapi::host::HostMessageContext
* context
) {
90 PPAPI_BEGIN_MESSAGE_MAP(PepperPDFHost
, msg
)
91 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_GetLocalizedString
,
92 OnHostMsgGetLocalizedString
)
93 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_DidStartLoading
,
94 OnHostMsgDidStartLoading
)
95 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_DidStopLoading
,
96 OnHostMsgDidStopLoading
)
97 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_UserMetricsRecordAction
,
98 OnHostMsgUserMetricsRecordAction
)
99 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_HasUnsupportedFeature
,
100 OnHostMsgHasUnsupportedFeature
)
101 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_Print
, OnHostMsgPrint
)
102 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_SaveAs
,
104 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_SetSelectedText
,
105 OnHostMsgSetSelectedText
)
106 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_SetLinkUnderCursor
,
107 OnHostMsgSetLinkUnderCursor
)
108 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_SetContentRestriction
,
109 OnHostMsgSetContentRestriction
)
110 PPAPI_END_MESSAGE_MAP()
111 return PP_ERROR_FAILED
;
114 int32_t PepperPDFHost::OnHostMsgGetLocalizedString(
115 ppapi::host::HostMessageContext
* context
,
116 PP_ResourceString string_id
) {
117 std::string rv
= GetStringResource(string_id
);
118 context
->reply_msg
= PpapiPluginMsg_PDF_GetLocalizedStringReply(rv
);
122 int32_t PepperPDFHost::OnHostMsgDidStartLoading(
123 ppapi::host::HostMessageContext
* context
) {
124 content::PepperPluginInstance
* instance
=
125 host_
->GetPluginInstance(pp_instance());
127 return PP_ERROR_FAILED
;
128 instance
->GetRenderView()->DidStartLoading();
132 int32_t PepperPDFHost::OnHostMsgDidStopLoading(
133 ppapi::host::HostMessageContext
* context
) {
134 content::PepperPluginInstance
* instance
=
135 host_
->GetPluginInstance(pp_instance());
137 return PP_ERROR_FAILED
;
138 instance
->GetRenderView()->DidStopLoading();
142 int32_t PepperPDFHost::OnHostMsgSetContentRestriction(
143 ppapi::host::HostMessageContext
* context
,
145 content::PepperPluginInstance
* instance
=
146 host_
->GetPluginInstance(pp_instance());
148 return PP_ERROR_FAILED
;
149 instance
->GetRenderView()->Send(new PDFHostMsg_PDFUpdateContentRestrictions(
150 instance
->GetRenderView()->GetRoutingID(), restrictions
));
154 int32_t PepperPDFHost::OnHostMsgUserMetricsRecordAction(
155 ppapi::host::HostMessageContext
* context
,
156 const std::string
& action
) {
158 return PP_ERROR_FAILED
;
159 content::RenderThread::Get()->RecordComputedAction(action
);
163 int32_t PepperPDFHost::OnHostMsgHasUnsupportedFeature(
164 ppapi::host::HostMessageContext
* context
) {
165 content::PepperPluginInstance
* instance
=
166 host_
->GetPluginInstance(pp_instance());
168 return PP_ERROR_FAILED
;
170 blink::WebView
* view
=
171 instance
->GetContainer()->element().document().frame()->view();
172 content::RenderView
* render_view
= content::RenderView::FromWebView(view
);
174 new PDFHostMsg_PDFHasUnsupportedFeature(render_view
->GetRoutingID()));
178 int32_t PepperPDFHost::OnHostMsgPrint(
179 ppapi::host::HostMessageContext
* context
) {
180 return InvokePrintingForInstance(pp_instance()) ? PP_OK
: PP_ERROR_FAILED
;
183 int32_t PepperPDFHost::OnHostMsgSaveAs(
184 ppapi::host::HostMessageContext
* context
) {
185 content::PepperPluginInstance
* instance
=
186 host_
->GetPluginInstance(pp_instance());
188 return PP_ERROR_FAILED
;
189 GURL url
= instance
->GetPluginURL();
190 content::RenderView
* render_view
= instance
->GetRenderView();
191 blink::WebLocalFrame
* frame
=
192 render_view
->GetWebView()->mainFrame()->toWebLocalFrame();
193 content::Referrer referrer
= content::Referrer::SanitizeForRequest(
194 url
, content::Referrer(frame
->document().url(),
195 frame
->document().referrerPolicy()));
197 new PDFHostMsg_PDFSaveURLAs(render_view
->GetRoutingID(), url
, referrer
));
201 int32_t PepperPDFHost::OnHostMsgSetSelectedText(
202 ppapi::host::HostMessageContext
* context
,
203 const base::string16
& selected_text
) {
204 content::PepperPluginInstance
* instance
=
205 host_
->GetPluginInstance(pp_instance());
207 return PP_ERROR_FAILED
;
208 instance
->SetSelectedText(selected_text
);
212 int32_t PepperPDFHost::OnHostMsgSetLinkUnderCursor(
213 ppapi::host::HostMessageContext
* context
,
214 const std::string
& url
) {
215 content::PepperPluginInstance
* instance
=
216 host_
->GetPluginInstance(pp_instance());
218 return PP_ERROR_FAILED
;
219 instance
->SetLinkUnderCursor(url
);