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