Extension syncing: Introduce a NeedsSync pref
[chromium-blink-merge.git] / components / pdf / renderer / pepper_pdf_host.cc
blob5714d26acbbd3b3e81d45f724940be4673ebd098
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"
34 namespace pdf {
36 namespace {
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) {
44 int resource_id = 0;
45 switch (string_id) {
46 case PP_RESOURCESTRING_PDFGETPASSWORD:
47 resource_id = IDS_PDF_NEED_PASSWORD;
48 break;
49 case PP_RESOURCESTRING_PDFLOADING:
50 resource_id = IDS_PDF_PAGE_LOADING;
51 break;
52 case PP_RESOURCESTRING_PDFLOAD_FAILED:
53 resource_id = IDS_PDF_PAGE_LOAD_FAILED;
54 break;
55 case PP_RESOURCESTRING_PDFPROGRESSLOADING:
56 resource_id = IDS_PDF_PROGRESS_LOADING;
57 break;
60 return l10n_util::GetStringUTF8(resource_id);
63 } // namespace
65 PepperPDFHost::PepperPDFHost(content::RendererPpapiHost* host,
66 PP_Instance instance,
67 PP_Resource resource)
68 : ppapi::host::ResourceHost(host->GetPpapiHost(), instance, resource),
69 host_(host) {}
71 PepperPDFHost::~PepperPDFHost() {}
73 // static
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)
77 : false;
80 // static
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,
103 OnHostMsgSaveAs)
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);
119 return PP_OK;
122 int32_t PepperPDFHost::OnHostMsgDidStartLoading(
123 ppapi::host::HostMessageContext* context) {
124 content::PepperPluginInstance* instance =
125 host_->GetPluginInstance(pp_instance());
126 if (!instance)
127 return PP_ERROR_FAILED;
128 instance->GetRenderView()->DidStartLoading();
129 return PP_OK;
132 int32_t PepperPDFHost::OnHostMsgDidStopLoading(
133 ppapi::host::HostMessageContext* context) {
134 content::PepperPluginInstance* instance =
135 host_->GetPluginInstance(pp_instance());
136 if (!instance)
137 return PP_ERROR_FAILED;
138 instance->GetRenderView()->DidStopLoading();
139 return PP_OK;
142 int32_t PepperPDFHost::OnHostMsgSetContentRestriction(
143 ppapi::host::HostMessageContext* context,
144 int restrictions) {
145 content::PepperPluginInstance* instance =
146 host_->GetPluginInstance(pp_instance());
147 if (!instance)
148 return PP_ERROR_FAILED;
149 instance->GetRenderView()->Send(new PDFHostMsg_PDFUpdateContentRestrictions(
150 instance->GetRenderView()->GetRoutingID(), restrictions));
151 return PP_OK;
154 int32_t PepperPDFHost::OnHostMsgUserMetricsRecordAction(
155 ppapi::host::HostMessageContext* context,
156 const std::string& action) {
157 if (action.empty())
158 return PP_ERROR_FAILED;
159 content::RenderThread::Get()->RecordComputedAction(action);
160 return PP_OK;
163 int32_t PepperPDFHost::OnHostMsgHasUnsupportedFeature(
164 ppapi::host::HostMessageContext* context) {
165 content::PepperPluginInstance* instance =
166 host_->GetPluginInstance(pp_instance());
167 if (!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);
173 render_view->Send(
174 new PDFHostMsg_PDFHasUnsupportedFeature(render_view->GetRoutingID()));
175 return PP_OK;
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());
187 if (!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()));
196 render_view->Send(
197 new PDFHostMsg_PDFSaveURLAs(render_view->GetRoutingID(), url, referrer));
198 return PP_OK;
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());
206 if (!instance)
207 return PP_ERROR_FAILED;
208 instance->SetSelectedText(selected_text);
209 return PP_OK;
212 int32_t PepperPDFHost::OnHostMsgSetLinkUnderCursor(
213 ppapi::host::HostMessageContext* context,
214 const std::string& url) {
215 content::PepperPluginInstance* instance =
216 host_->GetPluginInstance(pp_instance());
217 if (!instance)
218 return PP_ERROR_FAILED;
219 instance->SetLinkUnderCursor(url);
220 return PP_OK;
223 } // namespace pdf