Home | History | Annotate | Download | only in ppapi_plugin
      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 
     30 class CommandLine;
     31 
     32 namespace base {
     33 class FilePath;
     34 }
     35 
     36 namespace IPC {
     37 struct ChannelHandle;
     38 }
     39 
     40 namespace content {
     41 
     42 class PpapiWebKitPlatformSupportImpl;
     43 
     44 class 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   // 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                     bool supports_dev_channel);
     98   void OnCreateChannel(base::ProcessId renderer_pid,
     99                        int renderer_child_id,
    100                        bool incognito);
    101   void OnSetNetworkState(bool online);
    102   void OnCrash();
    103   void OnHang();
    104 
    105   // Sets up the channel to the given renderer. On success, returns true and
    106   // fills the given ChannelHandle with the information from the new channel.
    107   bool SetupRendererChannel(base::ProcessId renderer_pid,
    108                             int renderer_child_id,
    109                             bool incognito,
    110                             IPC::ChannelHandle* handle);
    111 
    112   // Sets up the name of the plugin for logging using the given path.
    113   void SavePluginName(const base::FilePath& path);
    114 
    115   void ReportLoadResult(const base::FilePath& path, LoadResult result);
    116 
    117   // True if running in a broker process rather than a normal plugin process.
    118   bool is_broker_;
    119 
    120   base::ScopedNativeLibrary library_;
    121 
    122   ppapi::PpapiPermissions permissions_;
    123 
    124   // Global state tracking for the proxy.
    125   ppapi::proxy::PluginGlobals plugin_globals_;
    126 
    127   // Storage for plugin entry points.
    128   PepperPluginInfo::EntryPoints plugin_entry_points_;
    129 
    130   // Callback to call when a new instance connects to the broker.
    131   // Used only when is_broker_.
    132   PP_ConnectInstance_Func connect_instance_func_;
    133 
    134   // Local concept of the module ID. Some functions take this. It's necessary
    135   // for the in-process PPAPI to handle this properly, but for proxied it's
    136   // unnecessary. The proxy talking to multiple renderers means that each
    137   // renderer has a different idea of what the module ID is for this plugin.
    138   // To force people to "do the right thing" we generate a random module ID
    139   // and pass it around as necessary.
    140   PP_Module local_pp_module_;
    141 
    142   // See Dispatcher::Delegate::GetGloballySeenInstanceIDSet.
    143   std::set<PP_Instance> globally_seen_instance_ids_;
    144 
    145   // The PluginDispatcher instances contained in the map are not owned by it.
    146   std::map<uint32, ppapi::proxy::PluginDispatcher*> plugin_dispatchers_;
    147   uint32 next_plugin_dispatcher_id_;
    148 
    149   // The WebKitPlatformSupport implementation.
    150   scoped_ptr<PpapiWebKitPlatformSupportImpl> webkit_platform_support_;
    151 
    152 #if defined(OS_WIN)
    153   // Caches the handle to the peer process if this is a broker.
    154   base::win::ScopedHandle peer_handle_;
    155 #endif
    156 
    157   DISALLOW_IMPLICIT_CONSTRUCTORS(PpapiThread);
    158 };
    159 
    160 }  // namespace content
    161 
    162 #endif  // CONTENT_PPAPI_PLUGIN_PPAPI_THREAD_H_
    163