ppapi_thread.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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_PPAPI_PLUGIN_PPAPI_THREAD_H_
6#define CONTENT_PPAPI_PLUGIN_PPAPI_THREAD_H_
7
8#include <map>
9#include <string>
10
11#include "base/basictypes.h"
12#include "base/compiler_specific.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/process.h"
15#include "base/scoped_native_library.h"
16#include "build/build_config.h"
17#include "content/common/child_thread.h"
18#include "ipc/ipc_listener.h"
19#include "ppapi/c/pp_module.h"
20#include "ppapi/c/trusted/ppp_broker.h"
21#include "ppapi/proxy/plugin_dispatcher.h"
22#include "ppapi/proxy/plugin_globals.h"
23#include "ppapi/proxy/plugin_proxy_delegate.h"
24#include "webkit/plugins/ppapi/plugin_module.h"
25
26#if defined(OS_WIN)
27#include "base/win/scoped_handle.h"
28#endif
29
30class CommandLine;
31
32namespace base {
33class FilePath;
34}
35
36namespace IPC {
37struct ChannelHandle;
38}
39
40namespace content {
41
42class PpapiWebKitPlatformSupportImpl;
43
44class PpapiThread : public ChildThread,
45                    public ppapi::proxy::PluginDispatcher::PluginDelegate,
46                    public ppapi::proxy::PluginProxyDelegate {
47 public:
48  PpapiThread(const CommandLine& command_line, bool is_broker);
49  virtual ~PpapiThread();
50  virtual void Shutdown() OVERRIDE;
51
52 private:
53  // Make sure the enum list in tools/histogram/histograms.xml is updated with
54  // any change in this list.
55  enum LoadResult {
56    LOAD_SUCCESS,
57    LOAD_FAILED,
58    ENTRY_POINT_MISSING,
59    INIT_FAILED,
60    // NOTE: Add new values only immediately above this line.
61    LOAD_RESULT_MAX  // Boundary value for UMA_HISTOGRAM_ENUMERATION.
62  };
63
64  // This class finds the target PluginDispatcher for each message it receives
65  // and forwards the message.
66  class DispatcherMessageListener : public IPC::Listener {
67   public:
68    explicit DispatcherMessageListener(PpapiThread* owner);
69    virtual ~DispatcherMessageListener();
70
71    // IPC::Listener implementation.
72    virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
73
74   private:
75    PpapiThread* owner_;
76
77    DISALLOW_COPY_AND_ASSIGN(DispatcherMessageListener);
78  };
79
80  // ChildThread overrides.
81  virtual bool Send(IPC::Message* msg) OVERRIDE;
82  virtual bool OnControlMessageReceived(const IPC::Message& msg) OVERRIDE;
83  virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
84
85  // PluginDispatcher::PluginDelegate implementation.
86  virtual std::set<PP_Instance>* GetGloballySeenInstanceIDSet() OVERRIDE;
87  virtual base::MessageLoopProxy* GetIPCMessageLoop() OVERRIDE;
88  virtual base::WaitableEvent* GetShutdownEvent() OVERRIDE;
89  virtual IPC::PlatformFileForTransit ShareHandleWithRemote(
90      base::PlatformFile handle,
91      base::ProcessId peer_pid,
92      bool should_close_source) OVERRIDE;
93  virtual uint32 Register(
94      ppapi::proxy::PluginDispatcher* plugin_dispatcher) OVERRIDE;
95  virtual void Unregister(uint32 plugin_dispatcher_id) OVERRIDE;
96
97  // PluginProxyDelegate.
98  // SendToBrowser() is intended to be safe to use on another thread so
99  // long as the main PpapiThread outlives it.
100  virtual IPC::Sender* GetBrowserSender() OVERRIDE;
101  virtual std::string GetUILanguage() OVERRIDE;
102  virtual void PreCacheFont(const void* logfontw) OVERRIDE;
103  virtual void SetActiveURL(const std::string& url) OVERRIDE;
104
105  // Message handlers.
106  void OnLoadPlugin(const base::FilePath& path,
107                    const ppapi::PpapiPermissions& permissions);
108  void OnCreateChannel(base::ProcessId renderer_pid,
109                       int renderer_child_id,
110                       bool incognito);
111  void OnResourceReply(
112      const ppapi::proxy::ResourceMessageReplyParams& reply_params,
113      const IPC::Message& nested_msg);
114  void OnSetNetworkState(bool online);
115  void OnCrash();
116  void OnHang();
117
118  // Sets up the channel to the given renderer. On success, returns true and
119  // fills the given ChannelHandle with the information from the new channel.
120  bool SetupRendererChannel(base::ProcessId renderer_pid,
121                            int renderer_child_id,
122                            bool incognito,
123                            IPC::ChannelHandle* handle);
124
125  // Sets up the name of the plugin for logging using the given path.
126  void SavePluginName(const base::FilePath& path);
127
128  void ReportLoadResult(const base::FilePath& path, LoadResult result);
129
130  // True if running in a broker process rather than a normal plugin process.
131  bool is_broker_;
132
133  base::ScopedNativeLibrary library_;
134
135  ppapi::PpapiPermissions permissions_;
136
137  // Global state tracking for the proxy.
138  ppapi::proxy::PluginGlobals plugin_globals_;
139
140  // Storage for plugin entry points.
141  webkit::ppapi::PluginModule::EntryPoints plugin_entry_points_;
142
143  // Callback to call when a new instance connects to the broker.
144  // Used only when is_broker_.
145  PP_ConnectInstance_Func connect_instance_func_;
146
147  // Local concept of the module ID. Some functions take this. It's necessary
148  // for the in-process PPAPI to handle this properly, but for proxied it's
149  // unnecessary. The proxy talking to multiple renderers means that each
150  // renderer has a different idea of what the module ID is for this plugin.
151  // To force people to "do the right thing" we generate a random module ID
152  // and pass it around as necessary.
153  PP_Module local_pp_module_;
154
155  // See Dispatcher::Delegate::GetGloballySeenInstanceIDSet.
156  std::set<PP_Instance> globally_seen_instance_ids_;
157
158  // The PluginDispatcher instances contained in the map are not owned by it.
159  std::map<uint32, ppapi::proxy::PluginDispatcher*> plugin_dispatchers_;
160  uint32 next_plugin_dispatcher_id_;
161
162  // The WebKitPlatformSupport implementation.
163  scoped_ptr<PpapiWebKitPlatformSupportImpl> webkit_platform_support_;
164
165#if defined(OS_WIN)
166  // Caches the handle to the peer process if this is a broker.
167  base::win::ScopedHandle peer_handle_;
168#endif
169
170  DispatcherMessageListener dispatcher_message_listener_;
171
172  DISALLOW_IMPLICIT_CONSTRUCTORS(PpapiThread);
173};
174
175}  // namespace content
176
177#endif  // CONTENT_PPAPI_PLUGIN_PPAPI_THREAD_H_
178