dlp_client.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2021 The Chromium 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. #ifndef CHROMEOS_DBUS_DLP_DLP_CLIENT_H_
  5. #define CHROMEOS_DBUS_DLP_DLP_CLIENT_H_
  6. #include <string>
  7. #include "base/callback.h"
  8. #include "base/component_export.h"
  9. #include "base/files/scoped_file.h"
  10. #include "chromeos/dbus/dlp/dlp_service.pb.h"
  11. #include "dbus/object_proxy.h"
  12. namespace dbus {
  13. class Bus;
  14. } // namespace dbus
  15. namespace chromeos {
  16. // DlpClient is used to communicate with the org.chromium.Dlp
  17. // service. All method should be called from the origin thread (UI thread) which
  18. // initializes the DBusThreadManager instance.
  19. class COMPONENT_EXPORT(DLP) DlpClient {
  20. public:
  21. using SetDlpFilesPolicyCallback =
  22. base::OnceCallback<void(const dlp::SetDlpFilesPolicyResponse response)>;
  23. using AddFileCallback =
  24. base::OnceCallback<void(const dlp::AddFileResponse response)>;
  25. using GetFilesSourcesCallback =
  26. base::OnceCallback<void(const dlp::GetFilesSourcesResponse response)>;
  27. using CheckFilesTransferCallback =
  28. base::OnceCallback<void(const dlp::CheckFilesTransferResponse response)>;
  29. using RequestFileAccessCallback =
  30. base::OnceCallback<void(const dlp::RequestFileAccessResponse response,
  31. base::ScopedFD fd)>;
  32. // Interface with testing functionality. Accessed through GetTestInterface(),
  33. // only implemented in the fake implementation.
  34. class TestInterface {
  35. public:
  36. // Returns how many times |SetDlpFilesPolicyCount| was called.
  37. virtual int GetSetDlpFilesPolicyCount() const = 0;
  38. // Sets source url string to be returned for any file inode.
  39. virtual void SetFakeSource(const std::string&) = 0;
  40. // Sets CheckFilesTransfer response proto.
  41. virtual void SetCheckFilesTransferResponse(
  42. dlp::CheckFilesTransferResponse response) = 0;
  43. // Sets response for RequestFileAccess call.
  44. virtual void SetFileAccessAllowed(bool allowed) = 0;
  45. // Sets the response for IsAlive call.
  46. virtual void SetIsAlive(bool is_alive) = 0;
  47. protected:
  48. virtual ~TestInterface() = default;
  49. };
  50. DlpClient(const DlpClient&) = delete;
  51. DlpClient& operator=(const DlpClient&) = delete;
  52. // Creates and initializes the global instance. |bus| must not be null.
  53. static void Initialize(dbus::Bus* bus);
  54. // Creates and initializes a fake global instance if not already created.
  55. static void InitializeFake();
  56. // Destroys the global instance.
  57. static void Shutdown();
  58. // Returns the global instance which may be null if not initialized.
  59. static DlpClient* Get();
  60. // Dlp daemon D-Bus method calls. See org.chromium.Dlp.xml and
  61. // dlp_service.proto in Chromium OS code for the documentation of the methods
  62. // and request/response messages.
  63. virtual void SetDlpFilesPolicy(const dlp::SetDlpFilesPolicyRequest request,
  64. SetDlpFilesPolicyCallback callback) = 0;
  65. virtual void AddFile(const dlp::AddFileRequest request,
  66. AddFileCallback callback) = 0;
  67. virtual void GetFilesSources(const dlp::GetFilesSourcesRequest request,
  68. GetFilesSourcesCallback callback) const = 0;
  69. virtual void CheckFilesTransfer(
  70. const dlp::CheckFilesTransferRequest request,
  71. CheckFilesTransferCallback callback) const = 0;
  72. virtual void RequestFileAccess(const dlp::RequestFileAccessRequest request,
  73. RequestFileAccessCallback callback) = 0;
  74. virtual bool IsAlive() const = 0;
  75. // Returns an interface for testing (fake only), or returns nullptr.
  76. virtual TestInterface* GetTestInterface() = 0;
  77. protected:
  78. // Initialize/Shutdown should be used instead.
  79. DlpClient();
  80. virtual ~DlpClient();
  81. };
  82. } // namespace chromeos
  83. // TODO(https://crbug.com/1164001): remove when moved to ash.
  84. namespace ash {
  85. using ::chromeos::DlpClient;
  86. } // namespace ash
  87. #endif // CHROMEOS_DBUS_DLP_DLP_CLIENT_H_