OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_COMPONENT_LOADER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_COMPONENT_LOADER_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_COMPONENT_LOADER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_COMPONENT_LOADER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
12 #include "base/values.h" | |
13 #include "chrome/browser/prefs/pref_change_registrar.h" | |
14 #include "content/public/browser/notification_observer.h" | |
15 #include "testing/gtest/include/gtest/gtest_prod.h" | |
12 | 16 |
13 class Extension; | 17 class Extension; |
14 class ExtensionService; | 18 class ExtensionServiceInterface; |
19 class PrefService; | |
15 | 20 |
16 namespace extensions { | 21 namespace extensions { |
17 | 22 |
18 // For registering, loading, and unloading component extensions. | 23 // For registering, loading, and unloading component extensions. |
19 class ComponentLoader { | 24 class ComponentLoader : public content::NotificationObserver { |
20 public: | 25 public: |
21 explicit ComponentLoader(ExtensionService* extension_service); | 26 ComponentLoader(ExtensionServiceInterface* extension_service, |
27 PrefService *prefs); | |
22 virtual ~ComponentLoader(); | 28 virtual ~ComponentLoader(); |
23 | 29 |
24 // Loads any registered component extensions. | 30 // Loads any registered component extensions. |
25 void LoadAll(); | 31 void LoadAll(); |
26 | 32 |
27 // Loads and registers a component extension. If ExtensionService has been | 33 // Registers and possibly loads a component extension. If ExtensionService |
28 // initialized, the extension is loaded; otherwise, the load is deferred | 34 // has been initialized, the extension is loaded; otherwise, the load is |
29 // until LoadAll is called. | 35 // deferred until LoadAll is called. |
30 const Extension* Add(const std::string& manifest, | 36 const Extension* Add(std::string& manifest_contents, |
37 const FilePath& root_directory); | |
38 | |
39 // Convenience method for registering a component extension by resource id. | |
40 const Extension* Add(int manifest_resource_id, | |
31 const FilePath& root_directory); | 41 const FilePath& root_directory); |
32 | 42 |
33 // Unloads a component extension and removes it from the list of component | 43 // Unloads a component extension and removes it from the list of component |
34 // extensions to be loaded. | 44 // extensions to be loaded. |
35 void Remove(const std::string& manifest_str); | 45 void Remove(const FilePath& root_directory); |
36 | 46 |
37 // Adds the default component extensions. | 47 // Adds the default component extensions. |
38 // | 48 // |
39 // Component extension manifests must contain a 'key' property with a unique | 49 // Component extension manifests must contain a 'key' property with a unique |
40 // public key, serialized in base64. You can create a suitable value with the | 50 // public key, serialized in base64. You can create a suitable value with the |
41 // following commands on a unixy system: | 51 // following commands on a unixy system: |
42 // | 52 // |
43 // ssh-keygen -t rsa -b 1024 -N '' -f /tmp/key.pem | 53 // ssh-keygen -t rsa -b 1024 -N '' -f /tmp/key.pem |
44 // openssl rsa -pubout -outform DER < /tmp/key.pem 2>/dev/null | base64 -w 0 | 54 // openssl rsa -pubout -outform DER < /tmp/key.pem 2>/dev/null | base64 -w 0 |
45 void AddDefaultComponentExtensions(); | 55 void AddDefaultComponentExtensions(); |
46 | 56 |
57 // content::NotificationObserver implementation | |
58 virtual void Observe(int type, | |
59 const content::NotificationSource& source, | |
60 const content::NotificationDetails& details) OVERRIDE; | |
61 | |
62 static void RegisterUserPrefs(PrefService* prefs); | |
63 | |
47 private: | 64 private: |
65 FRIEND_TEST(ComponentLoaderTest, ParseManifest); | |
66 FRIEND_TEST(ComponentLoaderTest, EnterpriseWebStore); | |
Aaron Boodman
2011/11/15 17:34:47
Hate friend. Can you modify the public API to make
Patrick Dubroy
2011/11/15 19:19:16
Sure, if you'd prefer. What's the problem with usi
| |
67 | |
48 // Information about a registered component extension. | 68 // Information about a registered component extension. |
49 struct ComponentExtensionInfo { | 69 struct ComponentExtensionInfo { |
50 ComponentExtensionInfo(const std::string& manifest, | 70 ComponentExtensionInfo(const DictionaryValue* manifest, |
51 const FilePath& root_directory) | 71 const FilePath& root_directory) |
52 : manifest(manifest), | 72 : manifest(manifest), |
53 root_directory(root_directory) { | 73 root_directory(root_directory) { |
54 } | 74 } |
55 | 75 |
56 bool Equals(const ComponentExtensionInfo& other) const; | 76 // The parsed contents of the extensions's manifest file. |
57 | 77 const DictionaryValue* manifest; |
58 // The extension's manifest. This is required for component extensions so | |
59 // that ExtensionService doesn't need to go to disk to load them. | |
60 std::string manifest; | |
61 | 78 |
62 // Directory where the extension is stored. | 79 // Directory where the extension is stored. |
63 FilePath root_directory; | 80 FilePath root_directory; |
64 }; | 81 }; |
65 | 82 |
83 // Parse the given JSON manifest. Returns NULL if it cannot be parsed, or if | |
84 // if the result is not a DictionaryValue. | |
85 DictionaryValue* ParseManifest(const std::string& manifest_contents) const; | |
86 | |
87 const Extension* Add(const DictionaryValue* parsed_manifest, | |
88 const FilePath& root_directory); | |
89 | |
66 // Loads a registered component extension. | 90 // Loads a registered component extension. |
67 const Extension* Load(const ComponentExtensionInfo& info); | 91 const Extension* Load(const ComponentExtensionInfo& info); |
68 | 92 |
69 // Registers an extension to be loaded as a component extension. | 93 void AddFileManagerExtension(); |
70 void Register(const ComponentExtensionInfo& info) { | 94 |
71 component_extensions_.push_back(info); | 95 // Add the enterprise webstore extension, or reload it if already loaded. |
96 void AddOrReloadEnterpriseWebStore(); | |
97 | |
98 // Clear the list of registered extensions (used for testing). | |
99 void ClearAllRegisteredForTesting() { | |
100 component_extensions_.clear(); | |
72 } | 101 } |
73 | 102 |
103 PrefService* prefs_; | |
104 | |
105 ExtensionServiceInterface* extension_service_; | |
106 | |
74 // List of registered component extensions (see Extension::Location). | 107 // List of registered component extensions (see Extension::Location). |
75 typedef std::vector<ComponentExtensionInfo> RegisteredComponentExtensions; | 108 typedef std::vector<ComponentExtensionInfo> RegisteredComponentExtensions; |
76 RegisteredComponentExtensions component_extensions_; | 109 RegisteredComponentExtensions component_extensions_; |
77 | 110 |
78 ExtensionService* extension_service_; | 111 PrefChangeRegistrar pref_change_registrar_; |
79 }; | 112 }; |
80 | 113 |
81 } // namespace extensions | 114 } // namespace extensions |
82 | 115 |
83 #endif // CHROME_BROWSER_EXTENSIONS_COMPONENT_LOADER_H_ | 116 #endif // CHROME_BROWSER_EXTENSIONS_COMPONENT_LOADER_H_ |
OLD | NEW |