Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 1 | // Copyright 2016 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Greg Kerr | 2f76fde | 2016-08-29 16:39:45 -0700 | [diff] [blame] | 4 | #ifndef IMAGELOADER_IMAGELOADER_IMPL_H_ |
| 5 | #define IMAGELOADER_IMAGELOADER_IMPL_H_ |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 6 | |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include <base/files/file_path.h> |
| 11 | #include <base/gtest_prod_util.h> |
| 12 | #include <base/macros.h> |
| 13 | |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 14 | #include "helper_process.h" |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 15 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 16 | namespace imageloader { |
| 17 | |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 18 | using Keys = std::vector<std::vector<uint8_t>>; |
| 19 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 20 | struct ImageLoaderConfig { |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 21 | ImageLoaderConfig(const Keys& keys, const char* storage_path, |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 22 | const char* mount_path) |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 23 | : keys(keys), |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 24 | storage_dir(storage_path), |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 25 | mount_path(mount_path) {} |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 26 | |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 27 | Keys keys; |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 28 | base::FilePath storage_dir; |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 29 | base::FilePath mount_path; |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | class ImageLoaderImpl { |
| 33 | public: |
| 34 | // Instantiate an object with a configuration object. |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 35 | explicit ImageLoaderImpl(ImageLoaderConfig config) |
| 36 | : config_(std::move(config)) {} |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 37 | |
| 38 | // Register a component. |
| 39 | bool RegisterComponent(const std::string& name, const std::string& version, |
| 40 | const std::string& component_folder_abs_path); |
| 41 | |
Xiaochu Liu | 7a224d9 | 2017-10-06 17:33:41 -0700 | [diff] [blame] | 42 | bool RemoveComponent(const std::string& name); |
| 43 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 44 | // Get component version given component name. |
| 45 | std::string GetComponentVersion(const std::string& name); |
| 46 | |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 47 | // Load the specified component. This returns the mount point or an empty |
| 48 | // string on failure. |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 49 | std::string LoadComponent(const std::string& name, HelperProcess* process); |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 50 | |
Greg Kerr | c5b9169 | 2016-09-14 12:09:22 -0700 | [diff] [blame] | 51 | // Load the specified component at a set mount point. |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 52 | bool LoadComponent(const std::string& name, const std::string& mount_point, |
| 53 | HelperProcess* process); |
Greg Kerr | c5b9169 | 2016-09-14 12:09:22 -0700 | [diff] [blame] | 54 | |
Greg Kerr | 772abab | 2017-06-16 14:51:01 -0700 | [diff] [blame] | 55 | // Load the specified component from the given path. |
| 56 | std::string LoadComponentAtPath(const std::string& name, |
| 57 | const base::FilePath& absolute_path, |
| 58 | HelperProcess* process); |
| 59 | |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 60 | // The directory hierarchy for a component consists of the storage_root (i.e. |
| 61 | // `/var/lib/imageloader`), the component_root |
| 62 | // (`/var/lib/imageloader/ComponentName`), and the version folder (i.e. |
| 63 | // `/var/lib/imageloader/ComponentName/23.0.0.205`). That is: |
| 64 | // [storage_root]/ |
| 65 | // [storage_root]/[component_root] |
| 66 | // [storage_root]/[component_root]/[version] |
| 67 | // |
| 68 | // Inside the `component_root` there is a current version hint file: |
| 69 | // [storage_root]/[component_root]/latest-version |
| 70 | |
| 71 | // Return the path to latest-version file for |component_name|. |
| 72 | base::FilePath GetLatestVersionFilePath(const std::string& component_name); |
| 73 | |
| 74 | // Return the path to the [component_root] folder for |component_name|. |
| 75 | base::FilePath GetComponentRoot(const std::string& component_name); |
| 76 | |
| 77 | // Return the path to a given version of |component_name|. |
| 78 | base::FilePath GetVersionPath(const std::string& component_name, |
| 79 | const std::string& version); |
| 80 | |
| 81 | // Return the path to the current version of |component_name|. |
| 82 | bool GetPathToCurrentComponentVersion(const std::string& component_name, |
| 83 | base::FilePath* result); |
| 84 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 85 | private: |
Xiaochu Liu | 7a224d9 | 2017-10-06 17:33:41 -0700 | [diff] [blame] | 86 | FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, RemoveImageAtPathRemovable); |
| 87 | FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, RemoveImageAtPathNotRemovable); |
| 88 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 89 | // The configuration traits. |
| 90 | ImageLoaderConfig config_; |
| 91 | |
Xiaochu Liu | 7a224d9 | 2017-10-06 17:33:41 -0700 | [diff] [blame] | 92 | // Remove component if removable. |
| 93 | bool RemoveComponentAtPath(const std::string& name, |
| 94 | const base::FilePath& component_root, |
| 95 | const base::FilePath& component_path); |
| 96 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 97 | DISALLOW_COPY_AND_ASSIGN(ImageLoaderImpl); |
| 98 | }; |
| 99 | |
| 100 | } // namespace imageloader |
| 101 | |
Greg Kerr | 2f76fde | 2016-08-29 16:39:45 -0700 | [diff] [blame] | 102 | #endif // IMAGELOADER_IMAGELOADER_IMPL_H_ |