blob: 7c8443158c6309c54f5e9688486273324c797a19 [file] [log] [blame]
Greg Kerra6c0c522016-07-25 11:15:31 -07001// 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 Kerr2f76fde2016-08-29 16:39:45 -07004#ifndef IMAGELOADER_IMAGELOADER_IMPL_H_
5#define IMAGELOADER_IMAGELOADER_IMPL_H_
Greg Kerra6c0c522016-07-25 11:15:31 -07006
Eric Caruso26a91442017-10-25 16:05:40 -07007#include <map>
Greg Kerra6c0c522016-07-25 11:15:31 -07008#include <string>
9#include <vector>
10
11#include <base/files/file_path.h>
12#include <base/gtest_prod_util.h>
13#include <base/macros.h>
14
Greg Kerr9944e242017-01-26 15:09:31 -080015#include "helper_process.h"
Greg Kerr89be05f2016-07-27 10:40:32 -070016
Greg Kerra6c0c522016-07-25 11:15:31 -070017namespace imageloader {
18
Eric Caruso0b79bc82017-03-21 13:44:34 -070019using Keys = std::vector<std::vector<uint8_t>>;
20
Greg Kerra6c0c522016-07-25 11:15:31 -070021struct ImageLoaderConfig {
Eric Caruso0b79bc82017-03-21 13:44:34 -070022 ImageLoaderConfig(const Keys& keys, const char* storage_path,
Greg Kerr9944e242017-01-26 15:09:31 -080023 const char* mount_path)
Eric Caruso0b79bc82017-03-21 13:44:34 -070024 : keys(keys),
Greg Kerr89be05f2016-07-27 10:40:32 -070025 storage_dir(storage_path),
Greg Kerr9944e242017-01-26 15:09:31 -080026 mount_path(mount_path) {}
Greg Kerr89be05f2016-07-27 10:40:32 -070027
Eric Caruso0b79bc82017-03-21 13:44:34 -070028 Keys keys;
Greg Kerra6c0c522016-07-25 11:15:31 -070029 base::FilePath storage_dir;
Greg Kerr89be05f2016-07-27 10:40:32 -070030 base::FilePath mount_path;
Greg Kerra6c0c522016-07-25 11:15:31 -070031};
32
33class ImageLoaderImpl {
34 public:
35 // Instantiate an object with a configuration object.
Greg Kerr89be05f2016-07-27 10:40:32 -070036 explicit ImageLoaderImpl(ImageLoaderConfig config)
37 : config_(std::move(config)) {}
Greg Kerra6c0c522016-07-25 11:15:31 -070038
39 // Register a component.
40 bool RegisterComponent(const std::string& name, const std::string& version,
41 const std::string& component_folder_abs_path);
42
Xiaochu Liu7a224d92017-10-06 17:33:41 -070043 bool RemoveComponent(const std::string& name);
44
Greg Kerra6c0c522016-07-25 11:15:31 -070045 // Get component version given component name.
46 std::string GetComponentVersion(const std::string& name);
47
Eric Caruso26a91442017-10-25 16:05:40 -070048 // Get component metadata given component name.
49 bool GetComponentMetadata(const std::string& name,
50 std::map<std::string, std::string>* out_metadata);
51
Greg Kerr019d59c2016-11-17 14:28:49 -080052 // Load the specified component. This returns the mount point or an empty
53 // string on failure.
Greg Kerr9944e242017-01-26 15:09:31 -080054 std::string LoadComponent(const std::string& name, HelperProcess* process);
Greg Kerra6c0c522016-07-25 11:15:31 -070055
Greg Kerrc5b91692016-09-14 12:09:22 -070056 // Load the specified component at a set mount point.
Greg Kerr9944e242017-01-26 15:09:31 -080057 bool LoadComponent(const std::string& name, const std::string& mount_point,
58 HelperProcess* process);
Greg Kerrc5b91692016-09-14 12:09:22 -070059
Greg Kerr772abab2017-06-16 14:51:01 -070060 // Load the specified component from the given path.
61 std::string LoadComponentAtPath(const std::string& name,
62 const base::FilePath& absolute_path,
63 HelperProcess* process);
64
Greg Kerr019d59c2016-11-17 14:28:49 -080065 // The directory hierarchy for a component consists of the storage_root (i.e.
66 // `/var/lib/imageloader`), the component_root
67 // (`/var/lib/imageloader/ComponentName`), and the version folder (i.e.
68 // `/var/lib/imageloader/ComponentName/23.0.0.205`). That is:
69 // [storage_root]/
70 // [storage_root]/[component_root]
71 // [storage_root]/[component_root]/[version]
72 //
73 // Inside the `component_root` there is a current version hint file:
74 // [storage_root]/[component_root]/latest-version
75
76 // Return the path to latest-version file for |component_name|.
77 base::FilePath GetLatestVersionFilePath(const std::string& component_name);
78
79 // Return the path to the [component_root] folder for |component_name|.
80 base::FilePath GetComponentRoot(const std::string& component_name);
81
82 // Return the path to a given version of |component_name|.
83 base::FilePath GetVersionPath(const std::string& component_name,
84 const std::string& version);
85
86 // Return the path to the current version of |component_name|.
87 bool GetPathToCurrentComponentVersion(const std::string& component_name,
88 base::FilePath* result);
89
Greg Kerra6c0c522016-07-25 11:15:31 -070090 private:
Xiaochu Liu7a224d92017-10-06 17:33:41 -070091 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, RemoveImageAtPathRemovable);
92 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, RemoveImageAtPathNotRemovable);
93
Greg Kerra6c0c522016-07-25 11:15:31 -070094 // The configuration traits.
95 ImageLoaderConfig config_;
96
Xiaochu Liu7a224d92017-10-06 17:33:41 -070097 // Remove component if removable.
98 bool RemoveComponentAtPath(const std::string& name,
99 const base::FilePath& component_root,
100 const base::FilePath& component_path);
101
Greg Kerra6c0c522016-07-25 11:15:31 -0700102 DISALLOW_COPY_AND_ASSIGN(ImageLoaderImpl);
103};
104
105} // namespace imageloader
106
Greg Kerr2f76fde2016-08-29 16:39:45 -0700107#endif // IMAGELOADER_IMAGELOADER_IMPL_H_