blob: 4c6e508cacf6dc48125e8d834eaf50b6a0845da6 [file] [log] [blame]
David Zeuthen27a48bc2013-08-06 12:06:29 -07001// 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 Arnoldcf175a02014-07-10 16:48:47 -07005#ifndef UPDATE_ENGINE_P2P_MANAGER_H_
6#define UPDATE_ENGINE_P2P_MANAGER_H_
David Zeuthen27a48bc2013-08-06 12:06:29 -07007
8#include <string>
9#include <vector>
10
11#include <base/callback.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070012#include <base/files/file_path.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070013#include <base/memory/ref_counted.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070014#include <base/time/time.h>
David Zeuthen92d9c8b2013-09-11 10:58:11 -070015#include <policy/device_policy.h>
16#include <policy/libpolicy.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070017
David Zeuthen41f2cf52014-11-05 12:29:45 -050018#include "update_engine/clock_interface.h"
David Zeuthen27a48bc2013-08-06 12:06:29 -070019#include "update_engine/prefs_interface.h"
20
21namespace chromeos_update_engine {
22
23// Interface for sharing and discovering files via p2p.
24class P2PManager {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070025 public:
David Zeuthen27a48bc2013-08-06 12:06:29 -070026 // Interface used for P2PManager implementations. The sole reason
27 // for this interface is unit testing.
28 class Configuration {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070029 public:
David Zeuthen27a48bc2013-08-06 12:06:29 -070030 virtual ~Configuration() {}
31
32 // Gets the path to the p2p dir being used, e.g. /var/cache/p2p.
33 virtual base::FilePath GetP2PDir() = 0;
34
35 // Gets the argument vector for starting (if |is_start| is True)
36 // resp. stopping (if |is_start| is False) the p2p service
37 // e.g. ["initctl", "start", "p2p"] or ["initctl", "stop", "p2p"].
38 virtual std::vector<std::string> GetInitctlArgs(bool is_start) = 0;
39
40 // Gets the argument vector for invoking p2p-client, e.g.
41 // "p2p-client --get-url=file_id_we_want --minimum-size=42123".
42 virtual std::vector<std::string> GetP2PClientArgs(
43 const std::string& file_id, size_t minimum_size) = 0;
44 };
45
46 virtual ~P2PManager() {}
47
48 // The type for the callback used in LookupUrlForFile().
49 // If the lookup failed, |url| is empty.
50 typedef base::Callback<void(const std::string& url)> LookupCallback;
51
David Zeuthen92d9c8b2013-09-11 10:58:11 -070052 // Use the device policy specified by |device_policy|. If this is
Alex Vakulenko88b591f2014-08-28 16:48:57 -070053 // null, then no device policy is used.
David Zeuthen92d9c8b2013-09-11 10:58:11 -070054 virtual void SetDevicePolicy(const policy::DevicePolicy* device_policy) = 0;
55
David Zeuthen27a48bc2013-08-06 12:06:29 -070056 // Returns true if - and only if - P2P should be used on this
57 // device. This value is derived from a variety of sources including
58 // enterprise policy.
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 Vakulenko75039d72014-03-25 12:36:28 -0700115 // method, an empty base::FilePath is returned - use FilePath::empty() to
David Zeuthen27a48bc2013-08-06 12:06:29 -0700116 // 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 Vakulenko88b591f2014-08-28 16:48:57 -0700137 // visible. If |out_result| is not null, the result is returned
David Zeuthen27a48bc2013-08-06 12:06:29 -0700138 // 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
Gilad Arnoldccd09572014-10-27 13:37:50 -0700153 // Updates the preference setting for enabling P2P. If P2P is disabled as a
154 // result, attempts to ensure that the service is not running. Returns true if
155 // the setting was updated successfully (even through stopping the service may
156 // have failed).
157 virtual bool SetP2PEnabledPref(bool enabled) = 0;
158
David Zeuthen27a48bc2013-08-06 12:06:29 -0700159 // Creates a suitable P2PManager instance and initializes the object
160 // so it's ready for use. The |file_extension| parameter is used to
161 // identify your application, use e.g. "cros_au". If
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700162 // |configuration| is non-null, the P2PManager will take ownership
David Zeuthen27a48bc2013-08-06 12:06:29 -0700163 // of the Configuration object and use it (hence, it must be
164 // heap-allocated).
165 //
166 // The |num_files_to_keep| parameter specifies how many files to
167 // keep after performing housekeeping (cf. the PerformHousekeeping()
David Zeuthen41f2cf52014-11-05 12:29:45 -0500168 // method) - pass zero to allow infinitely many files. The
169 // |max_file_age| parameter specifies the maximum file age after
170 // performing housekeeping (pass zero to allow files of any age).
David Zeuthen27a48bc2013-08-06 12:06:29 -0700171 static P2PManager* Construct(Configuration *configuration,
172 PrefsInterface *prefs,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500173 ClockInterface *clock,
David Zeuthen27a48bc2013-08-06 12:06:29 -0700174 const std::string& file_extension,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500175 const int num_files_to_keep,
176 const base::TimeDelta& max_file_age);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700177};
178
179} // namespace chromeos_update_engine
180
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700181#endif // UPDATE_ENGINE_P2P_MANAGER_H_