blob: 14567e691fdf39d580a956aeac546cdcd78332df [file] [log] [blame]
[email protected]de0fdca22014-08-19 05:26:091// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]2cddef42013-11-22 08:23:222// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
sorin52ac0882015-01-24 01:15:005#ifndef COMPONENTS_UPDATE_CLIENT_UTILS_H_
6#define COMPONENTS_UPDATE_CLIENT_UTILS_H_
[email protected]2cddef42013-11-22 08:23:227
dchengd0fc6aa92016-04-22 18:03:128#include <memory>
[email protected]2cddef42013-11-22 08:23:229#include <string>
sorin2adb2ca2016-06-29 01:44:3510#include <utility>
sorinfccbf2d2016-04-04 20:34:3411#include <vector>
tfarinabe264fa2016-01-11 19:55:0112
sorin2892f7212016-11-07 18:59:4313#include "base/callback_forward.h"
Sorin Jianu556147d2018-01-19 21:55:2114#include "base/memory/ref_counted.h"
sorin2892f7212016-11-07 18:59:4315#include "components/update_client/update_client.h"
[email protected]2cddef42013-11-22 08:23:2216
17class GURL;
18
[email protected]2e2c5292013-12-17 03:48:4019namespace base {
Sorin Jianu7aa6d1f2017-10-13 20:29:2920class DictionaryValue;
[email protected]2e2c5292013-12-17 03:48:4021class FilePath;
22}
23
sorin52ac0882015-01-24 01:15:0024namespace update_client {
[email protected]2cddef42013-11-22 08:23:2225
sorin30474f02017-04-27 00:45:4826class Component;
sorin52ac0882015-01-24 01:15:0027struct CrxComponent;
[email protected]da37c1d2013-12-19 01:04:3828
sorin2adb2ca2016-06-29 01:44:3529// 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.
32using InstallerAttribute = std::pair<std::string, std::string>;
33
sorin30474f02017-04-27 00:45:4834// Returns true if the |component| contains a valid differential update url.
35bool HasDiffUpdate(const Component& component);
[email protected]da37c1d2013-12-19 01:04:3836
[email protected]3cb2a4f2013-12-07 21:54:3437// Returns true if the |status_code| represents a server error 5xx.
38bool IsHttpServerError(int status_code);
39
[email protected]2e2c5292013-12-17 03:48:4040// 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.
43bool DeleteFileAndEmptyParentDirectory(const base::FilePath& filepath);
44
[email protected]93e8e2c2014-01-04 12:29:2345// Returns the component id of the |component|. The component id is in a
46// format similar with the format of an extension id.
47std::string GetCrxComponentID(const CrxComponent& component);
48
Sorin Jianu925cf982019-05-28 23:50:4049// Returns a CRX id from a public key hash.
50std::string GetCrxIdFromPublicKeyHash(const std::vector<uint8_t>& pk_hash);
51
sorin74e70672016-02-03 03:13:1052// Returns true if the actual SHA-256 hash of the |filepath| matches the
53// |expected_hash|.
54bool VerifyFileHash256(const base::FilePath& filepath,
55 const std::string& expected_hash);
56
sorinaf402ee2016-04-09 01:02:0657// Returns true if the |brand| parameter matches ^[a-zA-Z]{4}?$ .
sorina1fafb7d2016-03-23 17:54:4258bool IsValidBrand(const std::string& brand);
59
sorin2adb2ca2016-06-29 01:44:3560// 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}$ .
63bool IsValidInstallerAttribute(const InstallerAttribute& attr);
sorin2b864bc72016-04-08 22:14:4764
sorinfccbf2d2016-04-04 20:34:3465// Removes the unsecure urls in the |urls| parameter.
66void RemoveUnsecureUrls(std::vector<GURL>* urls);
67
sorin2892f7212016-11-07 18:59:4368// Adapter function for the old definitions of CrxInstaller::Install until the
Sorin Jianu990ee142017-06-02 22:34:0869// component installer code is migrated to use a Result instead of bool.
Sorin Jianua8ef73d2017-11-02 16:55:1770CrxInstaller::Result InstallFunctionWrapper(
71 base::OnceCallback<bool()> callback);
sorin2892f7212016-11-07 18:59:4372
Sorin Jianu7aa6d1f2017-10-13 20:29:2973// Deserializes the CRX manifest. The top level must be a dictionary.
74std::unique_ptr<base::DictionaryValue> ReadManifest(
75 const base::FilePath& unpack_path);
76
Minh X. Nguyenac280f22018-05-24 17:41:2977// Converts a custom, specific installer error (and optionally extended error)
78// to an installer result.
79template <typename T>
80CrxInstaller::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
sorin52ac0882015-01-24 01:15:0089} // namespace update_client
[email protected]2cddef42013-11-22 08:23:2290
sorin52ac0882015-01-24 01:15:0091#endif // COMPONENTS_UPDATE_CLIENT_UTILS_H_