| [email protected] | de0fdca2 | 2014-08-19 05:26:09 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| [email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 5 | #ifndef COMPONENTS_UPDATE_CLIENT_UTILS_H_ |
| 6 | #define COMPONENTS_UPDATE_CLIENT_UTILS_H_ |
| [email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 7 | |
| dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 8 | #include <memory> |
| [email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 9 | #include <string> |
| sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 10 | #include <utility> |
| sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 11 | #include <vector> |
| tfarina | be264fa | 2016-01-11 19:55:01 | [diff] [blame] | 12 | |
| sorin | 2892f721 | 2016-11-07 18:59:43 | [diff] [blame] | 13 | #include "base/callback_forward.h" |
| Sorin Jianu | 556147d | 2018-01-19 21:55:21 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
| sorin | 2892f721 | 2016-11-07 18:59:43 | [diff] [blame] | 15 | #include "components/update_client/update_client.h" |
| [email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 16 | |
| 17 | class GURL; |
| 18 | |
| [email protected] | 2e2c529 | 2013-12-17 03:48:40 | [diff] [blame] | 19 | namespace base { |
| Sorin Jianu | 7aa6d1f | 2017-10-13 20:29:29 | [diff] [blame] | 20 | class DictionaryValue; |
| [email protected] | 2e2c529 | 2013-12-17 03:48:40 | [diff] [blame] | 21 | class FilePath; |
| 22 | } |
| 23 | |
| sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 24 | namespace update_client { |
| [email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 25 | |
| sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 26 | class Component; |
| sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 27 | struct CrxComponent; |
| [email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 28 | |
| sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 29 | // Defines a name-value pair that represents an installer attribute. |
| 30 | // Installer attributes are component-specific metadata, which may be serialized |
| 31 | // in an update check request. |
| 32 | using InstallerAttribute = std::pair<std::string, std::string>; |
| 33 | |
| sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 34 | // Returns true if the |component| contains a valid differential update url. |
| 35 | bool HasDiffUpdate(const Component& component); |
| [email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 36 | |
| [email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 37 | // Returns true if the |status_code| represents a server error 5xx. |
| 38 | bool IsHttpServerError(int status_code); |
| 39 | |
| [email protected] | 2e2c529 | 2013-12-17 03:48:40 | [diff] [blame] | 40 | // Deletes the file and its directory, if the directory is empty. If the |
| 41 | // parent directory is not empty, the function ignores deleting the directory. |
| 42 | // Returns true if the file and the empty directory are deleted. |
| 43 | bool DeleteFileAndEmptyParentDirectory(const base::FilePath& filepath); |
| 44 | |
| [email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 45 | // Returns the component id of the |component|. The component id is in a |
| 46 | // format similar with the format of an extension id. |
| 47 | std::string GetCrxComponentID(const CrxComponent& component); |
| 48 | |
| Sorin Jianu | 925cf98 | 2019-05-28 23:50:40 | [diff] [blame^] | 49 | // Returns a CRX id from a public key hash. |
| 50 | std::string GetCrxIdFromPublicKeyHash(const std::vector<uint8_t>& pk_hash); |
| 51 | |
| sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 52 | // Returns true if the actual SHA-256 hash of the |filepath| matches the |
| 53 | // |expected_hash|. |
| 54 | bool VerifyFileHash256(const base::FilePath& filepath, |
| 55 | const std::string& expected_hash); |
| 56 | |
| sorin | af402ee | 2016-04-09 01:02:06 | [diff] [blame] | 57 | // Returns true if the |brand| parameter matches ^[a-zA-Z]{4}?$ . |
| sorin | a1fafb7d | 2016-03-23 17:54:42 | [diff] [blame] | 58 | bool IsValidBrand(const std::string& brand); |
| 59 | |
| sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 60 | // Returns true if the name part of the |attr| parameter matches |
| 61 | // ^[-_a-zA-Z0-9]{1,256}$ and the value part of the |attr| parameter |
| 62 | // matches ^[-.,;+_=a-zA-Z0-9]{0,256}$ . |
| 63 | bool IsValidInstallerAttribute(const InstallerAttribute& attr); |
| sorin | 2b864bc7 | 2016-04-08 22:14:47 | [diff] [blame] | 64 | |
| sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 65 | // Removes the unsecure urls in the |urls| parameter. |
| 66 | void RemoveUnsecureUrls(std::vector<GURL>* urls); |
| 67 | |
| sorin | 2892f721 | 2016-11-07 18:59:43 | [diff] [blame] | 68 | // Adapter function for the old definitions of CrxInstaller::Install until the |
| Sorin Jianu | 990ee14 | 2017-06-02 22:34:08 | [diff] [blame] | 69 | // component installer code is migrated to use a Result instead of bool. |
| Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 70 | CrxInstaller::Result InstallFunctionWrapper( |
| 71 | base::OnceCallback<bool()> callback); |
| sorin | 2892f721 | 2016-11-07 18:59:43 | [diff] [blame] | 72 | |
| Sorin Jianu | 7aa6d1f | 2017-10-13 20:29:29 | [diff] [blame] | 73 | // Deserializes the CRX manifest. The top level must be a dictionary. |
| 74 | std::unique_ptr<base::DictionaryValue> ReadManifest( |
| 75 | const base::FilePath& unpack_path); |
| 76 | |
| Minh X. Nguyen | ac280f2 | 2018-05-24 17:41:29 | [diff] [blame] | 77 | // Converts a custom, specific installer error (and optionally extended error) |
| 78 | // to an installer result. |
| 79 | template <typename T> |
| 80 | CrxInstaller::Result ToInstallerResult(const T& error, int extended_error = 0) { |
| 81 | static_assert(std::is_enum<T>::value, |
| 82 | "Use an enum class to define custom installer errors"); |
| 83 | return CrxInstaller::Result( |
| 84 | static_cast<int>(update_client::InstallError::CUSTOM_ERROR_BASE) + |
| 85 | static_cast<int>(error), |
| 86 | extended_error); |
| 87 | } |
| 88 | |
| sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 89 | } // namespace update_client |
| [email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 90 | |
| sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 91 | #endif // COMPONENTS_UPDATE_CLIENT_UTILS_H_ |