Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / child / npapi / plugin_lib.h
blob382e281c4a2083b7611a5c78f948a82c778fab84
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.
5 #ifndef CONTENT_CHILD_NPAPI_PLUGIN_LIB_H_
6 #define CONTENT_CHILD_NPAPI_PLUGIN_LIB_H_
8 #include <string>
9 #include <vector>
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"
20 namespace base {
21 class FilePath;
24 namespace content {
26 class PluginInstance;
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;
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);
46 // Unloads all the loaded plugin libraries and cleans up the plugin map.
47 static void UnloadAllPlugins();
49 // Shuts down all loaded plugin instances.
50 static void ShutdownAllPlugins();
52 // Get the Plugin's function pointer table.
53 NPPluginFuncs* functions();
55 // Creates a new instance of this plugin.
56 PluginInstance* CreateInstance(const std::string& mime_type);
58 // Called by the instance when the instance is tearing down.
59 void CloseInstance();
61 // Gets information about this plugin and the mime types that it
62 // supports.
63 const WebPluginInfo& plugin_info() { return web_plugin_info_; }
66 // NPAPI functions
69 // NPAPI method to initialize a Plugin.
70 // Initialize can be safely called multiple times
71 NPError NP_Initialize();
73 // NPAPI method to shutdown a Plugin.
74 void NP_Shutdown(void);
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);
79 // NPAPI method to get a NULL-terminated list of all sites under which data
80 // is stored.
81 char** NP_GetSitesWithData();
83 int instance_count() const { return instance_count_; }
85 // Prevents the library code from being unload when Unload() is called (since
86 // some plugins crash if unloaded).
87 void PreventLibraryUnload();
89 // Indicates whether plugin unload can be deferred.
90 void set_defer_unload(bool defer_unload) {
91 defer_unload_ = defer_unload;
94 // protected for testability.
95 protected:
96 friend class base::RefCounted<PluginLib>;
98 // Creates a new PluginLib.
99 explicit PluginLib(const WebPluginInfo& info);
101 virtual ~PluginLib();
103 // Attempts to load the plugin from the library.
104 // Returns true if it is a legitimate plugin, false otherwise
105 bool Load();
107 // Unloads the plugin library.
108 void Unload();
110 // Shutdown the plugin library.
111 void Shutdown();
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.
122 // Function pointers to entry points into the plugin.
123 PluginEntryPoints entry_points_;
125 // Set to true if unloading of the plugin dll is to be deferred.
126 bool defer_unload_;
128 DISALLOW_COPY_AND_ASSIGN(PluginLib);
131 } // namespace content
133 #endif // CONTENT_CHILD_NPAPI_PLUGIN_LIB_H_