Index: chrome/browser/component_updater/cros_component_installer.cc |
diff --git a/chrome/browser/component_updater/cros_component_installer.cc b/chrome/browser/component_updater/cros_component_installer.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8ff464e8a533ee2c02f05f26c01413d80fa49373 |
--- /dev/null |
+++ b/chrome/browser/component_updater/cros_component_installer.cc |
@@ -0,0 +1,310 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/component_updater/cros_component_installer.h" |
+ |
+using content::BrowserThread; |
+using content::PluginService; |
+ |
+namespace component_updater { |
+ |
+#if defined(OS_CHROMEOS) |
+// CRX hash for Chrome OS. |
+// appid: iibmmgakkjjfeplohmmninjahpefocna |
+const uint8_t kSha2Hash[] = {0x88, 0x1c, 0xc6, 0x0a, 0xa9, 0x95, 0x4f, 0xbe, |
+ 0x7c, 0xcd, 0x8d, 0x90, 0x7f, 0x45, 0xe2, 0xd0, |
+ 0xe6, 0x30, 0x8c, 0x37, 0x7d, 0x4e, 0x61, 0x8b, |
+ 0xe7, 0x08, 0xe6, 0x93, 0x2d, 0x13, 0x5d, 0x8a}; |
waffles
2017/02/21 21:16:04
Delete these lines.
xiaochu
2017/02/21 21:39:40
Done.
|
+#else |
+#endif // defined(OS_CHROMEOS) |
+ |
+#if defined(OS_CHROMEOS) |
+void LogRegistrationResult(chromeos::DBusMethodCallStatus call_status, |
+ bool result) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ if (call_status != chromeos::DBUS_METHOD_CALL_SUCCESS) { |
+ LOG(ERROR) << "Call to imageloader service failed."; |
+ return; |
+ } |
+ if (!result) { |
+ LOG(ERROR) << "Component registration failed"; |
+ return; |
+ } |
+} |
+void ImageLoaderRegistration(const std::string& version, |
+ const base::FilePath& install_dir, |
+ const std::string& name) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ chromeos::ImageLoaderClient* loader = |
+ chromeos::DBusThreadManager::Get()->GetImageLoaderClient(); |
+ |
+ if (loader) { |
+ loader->RegisterComponent(name, version, install_dir.value(), |
+ base::Bind(&LogRegistrationResult)); |
+ } else { |
+ LOG(ERROR) << "Failed to get ImageLoaderClient object."; |
+ } |
+} |
+ |
+// Determine whether or not to skip registering this cros component updates. |
+bool SkipCupsRegistration(ComponentUpdateService* cus) { |
+ return false; |
+} |
+#endif // defined(OS_CHROMEOS) |
+ |
+CrOSComponentInstallerTraits::CrOSComponentInstallerTraits( |
+ std::string dir_name, |
+ std::string name, |
+ std::string sha2HashStr) |
+ : dir_name(dir_name), name(name) { |
+ if (sha2HashStr.length() != 64) |
+ return; |
+ for (unsigned int i = 0; i < sizeof(kSha2Hash_); i++) { |
+ kSha2Hash_[i] = stoul(sha2HashStr.substr(i * 2, 2), nullptr, 16); |
+ } |
+} |
+ |
+bool CrOSComponentInstallerTraits::SupportsGroupPolicyEnabledComponentUpdates() |
+ const { |
+ return true; |
+} |
+ |
+bool CrOSComponentInstallerTraits::RequiresNetworkEncryption() const { |
+ return false; |
+} |
+ |
+update_client::CrxInstaller::Result |
+CrOSComponentInstallerTraits::OnCustomInstall( |
+ const base::DictionaryValue& manifest, |
+ const base::FilePath& install_dir) { |
+#if defined(OS_CHROMEOS) |
+ std::string version; |
+ if (!manifest.GetString("version", &version)) { |
+ return ToInstallerResult(update_client::InstallError::GENERIC_ERROR); |
+ } |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ base::Bind(&ImageLoaderRegistration, version, install_dir, name)); |
+ return update_client::CrxInstaller::Result(update_client::InstallError::NONE); |
+#else |
+ return ToInstallerResult(update_client::InstallError::GENERIC_ERROR); |
+#endif // defined(OS_LINUX) |
waffles
2017/02/21 21:16:04
OS_LINUX → OS_CHROMEOS
xiaochu
2017/02/21 21:39:39
Done.
|
+} |
+ |
+void CrOSComponentInstallerTraits::ComponentReady( |
+ const base::Version& version, |
+ const base::FilePath& path, |
+ std::unique_ptr<base::DictionaryValue> manifest) {} |
+ |
+bool CrOSComponentInstallerTraits::VerifyInstallation( |
+ const base::DictionaryValue& manifest, |
+ const base::FilePath& install_dir) const { |
+ // TODO: verify installation. |
waffles
2017/02/21 21:16:03
What does verifying the installation look like?
waffles
2017/02/21 21:39:25
Maybe I should be more clear: if there isn't a con
xiaochu
2017/02/21 21:39:40
For adobe flash, it checks the content of the mani
|
+ return true; |
+} |
+ |
+base::FilePath CrOSComponentInstallerTraits::GetRelativeInstallDir() const { |
+ return base::FilePath(FILE_PATH_LITERAL(dir_name)); |
+} |
+ |
+void CrOSComponentInstallerTraits::GetHash(std::vector<uint8_t>* hash) const { |
+ hash->assign(kSha2Hash_, kSha2Hash_ + arraysize(kSha2Hash_)); |
+} |
+ |
+std::string CrOSComponentInstallerTraits::GetName() const { |
+ return name; |
+} |
+ |
+update_client::InstallerAttributes |
+CrOSComponentInstallerTraits::GetInstallerAttributes() const { |
+ return update_client::InstallerAttributes(); |
+} |
+ |
+std::vector<std::string> CrOSComponentInstallerTraits::GetMimeTypes() const { |
+ std::vector<std::string> mime_types; |
+ return mime_types; |
+} |
+ |
+void ConfigParser::XmlErrorFunc(void* context, const char* message, ...) { |
+ va_list args; |
+ va_start(args, message); |
+ std::string* error = static_cast<std::string*>(context); |
+ base::StringAppendV(error, message, args); |
+ va_end(args); |
+} |
+bool ConfigParser::TagNameEquals(const xmlNode* node, |
+ const char* expected_name) { |
+ return 0 == strcmp(expected_name, reinterpret_cast<const char*>(node->name)); |
+} |
+std::unique_ptr<std::string> ConfigParser::GetAttributePtr( |
+ xmlNode* node, |
+ const char* attribute_name) { |
+ const xmlChar* name = reinterpret_cast<const xmlChar*>(attribute_name); |
+ for (xmlAttr* attr = node->properties; attr != NULL; attr = attr->next) { |
+ if (!xmlStrcmp(attr->name, name) && attr->children && |
+ attr->children->content) { |
+ return base::MakeUnique<std::string>( |
+ reinterpret_cast<const char*>(attr->children->content)); |
+ } |
+ } |
+ return nullptr; |
+} |
+void ConfigParser::ParseError(const char* details, ...) { |
+ va_list args; |
+ va_start(args, details); |
+ |
+ if (!errors_.empty()) { |
+ errors_ += "\r\n"; |
+ } |
+ |
+ base::StringAppendV(&errors_, details, args); |
+ va_end(args); |
+} |
+ |
+bool ConfigParser::IsValidSha256(const std::string& sha2hashstr) { |
+ for (unsigned long i = 0; i < sha2hashstr.size(); i++) { |
+ char c = sha2hashstr.at(i); |
+ if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || |
+ (c >= 'A' && c <= 'F'))) { |
+ return false; |
+ } |
+ } |
+ return true; |
+} |
+ |
+bool ConfigParser::ParseComponentTag(xmlNode* component, |
+ Result* result, |
+ std::string* error) { |
+ auto name = GetAttributePtr(component, "name"); |
+ auto dir_name = GetAttributePtr(component, "dir_name"); |
+ auto sha2hashstr = GetAttributePtr(component, "sha2hashstr"); |
+ // empty attibute is not allowed. |
+ if (name == nullptr || dir_name == nullptr || sha2hashstr == nullptr || |
+ dir_name->size() == 0 || name->size() == 0 || sha2hashstr->size() == 0) |
+ return false; |
+ // invalid sha2hashstr is not allowed. |
+ if (sha2hashstr->size() != 64) { |
+ return false; |
+ } |
+ if (!IsValidSha256(*sha2hashstr)) { |
+ return false; |
+ } |
+ result->name = *name; |
+ result->dir_name = *dir_name; |
+ result->sha2hashstr = *sha2hashstr; |
+ return true; |
+} |
+ |
+std::vector<xmlNode*> ConfigParser::GetChildren(xmlNode* root, |
+ const char* name) { |
+ std::vector<xmlNode*> result; |
+ for (xmlNode* child = root->children; child != NULL; child = child->next) { |
+ if (!TagNameEquals(child, name)) { |
+ continue; |
+ } |
+ result.push_back(child); |
+ } |
+ return result; |
+} |
+ |
+bool ConfigParser::Parse(std::string config_xml, std::vector<Result>& results) { |
+ if (config_xml.length() < 1) { |
+ ParseError("Empty config_xml"); |
+ return false; |
+ } |
+ |
+ std::string xml_errors; |
+ ScopedXmlErrorFunc error_func(&xml_errors, &XmlErrorFunc); |
+ |
+ ScopedXmlDocument document( |
+ xmlParseDoc(reinterpret_cast<const xmlChar*>(config_xml.c_str()))); |
+ if (!document.get()) { |
+ ParseError("%s", xml_errors.c_str()); |
+ return false; |
+ } |
+ xmlNode* root = xmlDocGetRootElement(document.get()); |
+ if (!root) { |
+ ParseError("Missing root node"); |
+ return false; |
+ } |
+ if (!TagNameEquals(root, "components")) { |
+ ParseError("Missing components tag"); |
+ return false; |
+ } |
+ std::vector<xmlNode*> components = GetChildren(root, "component"); |
+ for (size_t i = 0; i != components.size(); ++i) { |
+ Result result; |
+ std::string error; |
+ if (ParseComponentTag(components[i], &result, &error)) { |
+ results.push_back(result); |
+ } else { |
+ results.clear(); |
+ ParseError("%s", error.c_str()); |
+ return false; |
+ } |
+ } |
+ return true; |
+} |
+ |
+void RegisterCrOSComponent(ComponentUpdateService* cus, |
+ ConfigParser::Result& result) { |
+ std::unique_ptr<ComponentInstallerTraits> traits( |
+ new CrOSComponentInstallerTraits(result.dir_name, result.name, |
+ result.sha2hashstr)); |
+ // |cus| will take ownership of |installer| during |
+ // installer->Register(cus). |
+ DefaultComponentInstaller* installer = |
+ new DefaultComponentInstaller(std::move(traits)); |
+ installer->Register(cus, base::Closure()); |
+} |
+ |
+bool RegisterCrOSComponent(ComponentUpdateService* cus, std::string name) { |
+#if defined(OS_CHROMEOS) |
+ base::FilePath configFilePath("/var/lib/imageloader/cros_dc.config"); |
+#else |
+ base::FilePath configFilePath("/tmp/cros_dc.config"); |
+#endif // OS_CHROMEOS |
+ std::string contents; |
+ if (base::ReadFileToString(configFilePath, &contents)) { |
+ VLOG(1) << "[RegisterCrOSComponents] configuration loaded:" << contents; |
+ ConfigParser parser; |
+ std::vector<ConfigParser::Result> results; |
+ if (parser.Parse(contents, results)) { |
+ VLOG(1) << "[RegisterCrOSComponents] xml parse succeeded"; |
+ for (auto result : results) { |
+ if (name.compare(result.name)) { |
+ RegisterCrOSComponent(cus, result); |
+ return true; |
+ } |
+ } |
+ } else { |
+ VLOG(0) << "[RegisterCrOSComponents] xml parse failed"; |
+ } |
+ } else { |
+ VLOG(0) << "[RegisterCrOSComponents] configuration not exist."; |
+ } |
+ return false; |
+} |
+ |
+bool InstallCrOSComponent(std::string name) { |
+ const auto cus = g_browser_process->component_updater(); |
+ if (RegisterCrOSComponent(cus, name)) { |
+ // component registered successfully. |
+ // you can wait until component_updater automatically install later; |
+ // or you can trigger an on-demand install right-away. |
+ // notes: non-block install is suggested; since it gives flexibility to API |
+ // callers. API callers can schedule other activities while polling isReady |
+ // to learn install progress. |
+ // e.g: cus->GetOnDemandUpdater().OnDemandUpdater(""); |
+ return true; |
+ } else { |
+ return false; |
+ } |
+} |
+ |
+bool IsReadyCrOSComponent(std::string name) { |
+ return false; |
+} |
+ |
+} // namespace component_updater |