David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 1 | // Copyright (c) 2013 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. |
| 4 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 5 | #ifndef UPDATE_ENGINE_P2P_MANAGER_H_ |
| 6 | #define UPDATE_ENGINE_P2P_MANAGER_H_ |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <base/callback.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 12 | #include <base/files/file_path.h> |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 13 | #include <base/memory/ref_counted.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 14 | #include <base/time/time.h> |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 15 | #include <policy/device_policy.h> |
| 16 | #include <policy/libpolicy.h> |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 17 | |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 18 | #include "update_engine/clock_interface.h" |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 19 | #include "update_engine/prefs_interface.h" |
Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 20 | #include "update_engine/update_manager/update_manager.h" |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 21 | |
| 22 | namespace chromeos_update_engine { |
| 23 | |
| 24 | // Interface for sharing and discovering files via p2p. |
| 25 | class P2PManager { |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 26 | public: |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 27 | // Interface used for P2PManager implementations. The sole reason |
| 28 | // for this interface is unit testing. |
| 29 | class Configuration { |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 30 | public: |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 31 | virtual ~Configuration() {} |
| 32 | |
| 33 | // Gets the path to the p2p dir being used, e.g. /var/cache/p2p. |
| 34 | virtual base::FilePath GetP2PDir() = 0; |
| 35 | |
| 36 | // Gets the argument vector for starting (if |is_start| is True) |
| 37 | // resp. stopping (if |is_start| is False) the p2p service |
| 38 | // e.g. ["initctl", "start", "p2p"] or ["initctl", "stop", "p2p"]. |
| 39 | virtual std::vector<std::string> GetInitctlArgs(bool is_start) = 0; |
| 40 | |
| 41 | // Gets the argument vector for invoking p2p-client, e.g. |
| 42 | // "p2p-client --get-url=file_id_we_want --minimum-size=42123". |
| 43 | virtual std::vector<std::string> GetP2PClientArgs( |
| 44 | const std::string& file_id, size_t minimum_size) = 0; |
| 45 | }; |
| 46 | |
| 47 | virtual ~P2PManager() {} |
| 48 | |
| 49 | // The type for the callback used in LookupUrlForFile(). |
| 50 | // If the lookup failed, |url| is empty. |
| 51 | typedef base::Callback<void(const std::string& url)> LookupCallback; |
| 52 | |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 53 | // Use the device policy specified by |device_policy|. If this is |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 54 | // null, then no device policy is used. |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 55 | virtual void SetDevicePolicy(const policy::DevicePolicy* device_policy) = 0; |
| 56 | |
Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 57 | // Returns true iff P2P is currently allowed for use on this device. This |
| 58 | // value is determined and maintained by the Update Manager. |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 59 | virtual bool IsP2PEnabled() = 0; |
| 60 | |
| 61 | // Ensures that the p2p subsystem is running (e.g. starts it if it's |
| 62 | // not already running) and blocks until this is so. Returns false |
| 63 | // if an error occurred. |
| 64 | virtual bool EnsureP2PRunning() = 0; |
| 65 | |
| 66 | // Ensures that the p2p subsystem is not running (e.g. stops it if |
| 67 | // it's running) and blocks until this is so. Returns false if an |
| 68 | // error occurred. |
| 69 | virtual bool EnsureP2PNotRunning() = 0; |
| 70 | |
| 71 | // Cleans up files in /var/cache/p2p owned by this application as |
| 72 | // per the |file_extension| and |num_files_to_keep| values passed |
| 73 | // when the object was constructed. This may be called even if |
| 74 | // the p2p subsystem is not running. |
| 75 | virtual bool PerformHousekeeping() = 0; |
| 76 | |
| 77 | // Asynchronously finds a peer that serves the file identified by |
| 78 | // |file_id|. If |minimum_size| is non-zero, will find a peer that |
| 79 | // has at least that many bytes. When the result is ready |callback| |
| 80 | // is called from the default GLib mainloop. |
| 81 | // |
| 82 | // This operation may take a very long time to complete because part |
| 83 | // of the p2p protocol involves waiting for the LAN-wide sum of all |
| 84 | // num-connections to drop below a given threshold. However, if |
| 85 | // |max_time_to_wait| is non-zero, the operation is guaranteed to |
| 86 | // not exceed this duration. |
| 87 | // |
| 88 | // If the file is not available on the LAN (or if mDNS/DNS-SD is |
| 89 | // filtered), this is guaranteed to not take longer than 5 seconds. |
| 90 | virtual void LookupUrlForFile(const std::string& file_id, |
| 91 | size_t minimum_size, |
| 92 | base::TimeDelta max_time_to_wait, |
| 93 | LookupCallback callback) = 0; |
| 94 | |
| 95 | // Shares a file identified by |file_id| in the directory |
| 96 | // /var/cache/p2p. Initially the file will not be visible, that is, |
| 97 | // it will have a .tmp extension and not be shared via p2p. Use the |
| 98 | // FileMakeVisible() method to change this. |
| 99 | // |
| 100 | // If you know the final size of the file, pass it in the |
| 101 | // |expected_size| parameter. Otherwise pass zero. If non-zero, the |
| 102 | // amount of free space in /var/cache/p2p is checked and if there is |
| 103 | // less than twice the amount of space available, this method |
| 104 | // fails. Additionally, disk space will be reserved via fallocate(2) |
| 105 | // and |expected_size| is written to the user.cros-p2p-filesize |
| 106 | // xattr of the created file. |
| 107 | // |
| 108 | // If the file already exists, true is returned. Any on-disk xattr |
| 109 | // is not updated. |
| 110 | virtual bool FileShare(const std::string& file_id, |
| 111 | size_t expected_size) = 0; |
| 112 | |
| 113 | // Gets a fully qualified path for the file identified by |file_id|. |
| 114 | // If the file has not been shared already using the FileShare() |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 115 | // method, an empty base::FilePath is returned - use FilePath::empty() to |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 116 | // find out. |
| 117 | virtual base::FilePath FileGetPath(const std::string& file_id) = 0; |
| 118 | |
| 119 | // Gets the actual size of the file identified by |file_id|. This is |
| 120 | // equivalent to reading the value of the st_size field of the |
| 121 | // struct stat on the file given by FileGetPath(). Returns -1 if an |
| 122 | // error occurs. |
| 123 | // |
| 124 | // For a file just created with FileShare() this will return 0. |
| 125 | virtual ssize_t FileGetSize(const std::string& file_id) = 0; |
| 126 | |
| 127 | // Gets the expected size of the file identified by |file_id|. This |
| 128 | // is equivalent to reading the value of the user.cros-p2p-filesize |
| 129 | // xattr on the file given by FileGetPath(). Returns -1 if an error |
| 130 | // occurs. |
| 131 | // |
| 132 | // For a file just created with FileShare() this will return the |
| 133 | // value of the |expected_size| parameter passed to that method. |
| 134 | virtual ssize_t FileGetExpectedSize(const std::string& file_id) = 0; |
| 135 | |
| 136 | // Gets whether the file identified by |file_id| is publicly |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 137 | // visible. If |out_result| is not null, the result is returned |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 138 | // there. Returns false if an error occurs. |
| 139 | virtual bool FileGetVisible(const std::string& file_id, |
| 140 | bool *out_result) = 0; |
| 141 | |
| 142 | // Makes the file identified by |file_id| publicly visible |
| 143 | // (e.g. removes the .tmp extension). If the file is already |
| 144 | // visible, this method does nothing. Returns False if |
| 145 | // the method fails or there is no file for |file_id|. |
| 146 | virtual bool FileMakeVisible(const std::string& file_id) = 0; |
| 147 | |
| 148 | // Counts the number of shared files used by this application |
| 149 | // (cf. the |file_extension parameter|. Returns -1 if an error |
| 150 | // occurred. |
| 151 | virtual int CountSharedFiles() = 0; |
| 152 | |
| 153 | // Creates a suitable P2PManager instance and initializes the object |
| 154 | // so it's ready for use. The |file_extension| parameter is used to |
| 155 | // identify your application, use e.g. "cros_au". If |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 156 | // |configuration| is non-null, the P2PManager will take ownership |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 157 | // of the Configuration object and use it (hence, it must be |
| 158 | // heap-allocated). |
| 159 | // |
| 160 | // The |num_files_to_keep| parameter specifies how many files to |
| 161 | // keep after performing housekeeping (cf. the PerformHousekeeping() |
David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 162 | // method) - pass zero to allow infinitely many files. The |
| 163 | // |max_file_age| parameter specifies the maximum file age after |
| 164 | // performing housekeeping (pass zero to allow files of any age). |
Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 165 | static P2PManager* Construct( |
| 166 | Configuration *configuration, |
| 167 | ClockInterface *clock, |
| 168 | chromeos_update_manager::UpdateManager* update_manager, |
| 169 | const std::string& file_extension, |
| 170 | const int num_files_to_keep, |
| 171 | const base::TimeDelta& max_file_age); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | } // namespace chromeos_update_engine |
| 175 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 176 | #endif // UPDATE_ENGINE_P2P_MANAGER_H_ |