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
;
22 namespace extensions
{
24 // For registering, loading, and unloading component extensions.
25 class ComponentLoader
{
27 ComponentLoader(ExtensionServiceInterface
* extension_service
,
29 PrefService
* local_state
,
30 content::BrowserContext
* browser_context
);
31 virtual ~ComponentLoader();
33 size_t registered_extensions_count() const {
34 return component_extensions_
.size();
37 // Creates and loads all registered component extensions.
40 // Registers and possibly loads a component extension. If ExtensionService
41 // has been initialized, the extension is loaded; otherwise, the load is
42 // deferred until LoadAll is called. The ID of the added extension is
45 // Component extension manifests must contain a "key" property with a unique
46 // public key, serialized in base64. You can create a suitable value with the
47 // following commands on a unixy system:
49 // ssh-keygen -t rsa -b 1024 -N '' -f /tmp/key.pem
50 // openssl rsa -pubout -outform DER < /tmp/key.pem 2>/dev/null | base64 -w 0
51 std::string
Add(const std::string
& manifest_contents
,
52 const base::FilePath
& root_directory
);
54 // Convenience method for registering a component extension by resource id.
55 std::string
Add(int manifest_resource_id
,
56 const base::FilePath
& root_directory
);
58 // Loads a component extension from file system. Replaces previously added
59 // extension with the same ID.
60 std::string
AddOrReplace(const base::FilePath
& path
);
62 // Returns the extension ID of a component extension specified by resource
63 // id of its manifest file.
64 std::string
GetExtensionID(int manifest_resource_id
,
65 const base::FilePath
& root_directory
);
67 // Returns true if an extension with the specified id has been added.
68 bool Exists(const std::string
& id
) const;
70 // Unloads a component extension and removes it from the list of component
71 // extensions to be loaded.
72 void Remove(const base::FilePath
& root_directory
);
73 void Remove(const std::string
& id
);
75 // Call this during test setup to load component extensions that have
76 // background pages for testing, which could otherwise interfere with tests.
77 static void EnableBackgroundExtensionsForTesting();
79 // Adds the default component extensions. If |skip_session_components|
80 // the loader will skip loading component extensions that weren't supposed to
81 // be loaded unless we are in signed user session (ChromeOS). For all other
82 // platforms this |skip_session_components| is expected to be unset.
83 void AddDefaultComponentExtensions(bool skip_session_components
);
85 // Similar to above but adds the default component extensions for kiosk mode.
86 void AddDefaultComponentExtensionsForKioskMode(bool skip_session_components
);
88 // Parse the given JSON manifest. Returns NULL if it cannot be parsed, or if
89 // if the result is not a DictionaryValue.
90 base::DictionaryValue
* ParseManifest(
91 const std::string
& manifest_contents
) const;
93 // Clear the list of registered extensions.
94 void ClearAllRegistered();
96 // Reloads a registered component extension.
97 void Reload(const std::string
& extension_id
);
100 // Information about a registered component extension.
101 struct ComponentExtensionInfo
{
102 ComponentExtensionInfo(const base::DictionaryValue
* manifest
,
103 const base::FilePath
& root_directory
);
105 // The parsed contents of the extensions's manifest file.
106 const base::DictionaryValue
* manifest
;
108 // Directory where the extension is stored.
109 base::FilePath root_directory
;
111 // The component extension's ID.
112 std::string extension_id
;
115 std::string
Add(const base::DictionaryValue
* parsed_manifest
,
116 const base::FilePath
& root_directory
);
118 // Loads a registered component extension.
119 void Load(const ComponentExtensionInfo
& info
);
121 void AddDefaultComponentExtensionsWithBackgroundPages(
122 bool skip_session_components
);
123 void AddFileManagerExtension();
124 void AddHangoutServicesExtension();
125 void AddImageLoaderExtension();
126 void AddBookmarksExtensions();
127 void AddNetworkSpeechSynthesisExtension();
128 #if defined(OS_CHROMEOS)
129 void AddChromeOsSpeechSynthesisExtension();
132 void AddWithName(int manifest_resource_id
,
133 const base::FilePath
& root_directory
,
134 const std::string
& name
);
136 void AddKeyboardApp();
137 void AddWebStoreApp();
139 // Unloads |component| from the memory.
140 void UnloadComponent(ComponentExtensionInfo
* component
);
142 PrefService
* profile_prefs_
;
143 PrefService
* local_state_
;
144 content::BrowserContext
* browser_context_
;
146 ExtensionServiceInterface
* extension_service_
;
148 // List of registered component extensions (see Manifest::Location).
149 typedef std::vector
<ComponentExtensionInfo
> RegisteredComponentExtensions
;
150 RegisteredComponentExtensions component_extensions_
;
152 FRIEND_TEST_ALL_PREFIXES(TtsApiTest
, NetworkSpeechEngine
);
153 FRIEND_TEST_ALL_PREFIXES(TtsApiTest
, NoNetworkSpeechEngineWhenOffline
);
155 DISALLOW_COPY_AND_ASSIGN(ComponentLoader
);
158 } // namespace extensions
160 #endif // CHROME_BROWSER_EXTENSIONS_COMPONENT_LOADER_H_