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 "chrome/renderer/extensions/dispatcher.h"
12 #include "chrome/renderer/extensions/extension_helper.h"
13 #include "content/public/renderer/render_view.h"
14 #include "content/public/renderer/renderer_ppapi_host.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/WebFrame.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::WebFrame
* frame
= container
->element().document().frame();
65 v8::HandleScope
scope(v8::Isolate::GetCurrent());
66 extensions::ChromeV8Context
* context
=
67 dispatcher
->v8_context_set().GetByV8Context(
68 frame
->mainWorldScriptContext());
72 return new PepperExtensionsCommonHost(
73 host
, instance
, resource
, context
->pepper_request_proxy());
76 int32_t PepperExtensionsCommonHost::OnResourceMessageReceived(
77 const IPC::Message
& msg
,
78 ppapi::host::HostMessageContext
* context
) {
79 IPC_BEGIN_MESSAGE_MAP(PepperExtensionsCommonHost
, msg
)
80 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_ExtensionsCommon_Post
,
82 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_ExtensionsCommon_Call
,
85 return PP_ERROR_FAILED
;
88 void PepperExtensionsCommonHost::OnResponseReceived(
89 scoped_ptr
<ppapi::host::ReplyMessageContext
> context
,
91 const base::ListValue
& response
,
92 const std::string
& /* error */) {
93 context
->params
.set_result(success
? PP_OK
: PP_ERROR_FAILED
);
94 SendReply(*context
, PpapiPluginMsg_ExtensionsCommon_CallReply(response
));
97 int32_t PepperExtensionsCommonHost::OnPost(
98 ppapi::host::HostMessageContext
* context
,
99 const std::string
& request_name
,
100 const base::ListValue
& args
) {
102 bool success
= pepper_request_proxy_
->StartRequest(
103 base::Bind(&DoNothing
), request_name
, args
, &error
);
104 return success
? PP_OK
: PP_ERROR_FAILED
;
107 int32_t PepperExtensionsCommonHost::OnCall(
108 ppapi::host::HostMessageContext
* context
,
109 const std::string
& request_name
,
110 const base::ListValue
& args
) {
112 scoped_ptr
<ppapi::host::ReplyMessageContext
> message_context(
113 new ppapi::host::ReplyMessageContext(context
->MakeReplyMessageContext()));
114 bool success
= pepper_request_proxy_
->StartRequest(
115 base::Bind(&PepperExtensionsCommonHost::OnResponseReceived
,
116 weak_factory_
.GetWeakPtr(),
117 base::Passed(&message_context
)),
121 return success
? PP_OK_COMPLETIONPENDING
: PP_ERROR_FAILED
;