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