1 // Copyright 2014 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 PPAPI_NACL_IRT_PPAPI_DISPATCHER_H_
6 #define PPAPI_NACL_IRT_PPAPI_DISPATCHER_H_
7 
8 #include <stdint.h>
9 
10 #include <map>
11 #include <set>
12 #include <string>
13 
14 #include "base/files/file.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/process/process_handle.h"
17 #include "ipc/ipc_channel_handle.h"
18 #include "ipc/ipc_listener.h"
19 #include "ipc/ipc_platform_file.h"
20 #include "ipc/ipc_sender.h"
21 #include "ppapi/c/pp_instance.h"
22 #include "ppapi/c/trusted/ppb_browser_font_trusted.h"
23 #include "ppapi/proxy/connection.h"
24 #include "ppapi/proxy/plugin_dispatcher.h"
25 #include "ppapi/proxy/plugin_proxy_delegate.h"
26 
27 struct PP_BrowserFont_Trusted_Description;
28 
29 namespace base {
30 class SingleThreadTaskRunner;
31 class WaitableEvent;
32 }  // namespace base
33 
34 namespace IPC {
35 class Message;
36 class SyncChannel;
37 }  // namespace IPC
38 
39 namespace ppapi {
40 
41 struct PpapiNaClPluginArgs;
42 struct Preferences;
43 
44 // This class manages communication between the plugin and the browser, and
45 // manages the PluginDispatcher instances for communication between the plugin
46 // and the renderer.
47 class PpapiDispatcher : public proxy::PluginDispatcher::PluginDelegate,
48                         public proxy::PluginProxyDelegate,
49                         public IPC::Listener,
50                         public IPC::Sender {
51  public:
52   PpapiDispatcher(scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
53                   base::WaitableEvent* shutdown_event,
54                   IPC::ChannelHandle browser_ipc_handle,
55                   IPC::ChannelHandle renderer_ipc_handle);
56 
57   // PluginDispatcher::PluginDelegate implementation.
58   base::SingleThreadTaskRunner* GetIPCTaskRunner() override;
59   base::WaitableEvent* GetShutdownEvent() override;
60   IPC::PlatformFileForTransit ShareHandleWithRemote(
61       base::PlatformFile handle,
62       base::ProcessId peer_pid,
63       bool should_close_source) override;
64   base::UnsafeSharedMemoryRegion ShareUnsafeSharedMemoryRegionWithRemote(
65       const base::UnsafeSharedMemoryRegion& region,
66       base::ProcessId remote_pid) override;
67   base::ReadOnlySharedMemoryRegion ShareReadOnlySharedMemoryRegionWithRemote(
68       const base::ReadOnlySharedMemoryRegion& region,
69       base::ProcessId remote_pid) override;
70   std::set<PP_Instance>* GetGloballySeenInstanceIDSet() override;
71   uint32_t Register(proxy::PluginDispatcher* plugin_dispatcher) override;
72   void Unregister(uint32_t plugin_dispatcher_id) override;
73 
74   // PluginProxyDelegate implementation.
75   IPC::Sender* GetBrowserSender() override;
76   std::string GetUILanguage() override;
77   void PreCacheFontForFlash(const void* logfontw) override;
78   void SetActiveURL(const std::string& url) override;
79   PP_Resource CreateBrowserFont(proxy::Connection connection,
80                                 PP_Instance instance,
81                                 const PP_BrowserFont_Trusted_Description& desc,
82                                 const Preferences& prefs) override;
83 
84   // IPC::Listener implementation.
85   bool OnMessageReceived(const IPC::Message& message) override;
86   void OnChannelError() override;
87 
88   // IPC::Sender implementation
89   bool Send(IPC::Message* message) override;
90 
91  private:
92   void OnMsgInitializeNaClDispatcher(const PpapiNaClPluginArgs& args);
93   void OnPluginDispatcherMessageReceived(const IPC::Message& msg);
94 
95   std::set<PP_Instance> instances_;
96   std::map<uint32_t, proxy::PluginDispatcher*> plugin_dispatchers_;
97   uint32_t next_plugin_dispatcher_id_;
98 
99   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
100   base::WaitableEvent* shutdown_event_;
101   IPC::ChannelHandle renderer_ipc_handle_;
102   std::unique_ptr<IPC::SyncChannel> channel_;
103 };
104 
105 }  // namespace ppapi
106 
107 #endif  // PPAPI_NACL_IRT_PPAPI_DISPATCHER_H_
108