Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 #include "chrome/browser/component_updater/cros_component_installer.h" | 5 #include "chrome/browser/component_updater/cros_component_installer.h" |
| 6 #include "base/task_scheduler/post_task.h" | 6 #include "base/task_scheduler/post_task.h" |
| 7 #include "chrome/browser/browser_process.h" | 7 #include "chrome/browser/browser_process.h" |
| 8 #include "chrome/browser/component_updater/component_installer_errors.h" | 8 #include "chrome/browser/component_updater/component_installer_errors.h" |
| 9 #include "components/component_updater/component_updater_paths.h" | 9 #include "components/component_updater/component_updater_paths.h" |
| 10 #include "components/crx_file/id_util.h" | 10 #include "components/crx_file/id_util.h" |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 } | 74 } |
| 75 | 75 |
| 76 bool CrOSComponentInstallerTraits::RequiresNetworkEncryption() const { | 76 bool CrOSComponentInstallerTraits::RequiresNetworkEncryption() const { |
| 77 return true; | 77 return true; |
| 78 } | 78 } |
| 79 | 79 |
| 80 update_client::CrxInstaller::Result | 80 update_client::CrxInstaller::Result |
| 81 CrOSComponentInstallerTraits::OnCustomInstall( | 81 CrOSComponentInstallerTraits::OnCustomInstall( |
| 82 const base::DictionaryValue& manifest, | 82 const base::DictionaryValue& manifest, |
| 83 const base::FilePath& install_dir) { | 83 const base::FilePath& install_dir) { |
| 84 DVLOG(1) << "[CrOSComponentInstallerTraits::OnCustomInstall]"; | |
| 85 std::string version; | 84 std::string version; |
| 86 if (!manifest.GetString("version", &version)) { | 85 if (!manifest.GetString("version", &version)) { |
| 87 return ToInstallerResult(update_client::InstallError::GENERIC_ERROR); | 86 return ToInstallerResult(update_client::InstallError::GENERIC_ERROR); |
| 88 } | 87 } |
| 89 BrowserThread::PostTask( | 88 BrowserThread::PostTask( |
| 90 BrowserThread::UI, FROM_HERE, | 89 BrowserThread::UI, FROM_HERE, |
| 91 base::Bind(&ImageLoaderRegistration, version, install_dir, name)); | 90 base::Bind(&ImageLoaderRegistration, version, install_dir, name)); |
| 92 return update_client::CrxInstaller::Result(update_client::InstallError::NONE); | 91 return update_client::CrxInstaller::Result(update_client::InstallError::NONE); |
| 93 } | 92 } |
| 94 | 93 |
| 95 void CrOSComponentInstallerTraits::ComponentReady( | 94 void CrOSComponentInstallerTraits::ComponentReady( |
| 96 const base::Version& version, | 95 const base::Version& version, |
| 97 const base::FilePath& path, | 96 const base::FilePath& path, |
| 98 std::unique_ptr<base::DictionaryValue> manifest) { | 97 std::unique_ptr<base::DictionaryValue> manifest) { |
| 99 g_browser_process->platform_part()->AddCompatibleCrOSComponent(GetName()); | 98 const ConfigMap components = CONFIG_MAP_CONTENT; |
| 99 const auto it = components.find(GetName()); | |
| 100 if (it != components.end()) { | |
| 101 std::string env_version = it->second.find("env_version")->second; | |
| 102 std::string min_env_version; | |
| 103 if (manifest && manifest->GetString("min_env_version", &min_env_version)) { | |
| 104 if (IsCompatible(env_version, min_env_version)) { | |
| 105 g_browser_process->platform_part()->AddCompatibleCrOSComponent( | |
| 106 GetName()); | |
| 107 } | |
| 108 } | |
| 109 } | |
| 100 } | 110 } |
| 101 | 111 |
| 102 bool CrOSComponentInstallerTraits::VerifyInstallation( | 112 bool CrOSComponentInstallerTraits::VerifyInstallation( |
| 103 const base::DictionaryValue& manifest, | 113 const base::DictionaryValue& manifest, |
| 104 const base::FilePath& install_dir) const { | 114 const base::FilePath& install_dir) const { |
| 105 return true; | 115 return true; |
| 106 } | 116 } |
| 107 | 117 |
| 108 base::FilePath CrOSComponentInstallerTraits::GetRelativeInstallDir() const { | 118 base::FilePath CrOSComponentInstallerTraits::GetRelativeInstallDir() const { |
| 109 return base::FilePath(name); | 119 return base::FilePath(name); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 122 update_client::InstallerAttributes attrs; | 132 update_client::InstallerAttributes attrs; |
| 123 attrs["_env_version"] = env_version; | 133 attrs["_env_version"] = env_version; |
| 124 return attrs; | 134 return attrs; |
| 125 } | 135 } |
| 126 | 136 |
| 127 std::vector<std::string> CrOSComponentInstallerTraits::GetMimeTypes() const { | 137 std::vector<std::string> CrOSComponentInstallerTraits::GetMimeTypes() const { |
| 128 std::vector<std::string> mime_types; | 138 std::vector<std::string> mime_types; |
| 129 return mime_types; | 139 return mime_types; |
| 130 } | 140 } |
| 131 | 141 |
| 142 bool IsValidEnvVersion(const std::string& env_version, | |
| 143 std::string& major, | |
| 144 std::string& minor) { | |
| 145 // 1) contains dot. | |
| 146 if (env_version.find('.') == std::string::npos) | |
| 147 return false; | |
| 148 // 2) contains only 1 dot. | |
| 149 if (env_version.find_first_of('.') != env_version.find_last_of('.')) | |
| 150 return false; | |
| 151 // 3) major and minor are all digits. | |
| 152 std::string major_ = env_version.substr(0, env_version.find('.')); | |
| 153 std::string minor_ = env_version.substr(env_version.find('.') + 1); | |
| 154 if (major_.find_first_not_of("1234567890") != std::string::npos) | |
| 155 return false; | |
| 156 if (minor_.find_first_not_of("1234567890") != std::string::npos) | |
| 157 return false; | |
| 158 major = major_; | |
| 159 minor = minor_; | |
| 160 return true; | |
| 161 } | |
|
waffles
2017/05/26 17:28:26
empty line between function definitions
xiaochu
2017/05/26 19:44:02
Done.
| |
| 162 bool CrOSComponentInstallerTraits::IsCompatible( | |
| 163 const std::string& env_version, | |
| 164 const std::string& min_env_version) { | |
| 165 // env_version format: [0-9]+.[0-9]+ | |
|
waffles
2017/05/26 17:28:26
Use base::Version, then you can get rid of IsValid
xiaochu
2017/05/26 19:44:02
Done.
| |
| 166 std::string env_version_major; | |
| 167 std::string env_version_minor; | |
| 168 std::string min_env_version_major; | |
| 169 std::string min_env_version_minor; | |
| 170 if (IsValidEnvVersion(env_version, env_version_major, env_version_minor) && | |
| 171 IsValidEnvVersion(min_env_version, min_env_version_major, | |
| 172 min_env_version_minor)) { | |
| 173 if (env_version_major == min_env_version_major) | |
|
waffles
2017/05/26 17:28:26
You also need to check that min_env_version_minor
xiaochu
2017/05/26 19:44:02
Done.
| |
| 174 return true; | |
| 175 } | |
| 176 return false; | |
| 177 } | |
| 178 | |
| 179 void CrOSComponent::InstallCrOSComponent( | |
| 180 const std::string& name, | |
| 181 const update_client::Callback& install_callback) { | |
| 182 auto* const cus = g_browser_process->component_updater(); | |
| 183 InstallCrOSComponent(cus, name, install_callback); | |
| 184 } | |
|
waffles
2017/05/26 17:28:26
Recommend you place this definition near the other
xiaochu
2017/05/26 19:44:02
Done.
I reordered all functions so caller and cal
| |
| 185 | |
| 186 void LoadResult(const base::Callback<void(const std::string&)>& load_callback, | |
| 187 chromeos::DBusMethodCallStatus call_status, | |
| 188 const std::string& result) { | |
| 189 if (call_status != chromeos::DBUS_METHOD_CALL_SUCCESS) { | |
| 190 base::PostTask(FROM_HERE, base::Bind(load_callback, "")); | |
| 191 return; | |
| 192 } | |
| 193 if (result.empty()) { | |
|
waffles
2017/05/26 17:28:26
Why is this block necessary?
xiaochu
2017/05/26 19:44:02
Done.
| |
| 194 base::PostTask(FROM_HERE, base::Bind(load_callback, "")); | |
| 195 return; | |
| 196 } | |
| 197 base::PostTask(FROM_HERE, base::Bind(load_callback, result)); | |
|
waffles
2017/05/26 17:28:26
Maybe just PostTask(FROM_HERE, base::Bind(load_cal
xiaochu
2017/05/26 19:44:02
Done.
| |
| 198 } | |
| 199 | |
| 200 void CrOSComponent::LoadCrOSComponent( | |
| 201 const std::string& name, | |
| 202 const base::Callback<void(const std::string&)>& load_callback) { | |
| 203 if (!g_browser_process->platform_part()->IsCompatibleCrOSComponent(name)) | |
| 204 return; | |
| 205 chromeos::ImageLoaderClient* loader = | |
| 206 chromeos::DBusThreadManager::Get()->GetImageLoaderClient(); | |
| 207 if (loader) { | |
| 208 loader->LoadComponent(name, base::Bind(&LoadResult, load_callback)); | |
| 209 return; | |
| 210 } | |
| 211 base::PostTask(FROM_HERE, base::Bind(load_callback, "")); | |
| 212 } | |
| 213 | |
| 132 void CrOSComponent::RegisterCrOSComponentInternal( | 214 void CrOSComponent::RegisterCrOSComponentInternal( |
| 133 ComponentUpdateService* cus, | 215 ComponentUpdateService* cus, |
| 134 const ComponentConfig& config, | 216 const ComponentConfig& config, |
| 135 const base::Closure& installcallback) { | 217 const base::Closure& installcallback) { |
| 136 std::unique_ptr<ComponentInstallerTraits> traits( | 218 std::unique_ptr<ComponentInstallerTraits> traits( |
| 137 new CrOSComponentInstallerTraits(config)); | 219 new CrOSComponentInstallerTraits(config)); |
| 138 // |cus| will take ownership of |installer| during | 220 // |cus| will take ownership of |installer| during |
| 139 // installer->Register(cus). | 221 // installer->Register(cus). |
| 140 DefaultComponentInstaller* installer = | 222 DefaultComponentInstaller* installer = |
| 141 new DefaultComponentInstaller(std::move(traits)); | 223 new DefaultComponentInstaller(std::move(traits)); |
| 142 installer->Register(cus, installcallback); | 224 installer->Register(cus, installcallback); |
| 143 } | 225 } |
| 144 | 226 |
| 145 void CrOSComponent::InstallChromeOSComponent( | 227 void CrOSComponent::InstallCrOSComponent( |
| 146 ComponentUpdateService* cus, | 228 ComponentUpdateService* cus, |
| 147 const std::string& id, | |
| 148 const update_client::Callback& install_callback) { | |
| 149 cus->GetOnDemandUpdater().OnDemandUpdate(id, install_callback); | |
| 150 } | |
| 151 | |
| 152 bool CrOSComponent::InstallCrOSComponent( | |
| 153 const std::string& name, | 229 const std::string& name, |
| 154 const update_client::Callback& install_callback) { | 230 const update_client::Callback& install_callback) { |
| 155 auto* const cus = g_browser_process->component_updater(); | 231 const ConfigMap components = CONFIG_MAP_CONTENT; |
| 156 const ConfigMap components = { | 232 const auto it = components.find(name); |
| 157 {"epson-inkjet-printer-escpr", | 233 if (name.empty() || it == components.end()) { |
| 158 {{"env_version", "0.0"}, | |
| 159 {"sha2hashstr", | |
| 160 "1913a5e0a6cad30b6f03e176177e0d7ed62c5d6700a9c66da556d7c3f5d6a47e"}}}}; | |
| 161 if (name.empty()) { | |
| 162 base::PostTask( | 234 base::PostTask( |
| 163 FROM_HERE, | 235 FROM_HERE, |
| 164 base::Bind(install_callback, update_client::Error::INVALID_ARGUMENT)); | 236 base::Bind(install_callback, update_client::Error::INVALID_ARGUMENT)); |
| 165 return false; | 237 return; |
| 166 } | |
| 167 const auto it = components.find(name); | |
| 168 if (it == components.end()) { | |
| 169 DVLOG(1) << "[RegisterCrOSComponents] component " << name | |
| 170 << " is not in configuration."; | |
| 171 base::PostTask( | |
| 172 FROM_HERE, | |
| 173 base::Bind(install_callback, update_client::Error::INVALID_ARGUMENT)); | |
| 174 return false; | |
| 175 } | 238 } |
| 176 ComponentConfig config(it->first, it->second.find("env_version")->second, | 239 ComponentConfig config(it->first, it->second.find("env_version")->second, |
| 177 it->second.find("sha2hashstr")->second); | 240 it->second.find("sha2hashstr")->second); |
| 178 RegisterCrOSComponentInternal( | 241 RegisterCrOSComponentInternal( |
| 179 cus, config, | 242 cus, config, |
| 180 base::Bind(InstallChromeOSComponent, cus, | 243 base::Bind(InstallChromeOSComponent, cus, |
| 181 crx_file::id_util::GenerateIdFromHex( | 244 crx_file::id_util::GenerateIdFromHex( |
| 182 it->second.find("sha2hashstr")->second) | 245 it->second.find("sha2hashstr")->second) |
| 183 .substr(0, 32), | 246 .substr(0, 32), |
| 184 install_callback)); | 247 install_callback)); |
| 185 return true; | |
| 186 } | 248 } |
| 187 | 249 |
| 188 void MountResult(const base::Callback<void(const std::string&)>& mount_callback, | 250 void CrOSComponent::InstallChromeOSComponent( |
| 189 chromeos::DBusMethodCallStatus call_status, | 251 ComponentUpdateService* cus, |
| 190 const std::string& result) { | 252 const std::string& id, |
| 191 if (call_status != chromeos::DBUS_METHOD_CALL_SUCCESS) { | 253 const update_client::Callback& install_callback) { |
| 192 DVLOG(1) << "Call to imageloader service failed."; | 254 cus->GetOnDemandUpdater().OnDemandUpdate(id, install_callback); |
| 193 base::PostTask(FROM_HERE, base::Bind(mount_callback, "")); | |
| 194 return; | |
| 195 } | |
| 196 if (result.empty()) { | |
| 197 DVLOG(1) << "Component load failed"; | |
| 198 base::PostTask(FROM_HERE, base::Bind(mount_callback, "")); | |
| 199 return; | |
| 200 } | |
| 201 base::PostTask(FROM_HERE, base::Bind(mount_callback, result)); | |
| 202 } | 255 } |
| 203 | 256 |
| 204 void CrOSComponent::LoadCrOSComponent( | |
| 205 const std::string& name, | |
| 206 const base::Callback<void(const std::string&)>& mount_callback) { | |
| 207 chromeos::ImageLoaderClient* loader = | |
| 208 chromeos::DBusThreadManager::Get()->GetImageLoaderClient(); | |
| 209 if (loader) { | |
| 210 loader->LoadComponent(name, base::Bind(&MountResult, mount_callback)); | |
| 211 } else { | |
| 212 DVLOG(1) << "Failed to get ImageLoaderClient object."; | |
| 213 } | |
| 214 } | |
| 215 #endif // defined(OS_CHROMEOS | 257 #endif // defined(OS_CHROMEOS |
| 216 | 258 |
| 217 } // namespace component_updater | 259 } // namespace component_updater |
| OLD | NEW |