component_loader.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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/file_path.h" 12#include "base/gtest_prod_util.h" 13#include "base/prefs/public/pref_change_registrar.h" 14#include "base/prefs/public/pref_observer.h" 15#include "base/values.h" 16 17class ExtensionServiceInterface; 18class PrefService; 19 20namespace extensions { 21 22class Extension; 23 24// For registering, loading, and unloading component extensions. 25class ComponentLoader : public PrefObserver { 26 public: 27 ComponentLoader(ExtensionServiceInterface* extension_service, 28 PrefService* prefs, 29 PrefService* local_state); 30 virtual ~ComponentLoader(); 31 32 size_t registered_extensions_count() const { 33 return component_extensions_.size(); 34 } 35 36 // Loads any registered component extensions. 37 void LoadAll(); 38 39 // Registers and possibly loads a component extension. If ExtensionService 40 // has been initialized, the extension is loaded; otherwise, the load is 41 // deferred until LoadAll is called. The ID of the added extension is 42 // returned. 43 // 44 // Component extension manifests must contain a "key" property with a unique 45 // public key, serialized in base64. You can create a suitable value with the 46 // following commands on a unixy system: 47 // 48 // ssh-keygen -t rsa -b 1024 -N '' -f /tmp/key.pem 49 // openssl rsa -pubout -outform DER < /tmp/key.pem 2>/dev/null | base64 -w 0 50 std::string Add(const std::string& manifest_contents, 51 const FilePath& root_directory); 52 53 // Convenience method for registering a component extension by resource id. 54 std::string Add(int manifest_resource_id, 55 const FilePath& root_directory); 56 57 // Loads a component extension from file system. Replaces previously added 58 // extension with the same ID. 59 std::string AddOrReplace(const FilePath& path); 60 61 // Returns true if an extension with the specified id has been added. 62 bool Exists(const std::string& id) const; 63 64 // Unloads a component extension and removes it from the list of component 65 // extensions to be loaded. 66 void Remove(const FilePath& root_directory); 67 void Remove(const std::string& id); 68 69 // Adds the default component extensions. 70 void AddDefaultComponentExtensions(); 71 72 // PrefObserver implementation 73 virtual void OnPreferenceChanged(PrefServiceBase* service, 74 const std::string& pref_name) OVERRIDE; 75 76 static void RegisterUserPrefs(PrefService* prefs); 77 78 // Parse the given JSON manifest. Returns NULL if it cannot be parsed, or if 79 // if the result is not a DictionaryValue. 80 DictionaryValue* ParseManifest(const std::string& manifest_contents) const; 81 82 // Clear the list of registered extensions. 83 void ClearAllRegistered(); 84 85 // Reloads a registered component extension. 86 void Reload(const std::string& extension_id); 87 88 // Adds the "Script Bubble" component extension, which puts an icon in the 89 // omnibox indiciating the number of extensions running script in a tab. 90 void AddScriptBubble(); 91 92 // Returns the extension previously added by AddScriptBubble(), if any. 93 const Extension* GetScriptBubble() const; 94 95 private: 96 // Information about a registered component extension. 97 struct ComponentExtensionInfo { 98 ComponentExtensionInfo(const DictionaryValue* manifest, 99 const FilePath& root_directory); 100 101 // The parsed contents of the extensions's manifest file. 102 const DictionaryValue* manifest; 103 104 // Directory where the extension is stored. 105 FilePath root_directory; 106 107 // The component extension's ID. 108 std::string extension_id; 109 }; 110 111 std::string Add(const DictionaryValue* parsed_manifest, 112 const FilePath& root_directory); 113 114 // Loads a registered component extension. 115 const Extension* Load(const ComponentExtensionInfo& info); 116 117 void AddFileManagerExtension(); 118 119#if defined(OS_CHROMEOS) 120 void AddGaiaAuthExtension(); 121#endif 122 123 // Add the enterprise webstore extension, or reload it if already loaded. 124 void AddOrReloadEnterpriseWebStore(); 125 126 void AddChromeApp(); 127 128 PrefService* prefs_; 129 PrefService* local_state_; 130 131 ExtensionServiceInterface* extension_service_; 132 133 // List of registered component extensions (see Extension::Location). 134 typedef std::vector<ComponentExtensionInfo> RegisteredComponentExtensions; 135 RegisteredComponentExtensions component_extensions_; 136 137 PrefChangeRegistrar pref_change_registrar_; 138 139 std::string script_bubble_id_; 140 141 DISALLOW_COPY_AND_ASSIGN(ComponentLoader); 142}; 143 144} // namespace extensions 145 146#endif // CHROME_BROWSER_EXTENSIONS_COMPONENT_LOADER_H_ 147