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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_COMPONENT_LOADER_H_
6 #define CHROME_BROWSER_EXTENSIONS_COMPONENT_LOADER_H_
11 #include "base/files/file_path.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/values.h"
15 class ExtensionServiceInterface
;
18 namespace extensions
{
20 // For registering, loading, and unloading component extensions.
21 class ComponentLoader
{
23 ComponentLoader(ExtensionServiceInterface
* extension_service
,
25 PrefService
* local_state
);
26 virtual ~ComponentLoader();
28 size_t registered_extensions_count() const {
29 return component_extensions_
.size();
32 // Creates and loads all registered component extensions.
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
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:
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
);
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
);
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
);
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
);
62 // Returns true if an extension with the specified id has been added.
63 bool Exists(const std::string
& id
) const;
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
);
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();
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
);
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;
84 // Clear the list of registered extensions.
85 void ClearAllRegistered();
87 // Reloads a registered component extension.
88 void Reload(const std::string
& extension_id
);
91 // Information about a registered component extension.
92 struct ComponentExtensionInfo
{
93 ComponentExtensionInfo(const DictionaryValue
* manifest
,
94 const base::FilePath
& root_directory
);
96 // The parsed contents of the extensions's manifest file.
97 const DictionaryValue
* manifest
;
99 // Directory where the extension is stored.
100 base::FilePath root_directory
;
102 // The component extension's ID.
103 std::string extension_id
;
106 std::string
Add(const DictionaryValue
* parsed_manifest
,
107 const base::FilePath
& root_directory
);
109 // Loads a registered component extension.
110 void Load(const ComponentExtensionInfo
& info
);
112 void AddDefaultComponentExtensionsWithBackgroundPages(
113 bool skip_session_components
);
114 void AddFileManagerExtension();
115 void AddImageLoaderExtension();
117 void AddWithName(int manifest_resource_id
,
118 const base::FilePath
& root_directory
,
119 const std::string
& name
);
121 void AddKeyboardApp();
122 void AddWebStoreApp();
124 // Unloads |component| from the memory.
125 void UnloadComponent(ComponentExtensionInfo
* component
);
127 PrefService
* profile_prefs_
;
128 PrefService
* local_state_
;
130 ExtensionServiceInterface
* extension_service_
;
132 // List of registered component extensions (see Manifest::Location).
133 typedef std::vector
<ComponentExtensionInfo
> RegisteredComponentExtensions
;
134 RegisteredComponentExtensions component_extensions_
;
136 DISALLOW_COPY_AND_ASSIGN(ComponentLoader
);
139 } // namespace extensions
141 #endif // CHROME_BROWSER_EXTENSIONS_COMPONENT_LOADER_H_