Home | History | Annotate | Download | only in extensions
      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 CHROME_BROWSER_EXTENSIONS_COMPONENT_LOADER_H_
      6 #define CHROME_BROWSER_EXTENSIONS_COMPONENT_LOADER_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/files/file_path.h"
     12 #include "base/gtest_prod_util.h"
     13 #include "base/values.h"
     14 
     15 class ExtensionServiceInterface;
     16 class PrefService;
     17 
     18 namespace extensions {
     19 
     20 class Extension;
     21 
     22 // For registering, loading, and unloading component extensions.
     23 class ComponentLoader {
     24  public:
     25   ComponentLoader(ExtensionServiceInterface* extension_service,
     26                   PrefService* prefs,
     27                   PrefService* local_state);
     28   virtual ~ComponentLoader();
     29 
     30   size_t registered_extensions_count() const {
     31     return component_extensions_.size();
     32   }
     33 
     34   // Creates and loads all registered component extensions.
     35   void LoadAll();
     36 
     37   // Clear the list of all registered extensions and unloads them from the
     38   // extension service.
     39   void RemoveAll();
     40 
     41   // Registers and possibly loads a component extension. If ExtensionService
     42   // has been initialized, the extension is loaded; otherwise, the load is
     43   // deferred until LoadAll is called. The ID of the added extension is
     44   // returned.
     45   //
     46   // Component extension manifests must contain a "key" property with a unique
     47   // public key, serialized in base64. You can create a suitable value with the
     48   // following commands on a unixy system:
     49   //
     50   //   ssh-keygen -t rsa -b 1024 -N '' -f /tmp/key.pem
     51   //   openssl rsa -pubout -outform DER < /tmp/key.pem 2>/dev/null | base64 -w 0
     52   std::string Add(const std::string& manifest_contents,
     53                   const base::FilePath& root_directory);
     54 
     55   // Convenience method for registering a component extension by resource id.
     56   std::string Add(int manifest_resource_id,
     57                   const base::FilePath& root_directory);
     58 
     59   // Loads a component extension from file system. Replaces previously added
     60   // extension with the same ID.
     61   std::string AddOrReplace(const base::FilePath& path);
     62 
     63   // Returns true if an extension with the specified id has been added.
     64   bool Exists(const std::string& id) const;
     65 
     66   // Unloads a component extension and removes it from the list of component
     67   // extensions to be loaded.
     68   void Remove(const base::FilePath& root_directory);
     69   void Remove(const std::string& id);
     70 
     71   // Call this during test setup to load component extensions that have
     72   // background pages for testing, which could otherwise interfere with tests.
     73   static void EnableBackgroundExtensionsForTesting();
     74 
     75   // Adds the default component extensions. If |skip_session_components|
     76   // the loader will skip loading component extensions that weren't supposed to
     77   // be loaded unless we are in signed user session (ChromeOS). For all other
     78   // platforms this |skip_session_components| is expected to be unset.
     79   void AddDefaultComponentExtensions(bool skip_session_components);
     80 
     81   // Parse the given JSON manifest. Returns NULL if it cannot be parsed, or if
     82   // if the result is not a DictionaryValue.
     83   DictionaryValue* ParseManifest(const std::string& manifest_contents) const;
     84 
     85   // Clear the list of registered extensions.
     86   void ClearAllRegistered();
     87 
     88   // Reloads a registered component extension.
     89   void Reload(const std::string& extension_id);
     90 
     91  private:
     92   // Information about a registered component extension.
     93   struct ComponentExtensionInfo {
     94     ComponentExtensionInfo(const DictionaryValue* manifest,
     95                            const base::FilePath& root_directory);
     96 
     97     // The parsed contents of the extensions's manifest file.
     98     const DictionaryValue* manifest;
     99 
    100     // Directory where the extension is stored.
    101     base::FilePath root_directory;
    102 
    103     // The component extension's ID.
    104     std::string extension_id;
    105   };
    106 
    107   std::string Add(const DictionaryValue* parsed_manifest,
    108                   const base::FilePath& root_directory);
    109 
    110   // Loads a registered component extension.
    111   void Load(const ComponentExtensionInfo& info);
    112 
    113   void AddDefaultComponentExtensionsWithBackgroundPages(
    114       bool skip_session_components);
    115   void AddFileManagerExtension();
    116   void AddImageLoaderExtension();
    117 
    118   void AddWithName(int manifest_resource_id,
    119                    const base::FilePath& root_directory,
    120                    const std::string& name);
    121   void AddChromeApp();
    122   void AddKeyboardApp();
    123   void AddWebStoreApp();
    124 
    125   // Unloads |component| from the memory.
    126   void UnloadComponent(ComponentExtensionInfo* component);
    127 
    128   PrefService* profile_prefs_;
    129   PrefService* local_state_;
    130 
    131   ExtensionServiceInterface* extension_service_;
    132 
    133   // List of registered component extensions (see Manifest::Location).
    134   typedef std::vector<ComponentExtensionInfo> RegisteredComponentExtensions;
    135   RegisteredComponentExtensions component_extensions_;
    136 
    137   DISALLOW_COPY_AND_ASSIGN(ComponentLoader);
    138 };
    139 
    140 }  // namespace extensions
    141 
    142 #endif  // CHROME_BROWSER_EXTENSIONS_COMPONENT_LOADER_H_
    143