Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Side by Side Diff: chrome/renderer/pepper/pepper_extensions_common_host.cc

Issue 61383003: Pass pepper apps API calls through the existing js apps API bindings. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/renderer/pepper/pepper_extensions_common_host.h" 5 #include "chrome/renderer/pepper/pepper_extensions_common_host.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "chrome/renderer/extensions/chrome_v8_context.h" 10 #include "chrome/renderer/extensions/chrome_v8_context.h"
11 #include "chrome/renderer/extensions/dispatcher.h" 11 #include "chrome/renderer/extensions/dispatcher.h"
12 #include "chrome/renderer/extensions/extension_helper.h" 12 #include "chrome/renderer/extensions/extension_helper.h"
13 #include "content/public/renderer/render_view.h" 13 #include "content/public/renderer/render_view.h"
14 #include "content/public/renderer/renderer_ppapi_host.h" 14 #include "content/public/renderer/renderer_ppapi_host.h"
15 #include "ppapi/c/pp_errors.h" 15 #include "ppapi/c/pp_errors.h"
16 #include "ppapi/host/dispatch_host_message.h" 16 #include "ppapi/host/dispatch_host_message.h"
17 #include "ppapi/host/host_message_context.h" 17 #include "ppapi/host/host_message_context.h"
18 #include "ppapi/host/ppapi_host.h" 18 #include "ppapi/host/ppapi_host.h"
19 #include "ppapi/proxy/ppapi_messages.h" 19 #include "ppapi/proxy/ppapi_messages.h"
20 #include "third_party/WebKit/public/web/WebDocument.h" 20 #include "third_party/WebKit/public/web/WebDocument.h"
21 #include "third_party/WebKit/public/web/WebElement.h" 21 #include "third_party/WebKit/public/web/WebElement.h"
22 #include "third_party/WebKit/public/web/WebFrame.h" 22 #include "third_party/WebKit/public/web/WebFrame.h"
23 #include "third_party/WebKit/public/web/WebPluginContainer.h" 23 #include "third_party/WebKit/public/web/WebPluginContainer.h"
24 24
25 PepperExtensionsCommonHost::PepperExtensionsCommonHost( 25 PepperExtensionsCommonHost::PepperExtensionsCommonHost(
26 content::RendererPpapiHost* host, 26 content::RendererPpapiHost* host,
27 PP_Instance instance, 27 PP_Instance instance,
28 PP_Resource resource, 28 PP_Resource resource,
29 extensions::Dispatcher* dispatcher) 29 extensions::PepperRequestProxy* pepper_request_proxy)
30 : ResourceHost(host->GetPpapiHost(), instance, resource), 30 : ResourceHost(host->GetPpapiHost(), instance, resource),
31 renderer_ppapi_host_(host), 31 renderer_ppapi_host_(host),
32 dispatcher_(dispatcher) { 32 pepper_request_proxy_(pepper_request_proxy),
33 weak_factory_(this) {
34 callback_ = base::Bind(&PepperExtensionsCommonHost::OnResponseReceived,
not at google - send to devlin 2013/12/03 17:32:50 holding onto this callback is weird. just create i
Sam McNally 2013/12/04 00:01:36 Done.
35 weak_factory_.GetWeakPtr());
33 } 36 }
34 37
35 PepperExtensionsCommonHost::~PepperExtensionsCommonHost() { 38 PepperExtensionsCommonHost::~PepperExtensionsCommonHost() {}
36 dispatcher_->request_sender()->InvalidateSource(this);
37 }
38 39
39 // static 40 // static
40 PepperExtensionsCommonHost* PepperExtensionsCommonHost::Create( 41 PepperExtensionsCommonHost* PepperExtensionsCommonHost::Create(
41 content::RendererPpapiHost* host, 42 content::RendererPpapiHost* host,
42 PP_Instance instance, 43 PP_Instance instance,
43 PP_Resource resource) { 44 PP_Resource resource) {
44 content::RenderView* render_view = host->GetRenderViewForInstance(instance); 45 content::RenderView* render_view = host->GetRenderViewForInstance(instance);
45 if (!render_view) 46 if (!render_view)
46 return NULL; 47 return NULL;
47 extensions::ExtensionHelper* extension_helper = 48 extensions::ExtensionHelper* extension_helper =
48 extensions::ExtensionHelper::Get(render_view); 49 extensions::ExtensionHelper::Get(render_view);
49 if (!extension_helper) 50 if (!extension_helper)
50 return NULL; 51 return NULL;
51 extensions::Dispatcher* dispatcher = extension_helper->dispatcher(); 52 extensions::Dispatcher* dispatcher = extension_helper->dispatcher();
52 if (!dispatcher) 53 if (!dispatcher)
53 return NULL; 54 return NULL;
55 blink::WebPluginContainer* container =
56 host->GetContainerForInstance(instance);
57 if (!container)
58 return NULL;
59 blink::WebFrame* frame = container->element().document().frame();
60 if (!frame)
61 return NULL;
62 v8::HandleScope scope(v8::Isolate::GetCurrent());
63 extensions::ChromeV8Context* context =
64 dispatcher->v8_context_set().GetByV8Context(
65 frame->mainWorldScriptContext());
66 if (!context)
67 return NULL;
54 68
55 return new PepperExtensionsCommonHost(host, instance, resource, dispatcher); 69 return new PepperExtensionsCommonHost(
70 host, instance, resource, context->pepper_request_proxy());
56 } 71 }
57 72
58 int32_t PepperExtensionsCommonHost::OnResourceMessageReceived( 73 int32_t PepperExtensionsCommonHost::OnResourceMessageReceived(
59 const IPC::Message& msg, 74 const IPC::Message& msg,
60 ppapi::host::HostMessageContext* context) { 75 ppapi::host::HostMessageContext* context) {
61 IPC_BEGIN_MESSAGE_MAP(PepperExtensionsCommonHost, msg) 76 IPC_BEGIN_MESSAGE_MAP(PepperExtensionsCommonHost, msg)
62 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_ExtensionsCommon_Post, 77 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_ExtensionsCommon_Post,
63 OnPost) 78 OnPost)
64 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_ExtensionsCommon_Call, 79 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_ExtensionsCommon_Call,
65 OnCall) 80 OnCall)
66 IPC_END_MESSAGE_MAP() 81 IPC_END_MESSAGE_MAP()
67 return PP_ERROR_FAILED; 82 return PP_ERROR_FAILED;
68 } 83 }
69 84
70 extensions::ChromeV8Context* PepperExtensionsCommonHost::GetContext() {
71 blink::WebPluginContainer* container =
72 renderer_ppapi_host_->GetContainerForInstance(pp_instance());
73 if (!container)
74 return NULL;
75
76 blink::WebFrame* frame = container->element().document().frame();
77 v8::HandleScope scope(v8::Isolate::GetCurrent());
78 return dispatcher_->v8_context_set().GetByV8Context(
79 frame->mainWorldScriptContext());
80 }
81
82 void PepperExtensionsCommonHost::OnResponseReceived( 85 void PepperExtensionsCommonHost::OnResponseReceived(
83 const std::string& /* name */,
84 int request_id, 86 int request_id,
85 bool success, 87 bool success,
86 const base::ListValue& response, 88 const base::ListValue& response,
87 const std::string& /* error */) { 89 const std::string& /* error */) {
88 PendingRequestMap::iterator iter = pending_request_map_.find(request_id); 90 PendingRequestMap::iterator iter = pending_request_map_.find(request_id);
89 91
90 // Ignore responses resulted from calls to OnPost(). 92 DCHECK(iter != pending_request_map_.end());
91 if (iter == pending_request_map_.end()) {
92 DCHECK_EQ(0u, response.GetSize());
93 return;
94 }
95 93
96 linked_ptr<ppapi::host::ReplyMessageContext> context = iter->second; 94 linked_ptr<ppapi::host::ReplyMessageContext> context = iter->second;
97 pending_request_map_.erase(iter); 95 pending_request_map_.erase(iter);
98 96
99 context->params.set_result(success ? PP_OK : PP_ERROR_FAILED); 97 context->params.set_result(success ? PP_OK : PP_ERROR_FAILED);
100 SendReply(*context, PpapiPluginMsg_ExtensionsCommon_CallReply(response)); 98 SendReply(*context, PpapiPluginMsg_ExtensionsCommon_CallReply(response));
101 } 99 }
102 100
103 int32_t PepperExtensionsCommonHost::OnPost( 101 int32_t PepperExtensionsCommonHost::OnPost(
104 ppapi::host::HostMessageContext* context, 102 ppapi::host::HostMessageContext* context,
105 const std::string& request_name, 103 const std::string& request_name,
106 base::ListValue& args) { 104 base::ListValue& args) {
not at google - send to devlin 2013/12/03 17:32:50 this (and below) is a non-const reference? ok well
Sam McNally 2013/12/04 00:01:36 Done.
107 // TODO(yzshen): Add support for calling into JS for APIs that have custom 105 std::string error;
108 // bindings. 106 bool success = pepper_request_proxy_->StartPost(request_name, &args, &error);
109 int request_id = dispatcher_->request_sender()->GetNextRequestId(); 107 if (!success)
110 dispatcher_->request_sender()->StartRequest(this, request_name, request_id, 108 return PP_ERROR_FAILED;
111 false, false, &args); 109
112 return PP_OK; 110 return PP_OK;
113 } 111 }
114 112
115 int32_t PepperExtensionsCommonHost::OnCall( 113 int32_t PepperExtensionsCommonHost::OnCall(
116 ppapi::host::HostMessageContext* context, 114 ppapi::host::HostMessageContext* context,
117 const std::string& request_name, 115 const std::string& request_name,
118 base::ListValue& args) { 116 base::ListValue& args) {
119 // TODO(yzshen): Add support for calling into JS for APIs that have custom 117 int request_id;
120 // bindings. 118 std::string error;
121 int request_id = dispatcher_->request_sender()->GetNextRequestId(); 119 bool success = pepper_request_proxy_->StartCall(
120 callback_, request_name, &args, &request_id, &error);
121 if (!success)
122 return PP_ERROR_FAILED;
123
122 pending_request_map_[request_id] = 124 pending_request_map_[request_id] =
not at google - send to devlin 2013/12/03 17:32:50 I think that pending_request_map_ is unnecessary i
Sam McNally 2013/12/04 00:01:36 Done.
123 linked_ptr<ppapi::host::ReplyMessageContext>( 125 linked_ptr<ppapi::host::ReplyMessageContext>(
124 new ppapi::host::ReplyMessageContext( 126 new ppapi::host::ReplyMessageContext(
125 context->MakeReplyMessageContext())); 127 context->MakeReplyMessageContext()));
126 128
127 dispatcher_->request_sender()->StartRequest(this, request_name, request_id,
128 true, false, &args);
129 return PP_OK_COMPLETIONPENDING; 129 return PP_OK_COMPLETIONPENDING;
130 } 130 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698