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 3 // found in the LICENSE file. 4 5 #ifndef CONTENT_RENDERER_PEPPER_PEPPER_BROKER_H_ 6 #define CONTENT_RENDERER_PEPPER_PEPPER_BROKER_H_ 7 8 #include "base/memory/ref_counted.h" 9 #include "base/process/process.h" 10 #include "base/sync_socket.h" 11 #include "content/common/content_export.h" 12 #include "content/renderer/pepper/ppb_broker_impl.h" 13 #include "ppapi/proxy/proxy_channel.h" 14 15 namespace IPC { 16 struct ChannelHandle; 17 } 18 19 namespace ppapi { 20 namespace proxy { 21 class BrokerDispatcher; 22 } 23 } 24 25 namespace content { 26 27 class PluginModule; 28 29 // This object is NOT thread-safe. 30 class CONTENT_EXPORT PepperBrokerDispatcherWrapper { 31 public: 32 PepperBrokerDispatcherWrapper(); 33 ~PepperBrokerDispatcherWrapper(); 34 35 bool Init(base::ProcessId broker_pid, 36 const IPC::ChannelHandle& channel_handle); 37 38 int32_t SendHandleToBroker(PP_Instance instance, 39 base::SyncSocket::Handle handle); 40 41 private: 42 scoped_ptr<ppapi::proxy::BrokerDispatcher> dispatcher_; 43 scoped_ptr<ppapi::proxy::ProxyChannel::Delegate> dispatcher_delegate_; 44 }; 45 46 class PepperBroker : public base::RefCountedThreadSafe<PepperBroker> { 47 public: 48 explicit PepperBroker(PluginModule* plugin_module); 49 50 // Decrements the references to the broker. 51 // When there are no more references, this renderer's dispatcher is 52 // destroyed, allowing the broker to shutdown if appropriate. 53 // Callers should not reference this object after calling Disconnect(). 54 void Disconnect(PPB_Broker_Impl* client); 55 56 // Adds a pending connection to the broker. Balances out Disconnect() calls. 57 void AddPendingConnect(PPB_Broker_Impl* client); 58 59 // Called when the channel to the broker has been established. 60 void OnBrokerChannelConnected(base::ProcessId broker_pid, 61 const IPC::ChannelHandle& channel_handle); 62 63 // Called when we know whether permission to access the PPAPI broker was 64 // granted. 65 void OnBrokerPermissionResult(PPB_Broker_Impl* client, bool result); 66 67 private: 68 friend class base::RefCountedThreadSafe<PepperBroker>; 69 70 struct PendingConnection { 71 PendingConnection(); 72 ~PendingConnection(); 73 74 bool is_authorized; 75 base::WeakPtr<PPB_Broker_Impl> client; 76 }; 77 78 virtual ~PepperBroker(); 79 80 // Reports failure to all clients that had pending operations. 81 void ReportFailureToClients(int error_code); 82 83 // Connects the plugin to the broker via a pipe. 84 void ConnectPluginToBroker(PPB_Broker_Impl* client); 85 86 scoped_ptr<PepperBrokerDispatcherWrapper> dispatcher_; 87 88 // A map of pointers to objects that have requested a connection to the weak 89 // pointer we can use to reference them. The mapping is needed so we can clean 90 // up entries for objects that may have been deleted. 91 typedef std::map<PPB_Broker_Impl*, PendingConnection> ClientMap; 92 ClientMap pending_connects_; 93 94 // Pointer to the associated plugin module. 95 // Always set and cleared at the same time as the module's pointer to this. 96 PluginModule* plugin_module_; 97 98 DISALLOW_COPY_AND_ASSIGN(PepperBroker); 99 }; 100 101 } // namespace content 102 103 #endif // CONTENT_RENDERER_PEPPER_PEPPER_BROKER_H_ 104