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