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 // Register and start installing a CrOS component. | |
| 17 // | |
| 18 // Developer API usage: | |
| 19 // ... | |
| 20 // void load_callback(const std::string& result){ | |
| 21 // if (result.empty) { | |
| 22 // // component is not mounted. | |
| 23 // return; | |
| 24 // } | |
| 25 // // [component mount point: result] | |
| 26 // } | |
| 27 // void install_callback(update_client::Error error){ | |
| 28 // // error code: 1) update_client::Error::NONE, success 2) otherwise, | |
| 29 // // fail. | |
| 30 // // Even when error code other than NONE returned (your install failed), | |
| 31 // // component might has already being installed previously. | |
| 32 // // Plus, if you want to know current version of component. | |
| 33 // component_updater:CrOSComponent::LoadCrOSComponent(name, load_callback); | |
|
waffles
2017/05/30 22:12:17
Sorry, I'm still confused about this API. We have
xiaochu
2017/05/31 15:40:11
Done.
This makes sense. Thanks!
| |
| 34 // } | |
| 35 // ... | |
| 36 // component_updater::CrOSComponent::InstallCrOSComponent( | |
| 37 // name, | |
| 38 // base::Bind(&install_callback)); | |
| 39 // | |
| 16 namespace component_updater { | 40 namespace component_updater { |
| 17 | 41 |
| 18 #if defined(OS_CHROMEOS) | 42 #if defined(OS_CHROMEOS) |
| 19 struct ComponentConfig { | 43 struct ComponentConfig { |
| 20 std::string name; | 44 std::string name; |
| 21 std::string env_version; | 45 std::string env_version; |
| 22 std::string sha2hashstr; | 46 std::string sha2hashstr; |
| 23 ComponentConfig(const std::string& name, | 47 ComponentConfig(const std::string& name, |
| 24 const std::string& env_version, | 48 const std::string& env_version, |
| 25 const std::string& sha2hashstr); | 49 const std::string& sha2hashstr); |
| 26 ~ComponentConfig(); | 50 ~ComponentConfig(); |
| 27 }; | 51 }; |
| 28 | 52 |
| 29 using ConfigMap = std::map<std::string, std::map<std::string, std::string>>; | 53 using ConfigMap = std::map<std::string, std::map<std::string, std::string>>; |
| 30 | 54 |
| 31 class CrOSComponentInstallerTraits : public ComponentInstallerTraits { | 55 class CrOSComponentInstallerTraits : public ComponentInstallerTraits { |
| 32 public: | 56 public: |
| 33 explicit CrOSComponentInstallerTraits(const ComponentConfig& config); | 57 explicit CrOSComponentInstallerTraits(const ComponentConfig& config); |
| 34 ~CrOSComponentInstallerTraits() override {} | 58 ~CrOSComponentInstallerTraits() override {} |
| 35 | 59 |
| 36 private: | 60 private: |
| 61 FRIEND_TEST_ALL_PREFIXES(CrOSComponentInstallerTest, IsCompatibleOrNot); | |
| 62 FRIEND_TEST_ALL_PREFIXES(CrOSComponentInstallerTest, | |
| 63 ComponentReadyCorrectManifest); | |
| 64 FRIEND_TEST_ALL_PREFIXES(CrOSComponentInstallerTest, | |
| 65 ComponentReadyWrongManifest); | |
| 37 // The following methods override ComponentInstallerTraits. | 66 // The following methods override ComponentInstallerTraits. |
| 38 bool SupportsGroupPolicyEnabledComponentUpdates() const override; | 67 bool SupportsGroupPolicyEnabledComponentUpdates() const override; |
| 39 bool RequiresNetworkEncryption() const override; | 68 bool RequiresNetworkEncryption() const override; |
| 40 update_client::CrxInstaller::Result OnCustomInstall( | 69 update_client::CrxInstaller::Result OnCustomInstall( |
| 41 const base::DictionaryValue& manifest, | 70 const base::DictionaryValue& manifest, |
| 42 const base::FilePath& install_dir) override; | 71 const base::FilePath& install_dir) override; |
| 43 bool VerifyInstallation(const base::DictionaryValue& manifest, | 72 bool VerifyInstallation(const base::DictionaryValue& manifest, |
| 44 const base::FilePath& install_dir) const override; | 73 const base::FilePath& install_dir) const override; |
| 45 void ComponentReady(const base::Version& version, | 74 void ComponentReady(const base::Version& version, |
| 46 const base::FilePath& path, | 75 const base::FilePath& path, |
| 47 std::unique_ptr<base::DictionaryValue> manifest) override; | 76 std::unique_ptr<base::DictionaryValue> manifest) override; |
| 48 base::FilePath GetRelativeInstallDir() const override; | 77 base::FilePath GetRelativeInstallDir() const override; |
| 49 void GetHash(std::vector<uint8_t>* hash) const override; | 78 void GetHash(std::vector<uint8_t>* hash) const override; |
| 50 std::string GetName() const override; | 79 std::string GetName() const override; |
| 51 update_client::InstallerAttributes GetInstallerAttributes() const override; | 80 update_client::InstallerAttributes GetInstallerAttributes() const override; |
| 52 std::vector<std::string> GetMimeTypes() const override; | 81 std::vector<std::string> GetMimeTypes() const override; |
| 82 | |
| 83 virtual bool IsCompatible(const std::string& env_version_str, | |
| 84 const std::string& min_env_version_str); | |
| 53 std::string name; | 85 std::string name; |
| 54 std::string env_version; | 86 std::string env_version; |
| 55 uint8_t kSha2Hash_[crypto::kSHA256Length] = {}; | 87 uint8_t kSha2Hash_[crypto::kSHA256Length] = {}; |
| 56 | 88 |
| 57 DISALLOW_COPY_AND_ASSIGN(CrOSComponentInstallerTraits); | 89 DISALLOW_COPY_AND_ASSIGN(CrOSComponentInstallerTraits); |
| 58 }; | 90 }; |
| 59 | 91 |
| 60 // This class contains functions used to register and install a component. | 92 // This class contains functions used to register and install a component. |
| 61 class CrOSComponent { | 93 class CrOSComponent { |
| 62 public: | 94 public: |
| 63 // Register and start installing a CrOS component. | 95 static void InstallCrOSComponent( |
| 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, | 96 const std::string& name, |
| 100 const update_client::Callback& install_callback); | 97 const update_client::Callback& install_callback); |
| 101 | 98 |
| 102 static void LoadCrOSComponent( | 99 private: |
| 100 // Install a component (called by InstallCrOSComponent). | |
| 101 static void InstallCrOSComponent( | |
| 102 ComponentUpdateService* cus, | |
| 103 const std::string& name, | 103 const std::string& name, |
| 104 const base::Callback<void(const std::string&)>& mount_callback); | 104 const update_client::Callback& install_callback); |
| 105 | 105 // Register a component (called by InstallCrOSComponent). |
| 106 private: | |
| 107 CrOSComponent() {} | |
| 108 // Register a component. | |
| 109 static void RegisterCrOSComponentInternal(ComponentUpdateService* cus, | 106 static void RegisterCrOSComponentInternal(ComponentUpdateService* cus, |
| 110 const ComponentConfig& config, | 107 const ComponentConfig& config, |
| 111 const base::Closure& callback); | 108 const base::Closure& callback); |
| 112 // A helper function to pass into RegisterCrOSComonentInternal as a callback. | 109 // A helper function to pass into RegisterCrOSComonentInternal as a callback. |
| 113 // It calls OnDemandUpdate to install the component right after being | 110 // It calls OnDemandUpdate to install the component right after being |
| 114 // registered. | 111 // registered. |
| 115 static void InstallChromeOSComponent( | 112 static void InstallChromeOSComponent( |
| 116 ComponentUpdateService* cus, | 113 ComponentUpdateService* cus, |
| 117 const std::string& id, | 114 const std::string& id, |
| 118 const update_client::Callback& install_callback); | 115 const update_client::Callback& install_callback); |
| 116 | |
| 117 public: | |
|
waffles
2017/05/30 22:12:17
Group all public members together. Same for privat
xiaochu
2017/05/31 15:40:11
Done.
| |
| 118 static void LoadCrOSComponent( | |
| 119 const std::string& name, | |
| 120 const base::Callback<void(const std::string&)>& load_callback); | |
| 121 | |
| 122 private: | |
| 123 FRIEND_TEST_ALL_PREFIXES(CrOSComponentInstallerTest, | |
| 124 RegisterComponentSuccess); | |
| 125 FRIEND_TEST_ALL_PREFIXES(CrOSComponentInstallerTest, RegisterComponentFail); | |
| 126 CrOSComponent() {} | |
| 119 }; | 127 }; |
| 120 #endif // defined(OS_CHROMEOS) | 128 #endif // defined(OS_CHROMEOS) |
| 121 | 129 |
| 122 } // namespace component_updater | 130 } // namespace component_updater |
| 123 | 131 |
| 124 #endif // CHROME_BROWSER_COMPONENT_UPDATER_CROS_COMPONENT_INSTALLER_H_ | 132 #endif // CHROME_BROWSER_COMPONENT_UPDATER_CROS_COMPONENT_INSTALLER_H_ |
| OLD | NEW |