blob: bef780633f35b6240c36b3ccdf5fd715d7ff79c3 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2013 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
David Zeuthen27a48bc2013-08-06 12:06:29 -070016
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#ifndef UPDATE_ENGINE_CROS_P2P_MANAGER_H_
18#define UPDATE_ENGINE_CROS_P2P_MANAGER_H_
David Zeuthen27a48bc2013-08-06 12:06:29 -070019
20#include <string>
21#include <vector>
22
23#include <base/callback.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070024#include <base/files/file_path.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070025#include <base/memory/ref_counted.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070026#include <base/time/time.h>
David Zeuthen92d9c8b2013-09-11 10:58:11 -070027#include <policy/device_policy.h>
28#include <policy/libpolicy.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070029
Alex Deymo39910dc2015-11-09 17:04:30 -080030#include "update_engine/common/prefs_interface.h"
Gilad Arnold4a0321b2014-10-28 15:57:30 -070031#include "update_engine/update_manager/update_manager.h"
David Zeuthen27a48bc2013-08-06 12:06:29 -070032
33namespace chromeos_update_engine {
34
35// Interface for sharing and discovering files via p2p.
36class P2PManager {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070037 public:
David Zeuthen27a48bc2013-08-06 12:06:29 -070038 // Interface used for P2PManager implementations. The sole reason
39 // for this interface is unit testing.
40 class Configuration {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070041 public:
David Zeuthen27a48bc2013-08-06 12:06:29 -070042 virtual ~Configuration() {}
43
44 // Gets the path to the p2p dir being used, e.g. /var/cache/p2p.
45 virtual base::FilePath GetP2PDir() = 0;
46
47 // Gets the argument vector for starting (if |is_start| is True)
48 // resp. stopping (if |is_start| is False) the p2p service
49 // e.g. ["initctl", "start", "p2p"] or ["initctl", "stop", "p2p"].
50 virtual std::vector<std::string> GetInitctlArgs(bool is_start) = 0;
51
52 // Gets the argument vector for invoking p2p-client, e.g.
53 // "p2p-client --get-url=file_id_we_want --minimum-size=42123".
54 virtual std::vector<std::string> GetP2PClientArgs(
55 const std::string& file_id, size_t minimum_size) = 0;
56 };
57
58 virtual ~P2PManager() {}
59
60 // The type for the callback used in LookupUrlForFile().
61 // If the lookup failed, |url| is empty.
62 typedef base::Callback<void(const std::string& url)> LookupCallback;
63
David Zeuthen92d9c8b2013-09-11 10:58:11 -070064 // Use the device policy specified by |device_policy|. If this is
Alex Vakulenko88b591f2014-08-28 16:48:57 -070065 // null, then no device policy is used.
David Zeuthen92d9c8b2013-09-11 10:58:11 -070066 virtual void SetDevicePolicy(const policy::DevicePolicy* device_policy) = 0;
67
Gilad Arnold4a0321b2014-10-28 15:57:30 -070068 // Returns true iff P2P is currently allowed for use on this device. This
69 // value is determined and maintained by the Update Manager.
David Zeuthen27a48bc2013-08-06 12:06:29 -070070 virtual bool IsP2PEnabled() = 0;
71
72 // Ensures that the p2p subsystem is running (e.g. starts it if it's
73 // not already running) and blocks until this is so. Returns false
74 // if an error occurred.
75 virtual bool EnsureP2PRunning() = 0;
76
77 // Ensures that the p2p subsystem is not running (e.g. stops it if
78 // it's running) and blocks until this is so. Returns false if an
79 // error occurred.
80 virtual bool EnsureP2PNotRunning() = 0;
81
82 // Cleans up files in /var/cache/p2p owned by this application as
83 // per the |file_extension| and |num_files_to_keep| values passed
84 // when the object was constructed. This may be called even if
85 // the p2p subsystem is not running.
86 virtual bool PerformHousekeeping() = 0;
87
88 // Asynchronously finds a peer that serves the file identified by
89 // |file_id|. If |minimum_size| is non-zero, will find a peer that
90 // has at least that many bytes. When the result is ready |callback|
Alex Deymo0b3db6b2015-08-10 15:19:37 -070091 // is called from the current message loop.
David Zeuthen27a48bc2013-08-06 12:06:29 -070092 //
93 // This operation may take a very long time to complete because part
94 // of the p2p protocol involves waiting for the LAN-wide sum of all
95 // num-connections to drop below a given threshold. However, if
96 // |max_time_to_wait| is non-zero, the operation is guaranteed to
97 // not exceed this duration.
98 //
99 // If the file is not available on the LAN (or if mDNS/DNS-SD is
100 // filtered), this is guaranteed to not take longer than 5 seconds.
101 virtual void LookupUrlForFile(const std::string& file_id,
102 size_t minimum_size,
103 base::TimeDelta max_time_to_wait,
104 LookupCallback callback) = 0;
105
106 // Shares a file identified by |file_id| in the directory
107 // /var/cache/p2p. Initially the file will not be visible, that is,
108 // it will have a .tmp extension and not be shared via p2p. Use the
109 // FileMakeVisible() method to change this.
110 //
111 // If you know the final size of the file, pass it in the
112 // |expected_size| parameter. Otherwise pass zero. If non-zero, the
113 // amount of free space in /var/cache/p2p is checked and if there is
114 // less than twice the amount of space available, this method
115 // fails. Additionally, disk space will be reserved via fallocate(2)
116 // and |expected_size| is written to the user.cros-p2p-filesize
117 // xattr of the created file.
118 //
119 // If the file already exists, true is returned. Any on-disk xattr
120 // is not updated.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800121 virtual bool FileShare(const std::string& file_id, size_t expected_size) = 0;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700122
123 // Gets a fully qualified path for the file identified by |file_id|.
124 // If the file has not been shared already using the FileShare()
Alex Vakulenko75039d72014-03-25 12:36:28 -0700125 // method, an empty base::FilePath is returned - use FilePath::empty() to
David Zeuthen27a48bc2013-08-06 12:06:29 -0700126 // find out.
127 virtual base::FilePath FileGetPath(const std::string& file_id) = 0;
128
129 // Gets the actual size of the file identified by |file_id|. This is
130 // equivalent to reading the value of the st_size field of the
131 // struct stat on the file given by FileGetPath(). Returns -1 if an
132 // error occurs.
133 //
134 // For a file just created with FileShare() this will return 0.
135 virtual ssize_t FileGetSize(const std::string& file_id) = 0;
136
137 // Gets the expected size of the file identified by |file_id|. This
138 // is equivalent to reading the value of the user.cros-p2p-filesize
139 // xattr on the file given by FileGetPath(). Returns -1 if an error
140 // occurs.
141 //
142 // For a file just created with FileShare() this will return the
143 // value of the |expected_size| parameter passed to that method.
144 virtual ssize_t FileGetExpectedSize(const std::string& file_id) = 0;
145
146 // Gets whether the file identified by |file_id| is publicly
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700147 // visible. If |out_result| is not null, the result is returned
David Zeuthen27a48bc2013-08-06 12:06:29 -0700148 // there. Returns false if an error occurs.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800149 virtual bool FileGetVisible(const std::string& file_id, bool* out_result) = 0;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700150
151 // Makes the file identified by |file_id| publicly visible
152 // (e.g. removes the .tmp extension). If the file is already
153 // visible, this method does nothing. Returns False if
154 // the method fails or there is no file for |file_id|.
155 virtual bool FileMakeVisible(const std::string& file_id) = 0;
156
157 // Counts the number of shared files used by this application
158 // (cf. the |file_extension parameter|. Returns -1 if an error
159 // occurred.
160 virtual int CountSharedFiles() = 0;
161
162 // Creates a suitable P2PManager instance and initializes the object
163 // so it's ready for use. The |file_extension| parameter is used to
164 // identify your application, use e.g. "cros_au". If
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700165 // |configuration| is non-null, the P2PManager will take ownership
David Zeuthen27a48bc2013-08-06 12:06:29 -0700166 // of the Configuration object and use it (hence, it must be
167 // heap-allocated).
168 //
169 // The |num_files_to_keep| parameter specifies how many files to
170 // keep after performing housekeeping (cf. the PerformHousekeeping()
David Zeuthen41f2cf52014-11-05 12:29:45 -0500171 // method) - pass zero to allow infinitely many files. The
172 // |max_file_age| parameter specifies the maximum file age after
173 // performing housekeeping (pass zero to allow files of any age).
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700174 static P2PManager* Construct(
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800175 Configuration* configuration,
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700176 chromeos_update_manager::UpdateManager* update_manager,
177 const std::string& file_extension,
178 const int num_files_to_keep,
179 const base::TimeDelta& max_file_age);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700180};
181
182} // namespace chromeos_update_engine
183
Amin Hassaniec7bc112020-10-29 16:47:58 -0700184#endif // UPDATE_ENGINE_CROS_P2P_MANAGER_H_