8cc541363eb627ea9319bb2ee846f843fadf4121
[platform/framework/web/crosswalk-tizen.git] /
1 // Copyright 2016 Samsung Electronics. 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 EWK_EFL_INTEGRATION_COMMON_TRUSTED_PEPPER_PLUGIN_INFO_CACHE_H_
6 #define EWK_EFL_INTEGRATION_COMMON_TRUSTED_PEPPER_PLUGIN_INFO_CACHE_H_
7
8 #include <string>
9 #include <memory>
10 #include <unordered_map>
11 #include <utility>
12 #include <vector>
13
14 #include "base/macros.h"
15 #include "base/files/file_path.h"
16 #include "content/public/common/pepper_plugin_info.h"
17
18 namespace base {
19 template <typename T> struct DefaultSingletonTraits;
20 }
21
22 namespace pepper {
23
24 // Class holding (caching) discovered plugins in given directories.
25 // Plugin needs to have manifest file pmf which descibes it.
26 //
27 // See |ParsePepperPluginManifest| for Plugin Manifest File (pmf) specification.
28 class TrustedPepperPluginInfoCache {
29  public:
30   // Gets singleton of representing cache built for predefined directories.
31   static TrustedPepperPluginInfoCache* GetInstance();
32
33   // Special constructor only used during unit testing:
34   explicit TrustedPepperPluginInfoCache(
35       const std::vector<base::FilePath>& paths);
36
37   ~TrustedPepperPluginInfoCache();
38
39   // Adds all the plugins present in the cache to the |plugins| vector.
40   //
41   // If the plugins vector is empty then adds all plugins present currently
42   // in the cache to the vector.
43   //
44   // If the plugins vector is not empty then adds only those plugins that
45   // are present in the cache, but are not present in the vector.
46   //
47   // |plugins| is *non-null* pointer to vector which will be filled
48   void GetPlugins(std::vector<content::PepperPluginInfo>* plugins) const;
49
50   // Searches cached pepper plugins whether there is plugin which supports
51   // given |mime_type|.
52   //
53   // If plugin was found function returns true and sets |found_plugin|
54   // information for plugin found. Otherwise function returns false and
55   // doesn't touch |found_plugin| agrument
56   //
57   // |found_plugin| must be *non-null* pointer
58   bool FindPlugin(const std::string& mime_type,
59                   content::PepperPluginInfo* found_plugin) const;
60
61  private:
62   friend struct base::DefaultSingletonTraits<TrustedPepperPluginInfoCache>;
63
64   // Builds plugin cache based on predefined directories.
65   TrustedPepperPluginInfoCache();
66
67   // Discover plugins in given directory by scanning it recursively
68   // and adds their descriptions to the cache.
69   void AddPluginsFromDirectory(const base::FilePath& dir);
70
71   using PluginEntry =
72       std::pair<base::FilePath, std::unique_ptr<content::PepperPluginInfo>>;
73
74   // cached plugin infos
75   std::vector<PluginEntry> plugins_;
76
77   // PepperPluginInfo is non owning pointer to plugins element
78   std::unordered_map<std::string, content::PepperPluginInfo*> mime_map_;
79
80   DISALLOW_COPY_AND_ASSIGN(TrustedPepperPluginInfoCache);
81 };
82
83 // Exposed in header for UnitTesting:
84 //
85 // Parses Plugin Manifest File (PMF) provided as string |contents|
86 // and fills properly |out_info|.
87 //
88 // |pmf| is path from which PMF was loaded from and its used to fill
89 // |PepperPluginInfo::path| field if such information is not present in the PMF.
90 //
91 // This function does not performs any operation on filesystem.
92 //
93 // Sample PMFs:
94 //
95 // libsample_plugin.pmf:
96 // {
97 //   "type" : "pepper",
98 //   "name" : "Sample Trusted Plugin",
99 //   "version" : "1.0",
100 //   "description" : "Example Pepper Trusted Plugin",
101 //   "mimes" : [
102 //      {
103 //        "type" : "application/x-ppapi-example",
104 //        "description" : "Example Ppapi",
105 //        "extensions" : [ ".ext1", ".ext2" ]
106 //      },
107 //      {
108 //          "type" : "application/x-ppapi-example2"
109 //      }
110 //   ]
111 // }
112 //
113 // test_plugin.pmf:
114 // {
115 //   "type" : "pepper",
116 //   "name" : "Test Trusted Plugin",
117 //   "version" : "1.2",
118 //   "description" : "Test Trusted Pepper Plugin",
119 //   "mimes": [ { type: "application/x-ppapi-test" } ],
120 //   "program": { "url": "libtest_plugin.so" }
121 // }
122 //
123 // PMF is a JSON wich have following fields:
124 // - type (mandatory)
125 //     type of the plugin, must be set to "pepper'
126 // - name (optional)
127 //     plugin name, value for |PepperPluginInfo::name|
128 // - description (optional)
129 //     plugin description, value for |PepperPluginInfo::description|
130 // - version (optional)
131 //     plugin version, value for |PepperPluginInfo::version|
132 // - mimes (mandatory)
133 //     provides list of mime types, value for |PepperPluginInfo::mime_types|
134 // - mimes/type (mandatory)
135 //     defines mime_type itself, value for |WebPluginMimeType::mime_type|
136 // - mimes/description (optional)
137 //     defines mime_type description, value for |WebPluginMimeType::description|
138 // - mimes/extensions (optional)
139 //     defines list of the file extensions for this mime type,
140 //     value for |WebPluginMimeType::file_extensions|
141 // - program (optional)
142 //     provides path for pepper plugin shared library, value for
143 //     |PepperPluginInfo::path|. If value is not present in the manifest,
144 //     then path is deduced from |pmf|, by replacing .pmf extension form the
145 //     manifest to platform dependent library extension (.so on POSIX systems).
146 // - program/url (mandatory if program argument is present)
147 //     provides relative or absolute path to pepper plugin shared library.
148 //
149 // Following fields are not present in PMF and are set to given values:
150 // - PepperPluginInfo::is_internal is set to |false|
151 // - PepperPluginInfo::is_out_of_process is set to |true|
152 //
153 // Function returns true if manifest is parsed and follows specification
154 // given above.
155 bool ParsePepperPluginManifest(
156     const base::FilePath& pmf,
157     const std::string& contents,
158     content::PepperPluginInfo* out_info);
159
160 // Exposed in header for UnitTesting:
161 //
162 // Reads plugin manifest from |pmf| path, parses it and validates
163 // whether plugin path (|PepperPluginInfo::path|) is valid (exsists).
164 //
165 // See |ParsePepperPluginManifest| for Plugin Manifest File (pmf) specification.
166 bool FillPepperPluginInfoFromManifest(
167     const base::FilePath& pmf,
168     content::PepperPluginInfo* out_info);
169
170 }  // namespace pepper
171
172 #endif  // EWK_EFL_INTEGRATION_COMMON_TRUSTED_PEPPER_PLUGIN_INFO_CACHE_H_