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 #ifndef CHROME_BROWSER_COMPONENT_UPDATER_CROS_COMPONENT_INSTALLER_H_ | 5 #ifndef CHROME_BROWSER_COMPONENT_UPDATER_CROS_COMPONENT_INSTALLER_H_ |
| 6 #define CHROME_BROWSER_COMPONENT_UPDATER_CROS_COMPONENT_INSTALLER_H_ | 6 #define CHROME_BROWSER_COMPONENT_UPDATER_CROS_COMPONENT_INSTALLER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "components/component_updater/component_updater_service.h" | 11 #include "components/component_updater/component_updater_service.h" |
| 12 #include "components/component_updater/default_component_installer.h" | 12 #include "components/component_updater/default_component_installer.h" |
| 13 #include "components/update_client/update_client.h" | 13 #include "components/update_client/update_client.h" |
| 14 #include "crypto/sha2.h" | 14 #include "crypto/sha2.h" |
| 15 | 15 |
| 16 #if defined(OS_CHROMEOS) | |
| 17 #include "chromeos/dbus/dbus_method_call_status.h" | |
| 18 #endif // defined(OS_CHROMEOS) | |
| 19 | |
| 20 // Developer API usage: | |
| 21 // ... | |
| 22 // void LoadCallback(const std::string& mount_point){ | |
| 23 // if (mount_point.empty()) { | |
| 24 // // component is not loaded. | |
| 25 // return; | |
| 26 // } | |
| 27 // ... | |
| 28 // } | |
| 29 // ... | |
| 30 // component_updater::CrOSComponent::LoadComponent( | |
| 31 // name, | |
| 32 // base::Bind(&LoadCallback)); | |
| 33 // | |
| 16 namespace component_updater { | 34 namespace component_updater { |
| 17 | 35 |
| 18 #if defined(OS_CHROMEOS) | 36 #if defined(OS_CHROMEOS) |
| 19 struct ComponentConfig { | 37 struct ComponentConfig { |
| 20 std::string name; | 38 std::string name; |
| 21 std::string env_version; | 39 std::string env_version; |
| 22 std::string sha2hashstr; | 40 std::string sha2hashstr; |
| 23 ComponentConfig(const std::string& name, | 41 ComponentConfig(const std::string& name, |
| 24 const std::string& env_version, | 42 const std::string& env_version, |
| 25 const std::string& sha2hashstr); | 43 const std::string& sha2hashstr); |
| 26 ~ComponentConfig(); | 44 ~ComponentConfig(); |
| 27 }; | 45 }; |
| 28 | 46 |
| 29 using ConfigMap = std::map<std::string, std::map<std::string, std::string>>; | 47 using ConfigMap = std::map<std::string, std::map<std::string, std::string>>; |
| 30 | 48 |
| 31 class CrOSComponentInstallerTraits : public ComponentInstallerTraits { | 49 class CrOSComponentInstallerTraits : public ComponentInstallerTraits { |
| 32 public: | 50 public: |
| 33 explicit CrOSComponentInstallerTraits(const ComponentConfig& config); | 51 explicit CrOSComponentInstallerTraits(const ComponentConfig& config); |
| 34 ~CrOSComponentInstallerTraits() override {} | 52 ~CrOSComponentInstallerTraits() override {} |
| 35 | 53 |
| 36 private: | 54 private: |
| 55 FRIEND_TEST_ALL_PREFIXES(CrOSComponentInstallerTest, IsCompatibleOrNot); | |
| 56 FRIEND_TEST_ALL_PREFIXES(CrOSComponentInstallerTest, | |
| 57 ComponentReadyCorrectManifest); | |
| 58 FRIEND_TEST_ALL_PREFIXES(CrOSComponentInstallerTest, | |
| 59 ComponentReadyWrongManifest); | |
| 37 // The following methods override ComponentInstallerTraits. | 60 // The following methods override ComponentInstallerTraits. |
| 38 bool SupportsGroupPolicyEnabledComponentUpdates() const override; | 61 bool SupportsGroupPolicyEnabledComponentUpdates() const override; |
| 39 bool RequiresNetworkEncryption() const override; | 62 bool RequiresNetworkEncryption() const override; |
| 40 update_client::CrxInstaller::Result OnCustomInstall( | 63 update_client::CrxInstaller::Result OnCustomInstall( |
| 41 const base::DictionaryValue& manifest, | 64 const base::DictionaryValue& manifest, |
| 42 const base::FilePath& install_dir) override; | 65 const base::FilePath& install_dir) override; |
| 43 bool VerifyInstallation(const base::DictionaryValue& manifest, | 66 bool VerifyInstallation(const base::DictionaryValue& manifest, |
| 44 const base::FilePath& install_dir) const override; | 67 const base::FilePath& install_dir) const override; |
| 45 void ComponentReady(const base::Version& version, | 68 void ComponentReady(const base::Version& version, |
| 46 const base::FilePath& path, | 69 const base::FilePath& path, |
| 47 std::unique_ptr<base::DictionaryValue> manifest) override; | 70 std::unique_ptr<base::DictionaryValue> manifest) override; |
| 48 base::FilePath GetRelativeInstallDir() const override; | 71 base::FilePath GetRelativeInstallDir() const override; |
| 49 void GetHash(std::vector<uint8_t>* hash) const override; | 72 void GetHash(std::vector<uint8_t>* hash) const override; |
| 50 std::string GetName() const override; | 73 std::string GetName() const override; |
| 51 update_client::InstallerAttributes GetInstallerAttributes() const override; | 74 update_client::InstallerAttributes GetInstallerAttributes() const override; |
| 52 std::vector<std::string> GetMimeTypes() const override; | 75 std::vector<std::string> GetMimeTypes() const override; |
| 76 | |
| 77 virtual bool IsCompatible(const std::string& env_version_str, | |
| 78 const std::string& min_env_version_str); | |
| 53 std::string name; | 79 std::string name; |
| 54 std::string env_version; | 80 std::string env_version; |
| 55 uint8_t kSha2Hash_[crypto::kSHA256Length] = {}; | 81 uint8_t kSha2Hash_[crypto::kSHA256Length] = {}; |
| 56 | 82 |
| 57 DISALLOW_COPY_AND_ASSIGN(CrOSComponentInstallerTraits); | 83 DISALLOW_COPY_AND_ASSIGN(CrOSComponentInstallerTraits); |
| 58 }; | 84 }; |
| 59 | 85 |
| 60 // This class contains functions used to register and install a component. | 86 // This class contains functions used to register and install a component. |
| 61 class CrOSComponent { | 87 class CrOSComponent { |
| 62 public: | 88 public: |
| 63 // Register and start installing a CrOS component. | 89 static void LoadComponent( |
| 64 // |install_callback| is triggered after install finishes and returns error | |
| 65 // code. | |
| 66 // | |
| 67 // example: | |
| 68 // ... | |
| 69 // void load_callback(const std::string& result){ | |
| 70 // if (result.empty) { | |
| 71 // // component is not mounted. | |
| 72 // return; | |
| 73 // } | |
| 74 // // [component mount point: result] | |
| 75 // } | |
| 76 // void install_callback(update_client::Error error){ | |
| 77 // // switch(error){ | |
| 78 // // case update_client::Error::NONE: | |
| 79 // // // component is installed | |
| 80 // // break; | |
| 81 // // case update_client::Error::INVALID_ARGUMENT: | |
| 82 // // // your install failed due to your wrong parameters. | |
| 83 // // break; | |
| 84 // // default: | |
| 85 // // // your install failed due to system failure. | |
| 86 // // break; | |
| 87 // // } | |
| 88 // // Even when error code other than NONE returned (your install failed), | |
| 89 // // component might has already being installed previously. | |
| 90 // // Plus, if you want to know current version of component. | |
| 91 // component_updater:CrOSComponent::LoadCrOSComponent(name, load_callback); | |
| 92 // } | |
| 93 // ... | |
| 94 // component_updater::CrOSComponent::InstallCrOSComponent( | |
| 95 // name, | |
| 96 // base::Bind(&install_callback)); | |
| 97 // | |
| 98 static bool InstallCrOSComponent( | |
| 99 const std::string& name, | 90 const std::string& name, |
| 100 const update_client::Callback& install_callback); | 91 const base::Callback<void(const std::string&)>& load_callback); |
| 101 | |
| 102 static void LoadCrOSComponent( | |
| 103 const std::string& name, | |
| 104 const base::Callback<void(const std::string&)>& mount_callback); | |
| 105 | 92 |
| 106 private: | 93 private: |
| 94 FRIEND_TEST_ALL_PREFIXES(CrOSComponentInstallerTest, | |
| 95 RegisterComponentSuccess); | |
| 96 FRIEND_TEST_ALL_PREFIXES(CrOSComponentInstallerTest, RegisterComponentFail); | |
| 107 CrOSComponent() {} | 97 CrOSComponent() {} |
| 108 // Register a component. | 98 // Install a component with a dedicated ComponentUpdateServie instance. |
|
waffles
2017/06/01 22:42:41
ComponentUpdateServie→ComponentUpdateService
xiaochu
2017/06/02 04:42:15
Done.
| |
| 109 static void RegisterCrOSComponentInternal(ComponentUpdateService* cus, | 99 static void InstallComponent( |
| 110 const ComponentConfig& config, | 100 ComponentUpdateService* cus, |
| 111 const base::Closure& callback); | 101 const std::string& name, |
| 112 // A helper function to pass into RegisterCrOSComonentInternal as a callback. | 102 const base::Callback<void(const std::string&)>& load_callback); |
| 103 // Register a component with a dedicated ComponentUpdateService instance. | |
| 104 static void RegisterComponent(ComponentUpdateService* cus, | |
| 105 const ComponentConfig& config, | |
| 106 const base::Closure& callback); | |
| 113 // It calls OnDemandUpdate to install the component right after being | 107 // It calls OnDemandUpdate to install the component right after being |
| 114 // registered. | 108 // registered. |
| 115 static void InstallChromeOSComponent( | 109 static void RegisterResult(ComponentUpdateService* cus, |
| 116 ComponentUpdateService* cus, | 110 const std::string& id, |
| 117 const std::string& id, | 111 const update_client::Callback& install_callback); |
| 118 const update_client::Callback& install_callback); | 112 // It call LoadComponentInternal to load the installed component. |
|
waffles
2017/06/01 22:42:41
call→calls
xiaochu
2017/06/02 04:42:15
Done.
| |
| 113 static void InstallResult( | |
| 114 const std::string& name, | |
| 115 const base::Callback<void(const std::string&)>& load_callback, | |
| 116 update_client::Error error); | |
| 117 // Internal function to load a component. | |
| 118 static void LoadComponentInternal( | |
| 119 const std::string& name, | |
| 120 const base::Callback<void(const std::string&)>& load_callback); | |
| 121 // It returns load result. | |
|
waffles
2017/06/01 22:42:41
The function returns void, maybe elaborate on what
xiaochu
2017/06/02 04:42:15
Done.
| |
| 122 static void LoadResult( | |
| 123 const base::Callback<void(const std::string&)>& load_callback, | |
| 124 chromeos::DBusMethodCallStatus call_status, | |
| 125 const std::string& result); | |
| 119 }; | 126 }; |
| 120 #endif // defined(OS_CHROMEOS) | 127 #endif // defined(OS_CHROMEOS) |
| 121 | 128 |
| 122 } // namespace component_updater | 129 } // namespace component_updater |
| 123 | 130 |
| 124 #endif // CHROME_BROWSER_COMPONENT_UPDATER_CROS_COMPONENT_INSTALLER_H_ | 131 #endif // CHROME_BROWSER_COMPONENT_UPDATER_CROS_COMPONENT_INSTALLER_H_ |
| OLD | NEW |