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 "chrome/renderer/pepper/pepper_extensions_common_host.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "chrome/renderer/extensions/chrome_v8_context.h"
11 #include "content/public/renderer/render_view.h"
12 #include "content/public/renderer/renderer_ppapi_host.h"
13 #include "extensions/renderer/dispatcher.h"
14 #include "extensions/renderer/extension_helper.h"
15 #include "ppapi/c/pp_errors.h"
16 #include "ppapi/host/dispatch_host_message.h"
17 #include "ppapi/host/host_message_context.h"
18 #include "ppapi/host/ppapi_host.h"
19 #include "ppapi/proxy/ppapi_messages.h"
20 #include "third_party/WebKit/public/web/WebDocument.h"
21 #include "third_party/WebKit/public/web/WebElement.h"
22 #include "third_party/WebKit/public/web/WebLocalFrame.h"
23 #include "third_party/WebKit/public/web/WebPluginContainer.h"
26 void DoNothing(bool success
,
27 const base::ListValue
& response
,
28 const std::string
& error
) {}
31 PepperExtensionsCommonHost::PepperExtensionsCommonHost(
32 content::RendererPpapiHost
* host
,
35 extensions::PepperRequestProxy
* pepper_request_proxy
)
36 : ResourceHost(host
->GetPpapiHost(), instance
, resource
),
37 renderer_ppapi_host_(host
),
38 pepper_request_proxy_(pepper_request_proxy
),
39 weak_factory_(this) {}
41 PepperExtensionsCommonHost::~PepperExtensionsCommonHost() {}
44 PepperExtensionsCommonHost
* PepperExtensionsCommonHost::Create(
45 content::RendererPpapiHost
* host
,
47 PP_Resource resource
) {
48 content::RenderView
* render_view
= host
->GetRenderViewForInstance(instance
);
51 extensions::ExtensionHelper
* extension_helper
=
52 extensions::ExtensionHelper::Get(render_view
);
53 if (!extension_helper
)
55 extensions::Dispatcher
* dispatcher
= extension_helper
->dispatcher();
58 blink::WebPluginContainer
* container
=
59 host
->GetContainerForInstance(instance
);
62 blink::WebLocalFrame
* frame
= container
->element().document().frame();
65 v8::HandleScope
scope(v8::Isolate::GetCurrent());
66 // TODO(rockot): Remove this downcast. See http://crbug.com/362616.
67 extensions::ChromeV8Context
* context
=
68 static_cast<extensions::ChromeV8Context
*>(
69 dispatcher
->script_context_set().GetByV8Context(
70 frame
->mainWorldScriptContext()));
74 return new PepperExtensionsCommonHost(
75 host
, instance
, resource
, context
->pepper_request_proxy());
78 int32_t PepperExtensionsCommonHost::OnResourceMessageReceived(
79 const IPC::Message
& msg
,
80 ppapi::host::HostMessageContext
* context
) {
81 PPAPI_BEGIN_MESSAGE_MAP(PepperExtensionsCommonHost
, msg
)
82 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_ExtensionsCommon_Post
,
84 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_ExtensionsCommon_Call
,
86 PPAPI_END_MESSAGE_MAP()
87 return PP_ERROR_FAILED
;
90 void PepperExtensionsCommonHost::OnResponseReceived(
91 ppapi::host::ReplyMessageContext reply_context
,
93 const base::ListValue
& response
,
94 const std::string
& /* error */) {
95 reply_context
.params
.set_result(success
? PP_OK
: PP_ERROR_FAILED
);
96 SendReply(reply_context
, PpapiPluginMsg_ExtensionsCommon_CallReply(response
));
99 int32_t PepperExtensionsCommonHost::OnPost(
100 ppapi::host::HostMessageContext
* context
,
101 const std::string
& request_name
,
102 const base::ListValue
& args
) {
104 bool success
= pepper_request_proxy_
->StartRequest(
105 base::Bind(&DoNothing
), request_name
, args
, &error
);
106 return success
? PP_OK
: PP_ERROR_FAILED
;
109 int32_t PepperExtensionsCommonHost::OnCall(
110 ppapi::host::HostMessageContext
* context
,
111 const std::string
& request_name
,
112 const base::ListValue
& args
) {
114 bool success
= pepper_request_proxy_
->StartRequest(
115 base::Bind(&PepperExtensionsCommonHost::OnResponseReceived
,
116 weak_factory_
.GetWeakPtr(),
117 context
->MakeReplyMessageContext()),
121 return success
? PP_OK_COMPLETIONPENDING
: PP_ERROR_FAILED
;