p2p_manager.h 7.8 KB

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