media_drm_storage_impl.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 COMPONENTS_CDM_BROWSER_MEDIA_DRM_STORAGE_IMPL_H_
  5. #define COMPONENTS_CDM_BROWSER_MEDIA_DRM_STORAGE_IMPL_H_
  6. #include <set>
  7. #include <string>
  8. #include <vector>
  9. #include "base/callback.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/threading/thread_checker.h"
  13. #include "base/time/time.h"
  14. #include "base/unguessable_token.h"
  15. #include "build/build_config.h"
  16. #include "content/public/browser/document_service.h"
  17. #include "content/public/browser/render_frame_host.h"
  18. #include "content/public/browser/web_contents_observer.h"
  19. #include "media/mojo/mojom/media_drm_storage.mojom.h"
  20. #include "mojo/public/cpp/bindings/pending_receiver.h"
  21. #include "third_party/abseil-cpp/absl/types/optional.h"
  22. #include "url/gurl.h"
  23. #include "url/origin.h"
  24. class PrefRegistrySimple;
  25. class PrefService;
  26. namespace content {
  27. class RenderFrameHost;
  28. }
  29. namespace cdm {
  30. namespace prefs {
  31. extern const char kMediaDrmStorage[];
  32. } // namespace prefs
  33. // Implements media::mojom::MediaDrmStorage using PrefService.
  34. // This file is located under components/ so that it can be shared by multiple
  35. // content embedders (e.g. chrome and chromecast).
  36. class MediaDrmStorageImpl final
  37. : public content::DocumentService<media::mojom::MediaDrmStorage> {
  38. public:
  39. // When using per-origin provisioning, this is the ID for the origin.
  40. // If not specified, the device specific origin ID is to be used.
  41. using MediaDrmOriginId = absl::optional<base::UnguessableToken>;
  42. // |success| is true if an origin ID was obtained and |origin_id| is
  43. // specified, false otherwise.
  44. using OriginIdObtainedCB =
  45. base::OnceCallback<void(bool success, const MediaDrmOriginId& origin_id)>;
  46. using GetOriginIdCB = base::RepeatingCallback<void(OriginIdObtainedCB)>;
  47. // |callback| returns true if an empty origin ID is allowed, false if not.
  48. using AllowEmptyOriginIdCB =
  49. base::RepeatingCallback<void(base::OnceCallback<void(bool)> callback)>;
  50. static void RegisterProfilePrefs(PrefRegistrySimple* registry);
  51. // Get a list of origins that have persistent storage on the device.
  52. static std::set<GURL> GetAllOrigins(const PrefService* pref_service);
  53. // Get a list of all origins that have been modified after |start|
  54. // and before |end|.
  55. static std::vector<GURL> GetOriginsModifiedBetween(
  56. const PrefService* pref_service,
  57. base::Time start,
  58. base::Time end);
  59. #if BUILDFLAG(IS_ANDROID)
  60. // Clear media licenses and related data if:
  61. // 1. Creation time falls in [delete_begin, delete_end], and
  62. // 2. |filter| returns true for the origin. |filter| is passed in to allow
  63. // licenses under specific origins to be cleared. Empty |filter| means remove
  64. // licenses for all origins.
  65. //
  66. // Media license session data will be removed from persist storage. Removing
  67. // the actual license file needs ack response from license server, so it's
  68. // hard for Chromium to do that. Since it's difficult to get the real id for
  69. // the license without the session data, we can treat the licenses as cleared.
  70. //
  71. // If all the licenses under the origin are cleared, the origin will be
  72. // unprovisioned, a.k.a the cert will be removed.
  73. // TODO(yucliu): Add unit test.
  74. static void ClearMatchingLicenses(
  75. PrefService* pref_service,
  76. base::Time start,
  77. base::Time end,
  78. const base::RepeatingCallback<bool(const GURL&)>& filter,
  79. base::OnceClosure complete_cb);
  80. #endif
  81. // |get_origin_id_cb| must be provided and is used to obtain an origin ID.
  82. // |allow_empty_origin_id_cb| is used to determine if an empty origin ID is
  83. // allowed or not. It is called if |get_origin_id_cb| is unable to return an
  84. // origin ID.
  85. MediaDrmStorageImpl(
  86. content::RenderFrameHost& render_frame_host,
  87. PrefService* pref_service,
  88. GetOriginIdCB get_origin_id_cb,
  89. AllowEmptyOriginIdCB allow_empty_origin_id_cb,
  90. mojo::PendingReceiver<media::mojom::MediaDrmStorage> receiver);
  91. // As above, but derives the PrefService from |render_frame_host|.
  92. // TODO(estade): make this the only constructor.
  93. MediaDrmStorageImpl(
  94. content::RenderFrameHost& render_frame_host,
  95. GetOriginIdCB get_origin_id_cb,
  96. AllowEmptyOriginIdCB allow_empty_origin_id_cb,
  97. mojo::PendingReceiver<media::mojom::MediaDrmStorage> receiver);
  98. // media::mojom::MediaDrmStorage implementation.
  99. void Initialize(InitializeCallback callback) final;
  100. void OnProvisioned(OnProvisionedCallback callback) final;
  101. void SavePersistentSession(const std::string& session_id,
  102. media::mojom::SessionDataPtr session_data,
  103. SavePersistentSessionCallback callback) final;
  104. void LoadPersistentSession(const std::string& session_id,
  105. LoadPersistentSessionCallback callback) final;
  106. void RemovePersistentSession(const std::string& session_id,
  107. RemovePersistentSessionCallback callback) final;
  108. private:
  109. // |this| can only be destructed as a DocumentService.
  110. ~MediaDrmStorageImpl() final;
  111. // Called when |get_origin_id_cb_| asynchronously returns a origin ID as part
  112. // of Initialize().
  113. void OnOriginIdObtained(bool success, const MediaDrmOriginId& origin_id);
  114. // Called after checking if an empty origin ID is allowed.
  115. void OnEmptyOriginIdAllowed(bool allowed);
  116. const raw_ptr<PrefService> pref_service_;
  117. GetOriginIdCB get_origin_id_cb_;
  118. AllowEmptyOriginIdCB allow_empty_origin_id_cb_;
  119. // ID for the current origin. Per EME spec on individualization,
  120. // implementation should not expose application-specific information.
  121. // If not specified, the device specific origin ID is to be used.
  122. MediaDrmOriginId origin_id_;
  123. // As Initialize() may be asynchronous, save the InitializeCallback when
  124. // necessary.
  125. InitializeCallback init_cb_;
  126. // Set when initialized.
  127. bool is_initialized_ = false;
  128. // NOTE: Weak pointers must be invalidated before all other member variables.
  129. base::WeakPtrFactory<MediaDrmStorageImpl> weak_factory_{this};
  130. };
  131. } // namespace cdm
  132. #endif // COMPONENTS_CDM_BROWSER_MEDIA_DRM_STORAGE_IMPL_H_