blob: ddd0fda8e7fa60a620cc259fb9eab2eae69a464a [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
18#include "update_engine/prefs_interface.h"
19
20namespace chromeos_update_engine {
21
22// Interface for sharing and discovering files via p2p.
23class P2PManager {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070024 public:
David Zeuthen27a48bc2013-08-06 12:06:29 -070025 // Interface used for P2PManager implementations. The sole reason
26 // for this interface is unit testing.
27 class Configuration {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070028 public:
David Zeuthen27a48bc2013-08-06 12:06:29 -070029 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 Zeuthen92d9c8b2013-09-11 10:58:11 -070051 // Use the device policy specified by |device_policy|. If this is
Alex Vakulenko88b591f2014-08-28 16:48:57 -070052 // null, then no device policy is used.
David Zeuthen92d9c8b2013-09-11 10:58:11 -070053 virtual void SetDevicePolicy(const policy::DevicePolicy* device_policy) = 0;
54
David Zeuthen27a48bc2013-08-06 12:06:29 -070055 // 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 Vakulenko75039d72014-03-25 12:36:28 -0700114 // method, an empty base::FilePath is returned - use FilePath::empty() to
David Zeuthen27a48bc2013-08-06 12:06:29 -0700115 // 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 Vakulenko88b591f2014-08-28 16:48:57 -0700136 // visible. If |out_result| is not null, the result is returned
David Zeuthen27a48bc2013-08-06 12:06:29 -0700137 // 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
Gilad Arnoldccd09572014-10-27 13:37:50 -0700152 // Updates the preference setting for enabling P2P. If P2P is disabled as a
153 // result, attempts to ensure that the service is not running. Returns true if
154 // the setting was updated successfully (even through stopping the service may
155 // have failed).
156 virtual bool SetP2PEnabledPref(bool enabled) = 0;
157
David Zeuthen27a48bc2013-08-06 12:06:29 -0700158 // Creates a suitable P2PManager instance and initializes the object
159 // so it's ready for use. The |file_extension| parameter is used to
160 // identify your application, use e.g. "cros_au". If
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700161 // |configuration| is non-null, the P2PManager will take ownership
David Zeuthen27a48bc2013-08-06 12:06:29 -0700162 // of the Configuration object and use it (hence, it must be
163 // heap-allocated).
164 //
165 // The |num_files_to_keep| parameter specifies how many files to
166 // keep after performing housekeeping (cf. the PerformHousekeeping()
167 // method). If zero is passed, no files will be deleted.
168 static P2PManager* Construct(Configuration *configuration,
169 PrefsInterface *prefs,
170 const std::string& file_extension,
171 const int num_files_to_keep);
172};
173
174} // namespace chromeos_update_engine
175
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700176#endif // UPDATE_ENGINE_P2P_MANAGER_H_