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/pdf/renderer/pdf_resource_util.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/layout.h"
30 #include "ui/gfx/geometry/point.h"
35 // --single-process model may fail in CHECK(!g_print_client) if there exist
36 // more than two RenderThreads, so here we use TLS for g_print_client.
37 // See http://crbug.com/457580.
38 base::LazyInstance
<base::ThreadLocalPointer
<PepperPDFHost::PrintClient
>>::Leaky
39 g_print_client_tls
= LAZY_INSTANCE_INITIALIZER
;
43 PepperPDFHost::PepperPDFHost(content::RendererPpapiHost
* host
,
46 : ppapi::host::ResourceHost(host
->GetPpapiHost(), instance
, resource
),
49 PepperPDFHost::~PepperPDFHost() {}
52 bool PepperPDFHost::InvokePrintingForInstance(PP_Instance instance_id
) {
53 return g_print_client_tls
.Pointer()->Get()
54 ? g_print_client_tls
.Pointer()->Get()->Print(instance_id
)
59 void PepperPDFHost::SetPrintClient(PepperPDFHost::PrintClient
* client
) {
60 CHECK(!g_print_client_tls
.Pointer()->Get())
61 << "There should only be a single PrintClient for one RenderThread.";
62 g_print_client_tls
.Pointer()->Set(client
);
65 int32_t PepperPDFHost::OnResourceMessageReceived(
66 const IPC::Message
& msg
,
67 ppapi::host::HostMessageContext
* context
) {
68 PPAPI_BEGIN_MESSAGE_MAP(PepperPDFHost
, msg
)
69 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_GetLocalizedString
,
70 OnHostMsgGetLocalizedString
)
71 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_DidStartLoading
,
72 OnHostMsgDidStartLoading
)
73 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_DidStopLoading
,
74 OnHostMsgDidStopLoading
)
75 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_UserMetricsRecordAction
,
76 OnHostMsgUserMetricsRecordAction
)
77 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_HasUnsupportedFeature
,
78 OnHostMsgHasUnsupportedFeature
)
79 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_Print
, OnHostMsgPrint
)
80 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_SaveAs
,
82 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_SetSelectedText
,
83 OnHostMsgSetSelectedText
)
84 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_SetLinkUnderCursor
,
85 OnHostMsgSetLinkUnderCursor
)
86 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_SetContentRestriction
,
87 OnHostMsgSetContentRestriction
)
88 PPAPI_END_MESSAGE_MAP()
89 return PP_ERROR_FAILED
;
92 int32_t PepperPDFHost::OnHostMsgGetLocalizedString(
93 ppapi::host::HostMessageContext
* context
,
94 PP_ResourceString string_id
) {
95 std::string rv
= GetStringResource(string_id
);
96 context
->reply_msg
= PpapiPluginMsg_PDF_GetLocalizedStringReply(rv
);
100 int32_t PepperPDFHost::OnHostMsgDidStartLoading(
101 ppapi::host::HostMessageContext
* context
) {
102 content::PepperPluginInstance
* instance
=
103 host_
->GetPluginInstance(pp_instance());
105 return PP_ERROR_FAILED
;
106 instance
->GetRenderView()->DidStartLoading();
110 int32_t PepperPDFHost::OnHostMsgDidStopLoading(
111 ppapi::host::HostMessageContext
* context
) {
112 content::PepperPluginInstance
* instance
=
113 host_
->GetPluginInstance(pp_instance());
115 return PP_ERROR_FAILED
;
116 instance
->GetRenderView()->DidStopLoading();
120 int32_t PepperPDFHost::OnHostMsgSetContentRestriction(
121 ppapi::host::HostMessageContext
* context
,
123 content::PepperPluginInstance
* instance
=
124 host_
->GetPluginInstance(pp_instance());
126 return PP_ERROR_FAILED
;
127 instance
->GetRenderView()->Send(new PDFHostMsg_PDFUpdateContentRestrictions(
128 instance
->GetRenderView()->GetRoutingID(), restrictions
));
132 int32_t PepperPDFHost::OnHostMsgUserMetricsRecordAction(
133 ppapi::host::HostMessageContext
* context
,
134 const std::string
& action
) {
136 return PP_ERROR_FAILED
;
137 content::RenderThread::Get()->RecordComputedAction(action
);
141 int32_t PepperPDFHost::OnHostMsgHasUnsupportedFeature(
142 ppapi::host::HostMessageContext
* context
) {
143 content::PepperPluginInstance
* instance
=
144 host_
->GetPluginInstance(pp_instance());
146 return PP_ERROR_FAILED
;
148 blink::WebView
* view
=
149 instance
->GetContainer()->element().document().frame()->view();
150 content::RenderView
* render_view
= content::RenderView::FromWebView(view
);
152 new PDFHostMsg_PDFHasUnsupportedFeature(render_view
->GetRoutingID()));
156 int32_t PepperPDFHost::OnHostMsgPrint(
157 ppapi::host::HostMessageContext
* context
) {
158 return InvokePrintingForInstance(pp_instance()) ? PP_OK
: PP_ERROR_FAILED
;
161 int32_t PepperPDFHost::OnHostMsgSaveAs(
162 ppapi::host::HostMessageContext
* context
) {
163 content::PepperPluginInstance
* instance
=
164 host_
->GetPluginInstance(pp_instance());
166 return PP_ERROR_FAILED
;
167 GURL url
= instance
->GetPluginURL();
168 content::RenderView
* render_view
= instance
->GetRenderView();
169 blink::WebLocalFrame
* frame
=
170 render_view
->GetWebView()->mainFrame()->toWebLocalFrame();
171 content::Referrer referrer
= content::Referrer::SanitizeForRequest(
172 url
, content::Referrer(frame
->document().url(),
173 frame
->document().referrerPolicy()));
175 new PDFHostMsg_PDFSaveURLAs(render_view
->GetRoutingID(), url
, referrer
));
179 int32_t PepperPDFHost::OnHostMsgSetSelectedText(
180 ppapi::host::HostMessageContext
* context
,
181 const base::string16
& selected_text
) {
182 content::PepperPluginInstance
* instance
=
183 host_
->GetPluginInstance(pp_instance());
185 return PP_ERROR_FAILED
;
186 instance
->SetSelectedText(selected_text
);
190 int32_t PepperPDFHost::OnHostMsgSetLinkUnderCursor(
191 ppapi::host::HostMessageContext
* context
,
192 const std::string
& url
) {
193 content::PepperPluginInstance
* instance
=
194 host_
->GetPluginInstance(pp_instance());
196 return PP_ERROR_FAILED
;
197 instance
->SetLinkUnderCursor(url
);