blob: fb07c2e015461eb97c87209efa24c2c07ff2475e [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"
Gilad Arnold4a0321b2014-10-28 15:57:30 -070020#include "update_engine/update_manager/update_manager.h"
David Zeuthen27a48bc2013-08-06 12:06:29 -070021
22namespace chromeos_update_engine {
23
24// Interface for sharing and discovering files via p2p.
25class P2PManager {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070026 public:
David Zeuthen27a48bc2013-08-06 12:06:29 -070027 // Interface used for P2PManager implementations. The sole reason
28 // for this interface is unit testing.
29 class Configuration {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070030 public:
David Zeuthen27a48bc2013-08-06 12:06:29 -070031 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 Zeuthen92d9c8b2013-09-11 10:58:11 -070053 // Use the device policy specified by |device_policy|. If this is
Alex Vakulenko88b591f2014-08-28 16:48:57 -070054 // null, then no device policy is used.
David Zeuthen92d9c8b2013-09-11 10:58:11 -070055 virtual void SetDevicePolicy(const policy::DevicePolicy* device_policy) = 0;
56
Gilad Arnold4a0321b2014-10-28 15:57:30 -070057 // Returns true iff P2P is currently allowed for use on this device. This
58 // value is determined and maintained by the Update Manager.
David Zeuthen27a48bc2013-08-06 12:06:29 -070059 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
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 Vakulenko88b591f2014-08-28 16:48:57 -0700156 // |configuration| is non-null, the P2PManager will take ownership
David Zeuthen27a48bc2013-08-06 12:06:29 -0700157 // 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 Zeuthen41f2cf52014-11-05 12:29:45 -0500162 // 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 Arnold4a0321b2014-10-28 15:57:30 -0700165 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 Zeuthen27a48bc2013-08-06 12:06:29 -0700172};
173
174} // namespace chromeos_update_engine
175
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700176#endif // UPDATE_ENGINE_P2P_MANAGER_H_