| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/component_loader.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/path_service.h" |
| 9 #include "base/json/json_value_serializer.h" |
| 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/extensions/extension_service.h" |
| 12 #include "chrome/common/chrome_paths.h" |
| 13 #include "chrome/common/chrome_switches.h" |
| 14 #include "chrome/common/extensions/extension.h" |
| 15 #include "chrome/common/pref_names.h" |
| 16 #include "grit/browser_resources.h" |
| 17 #include "ui/base/resource/resource_bundle.h" |
| 18 |
| 19 namespace { |
| 20 |
| 21 typedef std::list<std::pair<FilePath::StringType, int> > |
| 22 ComponentExtensionList; |
| 23 |
| 24 #if defined(FILE_MANAGER_EXTENSION) |
| 25 void AddFileManagerExtension(ComponentExtensionList* component_extensions) { |
| 26 #ifndef NDEBUG |
| 27 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 28 if (command_line->HasSwitch(switches::kFileManagerExtensionPath)) { |
| 29 FilePath filemgr_extension_path = |
| 30 command_line->GetSwitchValuePath(switches::kFileManagerExtensionPath); |
| 31 component_extensions->push_back(std::make_pair( |
| 32 filemgr_extension_path.value(), |
| 33 IDR_FILEMANAGER_MANIFEST)); |
| 34 return; |
| 35 } |
| 36 #endif // NDEBUG |
| 37 component_extensions->push_back(std::make_pair( |
| 38 FILE_PATH_LITERAL("file_manager"), |
| 39 IDR_FILEMANAGER_MANIFEST)); |
| 40 } |
| 41 #endif // defined(FILE_MANAGER_EXTENSION) |
| 42 |
| 43 } // namespace |
| 44 |
| 45 namespace extensions { |
| 46 |
| 47 bool ComponentLoader::ComponentExtensionInfo::Equals( |
| 48 const ComponentExtensionInfo& other) const { |
| 49 return other.manifest == manifest && other.root_directory == root_directory; |
| 50 } |
| 51 |
| 52 ComponentLoader::ComponentLoader(ExtensionService* extension_service) |
| 53 : extension_service_(extension_service) { |
| 54 } |
| 55 |
| 56 ComponentLoader::~ComponentLoader() { |
| 57 } |
| 58 |
| 59 void ComponentLoader::LoadAll() { |
| 60 for (RegisteredComponentExtensions::iterator it = |
| 61 component_extensions_.begin(); |
| 62 it != component_extensions_.end(); ++it) { |
| 63 Load(*it); |
| 64 } |
| 65 } |
| 66 |
| 67 const Extension* ComponentLoader::Add( |
| 68 const std::string& manifest, const FilePath& root_directory) { |
| 69 ComponentExtensionInfo info(manifest, root_directory); |
| 70 Register(info); |
| 71 if (extension_service_->is_ready()) |
| 72 return Load(info); |
| 73 return NULL; |
| 74 } |
| 75 |
| 76 const Extension* ComponentLoader::Load(const ComponentExtensionInfo& info) { |
| 77 JSONStringValueSerializer serializer(info.manifest); |
| 78 scoped_ptr<Value> manifest(serializer.Deserialize(NULL, NULL)); |
| 79 if (!manifest.get()) { |
| 80 LOG(ERROR) << "Failed to parse manifest for extension"; |
| 81 return NULL; |
| 82 } |
| 83 |
| 84 int flags = Extension::REQUIRE_KEY; |
| 85 if (Extension::ShouldDoStrictErrorChecking(Extension::COMPONENT)) |
| 86 flags |= Extension::STRICT_ERROR_CHECKS; |
| 87 std::string error; |
| 88 scoped_refptr<const Extension> extension(Extension::Create( |
| 89 info.root_directory, |
| 90 Extension::COMPONENT, |
| 91 *static_cast<DictionaryValue*>(manifest.get()), |
| 92 flags, |
| 93 &error)); |
| 94 if (!extension.get()) { |
| 95 LOG(ERROR) << error; |
| 96 return NULL; |
| 97 } |
| 98 extension_service_->AddExtension(extension); |
| 99 return extension; |
| 100 } |
| 101 |
| 102 void ComponentLoader::Remove(const std::string& manifest_str) { |
| 103 // Unload the extension. |
| 104 JSONStringValueSerializer serializer(manifest_str); |
| 105 scoped_ptr<Value> manifest(serializer.Deserialize(NULL, NULL)); |
| 106 if (!manifest.get()) { |
| 107 LOG(ERROR) << "Failed to parse manifest for extension"; |
| 108 return; |
| 109 } |
| 110 std::string public_key; |
| 111 std::string public_key_bytes; |
| 112 std::string id; |
| 113 if (!static_cast<DictionaryValue*>(manifest.get())-> |
| 114 GetString(extension_manifest_keys::kPublicKey, &public_key) || |
| 115 !Extension::ParsePEMKeyBytes(public_key, &public_key_bytes) || |
| 116 !Extension::GenerateId(public_key_bytes, &id)) { |
| 117 LOG(ERROR) << "Failed to get extension id"; |
| 118 return; |
| 119 } |
| 120 extension_service_-> |
| 121 UnloadExtension(id, extension_misc::UNLOAD_REASON_DISABLE); |
| 122 |
| 123 // Unregister the extension. |
| 124 RegisteredComponentExtensions new_component_extensions; |
| 125 for (RegisteredComponentExtensions::iterator it = |
| 126 component_extensions_.begin(); |
| 127 it != component_extensions_.end(); ++it) { |
| 128 if (it->manifest != manifest_str) |
| 129 new_component_extensions.push_back(*it); |
| 130 } |
| 131 component_extensions_.swap(new_component_extensions); |
| 132 } |
| 133 |
| 134 // We take ComponentExtensionList: |
| 135 // path, manifest ID => full manifest, absolute path |
| 136 void ComponentLoader::AddDefaultComponentExtensions() { |
| 137 ComponentExtensionList component_extensions; |
| 138 |
| 139 // Bookmark manager. |
| 140 component_extensions.push_back(std::make_pair( |
| 141 FILE_PATH_LITERAL("bookmark_manager"), |
| 142 IDR_BOOKMARKS_MANIFEST)); |
| 143 |
| 144 #if defined(FILE_MANAGER_EXTENSION) |
| 145 AddFileManagerExtension(&component_extensions); |
| 146 #endif |
| 147 |
| 148 #if defined(TOUCH_UI) |
| 149 component_extensions.push_back(std::make_pair( |
| 150 FILE_PATH_LITERAL("keyboard"), |
| 151 IDR_KEYBOARD_MANIFEST)); |
| 152 #endif |
| 153 |
| 154 #if defined(OS_CHROMEOS) |
| 155 component_extensions.push_back(std::make_pair( |
| 156 FILE_PATH_LITERAL("/usr/share/chromeos-assets/mobile"), |
| 157 IDR_MOBILE_MANIFEST)); |
| 158 |
| 159 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 160 if (command_line->HasSwitch(switches::kAuthExtensionPath)) { |
| 161 FilePath auth_extension_path = |
| 162 command_line->GetSwitchValuePath(switches::kAuthExtensionPath); |
| 163 component_extensions.push_back(std::make_pair( |
| 164 auth_extension_path.value(), |
| 165 IDR_GAIA_TEST_AUTH_MANIFEST)); |
| 166 } else { |
| 167 component_extensions.push_back(std::make_pair( |
| 168 FILE_PATH_LITERAL("/usr/share/chromeos-assets/gaia_auth"), |
| 169 IDR_GAIA_AUTH_MANIFEST)); |
| 170 } |
| 171 |
| 172 #if defined(OFFICIAL_BUILD) |
| 173 if (browser_defaults::enable_help_app) { |
| 174 component_extensions.push_back(std::make_pair( |
| 175 FILE_PATH_LITERAL("/usr/share/chromeos-assets/helpapp"), |
| 176 IDR_HELP_MANIFEST)); |
| 177 } |
| 178 #endif |
| 179 #endif |
| 180 |
| 181 // Web Store. |
| 182 component_extensions.push_back(std::make_pair( |
| 183 FILE_PATH_LITERAL("web_store"), |
| 184 IDR_WEBSTORE_MANIFEST)); |
| 185 |
| 186 #if !defined(OS_CHROMEOS) |
| 187 // Cloud Print component app. Not required on Chrome OS. |
| 188 component_extensions.push_back(std::make_pair( |
| 189 FILE_PATH_LITERAL("cloud_print"), |
| 190 IDR_CLOUDPRINT_MANIFEST)); |
| 191 #endif // !defined(OS_CHROMEOS) |
| 192 |
| 193 for (ComponentExtensionList::iterator iter = component_extensions.begin(); |
| 194 iter != component_extensions.end(); ++iter) { |
| 195 FilePath path(iter->first); |
| 196 if (!path.IsAbsolute()) { |
| 197 if (PathService::Get(chrome::DIR_RESOURCES, &path)) { |
| 198 path = path.Append(iter->first); |
| 199 } else { |
| 200 NOTREACHED(); |
| 201 } |
| 202 } |
| 203 |
| 204 std::string manifest = |
| 205 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 206 iter->second).as_string(); |
| 207 Add(manifest, path); |
| 208 } |
| 209 |
| 210 #if defined(OS_CHROMEOS) |
| 211 // Register access extensions only if accessibility is enabled. |
| 212 if (g_browser_process->local_state()-> |
| 213 GetBoolean(prefs::kAccessibilityEnabled)) { |
| 214 FilePath path = FilePath(extension_misc::kAccessExtensionPath) |
| 215 .AppendASCII(extension_misc::kChromeVoxDirectoryName); |
| 216 std::string manifest = |
| 217 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 218 IDR_CHROMEVOX_MANIFEST).as_string(); |
| 219 Add(manifest, path); |
| 220 } |
| 221 #endif |
| 222 } |
| 223 |
| 224 } // namespace extensions |
| OLD | NEW |