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/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/values.h"
17 class ExtensionServiceInterface
;
21 namespace extensions
{
25 // For registering, loading, and unloading component extensions.
26 class ComponentLoader
{
28 ComponentLoader(ExtensionServiceInterface
* extension_service
,
30 PrefService
* local_state
,
31 Profile
* browser_context
);
32 virtual ~ComponentLoader();
34 size_t registered_extensions_count() const {
35 return component_extensions_
.size();
38 // Creates and loads all registered component extensions.
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
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:
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
);
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
);
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
);
63 // Returns the extension ID of a component extension specified by resource
64 // id of its manifest file.
65 std::string
GetExtensionID(int manifest_resource_id
,
66 const base::FilePath
& root_directory
);
68 // Returns true if an extension with the specified id has been added.
69 bool Exists(const std::string
& id
) const;
71 // Unloads a component extension and removes it from the list of component
72 // extensions to be loaded.
73 void Remove(const base::FilePath
& root_directory
);
74 void Remove(const std::string
& id
);
76 // Call this during test setup to load component extensions that have
77 // background pages for testing, which could otherwise interfere with tests.
78 static void EnableBackgroundExtensionsForTesting();
80 // Adds the default component extensions. If |skip_session_components|
81 // the loader will skip loading component extensions that weren't supposed to
82 // be loaded unless we are in signed user session (ChromeOS). For all other
83 // platforms this |skip_session_components| is expected to be unset.
84 void AddDefaultComponentExtensions(bool skip_session_components
);
86 // Similar to above but adds the default component extensions for kiosk mode.
87 void AddDefaultComponentExtensionsForKioskMode(bool skip_session_components
);
89 // Parse the given JSON manifest. Returns NULL if it cannot be parsed, or if
90 // if the result is not a DictionaryValue.
91 base::DictionaryValue
* ParseManifest(
92 const std::string
& manifest_contents
) const;
94 // Clear the list of registered extensions.
95 void ClearAllRegistered();
97 // Reloads a registered component extension.
98 void Reload(const std::string
& extension_id
);
100 #if defined(OS_CHROMEOS)
101 // Calls |done_cb|, if not a null callback, on success.
102 // NOTE: |done_cb| is not called if the component loader is shut down
104 void AddChromeVoxExtension(const base::Closure
& done_cb
);
105 void AddChromeOsSpeechSynthesisExtension();
108 void set_ignore_whitelist_for_testing(bool value
) {
109 ignore_whitelist_for_testing_
= value
;
113 // Information about a registered component extension.
114 struct ComponentExtensionInfo
{
115 ComponentExtensionInfo(const base::DictionaryValue
* manifest
,
116 const base::FilePath
& root_directory
);
118 // The parsed contents of the extensions's manifest file.
119 const base::DictionaryValue
* manifest
;
121 // Directory where the extension is stored.
122 base::FilePath root_directory
;
124 // The component extension's ID.
125 std::string extension_id
;
128 std::string
Add(const std::string
& manifest_contents
,
129 const base::FilePath
& root_directory
,
130 bool skip_whitelist
);
131 std::string
Add(const base::DictionaryValue
* parsed_manifest
,
132 const base::FilePath
& root_directory
,
133 bool skip_whitelist
);
135 // Loads a registered component extension.
136 void Load(const ComponentExtensionInfo
& info
);
138 void AddDefaultComponentExtensionsWithBackgroundPages(
139 bool skip_session_components
);
140 void AddFileManagerExtension();
141 void AddVideoPlayerExtension();
142 void AddAudioPlayerExtension();
143 void AddGalleryExtension();
144 void AddWebstoreWidgetExtension();
145 void AddHangoutServicesExtension();
146 void AddHotwordHelperExtension();
147 void AddImageLoaderExtension();
148 void AddNetworkSpeechSynthesisExtension();
149 void AddGoogleNowExtension();
151 void AddWithNameAndDescription(int manifest_resource_id
,
152 const base::FilePath
& root_directory
,
153 const std::string
& name_string
,
154 const std::string
& description_string
);
156 void AddHotwordAudioVerificationApp();
157 void AddKeyboardApp();
158 void AddWebStoreApp();
160 scoped_refptr
<const Extension
> CreateExtension(
161 const ComponentExtensionInfo
& info
, std::string
* utf8_error
);
163 // Deletes the extension storage for an extension that has not yet been
164 // loaded. If the extension has been loaded, use ComponentLoader::Remove
166 void DeleteData(int manifest_resource_id
,
167 const base::FilePath
& root_directory
);
169 // Unloads |component| from the memory.
170 void UnloadComponent(ComponentExtensionInfo
* component
);
172 // Enable HTML5 FileSystem for given component extension in Guest mode.
173 void EnableFileSystemInGuestMode(const std::string
& id
);
175 #if defined(OS_CHROMEOS)
176 // Adds an extension where the manifest file is stored on the file system.
177 // |manifest_filename| can be relative to the |root_directory|.
178 void AddWithManifestFile(
179 const base::FilePath::CharType
* manifest_filename
,
180 const base::FilePath
& root_directory
,
181 const char* extension_id
,
182 const base::Closure
& done_cb
);
184 // Used as a reply callback by |AddWithManifestFile|.
185 // Called with a |root_directory| and parsed |manifest| and invokes
186 // |done_cb| after adding the extension.
187 void FinishAddWithManifestFile(
188 const base::FilePath
& root_directory
,
189 const char* extension_id
,
190 const base::Closure
& done_cb
,
191 scoped_ptr
<base::DictionaryValue
> manifest
);
194 PrefService
* profile_prefs_
;
195 PrefService
* local_state_
;
198 ExtensionServiceInterface
* extension_service_
;
200 // List of registered component extensions (see Manifest::Location).
201 typedef std::vector
<ComponentExtensionInfo
> RegisteredComponentExtensions
;
202 RegisteredComponentExtensions component_extensions_
;
204 bool ignore_whitelist_for_testing_
;
206 base::WeakPtrFactory
<ComponentLoader
> weak_factory_
;
208 friend class TtsApiTest
;
210 DISALLOW_COPY_AND_ASSIGN(ComponentLoader
);
213 } // namespace extensions
215 #endif // CHROME_BROWSER_EXTENSIONS_COMPONENT_LOADER_H_