media_drm_storage.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2017 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 MEDIA_BASE_MEDIA_DRM_STORAGE_H_
  5. #define MEDIA_BASE_MEDIA_DRM_STORAGE_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/callback.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "media/base/media_drm_key_type.h"
  13. #include "media/base/media_export.h"
  14. #include "third_party/abseil-cpp/absl/types/optional.h"
  15. #include "url/origin.h"
  16. namespace base {
  17. class UnguessableToken;
  18. } // namespace base
  19. namespace media {
  20. // Allows MediaDrmBridge to store and retrieve persistent data. This is needed
  21. // for features like per-origin provisioning and persistent license support.
  22. class MEDIA_EXPORT MediaDrmStorage
  23. : public base::SupportsWeakPtr<MediaDrmStorage> {
  24. public:
  25. // When using per-origin provisioning, this is the ID for the origin.
  26. // If not specified, the device specific origin ID is to be used.
  27. using MediaDrmOriginId = absl::optional<base::UnguessableToken>;
  28. struct MEDIA_EXPORT SessionData {
  29. SessionData(std::vector<uint8_t> key_set_id,
  30. std::string mime_type,
  31. MediaDrmKeyType key_type);
  32. SessionData(const SessionData& other);
  33. ~SessionData();
  34. std::vector<uint8_t> key_set_id;
  35. std::string mime_type;
  36. MediaDrmKeyType key_type;
  37. };
  38. MediaDrmStorage();
  39. MediaDrmStorage(const MediaDrmStorage&) = delete;
  40. MediaDrmStorage& operator=(const MediaDrmStorage&) = delete;
  41. virtual ~MediaDrmStorage();
  42. // Callback to return whether the operation succeeded.
  43. using ResultCB = base::OnceCallback<void(bool)>;
  44. // Callback for storage initialization.
  45. using InitCB =
  46. base::OnceCallback<void(bool success, const MediaDrmOriginId& origin_id)>;
  47. // Callback to return the result of LoadPersistentSession. |key_set_id| and
  48. // |mime_type| must be non-empty if |success| is true, and vice versa.
  49. using LoadPersistentSessionCB =
  50. base::OnceCallback<void(std::unique_ptr<SessionData> session_data)>;
  51. // Initialize the storage for current origin. The implementation already know
  52. // the origin for the storage.
  53. // Implementation should return a random origin id in |init_cb|. The ID should
  54. // be unique and persisted. Origin ID must be valid. If any corruption is
  55. // detected, the old map should be removed in OnProvisioned.
  56. virtual void Initialize(InitCB init_cb) = 0;
  57. // Called when MediaDrm is provisioned for the origin bound to |this|.
  58. // The implementation should keep track of the storing time so that the
  59. // information can be cleared based on selected time range (e.g. for clearing
  60. // browsing data).
  61. virtual void OnProvisioned(ResultCB result_cb) = 0;
  62. // Saves the persistent session info for |session_id| in the storage.
  63. // The implementation should keep track of the storing time so that the
  64. // information can be cleared based on selected time range (e.g. for clearing
  65. // browsing data).
  66. virtual void SavePersistentSession(const std::string& session_id,
  67. const SessionData& session_data,
  68. ResultCB result_cb) = 0;
  69. // Loads the persistent session info for |session_id| from the storage.
  70. virtual void LoadPersistentSession(
  71. const std::string& session_id,
  72. LoadPersistentSessionCB load_persistent_session_cb) = 0;
  73. // Removes the persistent session info for |session_id| from the storage.
  74. // If the session for |session_id| exists in the storage, it is removed.
  75. // Otherwise, this call is a no-op. In both cases, the result will be true.
  76. // The result will be false on other unexpected errors, e.g. connection error
  77. // to the storage backend.
  78. virtual void RemovePersistentSession(const std::string& session_id,
  79. ResultCB result_cb) = 0;
  80. };
  81. using CreateStorageCB =
  82. base::RepeatingCallback<std::unique_ptr<MediaDrmStorage>()>;
  83. } // namespace media
  84. #endif // MEDIA_BASE_MEDIA_DRM_STORAGE_H_