Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / component_loader.h
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.
4
5 #ifndef CHROME_BROWSER_EXTENSIONS_COMPONENT_LOADER_H_
6 #define CHROME_BROWSER_EXTENSIONS_COMPONENT_LOADER_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "base/files/file_path.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/values.h"
14
15 class ExtensionServiceInterface;
16 class PrefService;
17
18 namespace content {
19 class BrowserContext;
20 }
21
22 namespace extensions {
23
24 // For registering, loading, and unloading component extensions.
25 class ComponentLoader {
26  public:
27   ComponentLoader(ExtensionServiceInterface* extension_service,
28                   PrefService* prefs,
29                   PrefService* local_state,
30                   content::BrowserContext* browser_context);
31   virtual ~ComponentLoader();
32
33   size_t registered_extensions_count() const {
34     return component_extensions_.size();
35   }
36
37   // Creates and loads all registered component extensions.
38   void LoadAll();
39
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
43   // returned.
44   //
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:
48   //
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);
53
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);
57
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);
61
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);
66
67   // Returns true if an extension with the specified id has been added.
68   bool Exists(const std::string& id) const;
69
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);
74
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();
78
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);
84
85   // Similar to above but adds the default component extensions for kiosk mode.
86   void AddDefaultComponentExtensionsForKioskMode(bool skip_session_components);
87
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;
92
93   // Clear the list of registered extensions.
94   void ClearAllRegistered();
95
96   // Reloads a registered component extension.
97   void Reload(const std::string& extension_id);
98
99 #if defined(OS_CHROMEOS)
100   std::string AddChromeVoxExtension();
101   std::string AddChromeOsSpeechSynthesisExtension();
102 #endif
103
104  private:
105   // Information about a registered component extension.
106   struct ComponentExtensionInfo {
107     ComponentExtensionInfo(const base::DictionaryValue* manifest,
108                            const base::FilePath& root_directory);
109
110     // The parsed contents of the extensions's manifest file.
111     const base::DictionaryValue* manifest;
112
113     // Directory where the extension is stored.
114     base::FilePath root_directory;
115
116     // The component extension's ID.
117     std::string extension_id;
118   };
119
120   std::string Add(const base::DictionaryValue* parsed_manifest,
121                   const base::FilePath& root_directory);
122
123   // Loads a registered component extension.
124   void Load(const ComponentExtensionInfo& info);
125
126   void AddDefaultComponentExtensionsWithBackgroundPages(
127       bool skip_session_components);
128   void AddFileManagerExtension();
129   void AddHangoutServicesExtension();
130   void AddHotwordHelperExtension();
131   void AddImageLoaderExtension();
132   void AddBookmarksExtensions();
133   void AddNetworkSpeechSynthesisExtension();
134
135   void AddWithName(int manifest_resource_id,
136                    const base::FilePath& root_directory,
137                    const std::string& name);
138   void AddChromeApp();
139   void AddKeyboardApp();
140   void AddWebStoreApp();
141
142   // Unloads |component| from the memory.
143   void UnloadComponent(ComponentExtensionInfo* component);
144
145   // Enable HTML5 FileSystem for given component extension in Guest mode.
146   void EnableFileSystemInGuestMode(const std::string& id);
147
148   PrefService* profile_prefs_;
149   PrefService* local_state_;
150   content::BrowserContext* browser_context_;
151
152   ExtensionServiceInterface* extension_service_;
153
154   // List of registered component extensions (see Manifest::Location).
155   typedef std::vector<ComponentExtensionInfo> RegisteredComponentExtensions;
156   RegisteredComponentExtensions component_extensions_;
157
158   FRIEND_TEST_ALL_PREFIXES(TtsApiTest, NetworkSpeechEngine);
159   FRIEND_TEST_ALL_PREFIXES(TtsApiTest, NoNetworkSpeechEngineWhenOffline);
160
161   DISALLOW_COPY_AND_ASSIGN(ComponentLoader);
162 };
163
164 }  // namespace extensions
165
166 #endif  // CHROME_BROWSER_EXTENSIONS_COMPONENT_LOADER_H_