| OLD | NEW | 
|    1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |    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 |    2 // Use of this source code is governed by a BSD-style license that can be | 
|    3 // found in the LICENSE file. |    3 // found in the LICENSE file. | 
|    4  |    4  | 
|    5 #ifndef CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ |    5 #ifndef CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ | 
|    6 #define CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ |    6 #define CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ | 
|    7  |    7  | 
|    8 #include <map> |    8 #include <map> | 
|    9 #include <set> |    9 #include <set> | 
|   10 #include <string> |   10 #include <string> | 
| (...skipping 19 matching lines...) Expand all  Loading... | 
|   30 namespace extensions { |   30 namespace extensions { | 
|   31  |   31  | 
|   32 class Extension; |   32 class Extension; | 
|   33 class Feature; |   33 class Feature; | 
|   34  |   34  | 
|   35 // C++ Wrapper for the JSON API definitions in chrome/common/extensions/api/. |   35 // C++ Wrapper for the JSON API definitions in chrome/common/extensions/api/. | 
|   36 // |   36 // | 
|   37 // WARNING: This class is accessed on multiple threads in the browser process |   37 // WARNING: This class is accessed on multiple threads in the browser process | 
|   38 // (see ExtensionFunctionDispatcher). No state should be modified after |   38 // (see ExtensionFunctionDispatcher). No state should be modified after | 
|   39 // construction. |   39 // construction. | 
|   40 class ExtensionAPI : public FeatureProvider { |   40 class ExtensionAPI { | 
|   41  public: |   41  public: | 
|   42   // Returns a single shared instance of this class. This is the typical use |   42   // Returns a single shared instance of this class. This is the typical use | 
|   43   // case in Chrome. |   43   // case in Chrome. | 
|   44   // |   44   // | 
|   45   // TODO(aa): Make this const to enforce thread-safe usage. |   45   // TODO(aa): Make this const to enforce thread-safe usage. | 
|   46   static ExtensionAPI* GetSharedInstance(); |   46   static ExtensionAPI* GetSharedInstance(); | 
|   47  |   47  | 
|   48   // Creates a new instance configured the way ExtensionAPI typically is in |   48   // Creates a new instance configured the way ExtensionAPI typically is in | 
|   49   // Chrome. Use the default constructor to get a clean instance. |   49   // Chrome. Use the default constructor to get a clean instance. | 
|   50   static ExtensionAPI* CreateWithDefaultConfiguration(); |   50   static ExtensionAPI* CreateWithDefaultConfiguration(); | 
| (...skipping 30 matching lines...) Expand all  Loading... | 
|   81   // extension process. They cannot be called from extension code running in |   81   // extension process. They cannot be called from extension code running in | 
|   82   // content scripts, or other low-privileged contexts. |   82   // content scripts, or other low-privileged contexts. | 
|   83   bool IsPrivileged(const std::string& name); |   83   bool IsPrivileged(const std::string& name); | 
|   84  |   84  | 
|   85   // Gets the schema for the extension API with namespace |full_name|. |   85   // Gets the schema for the extension API with namespace |full_name|. | 
|   86   // Ownership remains with this object. |   86   // Ownership remains with this object. | 
|   87   const base::DictionaryValue* GetSchema(const std::string& full_name); |   87   const base::DictionaryValue* GetSchema(const std::string& full_name); | 
|   88  |   88  | 
|   89   std::set<std::string> GetAllAPINames(); |   89   std::set<std::string> GetAllAPINames(); | 
|   90  |   90  | 
|   91   // Gets a Feature object describing the API with the specified |full_name|. |  | 
|   92   // This can be either an API namespace (like history, or |  | 
|   93   // experimental.bookmarks), or it can be an individual function or event. |  | 
|   94   virtual Feature* GetFeature(const std::string& full_name) OVERRIDE; |  | 
|   95  |  | 
|   96   // Splits a full name from the extension API into its API and child name |   91   // Splits a full name from the extension API into its API and child name | 
|   97   // parts. Some examples: |   92   // parts. Some examples: | 
|   98   // |   93   // | 
|   99   // "bookmarks.create" -> ("bookmarks", "create") |   94   // "bookmarks.create" -> ("bookmarks", "create") | 
|  100   // "experimental.input.ui.cursorUp" -> ("experimental.input.ui", "cursorUp") |   95   // "experimental.input.ui.cursorUp" -> ("experimental.input.ui", "cursorUp") | 
|  101   // "storage.sync.set" -> ("storage", "sync.get") |   96   // "storage.sync.set" -> ("storage", "sync.get") | 
|  102   // "<unknown-api>.monkey" -> ("", "") |   97   // "<unknown-api>.monkey" -> ("", "") | 
|  103   // |   98   // | 
|  104   // The |child_name| parameter can be be NULL if you don't need that part. |   99   // The |child_name| parameter can be be NULL if you don't need that part. | 
|  105   std::string GetAPINameFromFullName(const std::string& full_name, |  100   std::string GetAPINameFromFullName(const std::string& full_name, | 
|  106                                      std::string* child_name); |  101                                      std::string* child_name); | 
|  107  |  102  | 
|  108   void InitDefaultConfiguration(); |  103   void InitDefaultConfiguration(); | 
|  109  |  104  | 
 |  105   // Gets a feature from any dependency provider registered with ExtensionAPI. | 
 |  106   // Returns NULL if the feature could not be found. | 
 |  107   Feature* GetFeatureDependency(const std::string& dependency_name); | 
 |  108  | 
|  110  private: |  109  private: | 
|  111   friend struct DefaultSingletonTraits<ExtensionAPI>; |  110   friend struct DefaultSingletonTraits<ExtensionAPI>; | 
|  112  |  111  | 
|  113   // Loads a schema. |  112   // Loads a schema. | 
|  114   void LoadSchema(const std::string& name, const base::StringPiece& schema); |  113   void LoadSchema(const std::string& name, const base::StringPiece& schema); | 
|  115  |  114  | 
|  116   // Returns true if the function or event under |namespace_node| with |  115   // Returns true if the function or event under |namespace_node| with | 
|  117   // the specified |child_name| is privileged, or false otherwise. If the name |  116   // the specified |child_name| is privileged, or false otherwise. If the name | 
|  118   // is not found, defaults to privileged. |  117   // is not found, defaults to privileged. | 
|  119   bool IsChildNamePrivileged(const base::DictionaryValue* namespace_node, |  118   bool IsChildNamePrivileged(const base::DictionaryValue* namespace_node, | 
|  120                              const std::string& child_name); |  119                              const std::string& child_name); | 
|  121  |  120  | 
|  122   // NOTE: This IsAPIAllowed() and IsNonFeatureAPIAvailable only work for |  121   // NOTE: This IsAPIAllowed() and IsNonFeatureAPIAvailable only work for | 
|  123   // non-feature-controlled APIs. |  122   // non-feature-controlled APIs. | 
|  124   // TODO(aa): Remove these when all APIs are converted to the feature system. |  123   // TODO(aa): Remove these when all APIs are converted to the feature system. | 
|  125  |  124  | 
|  126   // Checks if API |name| is allowed. |  125   // Checks if API |name| is allowed. | 
|  127   bool IsAPIAllowed(const std::string& name, const Extension* extension); |  126   bool IsAPIAllowed(const std::string& name, const Extension* extension); | 
|  128  |  127  | 
|  129   // Check if an API is available to |context| given an |extension| and |url|. |  128   // Check if an API is available to |context| given an |extension| and |url|. | 
|  130   // The extension or URL may not be relevant to all contexts, and may be left |  129   // The extension or URL may not be relevant to all contexts, and may be left | 
|  131   // NULL/empty. |  130   // NULL/empty. | 
|  132   bool IsNonFeatureAPIAvailable(const std::string& name, |  131   bool IsNonFeatureAPIAvailable(const std::string& name, | 
|  133                                 Feature::Context context, |  132                                 Feature::Context context, | 
|  134                                 const Extension* extension, |  133                                 const Extension* extension, | 
|  135                                 const GURL& url); |  134                                 const GURL& url); | 
|  136  |  135  | 
|  137   // Returns true if the API uses the feature system. |  | 
|  138   bool UsesFeatureSystem(const std::string& full_name); |  | 
|  139  |  | 
|  140   // Gets a feature from any dependency provider. |  | 
|  141   Feature* GetFeatureDependency(const std::string& dependency_name); |  | 
|  142  |  | 
|  143   // Adds dependent schemas to |out| as determined by the "dependencies" |  | 
|  144   // property. |  | 
|  145   // TODO(aa): Consider making public and adding tests. |  | 
|  146   void ResolveDependencies(std::set<std::string>* out); |  | 
|  147  |  | 
|  148   // Adds any APIs listed in "dependencies" found in the schema for |api_name| |  | 
|  149   // but not in |excluding| to |out|. |  | 
|  150   void GetMissingDependencies( |  | 
|  151       const std::string& api_name, |  | 
|  152       const std::set<std::string>& excluding, |  | 
|  153       std::set<std::string>* out); |  | 
|  154  |  | 
|  155   // Checks if an API is *entirely* privileged. This won't include APIs such as |  136   // Checks if an API is *entirely* privileged. This won't include APIs such as | 
|  156   // "storage" which is entirely unprivileged, nor "extension" which has |  137   // "storage" which is entirely unprivileged, nor "extension" which has | 
|  157   // unprivileged components. |  138   // unprivileged components. | 
|  158   bool IsPrivilegedAPI(const std::string& name); |  139   bool IsPrivilegedAPI(const std::string& name); | 
|  159  |  140  | 
|  160   // Map from each API that hasn't been loaded yet to the schema which defines |  141   // Map from each API that hasn't been loaded yet to the schema which defines | 
|  161   // it. Note that there may be multiple APIs per schema. |  142   // it. Note that there may be multiple APIs per schema. | 
|  162   typedef std::map<std::string, base::StringPiece> UnloadedSchemaMap; |  143   typedef std::map<std::string, base::StringPiece> UnloadedSchemaMap; | 
|  163   UnloadedSchemaMap unloaded_schemas_; |  144   UnloadedSchemaMap unloaded_schemas_; | 
|  164  |  145  | 
| (...skipping 17 matching lines...) Expand all  Loading... | 
|  182   // FeatureProviders used for resolving dependencies. |  163   // FeatureProviders used for resolving dependencies. | 
|  183   typedef std::map<std::string, FeatureProvider*> FeatureProviderMap; |  164   typedef std::map<std::string, FeatureProvider*> FeatureProviderMap; | 
|  184   FeatureProviderMap dependency_providers_; |  165   FeatureProviderMap dependency_providers_; | 
|  185  |  166  | 
|  186   DISALLOW_COPY_AND_ASSIGN(ExtensionAPI); |  167   DISALLOW_COPY_AND_ASSIGN(ExtensionAPI); | 
|  187 }; |  168 }; | 
|  188  |  169  | 
|  189 }  // extensions |  170 }  // extensions | 
|  190  |  171  | 
|  191 #endif  // CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ |  172 #endif  // CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ | 
| OLD | NEW |