Home | History | Annotate | Download | only in npapi
      1 // Copyright (c) 2011 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_CHILD_NPAPI_PLUGIN_LIB_H_
      6 #define CONTENT_CHILD_NPAPI_PLUGIN_LIB_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/native_library.h"
     14 #include "build/build_config.h"
     15 #include "content/child/npapi/webplugin.h"
     16 #include "content/common/content_export.h"
     17 #include "content/public/common/webplugininfo.h"
     18 #include "third_party/npapi/bindings/nphostapi.h"
     19 
     20 namespace base {
     21 class FilePath;
     22 }
     23 
     24 namespace content {
     25 
     26 class PluginInstance;
     27 
     28 // This struct holds entry points into a plugin.  The entry points are
     29 // slightly different between Win/Mac and Unixes.  Note that the interface for
     30 // querying plugins is synchronous and it is preferable to use a higher-level
     31 // asynchronous information to query information.
     32 struct PluginEntryPoints {
     33 #if !defined(OS_POSIX) || defined(OS_MACOSX)
     34   NP_GetEntryPointsFunc np_getentrypoints;
     35 #endif
     36   NP_InitializeFunc np_initialize;
     37   NP_ShutdownFunc np_shutdown;
     38 };
     39 
     40 // A PluginLib is a single NPAPI Plugin Library, and is the lifecycle
     41 // manager for new PluginInstances.
     42 class CONTENT_EXPORT PluginLib : public base::RefCounted<PluginLib> {
     43  public:
     44   static PluginLib* CreatePluginLib(const base::FilePath& filename);
     45 
     46   // Unloads all the loaded plugin libraries and cleans up the plugin map.
     47   static void UnloadAllPlugins();
     48 
     49   // Shuts down all loaded plugin instances.
     50   static void ShutdownAllPlugins();
     51 
     52   // Get the Plugin's function pointer table.
     53   NPPluginFuncs* functions();
     54 
     55   // Creates a new instance of this plugin.
     56   PluginInstance* CreateInstance(const std::string& mime_type);
     57 
     58   // Called by the instance when the instance is tearing down.
     59   void CloseInstance();
     60 
     61   // Gets information about this plugin and the mime types that it
     62   // supports.
     63   const WebPluginInfo& plugin_info() { return web_plugin_info_; }
     64 
     65   //
     66   // NPAPI functions
     67   //
     68 
     69   // NPAPI method to initialize a Plugin.
     70   // Initialize can be safely called multiple times
     71   NPError NP_Initialize();
     72 
     73   // NPAPI method to shutdown a Plugin.
     74   void NP_Shutdown(void);
     75 
     76   // NPAPI method to clear locally stored data (LSO's or "Flash cookies").
     77   NPError NP_ClearSiteData(const char* site, uint64 flags, uint64 max_age);
     78 
     79   // NPAPI method to get a NULL-terminated list of all sites under which data
     80   // is stored.
     81   char** NP_GetSitesWithData();
     82 
     83   int instance_count() const { return instance_count_; }
     84 
     85   // Prevents the library code from being unload when Unload() is called (since
     86   // some plugins crash if unloaded).
     87   void PreventLibraryUnload();
     88 
     89   // Indicates whether plugin unload can be deferred.
     90   void set_defer_unload(bool defer_unload) {
     91     defer_unload_ = defer_unload;
     92   }
     93 
     94   // protected for testability.
     95  protected:
     96   friend class base::RefCounted<PluginLib>;
     97 
     98   // Creates a new PluginLib.
     99   explicit PluginLib(const WebPluginInfo& info);
    100 
    101   virtual ~PluginLib();
    102 
    103   // Attempts to load the plugin from the library.
    104   // Returns true if it is a legitimate plugin, false otherwise
    105   bool Load();
    106 
    107   // Unloads the plugin library.
    108   void Unload();
    109 
    110   // Shutdown the plugin library.
    111   void Shutdown();
    112 
    113  private:
    114   WebPluginInfo web_plugin_info_;  // Supported mime types, description
    115   base::NativeLibrary library_;  // The opened library reference.
    116   NPPluginFuncs plugin_funcs_;  // The struct of plugin side functions.
    117   bool initialized_;  // Is the plugin initialized?
    118   NPSavedData *saved_data_;  // Persisted plugin info for NPAPI.
    119   int instance_count_;  // Count of plugins in use.
    120   bool skip_unload_;  // True if library_ should not be unloaded.
    121 
    122   // Function pointers to entry points into the plugin.
    123   PluginEntryPoints entry_points_;
    124 
    125   // Set to true if unloading of the plugin dll is to be deferred.
    126   bool defer_unload_;
    127 
    128   DISALLOW_COPY_AND_ASSIGN(PluginLib);
    129 };
    130 
    131 }  // namespace content
    132 
    133 #endif  // CONTENT_CHILD_NPAPI_PLUGIN_LIB_H_
    134