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/callback_forward.h"
12 #include "base/files/file_path.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/values.h"
17 class ExtensionServiceInterface
;
24 namespace extensions
{
26 // For registering, loading, and unloading component extensions.
27 class ComponentLoader
{
29 ComponentLoader(ExtensionServiceInterface
* extension_service
,
31 PrefService
* local_state
,
32 content::BrowserContext
* browser_context
);
33 virtual ~ComponentLoader();
35 size_t registered_extensions_count() const {
36 return component_extensions_
.size();
39 // Creates and loads all registered component extensions.
42 // Registers and possibly loads a component extension. If ExtensionService
43 // has been initialized, the extension is loaded; otherwise, the load is
44 // deferred until LoadAll is called. The ID of the added extension is
47 // Component extension manifests must contain a "key" property with a unique
48 // public key, serialized in base64. You can create a suitable value with the
49 // following commands on a unixy system:
51 // ssh-keygen -t rsa -b 1024 -N '' -f /tmp/key.pem
52 // openssl rsa -pubout -outform DER < /tmp/key.pem 2>/dev/null | base64 -w 0
53 std::string
Add(const std::string
& manifest_contents
,
54 const base::FilePath
& root_directory
);
56 // Convenience method for registering a component extension by resource id.
57 std::string
Add(int manifest_resource_id
,
58 const base::FilePath
& root_directory
);
60 // Loads a component extension from file system. Replaces previously added
61 // extension with the same ID.
62 std::string
AddOrReplace(const base::FilePath
& path
);
64 // Returns the extension ID of a component extension specified by resource
65 // id of its manifest file.
66 std::string
GetExtensionID(int manifest_resource_id
,
67 const base::FilePath
& root_directory
);
69 // Returns true if an extension with the specified id has been added.
70 bool Exists(const std::string
& id
) const;
72 // Unloads a component extension and removes it from the list of component
73 // extensions to be loaded.
74 void Remove(const base::FilePath
& root_directory
);
75 void Remove(const std::string
& id
);
77 // Call this during test setup to load component extensions that have
78 // background pages for testing, which could otherwise interfere with tests.
79 static void EnableBackgroundExtensionsForTesting();
81 // Adds the default component extensions. If |skip_session_components|
82 // the loader will skip loading component extensions that weren't supposed to
83 // be loaded unless we are in signed user session (ChromeOS). For all other
84 // platforms this |skip_session_components| is expected to be unset.
85 void AddDefaultComponentExtensions(bool skip_session_components
);
87 // Similar to above but adds the default component extensions for kiosk mode.
88 void AddDefaultComponentExtensionsForKioskMode(bool skip_session_components
);
90 // Parse the given JSON manifest. Returns NULL if it cannot be parsed, or if
91 // if the result is not a DictionaryValue.
92 base::DictionaryValue
* ParseManifest(
93 const std::string
& manifest_contents
) const;
95 // Clear the list of registered extensions.
96 void ClearAllRegistered();
98 // Reloads a registered component extension.
99 void Reload(const std::string
& extension_id
);
101 #if defined(OS_CHROMEOS)
102 // Calls |done_cb|, if not a null callback, on success.
103 // NOTE: |done_cb| is not called if the component loader is shut down
105 void AddChromeVoxExtension(const base::Closure
& done_cb
);
106 std::string
AddChromeOsSpeechSynthesisExtension();
110 // Information about a registered component extension.
111 struct ComponentExtensionInfo
{
112 ComponentExtensionInfo(const base::DictionaryValue
* manifest
,
113 const base::FilePath
& root_directory
);
115 // The parsed contents of the extensions's manifest file.
116 const base::DictionaryValue
* manifest
;
118 // Directory where the extension is stored.
119 base::FilePath root_directory
;
121 // The component extension's ID.
122 std::string extension_id
;
125 std::string
Add(const base::DictionaryValue
* parsed_manifest
,
126 const base::FilePath
& root_directory
);
128 // Loads a registered component extension.
129 void Load(const ComponentExtensionInfo
& info
);
131 void AddDefaultComponentExtensionsWithBackgroundPages(
132 bool skip_session_components
);
133 void AddFileManagerExtension();
134 void AddVideoPlayerExtension();
135 void AddAudioPlayerExtension();
136 void AddGalleryExtension();
137 void AddHangoutServicesExtension();
138 void AddHotwordHelperExtension();
139 void AddImageLoaderExtension();
140 void AddNetworkSpeechSynthesisExtension();
142 void AddWithNameAndDescription(int manifest_resource_id
,
143 const base::FilePath
& root_directory
,
145 int description_string_id
);
147 void AddHotwordAudioVerificationApp();
148 void AddKeyboardApp();
149 void AddWebStoreApp();
151 // Unloads |component| from the memory.
152 void UnloadComponent(ComponentExtensionInfo
* component
);
154 // Enable HTML5 FileSystem for given component extension in Guest mode.
155 void EnableFileSystemInGuestMode(const std::string
& id
);
157 #if defined(OS_CHROMEOS)
158 // Used as a reply callback when loading the ChromeVox extension.
159 // Called with a |chromevox_path| and parsed |manifest| and invokes
160 // |done_cb| after adding the extension.
161 void AddChromeVoxExtensionWithManifest(
162 const base::FilePath
& chromevox_path
,
163 const base::Closure
& done_cb
,
164 scoped_ptr
<base::DictionaryValue
> manifest
);
167 PrefService
* profile_prefs_
;
168 PrefService
* local_state_
;
169 content::BrowserContext
* browser_context_
;
171 ExtensionServiceInterface
* extension_service_
;
173 // List of registered component extensions (see Manifest::Location).
174 typedef std::vector
<ComponentExtensionInfo
> RegisteredComponentExtensions
;
175 RegisteredComponentExtensions component_extensions_
;
177 base::WeakPtrFactory
<ComponentLoader
> weak_factory_
;
179 FRIEND_TEST_ALL_PREFIXES(TtsApiTest
, NetworkSpeechEngine
);
180 FRIEND_TEST_ALL_PREFIXES(TtsApiTest
, NoNetworkSpeechEngineWhenOffline
);
182 DISALLOW_COPY_AND_ASSIGN(ComponentLoader
);
185 } // namespace extensions
187 #endif // CHROME_BROWSER_EXTENSIONS_COMPONENT_LOADER_H_