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