| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/renderer/pepper/pepper_in_process_resource_creation.h" | 5 #include "content/renderer/pepper/pepper_in_process_resource_creation.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "content/renderer/pepper/content_renderer_pepper_host_factory.h" | |
| 11 #include "content/renderer/render_view_impl.h" | 10 #include "content/renderer/render_view_impl.h" |
| 11 #include "content/renderer/pepper/renderer_ppapi_host_impl.h" |
| 12 #include "content/renderer/pepper/pepper_in_process_router.h" |
| 12 #include "ipc/ipc_message.h" | 13 #include "ipc/ipc_message.h" |
| 13 #include "ipc/ipc_message_macros.h" | 14 #include "ipc/ipc_message_macros.h" |
| 14 #include "ppapi/host/ppapi_host.h" | 15 #include "ppapi/host/ppapi_host.h" |
| 15 #include "ppapi/proxy/file_chooser_resource.h" | 16 #include "ppapi/proxy/file_chooser_resource.h" |
| 16 #include "ppapi/proxy/ppapi_messages.h" | 17 #include "ppapi/proxy/ppapi_messages.h" |
| 17 #include "ppapi/shared_impl/ppapi_globals.h" | 18 #include "ppapi/shared_impl/ppapi_globals.h" |
| 18 #include "ppapi/shared_impl/ppapi_permissions.h" | 19 #include "ppapi/shared_impl/ppapi_permissions.h" |
| 19 #include "ppapi/shared_impl/resource_tracker.h" | 20 #include "ppapi/shared_impl/resource_tracker.h" |
| 20 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 21 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 21 | 22 |
| 22 // Note that the code in the creation functions in this file should generally | 23 // Note that the code in the creation functions in this file should generally |
| 23 // be the same as that in ppapi/proxy/resource_creation_proxy.cc. See | 24 // be the same as that in ppapi/proxy/resource_creation_proxy.cc. See |
| 24 // pepper_in_process_resource_creation.h for what this file is for. | 25 // pepper_in_process_resource_creation.h for what this file is for. |
| 25 | 26 |
| 26 namespace content { | 27 namespace content { |
| 27 | 28 |
| 28 class PepperInProcessResourceCreation::PluginToHostRouter | |
| 29 : public IPC::Sender { | |
| 30 public: | |
| 31 PluginToHostRouter(RenderViewImpl* render_view, | |
| 32 PepperInstanceStateAccessor* state, | |
| 33 IPC::Sender* host_to_plugin_sender, | |
| 34 const ppapi::PpapiPermissions& perms); | |
| 35 virtual ~PluginToHostRouter() {} | |
| 36 | |
| 37 ppapi::host::PpapiHost& host() { return host_; } | |
| 38 | |
| 39 // Sender implementation. | |
| 40 virtual bool Send(IPC::Message* msg) OVERRIDE; | |
| 41 | |
| 42 private: | |
| 43 void DoSend(IPC::Message* msg); | |
| 44 | |
| 45 base::WeakPtrFactory<PluginToHostRouter> weak_factory_; | |
| 46 | |
| 47 ContentRendererPepperHostFactory factory_; | |
| 48 ppapi::host::PpapiHost host_; | |
| 49 | |
| 50 DISALLOW_COPY_AND_ASSIGN(PluginToHostRouter); | |
| 51 }; | |
| 52 | |
| 53 PepperInProcessResourceCreation::PluginToHostRouter::PluginToHostRouter( | |
| 54 RenderViewImpl* render_view, | |
| 55 PepperInstanceStateAccessor* state, | |
| 56 IPC::Sender* host_to_plugin_sender, | |
| 57 const ppapi::PpapiPermissions& perms) | |
| 58 : weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
| 59 factory_(render_view, perms, state), | |
| 60 host_(host_to_plugin_sender, &factory_, perms) { | |
| 61 } | |
| 62 | |
| 63 bool PepperInProcessResourceCreation::PluginToHostRouter::Send( | |
| 64 IPC::Message* msg) { | |
| 65 // Don't directly call into the message handler to avoid reentrancy. The IPC | |
| 66 // systen assumes everything is executed from the message loop, so emulate | |
| 67 // the same thing for in-process. | |
| 68 MessageLoop::current()->PostTask(FROM_HERE, | |
| 69 base::Bind(&PluginToHostRouter::DoSend, weak_factory_.GetWeakPtr(), | |
| 70 base::Owned(msg))); | |
| 71 return true; | |
| 72 } | |
| 73 | |
| 74 void PepperInProcessResourceCreation::PluginToHostRouter::DoSend( | |
| 75 IPC::Message* msg) { | |
| 76 host_.OnMessageReceived(*msg); | |
| 77 } | |
| 78 | |
| 79 // HostToPluginRouter --------------------------------------------------------- | |
| 80 | |
| 81 class PepperInProcessResourceCreation::HostToPluginRouter | |
| 82 : public IPC::Sender { | |
| 83 public: | |
| 84 HostToPluginRouter(); | |
| 85 virtual ~HostToPluginRouter() {} | |
| 86 | |
| 87 // Sender implementation. | |
| 88 virtual bool Send(IPC::Message* msg) OVERRIDE; | |
| 89 | |
| 90 private: | |
| 91 void DispatchMsg(IPC::Message* msg); | |
| 92 | |
| 93 void OnMsgResourceReply( | |
| 94 const ppapi::proxy::ResourceMessageReplyParams& reply_params, | |
| 95 const IPC::Message& nested_msg); | |
| 96 | |
| 97 base::WeakPtrFactory<HostToPluginRouter> weak_factory_; | |
| 98 | |
| 99 DISALLOW_COPY_AND_ASSIGN(HostToPluginRouter); | |
| 100 }; | |
| 101 | |
| 102 PepperInProcessResourceCreation::HostToPluginRouter::HostToPluginRouter() | |
| 103 : weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
| 104 } | |
| 105 | |
| 106 bool PepperInProcessResourceCreation::HostToPluginRouter::Send( | |
| 107 IPC::Message* msg) { | |
| 108 // As in the PluginToHostRouter, dispatch from the message loop. | |
| 109 MessageLoop::current()->PostTask(FROM_HERE, | |
| 110 base::Bind(&HostToPluginRouter::DispatchMsg, | |
| 111 weak_factory_.GetWeakPtr(), | |
| 112 base::Owned(msg))); | |
| 113 return true; | |
| 114 } | |
| 115 | |
| 116 void PepperInProcessResourceCreation::HostToPluginRouter::DispatchMsg( | |
| 117 IPC::Message* msg) { | |
| 118 // Emulate the proxy by dispatching the relevant message here. | |
| 119 IPC_BEGIN_MESSAGE_MAP(HostToPluginRouter, *msg) | |
| 120 IPC_MESSAGE_HANDLER(PpapiPluginMsg_ResourceReply, OnMsgResourceReply) | |
| 121 IPC_END_MESSAGE_MAP() | |
| 122 } | |
| 123 | |
| 124 void PepperInProcessResourceCreation::HostToPluginRouter::OnMsgResourceReply( | |
| 125 const ppapi::proxy::ResourceMessageReplyParams& reply_params, | |
| 126 const IPC::Message& nested_msg) { | |
| 127 ppapi::Resource* resource = | |
| 128 ppapi::PpapiGlobals::Get()->GetResourceTracker()->GetResource( | |
| 129 reply_params.pp_resource()); | |
| 130 if (!resource) { | |
| 131 // The resource could have been destroyed while the async processing was | |
| 132 // pending. Just drop the message. | |
| 133 return; | |
| 134 } | |
| 135 resource->OnReplyReceived(reply_params.sequence(), reply_params.result(), | |
| 136 nested_msg); | |
| 137 } | |
| 138 | |
| 139 // PepperInProcessResourceCreation -------------------------------------------- | 29 // PepperInProcessResourceCreation -------------------------------------------- |
| 140 | 30 |
| 141 PepperInProcessResourceCreation::PepperInProcessResourceCreation( | 31 PepperInProcessResourceCreation::PepperInProcessResourceCreation( |
| 142 RenderViewImpl* render_view, | 32 RendererPpapiHostImpl* host_impl, |
| 143 webkit::ppapi::PluginInstance* instance, | 33 webkit::ppapi::PluginInstance* instance) |
| 144 const ppapi::PpapiPermissions& perms) | |
| 145 : ResourceCreationImpl(instance), | 34 : ResourceCreationImpl(instance), |
| 146 instance_state_(instance->module()), | 35 host_impl_(host_impl) { |
| 147 host_to_plugin_router_(new HostToPluginRouter), | |
| 148 plugin_to_host_router_( | |
| 149 new PluginToHostRouter(render_view, &instance_state_, | |
| 150 host_to_plugin_router_.get(), | |
| 151 perms)) { | |
| 152 render_view->PpapiPluginCreated(&plugin_to_host_router_->host()); | |
| 153 } | 36 } |
| 154 | 37 |
| 155 PepperInProcessResourceCreation::~PepperInProcessResourceCreation() { | 38 PepperInProcessResourceCreation::~PepperInProcessResourceCreation() { |
| 156 } | 39 } |
| 157 | 40 |
| 158 PP_Resource PepperInProcessResourceCreation::CreateFileChooser( | 41 PP_Resource PepperInProcessResourceCreation::CreateFileChooser( |
| 159 PP_Instance instance, | 42 PP_Instance instance, |
| 160 PP_FileChooserMode_Dev mode, | 43 PP_FileChooserMode_Dev mode, |
| 161 const char* accept_types) { | 44 const char* accept_types) { |
| 162 return (new ppapi::proxy::FileChooserResource( | 45 return (new ppapi::proxy::FileChooserResource( |
| 163 plugin_to_host_router_.get(), | 46 host_impl_->in_process_router()->GetPluginConnection(), |
| 164 instance, mode, accept_types))->GetReference(); | 47 instance, mode, accept_types))->GetReference(); |
| 165 } | 48 } |
| 166 | 49 |
| 167 } // namespace content | 50 } // namespace content |
| OLD | NEW |