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